@@ -20,8 +20,6 @@ import (
20
20
"context"
21
21
"fmt"
22
22
"net/http"
23
- "net/http/httptest"
24
- "net/url"
25
23
"testing"
26
24
"time"
27
25
@@ -33,6 +31,7 @@ import (
33
31
"sigs.k8s.io/cluster-api/cmd/clusterctl/client/config"
34
32
"sigs.k8s.io/cluster-api/cmd/clusterctl/internal/test"
35
33
"sigs.k8s.io/cluster-api/internal/goproxy"
34
+ goproxytest "sigs.k8s.io/cluster-api/internal/goproxy/test"
36
35
)
37
36
38
37
func Test_gitHubRepository_GetVersions (t * testing.T ) {
@@ -44,7 +43,7 @@ func Test_gitHubRepository_GetVersions(t *testing.T) {
44
43
45
44
// Setup an handler for returning 5 fake releases.
46
45
mux .HandleFunc ("/repos/o/r1/releases" , func (w http.ResponseWriter , r * http.Request ) {
47
- testMethod (t , r , "GET" )
46
+ goproxytest . HTTPTestMethod (t , r , "GET" )
48
47
fmt .Fprint (w , `[` )
49
48
fmt .Fprint (w , `{"id":1, "tag_name": "v0.4.0"},` )
50
49
fmt .Fprint (w , `{"id":2, "tag_name": "v0.4.1"},` )
@@ -53,12 +52,13 @@ func Test_gitHubRepository_GetVersions(t *testing.T) {
53
52
fmt .Fprint (w , `]` )
54
53
})
55
54
56
- clientGoproxy , muxGoproxy , teardownGoproxy := newFakeGoproxy ()
55
+ scheme , host , muxGoproxy , teardownGoproxy := goproxytest .NewFakeGoproxy ()
56
+ clientGoproxy := goproxy .NewClient (scheme , host )
57
57
defer teardownGoproxy ()
58
58
59
59
// Setup a handler for returning 4 fake releases.
60
60
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" )
62
62
fmt .Fprint (w , "v0.5.0\n " )
63
63
fmt .Fprint (w , "v0.4.0\n " )
64
64
fmt .Fprint (w , "v0.3.2\n " )
@@ -67,16 +67,16 @@ func Test_gitHubRepository_GetVersions(t *testing.T) {
67
67
68
68
// Setup a handler for returning 3 different major fake releases.
69
69
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" )
71
71
fmt .Fprint (w , "v1.0.0\n " )
72
72
fmt .Fprint (w , "v0.1.0\n " )
73
73
})
74
74
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" )
76
76
fmt .Fprint (w , "v2.0.0\n " )
77
77
})
78
78
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" )
80
80
fmt .Fprint (w , "v3.0.0\n " )
81
81
})
82
82
@@ -271,13 +271,13 @@ func Test_githubRepository_getFile(t *testing.T) {
271
271
272
272
// Setup a handler for returning a fake release.
273
273
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" )
275
275
fmt .Fprint (w , `{"id":13, "tag_name": "v0.4.1", "assets": [{"id": 1, "name": "file.yaml"}] }` )
276
276
})
277
277
278
278
// Setup a handler for returning a fake release asset.
279
279
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" )
281
281
w .Header ().Set ("Content-Type" , "application/octet-stream" )
282
282
w .Header ().Set ("Content-Disposition" , "attachment; filename=file.yaml" )
283
283
fmt .Fprint (w , "content" )
@@ -345,7 +345,7 @@ func Test_gitHubRepository_getVersions(t *testing.T) {
345
345
// Each response contains a link to the next page (if available) which
346
346
// is parsed by the handler to navigate through all pages
347
347
mux .HandleFunc ("/repos/o/r1/releases" , func (w http.ResponseWriter , r * http.Request ) {
348
- testMethod (t , r , "GET" )
348
+ goproxytest . HTTPTestMethod (t , r , "GET" )
349
349
page := r .URL .Query ().Get ("page" )
350
350
switch page {
351
351
case "" , "1" :
@@ -424,24 +424,26 @@ func Test_gitHubRepository_getLatestContractRelease(t *testing.T) {
424
424
425
425
// Setup a handler for returning a fake release.
426
426
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" )
428
428
fmt .Fprint (w , `{"id":13, "tag_name": "v0.5.0", "assets": [{"id": 1, "name": "metadata.yaml"}] }` )
429
429
})
430
430
431
431
// Setup a handler for returning a fake release metadata file.
432
432
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" )
434
434
w .Header ().Set ("Content-Type" , "application/octet-stream" )
435
435
w .Header ().Set ("Content-Disposition" , "attachment; filename=metadata.yaml" )
436
436
fmt .Fprint (w , "apiVersion: clusterctl.cluster.x-k8s.io/v1alpha3\n releaseSeries:\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 " )
437
437
})
438
438
439
- clientGoproxy , muxGoproxy , teardownGoproxy := newFakeGoproxy ()
439
+ scheme , host , muxGoproxy , teardownGoproxy := goproxytest .NewFakeGoproxy ()
440
+ clientGoproxy := goproxy .NewClient (scheme , host )
441
+
440
442
defer teardownGoproxy ()
441
443
442
444
// Setup a handler for returning 4 fake releases.
443
445
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" )
445
447
fmt .Fprint (w , "v0.5.0\n " )
446
448
fmt .Fprint (w , "v0.4.0\n " )
447
449
fmt .Fprint (w , "v0.3.2\n " )
@@ -450,7 +452,7 @@ func Test_gitHubRepository_getLatestContractRelease(t *testing.T) {
450
452
451
453
// setup an handler for returning 4 fake releases but no actual tagged release
452
454
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" )
454
456
fmt .Fprint (w , "v0.5.0\n " )
455
457
fmt .Fprint (w , "v0.4.0\n " )
456
458
fmt .Fprint (w , "v0.3.2\n " )
@@ -528,32 +530,33 @@ func Test_gitHubRepository_getLatestContractRelease(t *testing.T) {
528
530
func Test_gitHubRepository_getLatestRelease (t * testing.T ) {
529
531
retryableOperationInterval = 200 * time .Millisecond
530
532
retryableOperationTimeout = 1 * time .Second
531
- clientGoproxy , muxGoproxy , teardownGoproxy := newFakeGoproxy ()
533
+ scheme , host , muxGoproxy , teardownGoproxy := goproxytest .NewFakeGoproxy ()
534
+ clientGoproxy := goproxy .NewClient (scheme , host )
532
535
defer teardownGoproxy ()
533
536
534
537
// Setup a handler for returning 4 fake releases.
535
538
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" )
537
540
fmt .Fprint (w , "v0.4.1\n " )
538
541
fmt .Fprint (w , "v0.4.2\n " )
539
542
fmt .Fprint (w , "v0.4.3-alpha\n " ) // prerelease
540
543
fmt .Fprint (w , "foo\n " ) // no semantic version tag
541
544
})
542
545
// And also expose a release for them
543
546
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" )
545
548
fmt .Fprint (w , "{}\n " )
546
549
})
547
550
548
551
// Setup a handler for returning no releases.
549
552
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" )
551
554
// no releases
552
555
})
553
556
554
557
// Setup a handler for returning fake prereleases only.
555
558
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" )
557
560
fmt .Fprint (w , "v0.1.0-alpha.0\n " )
558
561
fmt .Fprint (w , "v0.1.0-alpha.1\n " )
559
562
fmt .Fprint (w , "v0.1.0-alpha.2\n " )
@@ -621,12 +624,13 @@ func Test_gitHubRepository_getLatestRelease(t *testing.T) {
621
624
func Test_gitHubRepository_getLatestPatchRelease (t * testing.T ) {
622
625
retryableOperationInterval = 200 * time .Millisecond
623
626
retryableOperationTimeout = 1 * time .Second
624
- clientGoproxy , muxGoproxy , teardownGoproxy := newFakeGoproxy ()
627
+ scheme , host , muxGoproxy , teardownGoproxy := goproxytest .NewFakeGoproxy ()
628
+ clientGoproxy := goproxy .NewClient (scheme , host )
625
629
defer teardownGoproxy ()
626
630
627
631
// Setup a handler for returning 4 fake releases.
628
632
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" )
630
634
fmt .Fprint (w , "v0.4.0\n " )
631
635
fmt .Fprint (w , "v0.3.2\n " )
632
636
fmt .Fprint (w , "v1.3.2\n " )
@@ -712,7 +716,7 @@ func Test_gitHubRepository_getReleaseByTag(t *testing.T) {
712
716
713
717
// Setup a handler for returning a fake release.
714
718
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" )
716
720
fmt .Fprint (w , `{"id":13, "tag_name": "v0.4.1"}` )
717
721
})
718
722
@@ -783,14 +787,14 @@ func Test_gitHubRepository_downloadFilesFromRelease(t *testing.T) {
783
787
784
788
// Setup a handler for returning a fake release asset.
785
789
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" )
787
791
w .Header ().Set ("Content-Type" , "application/octet-stream" )
788
792
w .Header ().Set ("Content-Disposition" , "attachment; filename=file.yaml" )
789
793
fmt .Fprint (w , "content" )
790
794
})
791
795
// Setup a handler which redirects to a different location.
792
796
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" )
794
798
http .Redirect (w , r , "/api-v3/repos/o/r/releases/assets/1" , http .StatusFound )
795
799
})
796
800
@@ -903,35 +907,9 @@ func Test_gitHubRepository_downloadFilesFromRelease(t *testing.T) {
903
907
}
904
908
}
905
909
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
-
914
910
// resetCaches is called repeatedly throughout tests to help avoid cross-test pollution.
915
911
func resetCaches () {
916
912
cacheVersions = map [string ][]string {}
917
913
cacheReleases = map [string ]* github.RepositoryRelease {}
918
914
cacheFiles = map [string ][]byte {}
919
915
}
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
- }
0 commit comments