From 43596c9cfbc87825e862eb6dba8602939c85d65a Mon Sep 17 00:00:00 2001 From: Yann Hamon Date: Sat, 27 Jul 2024 15:48:18 +0200 Subject: [PATCH] Iterate on new DownloadSchema testing --- pkg/registry/http_test.go | 99 +++++++++++++++++---------------------- 1 file changed, 44 insertions(+), 55 deletions(-) diff --git a/pkg/registry/http_test.go b/pkg/registry/http_test.go index b91e7e4..61b02b8 100644 --- a/pkg/registry/http_test.go +++ b/pkg/registry/http_test.go @@ -5,7 +5,6 @@ import ( "fmt" "math/rand" "net/http" - "os" "testing" "time" ) @@ -27,74 +26,70 @@ func (m *mockHTTPGetter) Get(url string) (resp *http.Response, err error) { } func TestDownloadSchema(t *testing.T) { - - firstAttempt := true + callCounts := map[string]int{} // http server to simulate different responses http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { - // Check if the request is asking to simulate a connection reset or 503 - // then return that status code once and then return a normal response - if r.URL.Path == "/simulate-reset" || r.URL.Path == "/503" { - if firstAttempt { - firstAttempt = false - if r.URL.Path == "/simulate-reset" { - if hj, ok := w.(http.Hijacker); ok { - conn, _, err := hj.Hijack() - if err != nil { - fmt.Printf("Hijacking failed: %v\n", err) - return - } - conn.Close() // Close the connection to simulate a reset + var s int + callCounts[r.URL.Path]++ + callCount := callCounts[r.URL.Path] + + switch r.URL.Path { + case "/404": + s = http.StatusNotFound + case "/500": + s = http.StatusInternalServerError + case "/503": + if callCount < 2 { + s = http.StatusServiceUnavailable + } else { + s = http.StatusOK // Should succeed on 3rd try + } + + case "/simulate-reset": + if callCount < 2 { + if hj, ok := w.(http.Hijacker); ok { + conn, _, err := hj.Hijack() + if err != nil { + fmt.Printf("Hijacking failed: %v\n", err) + return } - } - if r.URL.Path == "/503" { - w.WriteHeader(http.StatusServiceUnavailable) - w.Write([]byte("503 Service Unavailable")) + conn.Close() // Close the connection to simulate a reset } return - } else { - firstAttempt = true - w.WriteHeader(http.StatusOK) - w.Write([]byte("Normal response")) - return } - } - - if r.URL.Path == "/404" { - w.WriteHeader(http.StatusNotFound) - w.Write([]byte("404 Not Found")) - return - } + s = http.StatusOK // Should succeed on third try - if r.URL.Path == "/500" { - w.WriteHeader(http.StatusInternalServerError) - w.Write([]byte("500 Internal Server Error")) - return + default: + s = http.StatusOK } - // Serve a normal response - w.WriteHeader(http.StatusOK) - w.Write([]byte("Normal response")) + w.WriteHeader(s) + w.Write([]byte(http.StatusText(s))) }) port := fmt.Sprint(rand.Intn(1000) + 9000) // random port server := &http.Server{Addr: "127.0.0.1:" + port} + url := fmt.Sprintf("http://localhost:%s", port) go func() { if err := server.ListenAndServe(); err != nil { fmt.Printf("Failed to start server: %v\n", err) } }() - - url := fmt.Sprintf("http://localhost:%s", port) + defer server.Shutdown(nil) // Wait for the server to start for i := 0; i < 20; i++ { - fmt.Printf("Trying to connect to server %d ...\n", i) - _, err := http.Get(url) - if err == nil { + if _, err := http.Get(url); err == nil { break } + + if i == 19 { + t.Error("http server did not start") + return + } + time.Sleep(50 * time.Millisecond) } @@ -113,7 +108,7 @@ func TestDownloadSchema(t *testing.T) { "Deployment", "v1", "1.18.0", - []byte("Normal response"), + []byte(http.StatusText(http.StatusOK)), nil, }, { @@ -143,7 +138,7 @@ func TestDownloadSchema(t *testing.T) { "Deployment", "v1", "1.18.0", - []byte("Normal response"), + []byte(http.StatusText(http.StatusOK)), nil, }, { @@ -153,19 +148,13 @@ func TestDownloadSchema(t *testing.T) { "Deployment", "v1", "1.18.0", - []byte("Normal response"), + []byte(http.StatusText(http.StatusOK)), nil, }, } { - // create a temporary directory for the cache - tmpDir, err := os.MkdirTemp("", "kubeconform-cache") - if err != nil { - t.Errorf("during test '%s': failed to create temp directory: %s", testCase.name, err) - continue - } - defer os.RemoveAll(tmpDir) // clean up the temporary directory + callCounts = map[string]int{} // Reinitialise counters - reg, err := newHTTPRegistry(testCase.schemaPathTemplate, tmpDir, testCase.strict, true, true) + reg, err := newHTTPRegistry(testCase.schemaPathTemplate, "", testCase.strict, true, true) if err != nil { t.Errorf("during test '%s': failed to create registry: %s", testCase.name, err) continue