From a7367107281e7ee5b8ec803211ce6d93bc09ed40 Mon Sep 17 00:00:00 2001 From: Artem Bortnikov Date: Tue, 4 Jun 2024 15:39:55 +0300 Subject: [PATCH] fix tests Signed-off-by: Artem Bortnikov --- internal/controller/oob_controller_test.go | 184 ++++++++++++--------- 1 file changed, 110 insertions(+), 74 deletions(-) diff --git a/internal/controller/oob_controller_test.go b/internal/controller/oob_controller_test.go index 003919f..2f512fc 100644 --- a/internal/controller/oob_controller_test.go +++ b/internal/controller/oob_controller_test.go @@ -29,14 +29,16 @@ var _ = Describe("OOB Controller", func() { var ( oob metalv1alpha1.OOB secret metalv1alpha1.OOBSecret - machine *metalv1alpha1.Machine + machine metalv1alpha1.Machine machines metalv1alpha1.MachineList ip ipamv1alpha1.IP mac string + ipAddr string ) BeforeEach(func() { mac, _ = generateMacAddress() + ipAddr, _ = generateIpAddress() DeferCleanup(func(ctx SpecContext) { machines = metalv1alpha1.MachineList{} Expect(k8sClient.List(ctx, &machines)).To(Succeed()) @@ -71,7 +73,7 @@ var _ = Describe("OOB Controller", func() { DeferCleanup(k8sClient.Delete, &ip) By("Patching IP reservation and state") - ipAddr, err := ipamv1alpha1.IPAddrFromString("1.2.3.4") + ipAddr, err := ipamv1alpha1.IPAddrFromString(ipAddr) Expect(err).NotTo(HaveOccurred()) Eventually(UpdateStatus(&ip, func() { ip.Status.Reserved = ipAddr @@ -114,7 +116,7 @@ var _ = Describe("OOB Controller", func() { DeferCleanup(k8sClient.Delete, &ip) By("Patching IP reservation and state") - ipAddr, err := ipamv1alpha1.IPAddrFromString("1.2.3.4") + ipAddr, err := ipamv1alpha1.IPAddrFromString(ipAddr) Expect(err).NotTo(HaveOccurred()) Eventually(UpdateStatus(&ip, func() { ip.Status.Reserved = ipAddr @@ -169,7 +171,7 @@ var _ = Describe("OOB Controller", func() { Expect(k8sClient.Create(ctx, &ip)).To(Succeed()) By("Patching IP reservation and state") - ipAddr, err := ipamv1alpha1.IPAddrFromString("1.2.3.4") + ipAddr, err := ipamv1alpha1.IPAddrFromString(ipAddr) Expect(err).NotTo(HaveOccurred()) Eventually(UpdateStatus(&ip, func() { ip.Status.Reserved = ipAddr @@ -245,7 +247,7 @@ var _ = Describe("OOB Controller", func() { DeferCleanup(k8sClient.Delete, &ip) By("Patching IP reservation and state") - ipAddr, err := ipamv1alpha1.IPAddrFromString("1.2.3.4") + ipAddr, err := ipamv1alpha1.IPAddrFromString(ipAddr) Expect(err).NotTo(HaveOccurred()) Eventually(UpdateStatus(&ip, func() { ip.Status.Reserved = ipAddr @@ -337,7 +339,7 @@ var _ = Describe("OOB Controller", func() { DeferCleanup(k8sClient.Delete, &ip) By("Patching IP reservation and state") - ipAddr, err := ipamv1alpha1.IPAddrFromString("1.2.3.4") + ipAddr, err := ipamv1alpha1.IPAddrFromString(ipAddr) Expect(err).NotTo(HaveOccurred()) Eventually(UpdateStatus(&ip, func() { ip.Status.Reserved = ipAddr @@ -375,7 +377,7 @@ var _ = Describe("OOB Controller", func() { DeferCleanup(k8sClient.Delete, &ip) By("Patching IP reservation and state") - ipAddr, err := ipamv1alpha1.IPAddrFromString("1.2.3.4") + ipAddr, err := ipamv1alpha1.IPAddrFromString(ipAddr) Expect(err).NotTo(HaveOccurred()) Eventually(UpdateStatus(&ip, func() { ip.Status.Reserved = ipAddr @@ -424,7 +426,7 @@ var _ = Describe("OOB Controller", func() { DeferCleanup(k8sClient.Delete, &ip) By("Patching IP reservation and state") - ipAddr, err := ipamv1alpha1.IPAddrFromString("1.2.3.4") + ipAddr, err := ipamv1alpha1.IPAddrFromString(ipAddr) Expect(err).NotTo(HaveOccurred()) Eventually(UpdateStatus(&ip, func() { ip.Status.Reserved = ipAddr @@ -498,7 +500,7 @@ var _ = Describe("OOB Controller", func() { DeferCleanup(k8sClient.Delete, &ip) By("Patching IP reservation and state") - ipAddr, err := ipamv1alpha1.IPAddrFromString("1.2.3.4") + ipAddr, err := ipamv1alpha1.IPAddrFromString(ipAddr) Expect(err).NotTo(HaveOccurred()) Eventually(UpdateStatus(&ip, func() { ip.Status.Reserved = ipAddr @@ -569,7 +571,7 @@ var _ = Describe("OOB Controller", func() { DeferCleanup(k8sClient.Delete, &ip) By("Patching IP reservation and state") - ipAddr, err := ipamv1alpha1.IPAddrFromString("1.2.3.4") + ipAddr, err := ipamv1alpha1.IPAddrFromString(ipAddr) Expect(err).NotTo(HaveOccurred()) Eventually(UpdateStatus(&ip, func() { ip.Status.Reserved = ipAddr @@ -610,7 +612,7 @@ var _ = Describe("OOB Controller", func() { DeferCleanup(k8sClient.Delete, &ip) By("Patching IP reservation and state") - ipAddr, err := ipamv1alpha1.IPAddrFromString("1.2.3.4") + ipAddr, err := ipamv1alpha1.IPAddrFromString(ipAddr) Expect(err).NotTo(HaveOccurred()) Eventually(UpdateStatus(&ip, func() { ip.Status.Reserved = ipAddr @@ -629,14 +631,14 @@ var _ = Describe("OOB Controller", func() { Eventually(ObjectList(&machines)).Should(SatisfyAll(HaveField("Items", Not(BeEmpty())))) for _, m := range machines.Items { if m.Spec.OOBRef.Name == oob.Name { - machine = m.DeepCopy() + machine = m break } } Expect(machine).NotTo(BeNil()) By("Expecting Machine to have the correct data") - Eventually(Object(machine)).Should(SatisfyAll( + Eventually(Object(&machine)).Should(SatisfyAll( HaveField("Spec.UUID", machine.Name), HaveField("Spec.OOBRef.Name", oob.Name), HaveField("Status.Manufacturer", "Fake"), @@ -648,14 +650,15 @@ var _ = Describe("OOB Controller", func() { }) It("should control locator LED", 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, @@ -665,68 +668,76 @@ var _ = Describe("OOB Controller", 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") + ipAddr, err := ipamv1alpha1.IPAddrFromString(ipAddr) 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 + break + } + } + Expect(machine).NotTo(BeNil()) By("Setting LED to lit") - Eventually(Update(machine, func() { + Eventually(Update(&machine, func() { machine.Spec.LocatorLED = metalv1alpha1.LEDLit })).Should(Succeed()) By("Expecting LED to be lit") - Eventually(Object(machine)).Should(SatisfyAll( + Eventually(Object(&machine)).Should(SatisfyAll( HaveField("Status.LocatorLED", metalv1alpha1.LEDLit), )) By("Setting LED to blinking") - Eventually(Update(machine, func() { + Eventually(Update(&machine, func() { machine.Spec.LocatorLED = metalv1alpha1.LEDBlinking })).Should(Succeed()) By("Expecting LED to be blinking") - Eventually(Object(machine)).Should(SatisfyAll( + Eventually(Object(&machine)).Should(SatisfyAll( HaveField("Status.LocatorLED", metalv1alpha1.LEDBlinking), )) By("Setting LED to off") - Eventually(Update(machine, func() { + Eventually(Update(&machine, func() { machine.Spec.LocatorLED = metalv1alpha1.LEDOff })).Should(Succeed()) By("Expecting LED to be off") - Eventually(Object(machine)).Should(SatisfyAll( + Eventually(Object(&machine)).Should(SatisfyAll( HaveField("Status.LocatorLED", metalv1alpha1.LEDOff), )) }) It("should control power", 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, @@ -736,61 +747,68 @@ var _ = Describe("OOB Controller", 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") + ipAddr, err := ipamv1alpha1.IPAddrFromString(ipAddr) 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 + break + } + } + Expect(machine).NotTo(BeNil()) By("Setting power to on") - Eventually(Update(machine, func() { + Eventually(Update(&machine, func() { machine.Spec.Power = metalv1alpha1.PowerOn })).Should(Succeed()) By("Expecting machine to be on") - Eventually(Object(machine), timeToReady).Should(SatisfyAll( + Eventually(Object(&machine), timeToReady).Should(SatisfyAll( HaveField("Status.Power", metalv1alpha1.PowerOn), )) By("Setting power to off") - Eventually(Update(machine, func() { + Eventually(Update(&machine, func() { machine.Spec.Power = metalv1alpha1.PowerOff })).Should(Succeed()) By("Expecting machine to be off and annotation to be cleared") - Eventually(Object(machine), timeToReady).Should(SatisfyAll( + Eventually(Object(&machine), timeToReady).Should(SatisfyAll( Not(HaveField("Annotations", HaveKey(metalv1alpha1.MachineOperationKeyName))), HaveField("Status.Power", metalv1alpha1.PowerOff), )) By("Setting power to on") - Eventually(Update(machine, func() { + Eventually(Update(&machine, func() { machine.Spec.Power = metalv1alpha1.PowerOn })).Should(Succeed()) By("Expecting machine to be on") - Eventually(Object(machine), timeToReady).Should(SatisfyAll( + Eventually(Object(&machine), timeToReady).Should(SatisfyAll( HaveField("Status.Power", metalv1alpha1.PowerOn), )) By("Setting power to force off") - Eventually(Update(machine, func() { + Eventually(Update(&machine, func() { if machine.Annotations == nil { machine.Annotations = make(map[string]string, 1) } @@ -799,47 +817,48 @@ var _ = Describe("OOB Controller", func() { })).Should(Succeed()) By("Expecting machine to be off and annotation to be cleared") - Eventually(Object(machine), timeToReady).Should(SatisfyAll( - Not(HaveField("Annotations", HaveKey(metalv1alpha1.MachineOperationKeyName))), + Eventually(Object(&machine), timeToReady).Should(SatisfyAll( + HaveField("Annotations", HaveKeyWithValue(metalv1alpha1.MachineOperationKeyName, "")), HaveField("Status.Power", metalv1alpha1.PowerOff), )) By("Setting power to on") - Eventually(Update(machine, func() { + Eventually(Update(&machine, func() { machine.Spec.Power = metalv1alpha1.PowerOn })).Should(Succeed()) By("Expecting machine to be on") - Eventually(Object(machine), timeToReady).Should(SatisfyAll( + Eventually(Object(&machine), timeToReady).Should(SatisfyAll( HaveField("Status.Power", metalv1alpha1.PowerOn), )) By("Setting power to off while the machine is hanging") - Eventually(Update(oob, func() { + Eventually(Update(&oob, func() { oob.Spec.Flags = map[string]string{ "fake.power": "stuck", } })).Should(Succeed()) - Eventually(Update(machine, func() { + Eventually(Update(&machine, func() { machine.Spec.Power = metalv1alpha1.PowerOff })).Should(Succeed()) By("Expecting machine to be off and annotation to be cleared") - Eventually(Object(machine), timeToReady+shutdownTimeout).Should(SatisfyAll( - Not(HaveField("Annotations", HaveKey(metalv1alpha1.MachineOperationKeyName))), + Eventually(Object(&machine), timeToReady+shutdownTimeout).Should(SatisfyAll( + HaveField("Annotations", HaveKeyWithValue(metalv1alpha1.MachineOperationKeyName, "")), HaveField("Status.Power", metalv1alpha1.PowerOff), )) }) It("should restart", 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, @@ -849,40 +868,47 @@ var _ = Describe("OOB Controller", 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") + ipAddr, err := ipamv1alpha1.IPAddrFromString(ipAddr) 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 + break + } + } + Expect(machine).NotTo(BeNil()) By("Setting power to on") - Eventually(Update(machine, func() { + Eventually(Update(&machine, func() { machine.Spec.Power = metalv1alpha1.PowerOn })).Should(Succeed()) By("Expecting machine to be on") - Eventually(Object(machine), timeToReady).Should(SatisfyAll( + Eventually(Object(&machine), timeToReady).Should(SatisfyAll( HaveField("Status.Power", metalv1alpha1.PowerOn), )) By("Setting restart annotation") - Eventually(Update(machine, func() { + Eventually(Update(&machine, func() { if machine.Annotations == nil { machine.Annotations = make(map[string]string, 1) } @@ -890,13 +916,13 @@ var _ = Describe("OOB Controller", func() { })).Should(Succeed()) By("Expecting machine to be on and annotation to be cleared") - Eventually(Object(machine), timeToReady).Should(SatisfyAll( - Not(HaveField("Annotations", HaveKey(metalv1alpha1.MachineOperationKeyName))), + Eventually(Object(&machine), timeToReady).Should(SatisfyAll( + HaveField("Annotations", HaveKeyWithValue(metalv1alpha1.MachineOperationKeyName, "")), HaveField("Status.Power", metalv1alpha1.PowerOn), )) By("Setting force restart annotation") - Eventually(Update(machine, func() { + Eventually(Update(&machine, func() { if machine.Annotations == nil { machine.Annotations = make(map[string]string, 1) } @@ -904,23 +930,23 @@ var _ = Describe("OOB Controller", func() { })).Should(Succeed()) By("Expecting machine to be on and annotation to be cleared") - Eventually(Object(machine), timeToReady).Should(SatisfyAll( - Not(HaveField("Annotations", HaveKey(metalv1alpha1.MachineOperationKeyName))), + Eventually(Object(&machine), timeToReady).Should(SatisfyAll( + HaveField("Annotations", HaveKeyWithValue(metalv1alpha1.MachineOperationKeyName, "")), HaveField("Status.Power", metalv1alpha1.PowerOn), )) By("Setting power to off") - Eventually(Update(machine, func() { + Eventually(Update(&machine, func() { machine.Spec.Power = metalv1alpha1.PowerOff })).Should(Succeed()) By("Expecting machine to be off") - Eventually(Object(machine), timeToReady).Should(SatisfyAll( + Eventually(Object(&machine), timeToReady).Should(SatisfyAll( HaveField("Status.Power", metalv1alpha1.PowerOff), )) By("Setting restart annotation") - Eventually(Update(machine, func() { + Eventually(Update(&machine, func() { if machine.Annotations == nil { machine.Annotations = make(map[string]string, 1) } @@ -928,8 +954,8 @@ var _ = Describe("OOB Controller", func() { })).Should(Succeed()) By("Expecting machine to be off and annotation to be cleared") - Eventually(Object(machine), timeToReady).Should(SatisfyAll( - Not(HaveField("Annotations", HaveKey(metalv1alpha1.MachineOperationKeyName))), + Eventually(Object(&machine), timeToReady).Should(SatisfyAll( + HaveField("Annotations", HaveKeyWithValue(metalv1alpha1.MachineOperationKeyName, "")), HaveField("Status.Power", metalv1alpha1.PowerOff), )) }) @@ -945,6 +971,16 @@ func generateMacAddress() (string, error) { return mac, nil } +func generateIpAddress() (string, error) { + buf := make([]byte, 2) + _, err := rand.Read(buf) + if err != nil { + return "", err + } + ip := fmt.Sprintf("100.64.%d.%d", buf[0], buf[1]) + return ip, nil +} + func readyReason(o client.Object) (string, error) { oob, ok := o.(*metalv1alpha1.OOB) if !ok {