-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
46412f0
commit 4ac1964
Showing
4 changed files
with
216 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
/* | ||
Copyright 2021 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package clusterctl | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"net/http/httptest" | ||
"net/url" | ||
"testing" | ||
|
||
. "github.com/onsi/gomega" | ||
"sigs.k8s.io/cluster-api/internal/goproxy" | ||
) | ||
|
||
func Test_resolveReleaseMarker(t *testing.T) { | ||
|
||
clientGoproxy, muxGoproxy, teardownGoproxy := newFakeGoproxy() | ||
defer teardownGoproxy() | ||
|
||
// setup an handler with fake releases | ||
muxGoproxy.HandleFunc("/github.com/o/r1/@v/list", func(w http.ResponseWriter, r *http.Request) { | ||
testMethod(t, r, "GET") | ||
fmt.Fprint(w, "v1.2.0\n") | ||
fmt.Fprint(w, "v1.2.1-rc.0\n") | ||
|
||
}) | ||
tests := []struct { | ||
name string | ||
releaseMarker string | ||
want string | ||
wantErr bool | ||
}{ | ||
{ | ||
"Invalid url", | ||
"github.com/o/doesntexist", | ||
"", | ||
true, | ||
}, | ||
{ | ||
"Get stable release", | ||
//"go://sigs.k8s.io/cluster-api@v1.0", | ||
"go://github.com/o/r1@v1.2", | ||
"1.2.0", | ||
false, | ||
}, | ||
{ | ||
"Get latest release", | ||
//"go://sigs.k8s.io/cluster-api@v1.0", | ||
"go://github.com/o/r1@latest-v1.2", | ||
"1.2.1-rc.0", | ||
false, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
g := NewWithT(t) | ||
|
||
got, err := resolveReleaseMarker(tt.releaseMarker, clientGoproxy) | ||
if tt.wantErr { | ||
g.Expect(err).To(HaveOccurred()) | ||
return | ||
} | ||
g.Expect(err).ToNot(HaveOccurred()) | ||
|
||
g.Expect(got).To(BeEquivalentTo(tt.want)) | ||
}) | ||
} | ||
|
||
} | ||
|
||
func testMethod(t *testing.T, r *http.Request, want string) { | ||
t.Helper() | ||
|
||
if got := r.Method; got != want { | ||
t.Errorf("Request method: %v, want %v", got, want) | ||
} | ||
} | ||
|
||
// newFakeGoproxy sets up a test HTTP server along with a github.Client that is | ||
// configured to talk to that test server. Tests should register handlers on | ||
// mux which provide mock responses for the API method being tested. | ||
func newFakeGoproxy() (client *goproxy.Client, mux *http.ServeMux, teardown func()) { | ||
// mux is the HTTP request multiplexer used with the test server. | ||
mux = http.NewServeMux() | ||
|
||
apiHandler := http.NewServeMux() | ||
apiHandler.Handle("/", mux) | ||
|
||
// server is a test HTTP server used to provide mock API responses. | ||
server := httptest.NewServer(apiHandler) | ||
|
||
// client is the GitHub client being tested and is configured to use test server. | ||
url, _ := url.Parse(server.URL + "/") | ||
return goproxy.NewClient(url.Scheme, url.Host), mux, server.Close | ||
} |