diff --git a/.golangci.yml b/.golangci.yml index b8c21d815..ef4578628 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -27,6 +27,7 @@ linters: - revive - staticcheck - unused + - usestdlibvars exclusions: # Helm, and the Go source code itself, sometimes uses these names outside their built-in diff --git a/internal/monocular/search.go b/internal/monocular/search.go index 6912be2ce..fcf04b7a4 100644 --- a/internal/monocular/search.go +++ b/internal/monocular/search.go @@ -129,7 +129,7 @@ func (c *Client) Search(term string) ([]SearchResult, error) { } defer res.Body.Close() - if res.StatusCode != 200 { + if res.StatusCode != http.StatusOK { return nil, fmt.Errorf("failed to fetch %s : %s", p.String(), res.Status) } diff --git a/pkg/kube/client_test.go b/pkg/kube/client_test.go index 56c7eebc9..a2c52c1f3 100644 --- a/pkg/kube/client_test.go +++ b/pkg/kube/client_test.go @@ -41,8 +41,10 @@ import ( cmdtesting "k8s.io/kubectl/pkg/cmd/testing" ) -var unstructuredSerializer = resource.UnstructuredPlusDefaultContentConfig().NegotiatedSerializer -var codec = scheme.Codecs.LegacyCodec(scheme.Scheme.PrioritizedVersionsAllGroups()...) +var ( + unstructuredSerializer = resource.UnstructuredPlusDefaultContentConfig().NegotiatedSerializer + codec = scheme.Codecs.LegacyCodec(scheme.Scheme.PrioritizedVersionsAllGroups()...) +) func objBody(obj runtime.Object) io.ReadCloser { return io.NopCloser(bytes.NewReader([]byte(runtime.EncodeOrDie(codec, obj)))) @@ -138,15 +140,15 @@ func TestCreate(t *testing.T) { actions = append(actions, path+":"+method) t.Logf("got request %s %s", path, method) switch { - case path == "/namespaces/default/pods" && method == "POST": + case path == "/namespaces/default/pods" && method == http.MethodPost: if strings.Contains(body, "starfish") { if iterationCounter < 2 { iterationCounter++ - return newResponseJSON(409, resourceQuotaConflict) + return newResponseJSON(http.StatusConflict, resourceQuotaConflict) } - return newResponse(200, &listA.Items[0]) + return newResponse(http.StatusOK, &listA.Items[0]) } - return newResponseJSON(409, resourceQuotaConflict) + return newResponseJSON(http.StatusConflict, resourceQuotaConflict) default: t.Fatalf("unexpected request: %s %s", method, path) return nil, nil @@ -230,11 +232,11 @@ func testUpdate(t *testing.T, threeWayMerge bool) { actions = append(actions, p+":"+m) t.Logf("got request %s %s", p, m) switch { - case p == "/namespaces/default/pods/starfish" && m == "GET": - return newResponse(200, &listA.Items[0]) - case p == "/namespaces/default/pods/otter" && m == "GET": - return newResponse(200, &listA.Items[1]) - case p == "/namespaces/default/pods/otter" && m == "PATCH": + case p == "/namespaces/default/pods/starfish" && m == http.MethodGet: + return newResponse(http.StatusOK, &listA.Items[0]) + case p == "/namespaces/default/pods/otter" && m == http.MethodGet: + return newResponse(http.StatusOK, &listA.Items[1]) + case p == "/namespaces/default/pods/otter" && m == http.MethodPatch: data, err := io.ReadAll(req.Body) if err != nil { t.Fatalf("could not dump request: %s", err) @@ -244,10 +246,10 @@ func testUpdate(t *testing.T, threeWayMerge bool) { if string(data) != expected { t.Errorf("expected patch\n%s\ngot\n%s", expected, string(data)) } - return newResponse(200, &listB.Items[0]) - case p == "/namespaces/default/pods/dolphin" && m == "GET": - return newResponse(404, notFoundBody()) - case p == "/namespaces/default/pods/starfish" && m == "PATCH": + return newResponse(http.StatusOK, &listB.Items[0]) + case p == "/namespaces/default/pods/dolphin" && m == http.MethodGet: + return newResponse(http.StatusNotFound, notFoundBody()) + case p == "/namespaces/default/pods/starfish" && m == http.MethodPatch: data, err := io.ReadAll(req.Body) if err != nil { t.Fatalf("could not dump request: %s", err) @@ -257,17 +259,17 @@ func testUpdate(t *testing.T, threeWayMerge bool) { if string(data) != expected { t.Errorf("expected patch\n%s\ngot\n%s", expected, string(data)) } - return newResponse(200, &listB.Items[0]) - case p == "/namespaces/default/pods" && m == "POST": + return newResponse(http.StatusOK, &listB.Items[0]) + case p == "/namespaces/default/pods" && m == http.MethodPost: if iterationCounter < 2 { iterationCounter++ - return newResponseJSON(409, resourceQuotaConflict) + return newResponseJSON(http.StatusConflict, resourceQuotaConflict) } - return newResponse(200, &listB.Items[1]) - case p == "/namespaces/default/pods/squid" && m == "DELETE": - return newResponse(200, &listB.Items[1]) - case p == "/namespaces/default/pods/squid" && m == "GET": - return newResponse(200, &listB.Items[2]) + return newResponse(http.StatusOK, &listB.Items[1]) + case p == "/namespaces/default/pods/squid" && m == http.MethodDelete: + return newResponse(http.StatusOK, &listB.Items[1]) + case p == "/namespaces/default/pods/squid" && m == http.MethodGet: + return newResponse(http.StatusOK, &listB.Items[2]) default: t.Fatalf("unexpected request: %s %s", req.Method, req.URL.Path) return nil, nil @@ -485,7 +487,7 @@ func TestWait(t *testing.T) { p, m := req.URL.Path, req.Method t.Logf("got request %s %s", p, m) switch { - case p == "/api/v1/namespaces/default/pods/starfish" && m == "GET": + case p == "/api/v1/namespaces/default/pods/starfish" && m == http.MethodGet: pod := &podList.Items[0] if created != nil && time.Since(*created) >= time.Second*5 { pod.Status.Conditions = []v1.PodCondition{ @@ -495,8 +497,8 @@ func TestWait(t *testing.T) { }, } } - return newResponse(200, pod) - case p == "/api/v1/namespaces/default/pods/otter" && m == "GET": + return newResponse(http.StatusOK, pod) + case p == "/api/v1/namespaces/default/pods/otter" && m == http.MethodGet: pod := &podList.Items[1] if created != nil && time.Since(*created) >= time.Second*5 { pod.Status.Conditions = []v1.PodCondition{ @@ -506,8 +508,8 @@ func TestWait(t *testing.T) { }, } } - return newResponse(200, pod) - case p == "/api/v1/namespaces/default/pods/squid" && m == "GET": + return newResponse(http.StatusOK, pod) + case p == "/api/v1/namespaces/default/pods/squid" && m == http.MethodGet: pod := &podList.Items[2] if created != nil && time.Since(*created) >= time.Second*5 { pod.Status.Conditions = []v1.PodCondition{ @@ -517,15 +519,15 @@ func TestWait(t *testing.T) { }, } } - return newResponse(200, pod) - case p == "/namespaces/default/pods" && m == "POST": + return newResponse(http.StatusOK, pod) + case p == "/namespaces/default/pods" && m == http.MethodPost: resources, err := c.Build(req.Body, false) if err != nil { t.Fatal(err) } now := time.Now() created = &now - return newResponse(200, resources[0].Object) + return newResponse(http.StatusOK, resources[0].Object) default: t.Fatalf("unexpected request: %s %s", req.Method, req.URL.Path) return nil, nil @@ -570,19 +572,19 @@ func TestWaitJob(t *testing.T) { p, m := req.URL.Path, req.Method t.Logf("got request %s %s", p, m) switch { - case p == "/apis/batch/v1/namespaces/default/jobs/starfish" && m == "GET": + case p == "/apis/batch/v1/namespaces/default/jobs/starfish" && m == http.MethodGet: if created != nil && time.Since(*created) >= time.Second*5 { job.Status.Succeeded = 1 } - return newResponse(200, job) - case p == "/namespaces/default/jobs" && m == "POST": + return newResponse(http.StatusOK, job) + case p == "/namespaces/default/jobs" && m == http.MethodPost: resources, err := c.Build(req.Body, false) if err != nil { t.Fatal(err) } now := time.Now() created = &now - return newResponse(200, resources[0].Object) + return newResponse(http.StatusOK, resources[0].Object) default: t.Fatalf("unexpected request: %s %s", req.Method, req.URL.Path) return nil, nil @@ -627,21 +629,21 @@ func TestWaitDelete(t *testing.T) { p, m := req.URL.Path, req.Method t.Logf("got request %s %s", p, m) switch { - case p == "/namespaces/default/pods/starfish" && m == "GET": + case p == "/namespaces/default/pods/starfish" && m == http.MethodGet: if deleted != nil && time.Since(*deleted) >= time.Second*5 { - return newResponse(404, notFoundBody()) + return newResponse(http.StatusNotFound, notFoundBody()) } - return newResponse(200, &pod) - case p == "/namespaces/default/pods/starfish" && m == "DELETE": + return newResponse(http.StatusOK, &pod) + case p == "/namespaces/default/pods/starfish" && m == http.MethodDelete: now := time.Now() deleted = &now - return newResponse(200, &pod) - case p == "/namespaces/default/pods" && m == "POST": + return newResponse(http.StatusOK, &pod) + case p == "/namespaces/default/pods" && m == http.MethodPost: resources, err := c.Build(req.Body, false) if err != nil { t.Fatal(err) } - return newResponse(200, resources[0].Object) + return newResponse(http.StatusOK, resources[0].Object) default: t.Fatalf("unexpected request: %s %s", req.Method, req.URL.Path) return nil, nil @@ -718,7 +720,6 @@ func TestReal(t *testing.T) { } func TestGetPodList(t *testing.T) { - namespace := "some-namespace" names := []string{"dave", "jimmy"} var responsePodList v1.PodList @@ -733,7 +734,6 @@ func TestGetPodList(t *testing.T) { clientAssertions := assert.New(t) clientAssertions.NoError(err) clientAssertions.Equal(&responsePodList, podList) - } func TestOutputContainerLogsForPodList(t *testing.T) { @@ -964,7 +964,7 @@ func (c createPatchTestCase) run(t *testing.T) { restClient := &fake.RESTClient{ NegotiatedSerializer: unstructuredSerializer, Resp: &http.Response{ - StatusCode: 200, + StatusCode: http.StatusOK, Body: objBody(c.actual), Header: header, }, diff --git a/pkg/registry/utils_test.go b/pkg/registry/utils_test.go index fe07c769a..174d7ccd1 100644 --- a/pkg/registry/utils_test.go +++ b/pkg/registry/utils_test.go @@ -182,7 +182,7 @@ func initCompromisedRegistryTestServer() string { s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if strings.Contains(r.URL.Path, "manifests") { w.Header().Set("Content-Type", "application/vnd.oci.image.manifest.v1+json") - w.WriteHeader(200) + w.WriteHeader(http.StatusOK) fmt.Fprintf(w, `{ "schemaVersion": 2, "config": { "mediaType": "%s", @@ -199,16 +199,16 @@ func initCompromisedRegistryTestServer() string { }`, ConfigMediaType, ChartLayerMediaType) } else if r.URL.Path == "/v2/testrepo/supposedlysafechart/blobs/sha256:a705ee2789ab50a5ba20930f246dbd5cc01ff9712825bb98f57ee8414377f133" { w.Header().Set("Content-Type", "application/json") - w.WriteHeader(200) + w.WriteHeader(http.StatusOK) w.Write([]byte("{\"name\":\"mychart\",\"version\":\"0.1.0\",\"description\":\"A Helm chart for Kubernetes\\n" + "an 'application' or a 'library' chart.\",\"apiVersion\":\"v2\",\"appVersion\":\"1.16.0\",\"type\":" + "\"application\"}")) } else if r.URL.Path == "/v2/testrepo/supposedlysafechart/blobs/sha256:ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb" { w.Header().Set("Content-Type", ChartLayerMediaType) - w.WriteHeader(200) + w.WriteHeader(http.StatusOK) w.Write([]byte("b")) } else { - w.WriteHeader(500) + w.WriteHeader(http.StatusInternalServerError) } })) diff --git a/pkg/repo/repotest/server_test.go b/pkg/repo/repotest/server_test.go index cf68e5110..4d62ef8ed 100644 --- a/pkg/repo/repotest/server_test.go +++ b/pkg/repo/repotest/server_test.go @@ -92,7 +92,7 @@ func TestServer(t *testing.T) { if err != nil { t.Fatal(err) } - if res.StatusCode != 404 { + if res.StatusCode != http.StatusNotFound { t.Fatalf("Expected 404, got %d", res.StatusCode) } } @@ -140,7 +140,7 @@ func TestNewTempServer(t *testing.T) { res.Body.Close() - if res.StatusCode != 200 { + if res.StatusCode != http.StatusOK { t.Errorf("Expected 200, got %d", res.StatusCode) } @@ -153,7 +153,7 @@ func TestNewTempServer(t *testing.T) { } res.Body.Close() - if res.StatusCode != 200 { + if res.StatusCode != http.StatusOK { t.Errorf("Expected 200, got %d", res.StatusCode) } } @@ -198,7 +198,7 @@ func TestNewTempServer(t *testing.T) { if err != nil { t.Fatal(err) } - if res.StatusCode != 404 { + if res.StatusCode != http.StatusNotFound { t.Fatalf("Expected 404, got %d", res.StatusCode) } })