diff --git a/internal/providercache/installer_test.go b/internal/providercache/installer_test.go index 9bceda7338..8b4f7562aa 100644 --- a/internal/providercache/installer_test.go +++ b/internal/providercache/installer_test.go @@ -2745,6 +2745,11 @@ func testRegistrySource(t *testing.T) (source *getproviders.RegistrySource, base } func fakeRegistryHandler(resp http.ResponseWriter, req *http.Request) { + write := func(data []byte) { + if _, err := resp.Write(data); err != nil { + panic(err) + } + } path := req.URL.EscapedPath() if strings.HasPrefix(path, "/fails-immediately/") { // Here we take over the socket and just close it immediately, to @@ -2754,13 +2759,13 @@ func fakeRegistryHandler(resp http.ResponseWriter, req *http.Request) { // Not hijackable, so we'll just fail normally. // If this happens, tests relying on this will fail. resp.WriteHeader(500) - resp.Write([]byte(`cannot hijack`)) + write([]byte(`cannot hijack`)) return } conn, _, err := hijacker.Hijack() if err != nil { resp.WriteHeader(500) - resp.Write([]byte(`hijack failed`)) + write([]byte(`hijack failed`)) return } conn.Close() @@ -2770,28 +2775,28 @@ func fakeRegistryHandler(resp http.ResponseWriter, req *http.Request) { if strings.HasPrefix(path, "/pkg/") { switch path { case "/pkg/awesomesauce/happycloud_1.2.0.zip": - resp.Write([]byte("some zip file")) + write([]byte("some zip file")) case "/pkg/awesomesauce/happycloud_1.2.0_SHA256SUMS": - resp.Write([]byte("000000000000000000000000000000000000000000000000000000000000f00d happycloud_1.2.0.zip\n")) + write([]byte("000000000000000000000000000000000000000000000000000000000000f00d happycloud_1.2.0.zip\n")) case "/pkg/awesomesauce/happycloud_1.2.0_SHA256SUMS.sig": - resp.Write([]byte("GPG signature")) + write([]byte("GPG signature")) default: resp.WriteHeader(404) - resp.Write([]byte("unknown package file download")) + write([]byte("unknown package file download")) } return } if !strings.HasPrefix(path, "/providers/v1/") { resp.WriteHeader(404) - resp.Write([]byte(`not a provider registry endpoint`)) + write([]byte(`not a provider registry endpoint`)) return } pathParts := strings.Split(path, "/")[3:] if len(pathParts) < 2 { resp.WriteHeader(404) - resp.Write([]byte(`unexpected number of path parts`)) + write([]byte(`unexpected number of path parts`)) return } log.Printf("[TRACE] fake provider registry request for %#v", pathParts) @@ -2804,24 +2809,24 @@ func fakeRegistryHandler(resp http.ResponseWriter, req *http.Request) { // registry host. resp.Header().Set("Content-Type", "application/json") resp.WriteHeader(200) - resp.Write([]byte(`{"namespace":"legacycorp"}`)) + write([]byte(`{"namespace":"legacycorp"}`)) default: resp.WriteHeader(404) - resp.Write([]byte(`unknown namespace or provider type for direct lookup`)) + write([]byte(`unknown namespace or provider type for direct lookup`)) } } if len(pathParts) < 3 { resp.WriteHeader(404) - resp.Write([]byte(`unexpected number of path parts`)) + write([]byte(`unexpected number of path parts`)) return } if pathParts[2] == "versions" { if len(pathParts) != 3 { resp.WriteHeader(404) - resp.Write([]byte(`extraneous path parts`)) + write([]byte(`extraneous path parts`)) return } @@ -2832,18 +2837,18 @@ func fakeRegistryHandler(resp http.ResponseWriter, req *http.Request) { // Note that these version numbers are intentionally misordered // so we can test that the client-side code places them in the // correct order (lowest precedence first). - resp.Write([]byte(`{"versions":[{"version":"0.1.0","protocols":["1.0"]},{"version":"2.0.0","protocols":["99.0"]},{"version":"1.2.0","protocols":["5.0"]}, {"version":"1.0.0","protocols":["5.0"]}]}`)) + write([]byte(`{"versions":[{"version":"0.1.0","protocols":["1.0"]},{"version":"2.0.0","protocols":["99.0"]},{"version":"1.2.0","protocols":["5.0"]}, {"version":"1.0.0","protocols":["5.0"]}]}`)) case "weaksauce/unsupported-protocol": resp.Header().Set("Content-Type", "application/json") resp.WriteHeader(200) - resp.Write([]byte(`{"versions":[{"version":"0.1.0","protocols":["0.1"]}]}`)) + write([]byte(`{"versions":[{"version":"0.1.0","protocols":["0.1"]}]}`)) case "weaksauce/no-versions": resp.Header().Set("Content-Type", "application/json") resp.WriteHeader(200) - resp.Write([]byte(`{"versions":[]}`)) + write([]byte(`{"versions":[]}`)) default: resp.WriteHeader(404) - resp.Write([]byte(`unknown namespace or provider type`)) + write([]byte(`unknown namespace or provider type`)) } return } @@ -2853,7 +2858,7 @@ func fakeRegistryHandler(resp http.ResponseWriter, req *http.Request) { case "awesomesauce/happycloud": if pathParts[4] == "nonexist" { resp.WriteHeader(404) - resp.Write([]byte(`unsupported OS`)) + write([]byte(`unsupported OS`)) return } version := pathParts[2] @@ -2877,11 +2882,11 @@ func fakeRegistryHandler(resp http.ResponseWriter, req *http.Request) { enc, err := json.Marshal(body) if err != nil { resp.WriteHeader(500) - resp.Write([]byte("failed to encode body")) + write([]byte("failed to encode body")) } resp.Header().Set("Content-Type", "application/json") resp.WriteHeader(200) - resp.Write(enc) + write(enc) case "weaksauce/unsupported-protocol": var protocols []string version := pathParts[2] @@ -2914,20 +2919,20 @@ func fakeRegistryHandler(resp http.ResponseWriter, req *http.Request) { enc, err := json.Marshal(body) if err != nil { resp.WriteHeader(500) - resp.Write([]byte("failed to encode body")) + write([]byte("failed to encode body")) } resp.Header().Set("Content-Type", "application/json") resp.WriteHeader(200) - resp.Write(enc) + write(enc) default: resp.WriteHeader(404) - resp.Write([]byte(`unknown namespace/provider/version/architecture`)) + write([]byte(`unknown namespace/provider/version/architecture`)) } return } resp.WriteHeader(404) - resp.Write([]byte(`unrecognized path scheme`)) + write([]byte(`unrecognized path scheme`)) } // In order to be able to compare the recorded temp dir paths, we need to