Skip to content

Commit

Permalink
Cleanup Created object in controller tests
Browse files Browse the repository at this point in the history
some controller tests were not cleaning up
created objects, add cleanup for those tests.

Signed-off-by: adrianc <[email protected]>
  • Loading branch information
adrianchiris committed Feb 1, 2024
1 parent 36394ac commit 0258c5a
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 4 deletions.
11 changes: 9 additions & 2 deletions controllers/sriovibnetwork_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,11 +223,18 @@ var _ = Describe("SriovIBNetwork Controller", func() {
err = k8sClient.Get(ctx, types.NamespacedName{Name: cr.GetName(), Namespace: "ib-ns-xxx"}, netAttDef)
Expect(err).To(HaveOccurred())

err = k8sClient.Create(goctx.TODO(), &corev1.Namespace{
// Create Namespace
nsObj := &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{Name: "ib-ns-xxx"},
})
}
err = k8sClient.Create(goctx.TODO(), nsObj)
Expect(err).NotTo(HaveOccurred())
DeferCleanup(func() {
err = k8sClient.Delete(goctx.TODO(), nsObj)
Expect(err).NotTo(HaveOccurred())
})

// Check that net-attach-def has been created
err = util.WaitForNamespacedObject(netAttDef, k8sClient, "ib-ns-xxx", cr.GetName(), util.RetryInterval, util.Timeout)
Expect(err).NotTo(HaveOccurred())

Expand Down
11 changes: 9 additions & 2 deletions controllers/sriovnetwork_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,11 +260,18 @@ var _ = Describe("SriovNetwork Controller", func() {
err = k8sClient.Get(ctx, types.NamespacedName{Name: cr.GetName(), Namespace: "ns-xxx"}, netAttDef)
Expect(err).To(HaveOccurred())

err = k8sClient.Create(goctx.TODO(), &corev1.Namespace{
// Create Namespace
nsObj := &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{Name: "ns-xxx"},
})
}
err = k8sClient.Create(goctx.TODO(), nsObj)
Expect(err).NotTo(HaveOccurred())
DeferCleanup(func() {
err = k8sClient.Delete(goctx.TODO(), nsObj)
Expect(err).NotTo(HaveOccurred())
})

// Check that net-attach-def has been created
err = util.WaitForNamespacedObject(netAttDef, k8sClient, "ns-xxx", cr.GetName(), util.RetryInterval, util.Timeout)
Expect(err).NotTo(HaveOccurred())

Expand Down
9 changes: 9 additions & 0 deletions controllers/sriovnetworkpoolconfig_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,21 @@ var _ = Describe("Operator", func() {
}
err = k8sClient.Create(goctx.TODO(), mcp)
Expect(err).NotTo(HaveOccurred())
DeferCleanup(func() {
err = k8sClient.Delete(goctx.TODO(), mcp)
Expect(err).ToNot(HaveOccurred())
})

config.Spec.OvsHardwareOffloadConfig = sriovnetworkv1.OvsHardwareOffloadConfig{
Name: mcpName,
}
err = k8sClient.Create(goctx.TODO(), config)
Expect(err).NotTo(HaveOccurred())
DeferCleanup(func() {
err = k8sClient.Delete(goctx.TODO(), config)
Expect(err).ToNot(HaveOccurred())
})

Eventually(func() error {
mc := &mcfgv1.MachineConfig{}
err := k8sClient.Get(goctx.TODO(), types.NamespacedName{Name: mcName, Namespace: testNamespace}, mc)
Expand Down
4 changes: 4 additions & 0 deletions controllers/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ var _ = BeforeSuite(func() {
}
Expect(k8sClient.Create(context.TODO(), ns)).Should(Succeed())

// Create default SriovOperatorConfig
config := &sriovnetworkv1.SriovOperatorConfig{}
config.SetNamespace(testNamespace)
config.SetName(constants.DefaultConfigName)
Expand All @@ -193,6 +194,7 @@ var _ = BeforeSuite(func() {
}
Expect(k8sClient.Create(context.TODO(), config)).Should(Succeed())

// Create default SriovNetworkNodePolicy
defaultPolicy := &sriovnetworkv1.SriovNetworkNodePolicy{}
defaultPolicy.SetNamespace(testNamespace)
defaultPolicy.SetName(constants.DefaultPolicyName)
Expand All @@ -203,6 +205,7 @@ var _ = BeforeSuite(func() {
}
Expect(k8sClient.Create(context.TODO(), defaultPolicy)).Should(Succeed())

// Create openshift Infrastructure
infra := &openshiftconfigv1.Infrastructure{
ObjectMeta: metav1.ObjectMeta{
Name: "cluster",
Expand All @@ -214,6 +217,7 @@ var _ = BeforeSuite(func() {
}
Expect(k8sClient.Create(context.TODO(), infra)).Should(Succeed())

// Create default SriovNetworkPoolConfig
poolConfig := &sriovnetworkv1.SriovNetworkPoolConfig{}
poolConfig.SetNamespace(testNamespace)
poolConfig.SetName(constants.DefaultConfigName)
Expand Down

0 comments on commit 0258c5a

Please sign in to comment.