diff --git a/operators/constellation-node-operator/controllers/autoscalingstrategy_controller_env_test.go b/operators/constellation-node-operator/controllers/autoscalingstrategy_controller_env_test.go index 97a12695d..c4fa92940 100644 --- a/operators/constellation-node-operator/controllers/autoscalingstrategy_controller_env_test.go +++ b/operators/constellation-node-operator/controllers/autoscalingstrategy_controller_env_test.go @@ -23,6 +23,12 @@ import ( ) var _ = Describe("AutoscalingStrategy controller", func() { + AfterEach(func() { + Eventually(func() error { + return resetEnv() + }, 30*time.Second, 1*time.Second).Should(Succeed()) + }) + // Define utility constants for object names and testing timeouts/durations and intervals. const ( ClusterAutoscalerDeploymentName = "cluster-autoscaler" @@ -331,7 +337,7 @@ var _ = Describe("AutoscalingStrategy controller", func() { return nil } return createdDeployment.Spec.Template.Spec.Containers[0].Command - }, timeout, interval).Should(Equal([]string{ + }, 2*time.Second, interval).Should(Equal([]string{ "./cluster-autoscaler", "--baz=qux", "--foo=bar", diff --git a/operators/constellation-node-operator/controllers/nodeversion_controller_env_test.go b/operators/constellation-node-operator/controllers/nodeversion_controller_env_test.go index eadadac91..91cd96774 100644 --- a/operators/constellation-node-operator/controllers/nodeversion_controller_env_test.go +++ b/operators/constellation-node-operator/controllers/nodeversion_controller_env_test.go @@ -27,36 +27,9 @@ import ( var _ = Describe("NodeVersion controller", func() { AfterEach(func() { - // cleanup all nodes - nodeList := &corev1.NodeList{} - Expect(k8sClient.List(context.Background(), nodeList)).To(Succeed()) - for _, node := range nodeList.Items { - Expect(k8sClient.Delete(context.Background(), &node)).To(Succeed()) - } - // cleanup all node versions - nodeVersionList := &updatev1alpha1.NodeVersionList{} - Expect(k8sClient.List(context.Background(), nodeVersionList)).To(Succeed()) - for _, nodeVersion := range nodeVersionList.Items { - Expect(k8sClient.Delete(context.Background(), &nodeVersion)).To(Succeed()) - } - // cleanup all scaling groups - scalingGroupList := &updatev1alpha1.ScalingGroupList{} - Expect(k8sClient.List(context.Background(), scalingGroupList)).To(Succeed()) - for _, scalingGroup := range scalingGroupList.Items { - Expect(k8sClient.Delete(context.Background(), &scalingGroup)).To(Succeed()) - } - // cleanup all pending nodes - pendingNodeList := &updatev1alpha1.PendingNodeList{} - Expect(k8sClient.List(context.Background(), pendingNodeList)).To(Succeed()) - for _, pendingNode := range pendingNodeList.Items { - Expect(k8sClient.Delete(context.Background(), &pendingNode)).To(Succeed()) - } - // cleanup all joining nodes - joiningNodeList := &updatev1alpha1.JoiningNodeList{} - Expect(k8sClient.List(context.Background(), joiningNodeList)).To(Succeed()) - for _, joiningNode := range joiningNodeList.Items { - Expect(k8sClient.Delete(context.Background(), &joiningNode)).To(Succeed()) - } + Eventually(func() error { + return resetEnv() + }, 30*time.Second, 1*time.Second).Should(Succeed()) }) // Define utility constants for object names and testing timeouts/durations and intervals. diff --git a/operators/constellation-node-operator/controllers/nodeversion_controller_test.go b/operators/constellation-node-operator/controllers/nodeversion_controller_test.go index 9849c9889..1d42691bd 100644 --- a/operators/constellation-node-operator/controllers/nodeversion_controller_test.go +++ b/operators/constellation-node-operator/controllers/nodeversion_controller_test.go @@ -816,6 +816,17 @@ func (r *stubNodeReplacer) setCreatedNode(nodeName, providerID string, err error r.createErr = err } +func (r *stubNodeReplacer) reset() { + r.Lock() + defer r.Unlock() + r.nodeImages = nil + r.scalingGroups = nil + r.createNodeName = "" + r.createProviderID = "" + r.createErr = nil + r.deleteErr = nil +} + type stubKubernetesServerVersionGetter struct { version string err error diff --git a/operators/constellation-node-operator/controllers/pendingnode_controller_env_test.go b/operators/constellation-node-operator/controllers/pendingnode_controller_env_test.go index 64c164766..a5ed6b079 100644 --- a/operators/constellation-node-operator/controllers/pendingnode_controller_env_test.go +++ b/operators/constellation-node-operator/controllers/pendingnode_controller_env_test.go @@ -24,6 +24,12 @@ import ( ) var _ = Describe("PendingNode controller", func() { + AfterEach(func() { + Eventually(func() error { + return resetEnv() + }, 30*time.Second, 1*time.Second).Should(Succeed()) + }) + // Define utility constants for object names and testing timeouts/durations and intervals. const ( pendingNodeName = "pending-node" diff --git a/operators/constellation-node-operator/controllers/scalinggroup_controller_env_test.go b/operators/constellation-node-operator/controllers/scalinggroup_controller_env_test.go index 45edacc41..fd8ee1a08 100644 --- a/operators/constellation-node-operator/controllers/scalinggroup_controller_env_test.go +++ b/operators/constellation-node-operator/controllers/scalinggroup_controller_env_test.go @@ -21,6 +21,12 @@ import ( ) var _ = Describe("ScalingGroup controller", func() { + AfterEach(func() { + Eventually(func() error { + return resetEnv() + }, 30*time.Second, 1*time.Second).Should(Succeed()) + }) + // Define utility constants for object names and testing timeouts/durations and intervals. const ( nodeVersionName = "node-version" diff --git a/operators/constellation-node-operator/controllers/scalinggroup_controller_test.go b/operators/constellation-node-operator/controllers/scalinggroup_controller_test.go index b21e46937..9769722bd 100644 --- a/operators/constellation-node-operator/controllers/scalinggroup_controller_test.go +++ b/operators/constellation-node-operator/controllers/scalinggroup_controller_test.go @@ -34,3 +34,9 @@ func (u *fakeScalingGroupUpdater) SetScalingGroupImage(_ context.Context, scalin u.scalingGroupImage[scalingGroupID] = imageURI return nil } + +func (u *fakeScalingGroupUpdater) reset() { + u.Lock() + defer u.Unlock() + u.scalingGroupImage = make(map[string]string) +} diff --git a/operators/constellation-node-operator/controllers/suite_test.go b/operators/constellation-node-operator/controllers/suite_test.go index bf8e19c13..6a41e2ccb 100644 --- a/operators/constellation-node-operator/controllers/suite_test.go +++ b/operators/constellation-node-operator/controllers/suite_test.go @@ -19,6 +19,8 @@ import ( nodemaintenancev1beta1 "github.com/edgelesssys/constellation/v2/3rdparty/node-maintenance-operator/api/v1beta1" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" + appsv1 "k8s.io/api/apps/v1" + corev1 "k8s.io/api/core/v1" "k8s.io/client-go/kubernetes/scheme" "k8s.io/client-go/rest" testclock "k8s.io/utils/clock/testing" @@ -45,6 +47,7 @@ var ( func TestAPIs(t *testing.T) { RegisterFailHandler(Fail) + // config.GinkgoConfig.RandomSeed = 1679587116 RunSpecs(t, "Controller Suite") } @@ -141,6 +144,12 @@ type fakeCollection struct { clock *testclock.FakeClock } +func (c *fakeCollection) reset() { + c.scalingGroupUpdater.reset() + c.nodeStateGetter.setNodeState("") + c.nodeReplacer.reset() +} + func newFakes() fakeCollection { return fakeCollection{ scalingGroupUpdater: newFakeScalingGroupUpdater(), @@ -150,3 +159,78 @@ func newFakes() fakeCollection { clock: testclock.NewFakeClock(time.Now()), } } + +func resetEnv() error { + // cleanup all nodes + nodeList := &corev1.NodeList{} + if err := k8sClient.List(context.Background(), nodeList); err != nil { + return err + } + for _, node := range nodeList.Items { + if err := k8sClient.Delete(context.Background(), &node); err != nil { + return err + } + } + // cleanup all node versions + nodeVersionList := &updatev1alpha1.NodeVersionList{} + if err := k8sClient.List(context.Background(), nodeVersionList); err != nil { + return err + } + for _, nodeVersion := range nodeVersionList.Items { + if err := k8sClient.Delete(context.Background(), &nodeVersion); err != nil { + return err + } + } + // cleanup all scaling groups + scalingGroupList := &updatev1alpha1.ScalingGroupList{} + if err := k8sClient.List(context.Background(), scalingGroupList); err != nil { + return err + } + for _, scalingGroup := range scalingGroupList.Items { + if err := k8sClient.Delete(context.Background(), &scalingGroup); err != nil { + return err + } + } + // cleanup all pending nodes + pendingNodeList := &updatev1alpha1.PendingNodeList{} + if err := k8sClient.List(context.Background(), pendingNodeList); err != nil { + return err + } + for _, pendingNode := range pendingNodeList.Items { + if err := k8sClient.Delete(context.Background(), &pendingNode); err != nil { + return err + } + } + // cleanup all joining nodes + joiningNodeList := &updatev1alpha1.JoiningNodeList{} + if err := k8sClient.List(context.Background(), joiningNodeList); err != nil { + return err + } + for _, joiningNode := range joiningNodeList.Items { + if err := k8sClient.Delete(context.Background(), &joiningNode); err != nil { + return err + } + } + // cleanup all autoscaling strategies + autoscalingStrategyList := &updatev1alpha1.AutoscalingStrategyList{} + if err := k8sClient.List(context.Background(), autoscalingStrategyList); err != nil { + return err + } + for _, autoscalingStrategy := range autoscalingStrategyList.Items { + if err := k8sClient.Delete(context.Background(), &autoscalingStrategy); err != nil { + return err + } + } + // cleanup all deployments + deploymentList := &appsv1.DeploymentList{} + if err := k8sClient.List(context.Background(), deploymentList); err != nil { + return err + } + for _, deployment := range deploymentList.Items { + if err := k8sClient.Delete(context.Background(), &deployment); err != nil { + return err + } + } + fakes.reset() + return nil +}