diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index b870425788..b70502f13c 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -40,7 +40,7 @@ jobs: uses: golangci/golangci-lint-action@aaa42aa0628b4ae2578232a66b541047968fac86 # v6.1.0 with: working-directory: ${{ matrix.directory }} - version: v1.60.3 # renovate: datasource=github-tags depName=golangci/golangci-lint + version: v1.61.0 # renovate: datasource=github-tags depName=golangci/golangci-lint njs-lint: name: NJS Lint diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index ec52a98bc5..c34162686c 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -39,7 +39,7 @@ repos: - javascript - repo: https://github.com/golangci/golangci-lint - rev: v1.60.3 + rev: v1.61.0 hooks: - id: golangci-lint-full name: golangci-lint-root diff --git a/Makefile b/Makefile index 9c146ea3d8..17cdcf8781 100644 --- a/Makefile +++ b/Makefile @@ -24,7 +24,7 @@ GO_LINKER_FLAGS = $(GO_LINKER_FLAGS_OPTIMIZATIONS) $(GO_LINKER_FlAGS_VARS) # tools versions # renovate: datasource=github-tags depName=golangci/golangci-lint -GOLANGCI_LINT_VERSION = v1.60.3 +GOLANGCI_LINT_VERSION = v1.61.0 # renovate: datasource=docker depName=kindest/node KIND_K8S_VERSION = v1.31.0 # renovate: datasource=github-tags depName=norwoodj/helm-docs diff --git a/internal/framework/events/loop_test.go b/internal/framework/events/loop_test.go index 807e1b0d4b..09e5c30603 100644 --- a/internal/framework/events/loop_test.go +++ b/internal/framework/events/loop_test.go @@ -3,6 +3,7 @@ package events_test import ( "context" "errors" + "time" "github.com/go-logr/logr" . "github.com/onsi/ginkgo/v2" @@ -19,8 +20,6 @@ var _ = Describe("EventLoop", func() { eventCh chan interface{} fakePreparer *eventsfakes.FakeFirstEventBatchPreparer eventLoop *events.EventLoop - ctx context.Context - cancel context.CancelFunc errorCh chan error ) @@ -31,12 +30,19 @@ var _ = Describe("EventLoop", func() { eventLoop = events.NewEventLoop(eventCh, zap.New(), fakeHandler, fakePreparer) - ctx, cancel = context.WithCancel(context.Background()) errorCh = make(chan error) }) Describe("Normal processing", func() { BeforeEach(func() { + ctx, cancel := context.WithCancel(context.Background()) + DeferCleanup(func(dctx SpecContext) { + cancel() + var err error + Eventually(errorCh).WithContext(dctx).Should(Receive(&err)) + Expect(err).ToNot(HaveOccurred()) + }, NodeTimeout(time.Second*10)) + batch := events.EventBatch{ "event0", } @@ -54,14 +60,6 @@ var _ = Describe("EventLoop", func() { Expect(batch).Should(Equal(expectedBatch)) }) - AfterEach(func() { - cancel() - - var err error - Eventually(errorCh).Should(Receive(&err)) - Expect(err).ToNot(HaveOccurred()) - }) - // Because BeforeEach() creates the first batch and waits for it to be handled, in the tests below // HandleEventBatchCallCount() is already 1. @@ -123,7 +121,7 @@ var _ = Describe("EventLoop", func() { }) Describe("Edge cases", func() { - It("should return error when preparer returns error without blocking", func() { + It("should return error when preparer returns error without blocking", func(ctx SpecContext) { preparerError := errors.New("test") fakePreparer.PrepareReturns(events.EventBatch{}, preparerError) @@ -132,9 +130,10 @@ var _ = Describe("EventLoop", func() { Expect(err).Should(MatchError(preparerError)) }) - It("should return nil when started with canceled context without blocking", func() { + It("should return nil when started with canceled context without blocking", func(ctx context.Context) { fakePreparer.PrepareReturns(events.EventBatch{}, nil) + ctx, cancel := context.WithCancel(ctx) cancel() err := eventLoop.Start(ctx) diff --git a/internal/mode/static/telemetry/collector_test.go b/internal/mode/static/telemetry/collector_test.go index 4aa1ab99bd..49f83147c7 100644 --- a/internal/mode/static/telemetry/collector_test.go +++ b/internal/mode/static/telemetry/collector_test.go @@ -78,7 +78,6 @@ var _ = Describe("Collector", Ordered, func() { dataCollector telemetry.DataCollector version string expData telemetry.Data - ctx context.Context podNSName types.NamespacedName ngfPod *v1.Pod ngfReplicaSet *appsv1.ReplicaSet @@ -90,7 +89,6 @@ var _ = Describe("Collector", Ordered, func() { ) BeforeAll(func() { - ctx = context.Background() version = "1.1" ngfPod = &v1.Pod{ @@ -230,7 +228,7 @@ var _ = Describe("Collector", Ordered, func() { Describe("Normal case", func() { When("collecting telemetry data", func() { - It("collects all fields", func() { + It("collects all fields", func(ctx SpecContext) { nodes := &v1.NodeList{ Items: []v1.Node{ { @@ -396,7 +394,7 @@ var _ = Describe("Collector", Ordered, func() { Describe("cluster information collector", func() { When("collecting cluster platform", func() { When("it encounters an error while collecting data", func() { - It("should error if the kubernetes client errored when getting the NamespaceList", func() { + It("should error if the kubernetes client errored when getting the NamespaceList", func(ctx SpecContext) { expectedError := errors.New("failed to get NamespaceList") k8sClientReader.ListCalls(mergeListCallsWithBase( func(_ context.Context, object client.ObjectList, _ ...client.ListOption) error { @@ -415,7 +413,7 @@ var _ = Describe("Collector", Ordered, func() { When("collecting clusterID data", func() { When("it encounters an error while collecting data", func() { - It("should error if the kubernetes client errored when getting the namespace", func() { + It("should error if the kubernetes client errored when getting the namespace", func(ctx SpecContext) { expectedError := errors.New("there was an error getting clusterID") k8sClientReader.GetCalls(mergeGetCallsWithBase( func(_ context.Context, _ types.NamespacedName, object client.Object, _ ...client.GetOption) error { @@ -434,7 +432,7 @@ var _ = Describe("Collector", Ordered, func() { When("collecting cluster version data", func() { When("the kubelet version is missing", func() { - It("should be report 'unknown'", func() { + It("should be report 'unknown'", func(ctx SpecContext) { nodes := &v1.NodeList{ Items: []v1.Node{ { @@ -463,7 +461,7 @@ var _ = Describe("Collector", Ordered, func() { Describe("node count collector", func() { When("collecting node count data", func() { - It("collects correct data for one node", func() { + It("collects correct data for one node", func(ctx SpecContext) { k8sClientReader.ListCalls(createListCallsFunc(nodeList)) expData.Data.ClusterNodeCount = 1 @@ -475,7 +473,7 @@ var _ = Describe("Collector", Ordered, func() { }) When("it encounters an error while collecting data", func() { - It("should error when there are no nodes", func() { + It("should error when there are no nodes", func(ctx SpecContext) { expectedError := errors.New("failed to collect cluster information: NodeList length is zero") k8sClientReader.ListCalls(createListCallsFunc(nil)) @@ -484,7 +482,7 @@ var _ = Describe("Collector", Ordered, func() { Expect(err).To(MatchError(expectedError)) }) - It("should error on kubernetes client api errors", func() { + It("should error on kubernetes client api errors", func(ctx SpecContext) { expectedError := errors.New("failed to get NodeList") k8sClientReader.ListCalls( func(_ context.Context, object client.ObjectList, _ ...client.ListOption) error { @@ -593,7 +591,7 @@ var _ = Describe("Collector", Ordered, func() { }) When("collecting NGF resource counts", func() { - It("collects correct data for graph with no resources", func() { + It("collects correct data for graph with no resources", func(ctx SpecContext) { fakeGraphGetter.GetLatestGraphReturns(&graph.Graph{}) fakeConfigurationGetter.GetLatestConfigurationReturns(&dataplane.Configuration{}) @@ -605,7 +603,7 @@ var _ = Describe("Collector", Ordered, func() { Expect(expData).To(Equal(data)) }) - It("collects correct data for graph with one of each resource", func() { + It("collects correct data for graph with one of each resource", func(ctx SpecContext) { fakeGraphGetter.GetLatestGraphReturns(graph1) fakeConfigurationGetter.GetLatestConfigurationReturns(config1) @@ -629,7 +627,7 @@ var _ = Describe("Collector", Ordered, func() { Expect(expData).To(Equal(data)) }) - It("ignores invalid and empty upstreams", func() { + It("ignores invalid and empty upstreams", func(ctx SpecContext) { fakeGraphGetter.GetLatestGraphReturns(&graph.Graph{}) fakeConfigurationGetter.GetLatestConfigurationReturns(invalidUpstreamsConfig) expData.NGFResourceCounts = telemetry.NGFResourceCounts{ @@ -659,7 +657,7 @@ var _ = Describe("Collector", Ordered, func() { fakeGraphGetter.GetLatestGraphReturns(&graph.Graph{}) fakeConfigurationGetter.GetLatestConfigurationReturns(&dataplane.Configuration{}) }) - It("should error on nil latest graph", func() { + It("should error on nil latest graph", func(ctx SpecContext) { expectedError := errors.New("latest graph cannot be nil") fakeGraphGetter.GetLatestGraphReturns(nil) @@ -667,7 +665,7 @@ var _ = Describe("Collector", Ordered, func() { Expect(err).To(MatchError(expectedError)) }) - It("should error on nil latest configuration", func() { + It("should error on nil latest configuration", func(ctx SpecContext) { expectedError := errors.New("latest configuration cannot be nil") fakeConfigurationGetter.GetLatestConfigurationReturns(nil) @@ -681,7 +679,7 @@ var _ = Describe("Collector", Ordered, func() { Describe("NGF replica count collector", func() { When("collecting NGF replica count", func() { When("it encounters an error while collecting data", func() { - It("should error if the kubernetes client errored when getting the Pod", func() { + It("should error if the kubernetes client errored when getting the Pod", func(ctx SpecContext) { expectedErr := errors.New("there was an error getting the Pod") k8sClientReader.GetCalls(mergeGetCallsWithBase( func(_ context.Context, _ client.ObjectKey, object client.Object, _ ...client.GetOption) error { @@ -697,7 +695,7 @@ var _ = Describe("Collector", Ordered, func() { Expect(err).To(MatchError(expectedErr)) }) - It("should error if the Pod's owner reference is nil", func() { + It("should error if the Pod's owner reference is nil", func(ctx SpecContext) { expectedErr := errors.New("expected one owner reference of the NGF Pod, got 0") k8sClientReader.GetCalls(mergeGetCallsWithBase(createGetCallsFunc( &v1.Pod{ @@ -712,7 +710,7 @@ var _ = Describe("Collector", Ordered, func() { Expect(err).To(MatchError(expectedErr)) }) - It("should error if the Pod has multiple owner references", func() { + It("should error if the Pod has multiple owner references", func(ctx SpecContext) { expectedErr := errors.New("expected one owner reference of the NGF Pod, got 2") k8sClientReader.GetCalls(mergeGetCallsWithBase(createGetCallsFunc( &v1.Pod{ @@ -736,7 +734,7 @@ var _ = Describe("Collector", Ordered, func() { Expect(err).To(MatchError(expectedErr)) }) - It("should error if the Pod's owner reference is not a ReplicaSet", func() { + It("should error if the Pod's owner reference is not a ReplicaSet", func(ctx SpecContext) { expectedErr := errors.New("expected pod owner reference to be ReplicaSet, got Deployment") k8sClientReader.GetCalls(mergeGetCallsWithBase(createGetCallsFunc( &v1.Pod{ @@ -757,7 +755,7 @@ var _ = Describe("Collector", Ordered, func() { Expect(err).To(MatchError(expectedErr)) }) - It("should error if the replica set's replicas is nil", func() { + It("should error if the replica set's replicas is nil", func(ctx SpecContext) { expectedErr := errors.New("replica set replicas was nil") k8sClientReader.GetCalls(mergeGetCallsWithBase(createGetCallsFunc( &appsv1.ReplicaSet{ @@ -771,7 +769,7 @@ var _ = Describe("Collector", Ordered, func() { Expect(err).To(MatchError(expectedErr)) }) - It("should error if the kubernetes client errored when getting the ReplicaSet", func() { + It("should error if the kubernetes client errored when getting the ReplicaSet", func(ctx SpecContext) { expectedErr := errors.New("there was an error getting the ReplicaSet") k8sClientReader.GetCalls(mergeGetCallsWithBase( func(_ context.Context, _ client.ObjectKey, object client.Object, _ ...client.GetOption) error { @@ -792,7 +790,7 @@ var _ = Describe("Collector", Ordered, func() { Describe("DeploymentID collector", func() { When("collecting deploymentID", func() { When("it encounters an error while collecting data", func() { - It("should error if the replicaSet's owner reference is nil", func() { + It("should error if the replicaSet's owner reference is nil", func(ctx SpecContext) { replicas := int32(1) k8sClientReader.GetCalls(mergeGetCallsWithBase(createGetCallsFunc( &appsv1.ReplicaSet{ @@ -807,7 +805,7 @@ var _ = Describe("Collector", Ordered, func() { Expect(err).To(MatchError(expectedErr)) }) - It("should error if the replicaSet's owner reference kind is not deployment", func() { + It("should error if the replicaSet's owner reference kind is not deployment", func(ctx SpecContext) { replicas := int32(1) k8sClientReader.GetCalls(mergeGetCallsWithBase(createGetCallsFunc( &appsv1.ReplicaSet{ @@ -829,7 +827,7 @@ var _ = Describe("Collector", Ordered, func() { _, err := dataCollector.Collect(ctx) Expect(err).To(MatchError(expectedErr)) }) - It("should error if the replicaSet's owner reference has empty UID", func() { + It("should error if the replicaSet's owner reference has empty UID", func(ctx SpecContext) { replicas := int32(1) k8sClientReader.GetCalls(mergeGetCallsWithBase(createGetCallsFunc( &appsv1.ReplicaSet{