From 07da6a91517d403cf41c608fd910daf406ddd8a3 Mon Sep 17 00:00:00 2001 From: Carson Long <12767276+ctlong@users.noreply.github.com> Date: Tue, 19 Dec 2023 09:36:19 -0800 Subject: [PATCH] ci(golangci-lint): Add ginkgolinter (#411) * ci(golangci-lint): Add ginkgolinter * Fix ginkgolinter warnings --- src/.golangci.yml | 2 ++ src/internal/auth/capi_client_test.go | 16 ++++++------- src/internal/auth/cf_auth_middleware_test.go | 4 ++-- src/internal/auth/uaa_client_test.go | 24 ++++++++++---------- src/internal/cache/log_cache_test.go | 4 ++-- src/internal/nozzle/nozzle_test.go | 4 ++-- src/pkg/marshaler/marshaler_test.go | 12 +++++----- 7 files changed, 34 insertions(+), 32 deletions(-) diff --git a/src/.golangci.yml b/src/.golangci.yml index d9a6ebd29..155df3786 100644 --- a/src/.golangci.yml +++ b/src/.golangci.yml @@ -11,6 +11,8 @@ linters: - gocyclo # Inspects source code for security problems. - gosec + # Enforces standards of using ginkgo and gomega. + - ginkgolinter issues: # Disable max issues per linter. diff --git a/src/internal/auth/capi_client_test.go b/src/internal/auth/capi_client_test.go index f3b921051..b5cc97802 100644 --- a/src/internal/auth/capi_client_test.go +++ b/src/internal/auth/capi_client_test.go @@ -58,11 +58,11 @@ var _ = Describe("CAPIClient", func() { By("calling isAuthorized the first time and caching response") Expect(tc.client.IsAuthorized("37cbff06-79ef-4146-a7b0-01838940f185", "some-token")).To(BeTrue()) - Expect(len(tc.capiClient.requests)).To(Equal(1)) + Expect(tc.capiClient.requests).To(HaveLen(1)) By("calling isAuthorized the second time and pulling from the cache") Expect(tc.client.IsAuthorized("37cbff06-79ef-4146-a7b0-01838940f185", "some-token")).To(BeTrue()) - Expect(len(tc.capiClient.requests)).To(Equal(1)) + Expect(tc.capiClient.requests).To(HaveLen(1)) }) It("sourceIDs from expired cached tokens are not authorized", func() { @@ -176,14 +176,14 @@ var _ = Describe("CAPIClient", func() { } authorized := tc.client.IsAuthorized("app-guid", "some-token") - Expect(len(tc.capiClient.requests)).To(Equal(1)) + Expect(tc.capiClient.requests).To(HaveLen(1)) Expect(authorized).To(BeTrue()) tc.capiClient.resps = []response{ newCapiResp(http.StatusOK), } Expect(tc.client.IsAuthorized("service-guid", "some-token")).To(BeTrue()) - Expect(len(tc.capiClient.requests)).To(Equal(2)) + Expect(tc.capiClient.requests).To(HaveLen(2)) }) It("stores the latency", func() { @@ -501,7 +501,7 @@ var _ = Describe("CAPIClient", func() { tc := setup() tc.client.GetRelatedSourceIds([]string{}, "some-token") - Expect(tc.capiClient.requests).To(HaveLen(0)) + Expect(tc.capiClient.requests).To(BeEmpty()) }) It("stores the latency", func() { @@ -524,7 +524,7 @@ var _ = Describe("CAPIClient", func() { {status: http.StatusNotFound}, } sourceIds := tc.client.GetRelatedSourceIds([]string{"app-name"}, "some-token") - Expect(sourceIds).To(HaveLen(0)) + Expect(sourceIds).To(BeEmpty()) }) It("returns no source IDs when the request returns a non-200 status code", func() { @@ -534,7 +534,7 @@ var _ = Describe("CAPIClient", func() { {err: errors.New("intentional error")}, } sourceIds := tc.client.GetRelatedSourceIds([]string{"app-name"}, "some-token") - Expect(sourceIds).To(HaveLen(0)) + Expect(sourceIds).To(BeEmpty()) }) It("returns no source IDs when JSON decoding fails", func() { @@ -544,7 +544,7 @@ var _ = Describe("CAPIClient", func() { {status: http.StatusOK, body: []byte(`{`)}, } sourceIds := tc.client.GetRelatedSourceIds([]string{"app-name"}, "some-token") - Expect(sourceIds).To(HaveLen(0)) + Expect(sourceIds).To(BeEmpty()) }) }) }) diff --git a/src/internal/auth/cf_auth_middleware_test.go b/src/internal/auth/cf_auth_middleware_test.go index 6c638dc3d..3386cdbdf 100644 --- a/src/internal/auth/cf_auth_middleware_test.go +++ b/src/internal/auth/cf_auth_middleware_test.go @@ -156,7 +156,7 @@ var _ = Describe("CfAuthMiddleware", func() { var m rpc.MetaResponse msg, err := io.ReadAll(tc.recorder.Body) - Expect(err).To(BeNil()) + Expect(err).NotTo(HaveOccurred()) Expect(protojson.Unmarshal(msg, &m)).To(Succeed()) Expect(m.Meta).To(HaveLen(2)) @@ -182,7 +182,7 @@ var _ = Describe("CfAuthMiddleware", func() { Expect(tc.recorder.Code).To(Equal(http.StatusOK)) var m rpc.MetaResponse msg, err := io.ReadAll(tc.recorder.Body) - Expect(err).To(BeNil()) + Expect(err).NotTo(HaveOccurred()) Expect(protojson.Unmarshal(msg, &m)).To(Succeed()) Expect(m.Meta).To(HaveLen(2)) Expect(m.Meta).To(HaveKey("source-0")) diff --git a/src/internal/auth/uaa_client_test.go b/src/internal/auth/uaa_client_test.go index d629b9981..a6bf05814 100644 --- a/src/internal/auth/uaa_client_test.go +++ b/src/internal/auth/uaa_client_test.go @@ -110,7 +110,7 @@ var _ = Describe("UAAClient", func() { _, err = tc.uaaClient.Read(withBearer(token)) Expect(err).ToNot(HaveOccurred()) - Expect(len(tc.httpClient.requests)).To(Equal(initialRequestCount)) + Expect(tc.httpClient.requests).To(HaveLen(initialRequestCount)) }) It("does not allow use of an expired token", func() { @@ -147,7 +147,7 @@ var _ = Describe("UAAClient", func() { Expect(err).ToNot(HaveOccurred()) Expect(c.Token).To(Equal(withBearer(token))) - Expect(len(tc.httpClient.requests)).To(Equal(initialRequestCount + 1)) + Expect(tc.httpClient.requests).To(HaveLen(initialRequestCount + 1)) }) It("returns an error when the matching public key cannot be retrieved from UAA", func() { @@ -161,7 +161,7 @@ var _ = Describe("UAAClient", func() { Expect(err).To(HaveOccurred()) Expect(err.Error()).To(ContainSubstring("failed to decode token: using unknown token key")) - Expect(len(tc.httpClient.requests)).To(Equal(initialRequestCount + 1)) + Expect(tc.httpClient.requests).To(HaveLen(initialRequestCount + 1)) }) It("returns an error when given a token signed by an public key that was purged from UAA", func() { @@ -182,7 +182,7 @@ var _ = Describe("UAAClient", func() { Expect(err).To(HaveOccurred()) Expect(err.Error()).To(ContainSubstring("failed to decode token: using unknown token key")) - Expect(len(tc.httpClient.requests)).To(Equal(initialRequestCount + 2)) + Expect(tc.httpClient.requests).To(HaveLen(initialRequestCount + 2)) }) It("continues to accept previously signed tokens when retrieving public keys from UAA fails", func() { @@ -193,7 +193,7 @@ var _ = Describe("UAAClient", func() { _, err := tc.uaaClient.Read(withBearer(toBeExpiredToken)) Expect(err).ToNot(HaveOccurred()) - Expect(len(tc.httpClient.requests)).To(Equal(initialRequestCount)) + Expect(tc.httpClient.requests).To(HaveLen(initialRequestCount)) newTokenKey := generateLegitTokenKey("testKey2") refreshedToken := tc.CreateSignedTokenUsingPrivateKey(payload, newTokenKey) @@ -202,11 +202,11 @@ var _ = Describe("UAAClient", func() { _, err = tc.uaaClient.Read(withBearer(refreshedToken)) Expect(err).To(HaveOccurred()) - Expect(len(tc.httpClient.requests)).To(Equal(initialRequestCount + 1)) + Expect(tc.httpClient.requests).To(HaveLen(initialRequestCount + 1)) _, err = tc.uaaClient.Read(withBearer(toBeExpiredToken)) Expect(err).ToNot(HaveOccurred()) - Expect(len(tc.httpClient.requests)).To(Equal(initialRequestCount + 1)) + Expect(tc.httpClient.requests).To(HaveLen(initialRequestCount + 1)) }) }) @@ -221,7 +221,7 @@ var _ = Describe("UAAClient", func() { Expect(err).To(HaveOccurred()) Expect(err.Error()).To(ContainSubstring("failed to decode token: using unknown token key")) - Expect(len(tc.httpClient.requests)).To(Equal(initialRequestCount + 1)) + Expect(tc.httpClient.requests).To(HaveLen(initialRequestCount + 1)) }) It("returns an error when the provided token cannot be decoded", func() { @@ -273,7 +273,7 @@ var _ = Describe("UAAClient", func() { wg.Wait() - Expect(len(tc.httpClient.requests)).To(Equal(numRequests + 4)) + Expect(tc.httpClient.requests).To(HaveLen(numRequests + 4)) }) It("calls UAA correctly", func() { @@ -427,17 +427,17 @@ var _ = Describe("UAAClient", func() { err := tc.uaaClient.RefreshTokenKeys() Expect(err).ToNot(HaveOccurred()) - Expect(len(tc.httpClient.requests)).To(Equal(initialRequestCount + 1)) + Expect(tc.httpClient.requests).To(HaveLen(initialRequestCount + 1)) time.Sleep(100 * time.Millisecond) err = tc.uaaClient.RefreshTokenKeys() Expect(err).To(HaveOccurred()) - Expect(len(tc.httpClient.requests)).To(Equal(initialRequestCount + 1)) + Expect(tc.httpClient.requests).To(HaveLen(initialRequestCount + 1)) time.Sleep(101 * time.Millisecond) err = tc.uaaClient.RefreshTokenKeys() Expect(err).To(HaveOccurred()) - Expect(len(tc.httpClient.requests)).To(Equal(initialRequestCount + 2)) + Expect(tc.httpClient.requests).To(HaveLen(initialRequestCount + 2)) }) }) }) diff --git a/src/internal/cache/log_cache_test.go b/src/internal/cache/log_cache_test.go index ed8e672f6..5be528129 100644 --- a/src/internal/cache/log_cache_test.go +++ b/src/internal/cache/log_cache_test.go @@ -297,8 +297,8 @@ var _ = Describe("LogCache", func() { return err } - Expect(len(resp.GetMatrix().GetSeries())).To(Equal(1)) - Expect(len(resp.GetMatrix().GetSeries()[0].GetPoints())).To(Equal(1)) + Expect(resp.GetMatrix().GetSeries()).To(HaveLen(1)) + Expect(resp.GetMatrix().GetSeries()[0].GetPoints()).To(HaveLen(1)) return nil } diff --git a/src/internal/nozzle/nozzle_test.go b/src/internal/nozzle/nozzle_test.go index f2f058261..714c883d6 100644 --- a/src/internal/nozzle/nozzle_test.go +++ b/src/internal/nozzle/nozzle_test.go @@ -100,7 +100,7 @@ var _ = Describe("Nozzle", func() { }, )) - Eventually(streamConnector.envelopes).Should(HaveLen(0)) + Eventually(streamConnector.envelopes).Should(BeEmpty()) }) }) @@ -167,7 +167,7 @@ var _ = Describe("Nozzle", func() { }, )) - Eventually(streamConnector.envelopes).Should(HaveLen(0)) + Eventually(streamConnector.envelopes).Should(BeEmpty()) }) It("writes each envelope to the LogCache", func() { diff --git a/src/pkg/marshaler/marshaler_test.go b/src/pkg/marshaler/marshaler_test.go index 80bef463b..06fca1745 100644 --- a/src/pkg/marshaler/marshaler_test.go +++ b/src/pkg/marshaler/marshaler_test.go @@ -557,7 +557,7 @@ var _ = Describe("PromqlMarshaler", func() { }`), &result) Expect(err).ToNot(HaveOccurred()) - Expect(len(result.GetVector().GetSamples())).To(Equal(2)) + Expect(result.GetVector().GetSamples()).To(HaveLen(2)) Expect(result.GetVector().GetSamples()[0].GetMetric()).To(Equal(map[string]string{ "deployment": "cf", "tag-name": "tag-value", @@ -606,7 +606,7 @@ var _ = Describe("PromqlMarshaler", func() { }`), &result) Expect(err).ToNot(HaveOccurred()) - Expect(len(result.GetMatrix().GetSeries())).To(Equal(2)) + Expect(result.GetMatrix().GetSeries()).To(HaveLen(2)) Expect(result.GetMatrix().GetSeries()[0].GetMetric()).To(Equal(map[string]string{ "deployment": "cf", "tag-name": "tag-value", @@ -615,12 +615,12 @@ var _ = Describe("PromqlMarshaler", func() { "deployment": "cf", "tag-name2": "tag-value2", })) - Expect(len(result.GetMatrix().GetSeries()[0].GetPoints())).To(Equal(2)) + Expect(result.GetMatrix().GetSeries()[0].GetPoints()).To(HaveLen(2)) Expect(result.GetMatrix().GetSeries()[0].GetPoints()[0].GetTime()).To(Equal("1.987")) Expect(result.GetMatrix().GetSeries()[0].GetPoints()[0].GetValue()).To(Equal(2.5)) Expect(result.GetMatrix().GetSeries()[0].GetPoints()[1].GetTime()).To(Equal("2.000")) Expect(result.GetMatrix().GetSeries()[0].GetPoints()[1].GetValue()).To(Equal(3.5)) - Expect(len(result.GetMatrix().GetSeries()[1].GetPoints())).To(Equal(2)) + Expect(result.GetMatrix().GetSeries()[1].GetPoints()).To(HaveLen(2)) Expect(result.GetMatrix().GetSeries()[1].GetPoints()[0].GetTime()).To(Equal("1.000")) Expect(result.GetMatrix().GetSeries()[1].GetPoints()[0].GetValue()).To(Equal(4.5)) Expect(result.GetMatrix().GetSeries()[1].GetPoints()[1].GetTime()).To(Equal("2.000")) @@ -661,12 +661,12 @@ var _ = Describe("PromqlMarshaler", func() { }`), &result) Expect(err).ToNot(HaveOccurred()) - Expect(len(result.GetMatrix().GetSeries()[0].GetPoints())).To(Equal(2)) + Expect(result.GetMatrix().GetSeries()[0].GetPoints()).To(HaveLen(2)) Expect(result.GetMatrix().GetSeries()[0].GetPoints()[0].GetTime()).To(Equal("1.987")) Expect(result.GetMatrix().GetSeries()[0].GetPoints()[0].GetValue()).To(Equal(2.5)) Expect(result.GetMatrix().GetSeries()[0].GetPoints()[1].GetTime()).To(Equal("2.000")) Expect(result.GetMatrix().GetSeries()[0].GetPoints()[1].GetValue()).To(Equal(3.5)) - Expect(len(result.GetMatrix().GetSeries()[1].GetPoints())).To(Equal(2)) + Expect(result.GetMatrix().GetSeries()[1].GetPoints()).To(HaveLen(2)) Expect(result.GetMatrix().GetSeries()[1].GetPoints()[0].GetTime()).To(Equal("1.000")) Expect(result.GetMatrix().GetSeries()[1].GetPoints()[0].GetValue()).To(Equal(4.5)) Expect(result.GetMatrix().GetSeries()[1].GetPoints()[1].GetTime()).To(Equal("2.000"))