From 3e1c38919009c21a687de14f06d417dcd31acb3a Mon Sep 17 00:00:00 2001 From: Artem Bortnikov Date: Fri, 24 May 2024 16:06:19 +0300 Subject: [PATCH] fix oob tests Signed-off-by: Artem Bortnikov --- internal/controller/oob_controller_test.go | 279 +++++++++++---------- test/macdb.yaml | 2 +- 2 files changed, 150 insertions(+), 131 deletions(-) diff --git a/internal/controller/oob_controller_test.go b/internal/controller/oob_controller_test.go index fff11bec..c44b27f9 100644 --- a/internal/controller/oob_controller_test.go +++ b/internal/controller/oob_controller_test.go @@ -4,6 +4,7 @@ package controller import ( + "crypto/rand" "fmt" "time" @@ -19,62 +20,43 @@ import ( "github.com/ironcore-dev/metal/internal/ssa" ) -var _ = Describe("OOB Controller", Serial, func() { - mac := "aabbccddeeff" - timeToReady := time.Second * 3 +const prefix = "aabbcc" - BeforeEach(func(ctx SpecContext) { - Eventually(ObjectList(&ipamv1alpha1.IPList{}, &client.ListOptions{ - Namespace: OOBTemporaryNamespaceHack, - })).Should(HaveField("Items", HaveLen(0))) - // Eventually(ObjectList(&metalv1alpha1.MachineList{})).Should(HaveField("Items", HaveLen(0))) - Eventually(ObjectList(&metalv1alpha1.OOBList{})).Should(HaveField("Items", HaveLen(0))) - Eventually(ObjectList(&metalv1alpha1.OOBSecretList{})).Should(HaveField("Items", HaveLen(0))) +var _ = Describe("OOB Controller", func() { + timeToReady := time.Second * 3 + var ( + oob metalv1alpha1.OOB + secret metalv1alpha1.OOBSecret + machine *metalv1alpha1.Machine + machines metalv1alpha1.MachineList + ip ipamv1alpha1.IP + mac string + ) + + BeforeEach(func() { + mac, _ = generateMacAddress() DeferCleanup(func(ctx SpecContext) { - Eventually(ctx, func(g Gomega, ctx SpecContext) { - var ips ipamv1alpha1.IPList - g.Expect(ObjectList(&ips)()).To(SatisfyAll()) - if len(ips.Items) > 0 { - g.Expect(k8sClient.DeleteAllOf(ctx, &ips.Items[0], &client.DeleteAllOfOptions{ - ListOptions: client.ListOptions{ - Namespace: OOBTemporaryNamespaceHack, - }, - })).To(Succeed()) - } - var machines metalv1alpha1.MachineList - g.Expect(ObjectList(&machines)()).To(SatisfyAll()) - if len(machines.Items) > 0 { - g.Expect(k8sClient.DeleteAllOf(ctx, &machines.Items[0])).To(Succeed()) + machines = metalv1alpha1.MachineList{} + Expect(k8sClient.List(ctx, &machines)).To(Succeed()) + for _, m := range machines.Items { + if m.Spec.OOBRef.Name == mac { + Expect(k8sClient.Delete(ctx, &m)).To(Succeed()) } - var oobs metalv1alpha1.OOBList - g.Expect(ObjectList(&oobs)()).To(SatisfyAll()) - if len(oobs.Items) > 0 { - g.Expect(k8sClient.DeleteAllOf(ctx, &oobs.Items[0])).To(Succeed()) - } - var secrets metalv1alpha1.OOBSecretList - g.Expect(ObjectList(&secrets)()).To(SatisfyAll()) - if len(secrets.Items) > 0 { - g.Expect(k8sClient.DeleteAllOf(ctx, &secrets.Items[0])).To(Succeed()) - } - - g.Expect(ObjectList(&ips)()).To(HaveField("Items", BeEmpty())) - g.Expect(ObjectList(&machines)()).To(HaveField("Items", BeEmpty())) - g.Expect(ObjectList(&oobs)()).To(HaveField("Items", BeEmpty())) - g.Expect(ObjectList(&secrets)()).To(HaveField("Items", BeEmpty())) - }, time.Second*3).Should(Succeed()) + } }) }) It("should create an OOB from an IP", func(ctx SpecContext) { - oob := &metalv1alpha1.OOB{ + oob = metalv1alpha1.OOB{ ObjectMeta: metav1.ObjectMeta{ Name: mac, }, } + DeferCleanup(k8sClient.Delete, &oob) By("Creating an IP") - ip := &ipamv1alpha1.IP{ + ip = ipamv1alpha1.IP{ ObjectMeta: metav1.ObjectMeta{ GenerateName: "test-", Namespace: OOBTemporaryNamespaceHack, @@ -84,18 +66,19 @@ var _ = Describe("OOB Controller", Serial, func() { }, }, } - Expect(k8sClient.Create(ctx, ip)).To(Succeed()) + Expect(k8sClient.Create(ctx, &ip)).To(Succeed()) + DeferCleanup(k8sClient.Delete, &ip) By("Patching IP reservation and state") ipAddr, err := ipamv1alpha1.IPAddrFromString("1.2.3.4") Expect(err).NotTo(HaveOccurred()) - Eventually(UpdateStatus(ip, func() { + Eventually(UpdateStatus(&ip, func() { ip.Status.Reserved = ipAddr ip.Status.State = ipamv1alpha1.CFinishedIPState })).Should(Succeed()) By("Expecting finalizer, mac, and endpointref to be correct on the OOB") - Eventually(Object(oob), timeToReady).Should(SatisfyAll( + Eventually(Object(&oob), timeToReady).Should(SatisfyAll( HaveField("Finalizers", ContainElement(OOBFinalizer)), HaveField("Spec.MACAddress", mac), HaveField("Spec.EndpointRef.Name", ip.Name), @@ -104,18 +87,19 @@ var _ = Describe("OOB Controller", Serial, func() { )) By("Expecting finalizer to be correct on the IP") - Eventually(Object(ip)).Should(HaveField("Finalizers", ContainElement(OOBFinalizer))) + Eventually(Object(&ip)).Should(HaveField("Finalizers", ContainElement(OOBFinalizer))) }) It("should set the OOB to ignored if the ignore annotation is set", func(ctx SpecContext) { - oob := &metalv1alpha1.OOB{ + oob = metalv1alpha1.OOB{ ObjectMeta: metav1.ObjectMeta{ Name: mac, }, } + DeferCleanup(k8sClient.Delete, &oob) By("Creating an IP") - ip := &ipamv1alpha1.IP{ + ip = ipamv1alpha1.IP{ ObjectMeta: metav1.ObjectMeta{ GenerateName: "test-", Namespace: OOBTemporaryNamespaceHack, @@ -125,18 +109,19 @@ var _ = Describe("OOB Controller", Serial, func() { }, }, } - Expect(k8sClient.Create(ctx, ip)).To(Succeed()) + Expect(k8sClient.Create(ctx, &ip)).To(Succeed()) + DeferCleanup(k8sClient.Delete, &ip) By("Patching IP reservation and state") ipAddr, err := ipamv1alpha1.IPAddrFromString("1.2.3.4") Expect(err).NotTo(HaveOccurred()) - Eventually(UpdateStatus(ip, func() { + Eventually(UpdateStatus(&ip, func() { ip.Status.Reserved = ipAddr ip.Status.State = ipamv1alpha1.CFinishedIPState })).Should(Succeed()) By("Setting an ignore annoation on the OOB") - Eventually(Update(oob, func() { + Eventually(Update(&oob, func() { if oob.Annotations == nil { oob.Annotations = make(map[string]string, 1) } @@ -144,32 +129,33 @@ var _ = Describe("OOB Controller", Serial, func() { })).Should(Succeed()) By("Expecting OOB to be ignored") - Eventually(Object(oob)).Should(SatisfyAll( + Eventually(Object(&oob)).Should(SatisfyAll( HaveField("Status.State", metalv1alpha1.OOBStateIgnored), WithTransform(readyReason, Equal(metalv1alpha1.OOBConditionReasonIgnored)), )) By("Clearing the ignore annoation on the OOB") - Eventually(Update(oob, func() { + Eventually(Update(&oob, func() { delete(oob.Annotations, OOBIgnoreAnnotation) })).Should(Succeed()) By("Expecting OOB not to be ignored") - Eventually(Object(oob), timeToReady).Should(SatisfyAll( + Eventually(Object(&oob), timeToReady).Should(SatisfyAll( HaveField("Status.State", metalv1alpha1.OOBStateReady), WithTransform(readyReason, Equal(metalv1alpha1.OOBConditionReasonReady)), )) }) It("should handle an unavailable endpoint", func(ctx SpecContext) { - oob := &metalv1alpha1.OOB{ + oob = metalv1alpha1.OOB{ ObjectMeta: metav1.ObjectMeta{ Name: mac, }, } + DeferCleanup(k8sClient.Delete, &oob) By("Creating an IP") - ip := &ipamv1alpha1.IP{ + ip = ipamv1alpha1.IP{ ObjectMeta: metav1.ObjectMeta{ GenerateName: "test-", Namespace: OOBTemporaryNamespaceHack, @@ -179,18 +165,18 @@ var _ = Describe("OOB Controller", Serial, func() { }, }, } - Expect(k8sClient.Create(ctx, ip)).To(Succeed()) + Expect(k8sClient.Create(ctx, &ip)).To(Succeed()) By("Patching IP reservation and state") ipAddr, err := ipamv1alpha1.IPAddrFromString("1.2.3.4") Expect(err).NotTo(HaveOccurred()) - Eventually(UpdateStatus(ip, func() { + Eventually(UpdateStatus(&ip, func() { ip.Status.Reserved = ipAddr ip.Status.State = ipamv1alpha1.CFinishedIPState })).Should(Succeed()) By("Expecting finalizer, mac, and endpointref to be correct on the OOB") - Eventually(Object(oob), timeToReady).Should(SatisfyAll( + Eventually(Object(&oob), timeToReady).Should(SatisfyAll( HaveField("Finalizers", ContainElement(OOBFinalizer)), HaveField("Spec.MACAddress", mac), HaveField("Spec.EndpointRef.Name", ip.Name), @@ -199,18 +185,18 @@ var _ = Describe("OOB Controller", Serial, func() { )) By("Deleting the IP") - Expect(k8sClient.Delete(ctx, ip)).To(Succeed()) - Eventually(Get(ip)).Should(Satisfy(errors.IsNotFound)) + Expect(k8sClient.Delete(ctx, &ip)).To(Succeed()) + Eventually(Get(&ip)).Should(Satisfy(errors.IsNotFound)) By("Expecting the OOB to have no endpoint") - Eventually(Object(oob)).Should(SatisfyAll( + Eventually(Object(&oob)).Should(SatisfyAll( HaveField("Spec.EndpointRef", BeNil()), HaveField("Status.State", metalv1alpha1.OOBStateNoEndpoint), WithTransform(readyReason, Equal(metalv1alpha1.OOBConditionReasonNoEndpoint)), )) By("Recreating the IP") - ip = &ipamv1alpha1.IP{ + ip = ipamv1alpha1.IP{ ObjectMeta: metav1.ObjectMeta{ Name: ip.Name, Namespace: OOBTemporaryNamespaceHack, @@ -220,14 +206,15 @@ var _ = Describe("OOB Controller", Serial, func() { }, }, } - Expect(k8sClient.Create(ctx, ip)).To(Succeed()) - Eventually(UpdateStatus(ip, func() { + Expect(k8sClient.Create(ctx, &ip)).To(Succeed()) + DeferCleanup(k8sClient.Delete, &ip) + Eventually(UpdateStatus(&ip, func() { ip.Status.Reserved = ipAddr ip.Status.State = ipamv1alpha1.CFinishedIPState })).Should(Succeed()) By("Expecting the OOB to have an endpoint") - Eventually(Object(oob), timeToReady).Should(SatisfyAll( + Eventually(Object(&oob), timeToReady).Should(SatisfyAll( HaveField("Spec.EndpointRef.Name", ip.Name), HaveField("Status.State", metalv1alpha1.OOBStateReady), WithTransform(readyReason, Equal(metalv1alpha1.OOBConditionReasonReady)), @@ -235,14 +222,15 @@ var _ = Describe("OOB Controller", Serial, func() { }) It("should handle a bad endpoint", func(ctx SpecContext) { - oob := &metalv1alpha1.OOB{ + oob = metalv1alpha1.OOB{ ObjectMeta: metav1.ObjectMeta{ Name: mac, }, } + DeferCleanup(k8sClient.Delete, &oob) By("Creating an IP") - ip := &ipamv1alpha1.IP{ + ip = ipamv1alpha1.IP{ ObjectMeta: metav1.ObjectMeta{ GenerateName: "test-", Namespace: OOBTemporaryNamespaceHack, @@ -252,18 +240,19 @@ var _ = Describe("OOB Controller", Serial, func() { }, }, } - Expect(k8sClient.Create(ctx, ip)).To(Succeed()) + Expect(k8sClient.Create(ctx, &ip)).To(Succeed()) + DeferCleanup(k8sClient.Delete, &ip) By("Patching IP reservation and state") ipAddr, err := ipamv1alpha1.IPAddrFromString("1.2.3.4") Expect(err).NotTo(HaveOccurred()) - Eventually(UpdateStatus(ip, func() { + Eventually(UpdateStatus(&ip, func() { ip.Status.Reserved = ipAddr ip.Status.State = ipamv1alpha1.CFinishedIPState })).Should(Succeed()) By("Expecting finalizer, mac, and endpointref to be correct on the OOB") - Eventually(Object(oob), timeToReady).Should(SatisfyAll( + Eventually(Object(&oob), timeToReady).Should(SatisfyAll( HaveField("Finalizers", ContainElement(OOBFinalizer)), HaveField("Spec.MACAddress", mac), HaveField("Spec.EndpointRef.Name", ip.Name), @@ -272,66 +261,68 @@ var _ = Describe("OOB Controller", Serial, func() { )) By("Setting an incorrect MAC on the IP") - Eventually(Update(ip, func() { + Eventually(Update(&ip, func() { ip.Labels[OOBIPMacLabel] = "000000000000" })).Should(Succeed()) By("Expecting the OOB to be in an error state") - Eventually(Object(oob)).Should(SatisfyAll( + Eventually(Object(&oob)).Should(SatisfyAll( HaveField("Status.State", metalv1alpha1.OOBStateError), WithTransform(readyReason, Equal(metalv1alpha1.OOBConditionReasonError)), WithTransform(readyMessage, HavePrefix(OOBErrorBadEndpoint+": ")), )) By("Restoring the MAC on the IP") - Eventually(Update(ip, func() { + Eventually(Update(&ip, func() { ip.Labels[OOBIPMacLabel] = oob.Name })).Should(Succeed()) By("Expecting the OOB to recover") - Eventually(Object(oob), timeToReady).Should(SatisfyAll( + Eventually(Object(&oob), timeToReady).Should(SatisfyAll( HaveField("Status.State", metalv1alpha1.OOBStateReady), WithTransform(readyReason, Equal(metalv1alpha1.OOBConditionReasonReady)), )) By("Setting a failed state on the IP") - Eventually(UpdateStatus(ip, func() { + Eventually(UpdateStatus(&ip, func() { ip.Status.State = ipamv1alpha1.CFailedIPState })).Should(Succeed()) By("Expecting the OOB to be in an error state") - Eventually(Object(oob)).Should(SatisfyAll( + Eventually(Object(&oob)).Should(SatisfyAll( HaveField("Status.State", metalv1alpha1.OOBStateError), WithTransform(readyReason, Equal(metalv1alpha1.OOBConditionReasonError)), WithTransform(readyMessage, HavePrefix(OOBErrorBadEndpoint+": ")), )) By("Restoring the state on the IP") - Eventually(UpdateStatus(ip, func() { + Eventually(UpdateStatus(&ip, func() { ip.Status.State = ipamv1alpha1.CFinishedIPState })).Should(Succeed()) By("Expecting the OOB to recover") - Eventually(Object(oob), timeToReady).Should(SatisfyAll( + Eventually(Object(&oob), timeToReady).Should(SatisfyAll( HaveField("Status.State", metalv1alpha1.OOBStateReady), WithTransform(readyReason, Equal(metalv1alpha1.OOBConditionReasonReady)), )) }) It("should create a new credentials secret", func(ctx SpecContext) { - oob := &metalv1alpha1.OOB{ + oob = metalv1alpha1.OOB{ ObjectMeta: metav1.ObjectMeta{ Name: mac, }, } - secret := &metalv1alpha1.OOBSecret{ + DeferCleanup(k8sClient.Delete, &oob) + secret = metalv1alpha1.OOBSecret{ ObjectMeta: metav1.ObjectMeta{ Name: mac, }, } + DeferCleanup(k8sClient.Delete, &secret) By("Creating an IP") - ip := &ipamv1alpha1.IP{ + ip = ipamv1alpha1.IP{ ObjectMeta: metav1.ObjectMeta{ GenerateName: "test-", Namespace: OOBTemporaryNamespaceHack, @@ -341,25 +332,26 @@ var _ = Describe("OOB Controller", Serial, func() { }, }, } - Expect(k8sClient.Create(ctx, ip)).To(Succeed()) + Expect(k8sClient.Create(ctx, &ip)).To(Succeed()) + DeferCleanup(k8sClient.Delete, &ip) By("Patching IP reservation and state") ipAddr, err := ipamv1alpha1.IPAddrFromString("1.2.3.4") Expect(err).NotTo(HaveOccurred()) - Eventually(UpdateStatus(ip, func() { + Eventually(UpdateStatus(&ip, func() { ip.Status.Reserved = ipAddr ip.Status.State = ipamv1alpha1.CFinishedIPState })).Should(Succeed()) By("Expecting a correct OOBSecret to have been created") - Eventually(Object(secret)).Should(SatisfyAll( + Eventually(Object(&secret)).Should(SatisfyAll( HaveField("Finalizers", ContainElement(OOBFinalizer)), HaveField("Spec.MACAddress", mac), HaveField("Spec.Username", HavePrefix("metal-")), HaveField("Spec.Password", Not(BeEmpty())), HaveField("Spec.ExpirationTime", Not(BeNil())), )) - Eventually(Object(oob), timeToReady).Should(SatisfyAll( + Eventually(Object(&oob), timeToReady).Should(SatisfyAll( HaveField("Spec.SecretRef.Name", secret.Name), HaveField("Status.State", metalv1alpha1.OOBStateReady), WithTransform(readyReason, Equal(metalv1alpha1.OOBConditionReasonReady)), @@ -368,53 +360,56 @@ var _ = Describe("OOB Controller", Serial, func() { It("should not create an OOB if the MAC is unknown", func(ctx SpecContext) { By("Creating an IP") - ip := &ipamv1alpha1.IP{ + ip = ipamv1alpha1.IP{ ObjectMeta: metav1.ObjectMeta{ GenerateName: "test-", Namespace: OOBTemporaryNamespaceHack, Labels: map[string]string{ - OOBIPMacLabel: "aabbccddee00", + OOBIPMacLabel: "ffbbccddee00", "test": "test", }, }, } - Expect(k8sClient.Create(ctx, ip)).To(Succeed()) + Expect(k8sClient.Create(ctx, &ip)).To(Succeed()) + DeferCleanup(k8sClient.Delete, &ip) By("Patching IP reservation and state") ipAddr, err := ipamv1alpha1.IPAddrFromString("1.2.3.4") Expect(err).NotTo(HaveOccurred()) - Eventually(UpdateStatus(ip, func() { + Eventually(UpdateStatus(&ip, func() { ip.Status.Reserved = ipAddr ip.Status.State = ipamv1alpha1.CFinishedIPState })).Should(Succeed()) By("Expecting the IP to have an unknown annotation") - Eventually(Object(ip)).Should(HaveField("Annotations", HaveKey(OOBUnknownAnnotation))) + Eventually(Object(&ip)).Should(HaveField("Annotations", HaveKey(OOBUnknownAnnotation))) - oob := &metalv1alpha1.OOB{ + oob = metalv1alpha1.OOB{ ObjectMeta: metav1.ObjectMeta{ - Name: "aabbccddee00", + Name: "ffbbccddee00", }, } By("Expecting OOB not to exist") - Eventually(Get(oob)).Should(Satisfy(errors.IsNotFound)) + Eventually(Get(&oob)).Should(Satisfy(errors.IsNotFound)) }) It("should handle a bad credentials secret", func(ctx SpecContext) { - oob := &metalv1alpha1.OOB{ + oob = metalv1alpha1.OOB{ ObjectMeta: metav1.ObjectMeta{ Name: mac, }, } - secret := &metalv1alpha1.OOBSecret{ + DeferCleanup(k8sClient.Delete, &oob) + secret = metalv1alpha1.OOBSecret{ ObjectMeta: metav1.ObjectMeta{ Name: mac, }, } + DeferCleanup(k8sClient.Delete, &secret) By("Creating an IP") - ip := &ipamv1alpha1.IP{ + ip = ipamv1alpha1.IP{ ObjectMeta: metav1.ObjectMeta{ GenerateName: "test-", Namespace: OOBTemporaryNamespaceHack, @@ -424,68 +419,71 @@ var _ = Describe("OOB Controller", Serial, func() { }, }, } - Expect(k8sClient.Create(ctx, ip)).To(Succeed()) + Expect(k8sClient.Create(ctx, &ip)).To(Succeed()) + DeferCleanup(k8sClient.Delete, &ip) By("Patching IP reservation and state") ipAddr, err := ipamv1alpha1.IPAddrFromString("1.2.3.4") Expect(err).NotTo(HaveOccurred()) - Eventually(UpdateStatus(ip, func() { + Eventually(UpdateStatus(&ip, func() { ip.Status.Reserved = ipAddr ip.Status.State = ipamv1alpha1.CFinishedIPState })).Should(Succeed()) By("Expecting a correct OOBSecret to have been created") - Eventually(Object(secret)).Should(SatisfyAll( + Eventually(Object(&secret)).Should(SatisfyAll( HaveField("Finalizers", ContainElement(OOBFinalizer)), HaveField("Spec.MACAddress", mac), HaveField("Spec.Username", HavePrefix("metal-")), HaveField("Spec.Password", Not(BeEmpty())), HaveField("Spec.ExpirationTime", Not(BeNil())), )) - Eventually(Object(oob), timeToReady).Should(SatisfyAll( + Eventually(Object(&oob), timeToReady).Should(SatisfyAll( HaveField("Spec.SecretRef.Name", secret.Name), HaveField("Status.State", metalv1alpha1.OOBStateReady), WithTransform(readyReason, Equal(metalv1alpha1.OOBConditionReasonReady)), )) By("Setting an incorrect MAC on the OOBSecret") - Eventually(Update(secret, func() { + Eventually(Update(&secret, func() { secret.Spec.MACAddress = "000000000000" })).Should(Succeed()) By("Expecting the OOB to be in an error state") - Eventually(Object(oob)).Should(SatisfyAll( + Eventually(Object(&oob)).Should(SatisfyAll( HaveField("Status.State", metalv1alpha1.OOBStateError), WithTransform(readyReason, Equal(metalv1alpha1.OOBConditionReasonError)), WithTransform(readyMessage, HavePrefix(OOBErrorBadCredentials+": ")), )) By("Restoring the MAC on the OOBSecret") - Eventually(Update(secret, func() { + Eventually(Update(&secret, func() { secret.Spec.MACAddress = mac })).Should(Succeed()) By("Expecting the OOB to recover") - Eventually(Object(oob), timeToReady).Should(SatisfyAll( + Eventually(Object(&oob), timeToReady).Should(SatisfyAll( HaveField("Status.State", metalv1alpha1.OOBStateReady), WithTransform(readyReason, Equal(metalv1alpha1.OOBConditionReasonReady)), )) }) It("should rotate expiring credentials", func(ctx SpecContext) { - oob := &metalv1alpha1.OOB{ + oob = metalv1alpha1.OOB{ ObjectMeta: metav1.ObjectMeta{ Name: mac, }, } - secret := &metalv1alpha1.OOBSecret{ + DeferCleanup(k8sClient.Delete, &oob) + secret = metalv1alpha1.OOBSecret{ ObjectMeta: metav1.ObjectMeta{ Name: mac, }, } + DeferCleanup(k8sClient.Delete, &secret) By("Creating an IP") - ip := &ipamv1alpha1.IP{ + ip = ipamv1alpha1.IP{ ObjectMeta: metav1.ObjectMeta{ GenerateName: "test-", Namespace: OOBTemporaryNamespaceHack, @@ -495,51 +493,52 @@ var _ = Describe("OOB Controller", Serial, func() { }, }, } - Expect(k8sClient.Create(ctx, ip)).To(Succeed()) + Expect(k8sClient.Create(ctx, &ip)).To(Succeed()) + DeferCleanup(k8sClient.Delete, &ip) By("Patching IP reservation and state") ipAddr, err := ipamv1alpha1.IPAddrFromString("1.2.3.4") Expect(err).NotTo(HaveOccurred()) - Eventually(UpdateStatus(ip, func() { + Eventually(UpdateStatus(&ip, func() { ip.Status.Reserved = ipAddr ip.Status.State = ipamv1alpha1.CFinishedIPState })).Should(Succeed()) By("Expecting a correct OOBSecret to have been created") - Eventually(Object(secret)).Should(SatisfyAll( + Eventually(Object(&secret)).Should(SatisfyAll( HaveField("Finalizers", ContainElement(OOBFinalizer)), HaveField("Spec.MACAddress", mac), HaveField("Spec.Username", HavePrefix("metal-")), HaveField("Spec.Password", Not(BeEmpty())), HaveField("Spec.ExpirationTime", Not(BeNil())), )) - Eventually(Object(oob), timeToReady).Should(SatisfyAll( + Eventually(Object(&oob), timeToReady).Should(SatisfyAll( HaveField("Spec.SecretRef.Name", secret.Name), HaveField("Status.State", metalv1alpha1.OOBStateReady), WithTransform(readyReason, Equal(metalv1alpha1.OOBConditionReasonReady)), )) var obj client.Object - obj, err = Object(secret)() + obj, err = Object(&secret)() Expect(err).NotTo(HaveOccurred()) - secret = obj.(*metalv1alpha1.OOBSecret) + secret = *obj.(*metalv1alpha1.OOBSecret) Expect(secret).NotTo(BeNil()) username, password, expiration := secret.Spec.Username, secret.Spec.Password, metav1.Now() By("Setting the expiration date of the OOBSecret to now") - Eventually(Update(secret, func() { + Eventually(Update(&secret, func() { secret.Spec.ExpirationTime = &expiration })).Should(Succeed()) By("Expecting the OOBSecret to contain new credentials") - Eventually(Object(secret)).Should(SatisfyAll( + Eventually(Object(&secret)).Should(SatisfyAll( HaveField("Finalizers", ContainElement(OOBFinalizer)), HaveField("Spec.MACAddress", mac), HaveField("Spec.Username", Not(Equal(username))), HaveField("Spec.Password", Not(Equal(password))), HaveField("Spec.ExpirationTime", Not(Equal(expiration))), )) - Eventually(Object(oob), timeToReady).Should(SatisfyAll( + Eventually(Object(&oob), timeToReady).Should(SatisfyAll( HaveField("Spec.SecretRef.Name", secret.Name), HaveField("Status.State", metalv1alpha1.OOBStateReady), WithTransform(readyReason, Equal(metalv1alpha1.OOBConditionReasonReady)), @@ -547,14 +546,15 @@ var _ = Describe("OOB Controller", Serial, func() { }) It("should retrieve BMC info", func(ctx SpecContext) { - oob := &metalv1alpha1.OOB{ + oob = metalv1alpha1.OOB{ ObjectMeta: metav1.ObjectMeta{ Name: mac, }, } + DeferCleanup(k8sClient.Delete, &oob) By("Creating an IP") - ip := &ipamv1alpha1.IP{ + ip = ipamv1alpha1.IP{ ObjectMeta: metav1.ObjectMeta{ GenerateName: "test-", Namespace: OOBTemporaryNamespaceHack, @@ -564,18 +564,19 @@ var _ = Describe("OOB Controller", Serial, func() { }, }, } - Expect(k8sClient.Create(ctx, ip)).To(Succeed()) + Expect(k8sClient.Create(ctx, &ip)).To(Succeed()) + DeferCleanup(k8sClient.Delete, &ip) By("Patching IP reservation and state") ipAddr, err := ipamv1alpha1.IPAddrFromString("1.2.3.4") Expect(err).NotTo(HaveOccurred()) - Eventually(UpdateStatus(ip, func() { + Eventually(UpdateStatus(&ip, func() { ip.Status.Reserved = ipAddr ip.Status.State = ipamv1alpha1.CFinishedIPState })).Should(Succeed()) By("Expecting the OOB to have the correct info") - Eventually(Object(oob), timeToReady).Should(SatisfyAll( + Eventually(Object(&oob), timeToReady).Should(SatisfyAll( HaveField("Status.Type", metalv1alpha1.OOBTypeMachine), HaveField("Status.Manufacturer", "Fake"), HaveField("Status.SerialNumber", "0"), @@ -586,14 +587,15 @@ var _ = Describe("OOB Controller", Serial, func() { }) It("should create Machine objects", func(ctx SpecContext) { - oob := &metalv1alpha1.OOB{ + oob = metalv1alpha1.OOB{ ObjectMeta: metav1.ObjectMeta{ Name: mac, }, } + DeferCleanup(k8sClient.Delete, &oob) By("Creating an IP") - ip := &ipamv1alpha1.IP{ + ip = ipamv1alpha1.IP{ ObjectMeta: metav1.ObjectMeta{ GenerateName: "test-", Namespace: OOBTemporaryNamespaceHack, @@ -603,27 +605,34 @@ var _ = Describe("OOB Controller", Serial, func() { }, }, } - Expect(k8sClient.Create(ctx, ip)).To(Succeed()) + Expect(k8sClient.Create(ctx, &ip)).To(Succeed()) + DeferCleanup(k8sClient.Delete, &ip) By("Patching IP reservation and state") ipAddr, err := ipamv1alpha1.IPAddrFromString("1.2.3.4") Expect(err).NotTo(HaveOccurred()) - Eventually(UpdateStatus(ip, func() { + Eventually(UpdateStatus(&ip, func() { ip.Status.Reserved = ipAddr ip.Status.State = ipamv1alpha1.CFinishedIPState })).Should(Succeed()) By("Expecting the OOB to have the correct info") - Eventually(Object(oob), timeToReady).Should(SatisfyAll( + Eventually(Object(&oob), timeToReady).Should(SatisfyAll( HaveField("Status.Type", metalv1alpha1.OOBTypeMachine), HaveField("Status.State", metalv1alpha1.OOBStateReady), WithTransform(readyReason, Equal(metalv1alpha1.OOBConditionReasonReady)), )) By("Listing machines") - machines := &metalv1alpha1.MachineList{} - Eventually(ObjectList(machines)).Should(HaveField("Items", HaveLen(1))) - machine := &machines.Items[0] + machines = metalv1alpha1.MachineList{} + Eventually(ObjectList(&machines)).Should(SatisfyAll(HaveField("Items", Not(BeEmpty())))) + for _, m := range machines.Items { + if m.Spec.OOBRef.Name == oob.Name { + machine = m.DeepCopy() + break + } + } + Expect(machine).NotTo(BeNil()) By("Expecting Machine to have the correct data") Eventually(Object(machine)).Should(SatisfyAll( @@ -638,6 +647,16 @@ var _ = Describe("OOB Controller", Serial, func() { }) }) +func generateMacAddress() (string, error) { + buf := make([]byte, 3) + _, err := rand.Read(buf) + if err != nil { + return "", err + } + mac := fmt.Sprintf("%s%02x%02x%02x", prefix, buf[0], buf[1], buf[2]) + return mac, nil +} + func readyReason(o client.Object) (string, error) { oob, ok := o.(*metalv1alpha1.OOB) if !ok { diff --git a/test/macdb.yaml b/test/macdb.yaml index cd833021..0e0ddb07 100644 --- a/test/macdb.yaml +++ b/test/macdb.yaml @@ -1,5 +1,5 @@ macs: -- prefix: aabbccddeeff +- prefix: aabbcc protocol: name: Redfish defaultCredentials: