Merge pull request #126579 from my-git9/ut-version

kubeadm: increase ut coverage for util/version
This commit is contained in:
Kubernetes Prow Robot 2024-08-13 22:11:41 -07:00 committed by GitHub
commit 82cfcbf86d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -18,6 +18,8 @@ package util
import (
"fmt"
"net/http"
"net/http/httptest"
"os"
"path"
"strings"
@ -525,3 +527,72 @@ func TestValidateStableVersion(t *testing.T) {
func errorFetcher(url string, timeout time.Duration) (string, error) {
return "should not make internet calls", errors.Errorf("should not make internet calls, tried to request url: %s", url)
}
func TestFetchFromURL(t *testing.T) {
tests := []struct {
name string
url string
expected string
timeout time.Duration
code int
body string
expectErr bool
}{
{
name: "normal success",
url: "/normal",
code: http.StatusOK,
body: "normal response",
expected: "normal response",
expectErr: false,
},
{
name: "HTTP error status",
url: "/error",
code: http.StatusBadRequest,
body: "bad request",
expected: "bad request",
expectErr: true,
},
{
name: "Request timeout",
url: "/timeout",
timeout: time.Millisecond * 50,
expectErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if tt.code != 0 {
w.WriteHeader(tt.code)
}
if tt.body != "" {
if _, err := w.Write([]byte(tt.body)); err != nil {
t.Error("Write body failed.")
}
}
if tt.timeout == time.Millisecond*50 {
time.Sleep(time.Millisecond * 200)
w.WriteHeader(http.StatusOK)
if _, err := w.Write([]byte("Delayed response")); err != nil {
t.Error("Write body failed.")
}
}
})
ts := httptest.NewServer(handler)
defer ts.Close()
url := ts.URL + tt.url
result, err := fetchFromURL(url, tt.timeout)
if (err != nil) != tt.expectErr {
t.Errorf("expected error: %v, got: %v", tt.expectErr, err)
}
if tt.expected != result {
t.Errorf("expected result: %q, got: %q", tt.expected, result)
}
})
}
}