Skip to content

Commit c3bdf69

Browse files
Resolve release markers
Signed-off-by: muhammad adil ghaffar <[email protected]>
1 parent 72e5bc9 commit c3bdf69

File tree

10 files changed

+354
-127
lines changed

10 files changed

+354
-127
lines changed

cmd/clusterctl/client/repository/repository_github_test.go

Lines changed: 30 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ import (
2020
"context"
2121
"fmt"
2222
"net/http"
23-
"net/http/httptest"
24-
"net/url"
2523
"testing"
2624
"time"
2725

@@ -33,6 +31,7 @@ import (
3331
"sigs.k8s.io/cluster-api/cmd/clusterctl/client/config"
3432
"sigs.k8s.io/cluster-api/cmd/clusterctl/internal/test"
3533
"sigs.k8s.io/cluster-api/internal/goproxy"
34+
goproxytest "sigs.k8s.io/cluster-api/internal/goproxy/test"
3635
)
3736

3837
func Test_gitHubRepository_GetVersions(t *testing.T) {
@@ -44,7 +43,7 @@ func Test_gitHubRepository_GetVersions(t *testing.T) {
4443

4544
// Setup an handler for returning 5 fake releases.
4645
mux.HandleFunc("/repos/o/r1/releases", func(w http.ResponseWriter, r *http.Request) {
47-
testMethod(t, r, "GET")
46+
goproxytest.HTTPTestMethod(t, r, "GET")
4847
fmt.Fprint(w, `[`)
4948
fmt.Fprint(w, `{"id":1, "tag_name": "v0.4.0"},`)
5049
fmt.Fprint(w, `{"id":2, "tag_name": "v0.4.1"},`)
@@ -53,12 +52,13 @@ func Test_gitHubRepository_GetVersions(t *testing.T) {
5352
fmt.Fprint(w, `]`)
5453
})
5554

56-
clientGoproxy, muxGoproxy, teardownGoproxy := newFakeGoproxy()
55+
scheme, host, muxGoproxy, teardownGoproxy := goproxytest.NewFakeGoproxy()
56+
clientGoproxy := goproxy.NewClient(scheme, host)
5757
defer teardownGoproxy()
5858

5959
// Setup a handler for returning 4 fake releases.
6060
muxGoproxy.HandleFunc("/github.com/o/r2/@v/list", func(w http.ResponseWriter, r *http.Request) {
61-
testMethod(t, r, "GET")
61+
goproxytest.HTTPTestMethod(t, r, "GET")
6262
fmt.Fprint(w, "v0.5.0\n")
6363
fmt.Fprint(w, "v0.4.0\n")
6464
fmt.Fprint(w, "v0.3.2\n")
@@ -67,16 +67,16 @@ func Test_gitHubRepository_GetVersions(t *testing.T) {
6767

6868
// Setup a handler for returning 3 different major fake releases.
6969
muxGoproxy.HandleFunc("/github.com/o/r3/@v/list", func(w http.ResponseWriter, r *http.Request) {
70-
testMethod(t, r, "GET")
70+
goproxytest.HTTPTestMethod(t, r, "GET")
7171
fmt.Fprint(w, "v1.0.0\n")
7272
fmt.Fprint(w, "v0.1.0\n")
7373
})
7474
muxGoproxy.HandleFunc("/github.com/o/r3/v2/@v/list", func(w http.ResponseWriter, r *http.Request) {
75-
testMethod(t, r, "GET")
75+
goproxytest.HTTPTestMethod(t, r, "GET")
7676
fmt.Fprint(w, "v2.0.0\n")
7777
})
7878
muxGoproxy.HandleFunc("/github.com/o/r3/v3/@v/list", func(w http.ResponseWriter, r *http.Request) {
79-
testMethod(t, r, "GET")
79+
goproxytest.HTTPTestMethod(t, r, "GET")
8080
fmt.Fprint(w, "v3.0.0\n")
8181
})
8282

@@ -271,13 +271,13 @@ func Test_githubRepository_getFile(t *testing.T) {
271271

272272
// Setup a handler for returning a fake release.
273273
mux.HandleFunc("/repos/o/r/releases/tags/v0.4.1", func(w http.ResponseWriter, r *http.Request) {
274-
testMethod(t, r, "GET")
274+
goproxytest.HTTPTestMethod(t, r, "GET")
275275
fmt.Fprint(w, `{"id":13, "tag_name": "v0.4.1", "assets": [{"id": 1, "name": "file.yaml"}] }`)
276276
})
277277

278278
// Setup a handler for returning a fake release asset.
279279
mux.HandleFunc("/repos/o/r/releases/assets/1", func(w http.ResponseWriter, r *http.Request) {
280-
testMethod(t, r, "GET")
280+
goproxytest.HTTPTestMethod(t, r, "GET")
281281
w.Header().Set("Content-Type", "application/octet-stream")
282282
w.Header().Set("Content-Disposition", "attachment; filename=file.yaml")
283283
fmt.Fprint(w, "content")
@@ -345,7 +345,7 @@ func Test_gitHubRepository_getVersions(t *testing.T) {
345345
// Each response contains a link to the next page (if available) which
346346
// is parsed by the handler to navigate through all pages
347347
mux.HandleFunc("/repos/o/r1/releases", func(w http.ResponseWriter, r *http.Request) {
348-
testMethod(t, r, "GET")
348+
goproxytest.HTTPTestMethod(t, r, "GET")
349349
page := r.URL.Query().Get("page")
350350
switch page {
351351
case "", "1":
@@ -424,24 +424,26 @@ func Test_gitHubRepository_getLatestContractRelease(t *testing.T) {
424424

425425
// Setup a handler for returning a fake release.
426426
mux.HandleFunc("/repos/o/r1/releases/tags/v0.5.0", func(w http.ResponseWriter, r *http.Request) {
427-
testMethod(t, r, "GET")
427+
goproxytest.HTTPTestMethod(t, r, "GET")
428428
fmt.Fprint(w, `{"id":13, "tag_name": "v0.5.0", "assets": [{"id": 1, "name": "metadata.yaml"}] }`)
429429
})
430430

431431
// Setup a handler for returning a fake release metadata file.
432432
mux.HandleFunc("/repos/o/r1/releases/assets/1", func(w http.ResponseWriter, r *http.Request) {
433-
testMethod(t, r, "GET")
433+
goproxytest.HTTPTestMethod(t, r, "GET")
434434
w.Header().Set("Content-Type", "application/octet-stream")
435435
w.Header().Set("Content-Disposition", "attachment; filename=metadata.yaml")
436436
fmt.Fprint(w, "apiVersion: clusterctl.cluster.x-k8s.io/v1alpha3\nreleaseSeries:\n - major: 0\n minor: 4\n contract: v1alpha4\n - major: 0\n minor: 5\n contract: v1alpha4\n - major: 0\n minor: 3\n contract: v1alpha3\n")
437437
})
438438

439-
clientGoproxy, muxGoproxy, teardownGoproxy := newFakeGoproxy()
439+
scheme, host, muxGoproxy, teardownGoproxy := goproxytest.NewFakeGoproxy()
440+
clientGoproxy := goproxy.NewClient(scheme, host)
441+
440442
defer teardownGoproxy()
441443

442444
// Setup a handler for returning 4 fake releases.
443445
muxGoproxy.HandleFunc("/github.com/o/r1/@v/list", func(w http.ResponseWriter, r *http.Request) {
444-
testMethod(t, r, "GET")
446+
goproxytest.HTTPTestMethod(t, r, "GET")
445447
fmt.Fprint(w, "v0.5.0\n")
446448
fmt.Fprint(w, "v0.4.0\n")
447449
fmt.Fprint(w, "v0.3.2\n")
@@ -450,7 +452,7 @@ func Test_gitHubRepository_getLatestContractRelease(t *testing.T) {
450452

451453
// setup an handler for returning 4 fake releases but no actual tagged release
452454
muxGoproxy.HandleFunc("/github.com/o/r2/@v/list", func(w http.ResponseWriter, r *http.Request) {
453-
testMethod(t, r, "GET")
455+
goproxytest.HTTPTestMethod(t, r, "GET")
454456
fmt.Fprint(w, "v0.5.0\n")
455457
fmt.Fprint(w, "v0.4.0\n")
456458
fmt.Fprint(w, "v0.3.2\n")
@@ -528,32 +530,33 @@ func Test_gitHubRepository_getLatestContractRelease(t *testing.T) {
528530
func Test_gitHubRepository_getLatestRelease(t *testing.T) {
529531
retryableOperationInterval = 200 * time.Millisecond
530532
retryableOperationTimeout = 1 * time.Second
531-
clientGoproxy, muxGoproxy, teardownGoproxy := newFakeGoproxy()
533+
scheme, host, muxGoproxy, teardownGoproxy := goproxytest.NewFakeGoproxy()
534+
clientGoproxy := goproxy.NewClient(scheme, host)
532535
defer teardownGoproxy()
533536

534537
// Setup a handler for returning 4 fake releases.
535538
muxGoproxy.HandleFunc("/github.com/o/r1/@v/list", func(w http.ResponseWriter, r *http.Request) {
536-
testMethod(t, r, "GET")
539+
goproxytest.HTTPTestMethod(t, r, "GET")
537540
fmt.Fprint(w, "v0.4.1\n")
538541
fmt.Fprint(w, "v0.4.2\n")
539542
fmt.Fprint(w, "v0.4.3-alpha\n") // prerelease
540543
fmt.Fprint(w, "foo\n") // no semantic version tag
541544
})
542545
// And also expose a release for them
543546
muxGoproxy.HandleFunc("/api.github.com/repos/o/r1/releases/tags/v0.4.2", func(w http.ResponseWriter, r *http.Request) {
544-
testMethod(t, r, "GET")
547+
goproxytest.HTTPTestMethod(t, r, "GET")
545548
fmt.Fprint(w, "{}\n")
546549
})
547550

548551
// Setup a handler for returning no releases.
549552
muxGoproxy.HandleFunc("/github.com/o/r2/@v/list", func(w http.ResponseWriter, r *http.Request) {
550-
testMethod(t, r, "GET")
553+
goproxytest.HTTPTestMethod(t, r, "GET")
551554
// no releases
552555
})
553556

554557
// Setup a handler for returning fake prereleases only.
555558
muxGoproxy.HandleFunc("/github.com/o/r3/@v/list", func(w http.ResponseWriter, r *http.Request) {
556-
testMethod(t, r, "GET")
559+
goproxytest.HTTPTestMethod(t, r, "GET")
557560
fmt.Fprint(w, "v0.1.0-alpha.0\n")
558561
fmt.Fprint(w, "v0.1.0-alpha.1\n")
559562
fmt.Fprint(w, "v0.1.0-alpha.2\n")
@@ -621,12 +624,13 @@ func Test_gitHubRepository_getLatestRelease(t *testing.T) {
621624
func Test_gitHubRepository_getLatestPatchRelease(t *testing.T) {
622625
retryableOperationInterval = 200 * time.Millisecond
623626
retryableOperationTimeout = 1 * time.Second
624-
clientGoproxy, muxGoproxy, teardownGoproxy := newFakeGoproxy()
627+
scheme, host, muxGoproxy, teardownGoproxy := goproxytest.NewFakeGoproxy()
628+
clientGoproxy := goproxy.NewClient(scheme, host)
625629
defer teardownGoproxy()
626630

627631
// Setup a handler for returning 4 fake releases.
628632
muxGoproxy.HandleFunc("/github.com/o/r1/@v/list", func(w http.ResponseWriter, r *http.Request) {
629-
testMethod(t, r, "GET")
633+
goproxytest.HTTPTestMethod(t, r, "GET")
630634
fmt.Fprint(w, "v0.4.0\n")
631635
fmt.Fprint(w, "v0.3.2\n")
632636
fmt.Fprint(w, "v1.3.2\n")
@@ -712,7 +716,7 @@ func Test_gitHubRepository_getReleaseByTag(t *testing.T) {
712716

713717
// Setup a handler for returning a fake release.
714718
mux.HandleFunc("/repos/o/r/releases/tags/foo", func(w http.ResponseWriter, r *http.Request) {
715-
testMethod(t, r, "GET")
719+
goproxytest.HTTPTestMethod(t, r, "GET")
716720
fmt.Fprint(w, `{"id":13, "tag_name": "v0.4.1"}`)
717721
})
718722

@@ -783,14 +787,14 @@ func Test_gitHubRepository_downloadFilesFromRelease(t *testing.T) {
783787

784788
// Setup a handler for returning a fake release asset.
785789
mux.HandleFunc("/repos/o/r/releases/assets/1", func(w http.ResponseWriter, r *http.Request) {
786-
testMethod(t, r, "GET")
790+
goproxytest.HTTPTestMethod(t, r, "GET")
787791
w.Header().Set("Content-Type", "application/octet-stream")
788792
w.Header().Set("Content-Disposition", "attachment; filename=file.yaml")
789793
fmt.Fprint(w, "content")
790794
})
791795
// Setup a handler which redirects to a different location.
792796
mux.HandleFunc("/repos/o/r-with-redirect/releases/assets/1", func(w http.ResponseWriter, r *http.Request) {
793-
testMethod(t, r, "GET")
797+
goproxytest.HTTPTestMethod(t, r, "GET")
794798
http.Redirect(w, r, "/api-v3/repos/o/r/releases/assets/1", http.StatusFound)
795799
})
796800

@@ -903,35 +907,9 @@ func Test_gitHubRepository_downloadFilesFromRelease(t *testing.T) {
903907
}
904908
}
905909

906-
func testMethod(t *testing.T, r *http.Request, want string) {
907-
t.Helper()
908-
909-
if got := r.Method; got != want {
910-
t.Errorf("Request method: %v, want %v", got, want)
911-
}
912-
}
913-
914910
// resetCaches is called repeatedly throughout tests to help avoid cross-test pollution.
915911
func resetCaches() {
916912
cacheVersions = map[string][]string{}
917913
cacheReleases = map[string]*github.RepositoryRelease{}
918914
cacheFiles = map[string][]byte{}
919915
}
920-
921-
// newFakeGoproxy sets up a test HTTP server along with a github.Client that is
922-
// configured to talk to that test server. Tests should register handlers on
923-
// mux which provide mock responses for the API method being tested.
924-
func newFakeGoproxy() (client *goproxy.Client, mux *http.ServeMux, teardown func()) {
925-
// mux is the HTTP request multiplexer used with the test server.
926-
mux = http.NewServeMux()
927-
928-
apiHandler := http.NewServeMux()
929-
apiHandler.Handle("/", mux)
930-
931-
// server is a test HTTP server used to provide mock API responses.
932-
server := httptest.NewServer(apiHandler)
933-
934-
// client is the GitHub client being tested and is configured to use test server.
935-
url, _ := url.Parse(server.URL + "/")
936-
return goproxy.NewClient(url.Scheme, url.Host), mux, server.Close
937-
}

cmd/clusterctl/client/repository/repository_gitlab_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
clusterctlv1 "sigs.k8s.io/cluster-api/cmd/clusterctl/api/v1alpha3"
2929
"sigs.k8s.io/cluster-api/cmd/clusterctl/client/config"
3030
"sigs.k8s.io/cluster-api/cmd/clusterctl/internal/test"
31+
goproxytest "sigs.k8s.io/cluster-api/internal/goproxy/test"
3132
)
3233

3334
func Test_gitLabRepository_newGitLabRepository(t *testing.T) {
@@ -152,7 +153,7 @@ func Test_gitLabRepository_getFile(t *testing.T) {
152153
providerConfig := config.NewProvider("test", providerURL, clusterctlv1.CoreProviderType)
153154

154155
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
155-
testMethod(t, r, "GET")
156+
goproxytest.HTTPTestMethod(t, r, "GET")
156157
if r.URL.RawPath == "/api/v4/projects/group%2Fproject/packages/generic/my-package/v0.4.1/file.yaml" {
157158
w.Header().Set("Content-Type", "application/octet-stream")
158159
w.Header().Set("Content-Disposition", "attachment; filename=file.yaml")

internal/goproxy/goproxy_test.go

Lines changed: 7 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -20,38 +20,39 @@ import (
2020
"context"
2121
"fmt"
2222
"net/http"
23-
"net/http/httptest"
24-
"net/url"
2523
"testing"
2624
"time"
2725

2826
"github.com/blang/semver/v4"
2927
. "github.com/onsi/gomega"
28+
29+
goproxytest "sigs.k8s.io/cluster-api/internal/goproxy/test"
3030
)
3131

3232
func TestClient_GetVersions(t *testing.T) {
3333
retryableOperationInterval = 200 * time.Millisecond
3434
retryableOperationTimeout = 1 * time.Second
3535

36-
clientGoproxy, muxGoproxy, teardownGoproxy := NewFakeGoproxy()
36+
scheme, host, muxGoproxy, teardownGoproxy := goproxytest.NewFakeGoproxy()
37+
clientGoproxy := NewClient(scheme, host)
3738
defer teardownGoproxy()
3839

3940
// setup an handler for returning 2 fake releases
4041
muxGoproxy.HandleFunc("/github.com/o/r1/@v/list", func(w http.ResponseWriter, r *http.Request) {
41-
testMethod(t, r, "GET")
42+
goproxytest.HTTPTestMethod(t, r, "GET")
4243
fmt.Fprint(w, "v1.1.0\n")
4344
fmt.Fprint(w, "v0.2.0\n")
4445
})
4546

4647
// setup an handler for returning 2 fake releases for v1
4748
muxGoproxy.HandleFunc("/github.com/o/r2/@v/list", func(w http.ResponseWriter, r *http.Request) {
48-
testMethod(t, r, "GET")
49+
goproxytest.HTTPTestMethod(t, r, "GET")
4950
fmt.Fprint(w, "v1.1.0\n")
5051
fmt.Fprint(w, "v0.2.0\n")
5152
})
5253
// setup an handler for returning 2 fake releases for v2
5354
muxGoproxy.HandleFunc("/github.com/o/r2/v2/@v/list", func(w http.ResponseWriter, r *http.Request) {
54-
testMethod(t, r, "GET")
55+
goproxytest.HTTPTestMethod(t, r, "GET")
5556
fmt.Fprint(w, "v2.0.1\n")
5657
fmt.Fprint(w, "v2.0.0\n")
5758
})
@@ -182,29 +183,3 @@ func Test_GetGoproxyHost(t *testing.T) {
182183
})
183184
}
184185
}
185-
186-
// NewFakeGoproxy sets up a test HTTP server along with a github.Client that is
187-
// configured to talk to that test server. Tests should register handlers on
188-
// mux which provide mock responses for the API method being tested.
189-
func NewFakeGoproxy() (client *Client, mux *http.ServeMux, teardown func()) {
190-
// mux is the HTTP request multiplexer used with the test server.
191-
mux = http.NewServeMux()
192-
193-
apiHandler := http.NewServeMux()
194-
apiHandler.Handle("/", mux)
195-
196-
// server is a test HTTP server used to provide mock API responses.
197-
server := httptest.NewServer(apiHandler)
198-
199-
// client is the GitHub client being tested and is configured to use test server.
200-
url, _ := url.Parse(server.URL + "/")
201-
return NewClient(url.Scheme, url.Host), mux, server.Close
202-
}
203-
204-
func testMethod(t *testing.T, r *http.Request, want string) {
205-
t.Helper()
206-
207-
if got := r.Method; got != want {
208-
t.Errorf("Request method: %v, want %v", got, want)
209-
}
210-
}

internal/goproxy/test/test_utils.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
Copyright 2020 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
// Package test contains test util functions for goproxy.
18+
package test
19+
20+
import (
21+
"net/http"
22+
"net/http/httptest"
23+
"net/url"
24+
"testing"
25+
)
26+
27+
// NewFakeGoproxy sets up a test HTTP server along with a github.Client that is
28+
// configured to talk to that test server. Tests should register handlers on
29+
// mux which provide mock responses for the API method being tested.
30+
func NewFakeGoproxy() (scheme string, host string, mux *http.ServeMux, teardown func()) {
31+
// mux is the HTTP request multiplexer used with the test server.
32+
mux = http.NewServeMux()
33+
34+
apiHandler := http.NewServeMux()
35+
apiHandler.Handle("/", mux)
36+
37+
// server is a test HTTP server used to provide mock API responses.
38+
server := httptest.NewServer(apiHandler)
39+
40+
// client is the GitHub client being tested and is configured to use test server.
41+
url, _ := url.Parse(server.URL + "/")
42+
return url.Scheme, url.Host, mux, server.Close
43+
}
44+
45+
// HTTPTestMethod reports error http.Request does not return want string.
46+
func HTTPTestMethod(t *testing.T, r *http.Request, want string) {
47+
t.Helper()
48+
49+
if got := r.Method; got != want {
50+
t.Errorf("Request method: %v, want %v", got, want)
51+
}
52+
}

test/e2e/clusterctl_upgrade.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ func ClusterctlUpgradeSpec(ctx context.Context, inputGetter func() ClusterctlUpg
187187

188188
clusterctlBinaryURLTemplate := input.InitWithBinary
189189
if clusterctlBinaryURLTemplate == "" {
190+
Expect(input.E2EConfig.ResolveReleases(ctx)).To(Succeed(), "Failed to resolve release markers in e2e test config file")
190191
Expect(input.E2EConfig.Variables).To(HaveKey(initWithBinaryVariableName), "Invalid argument. %s variable must be defined when calling %s spec", initWithBinaryVariableName, specName)
191192
Expect(input.E2EConfig.Variables[initWithBinaryVariableName]).ToNot(BeEmpty(), "Invalid argument. %s variable can't be empty when calling %s spec", initWithBinaryVariableName, specName)
192193
clusterctlBinaryURLTemplate = input.E2EConfig.GetVariable(initWithBinaryVariableName)

0 commit comments

Comments
 (0)