diff --git a/esx/check_test.go b/esx/check_test.go index ef59bce..28fbf64 100644 --- a/esx/check_test.go +++ b/esx/check_test.go @@ -20,8 +20,6 @@ package esx import ( - "context" - . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" "github.com/vmware/govmomi/object" @@ -33,7 +31,7 @@ const HostSystemName string = "DC0_H0" var _ = Describe("CheckForMaintenance", func() { var vCenters *VCenters - BeforeEach(func() { + BeforeEach(func(ctx SpecContext) { vCenters = &VCenters{ Template: TemplateURL, Credentials: map[string]Credential{ @@ -45,34 +43,34 @@ var _ = Describe("CheckForMaintenance", func() { } // set host out of maintenance - client, err := vCenters.Client(context.Background(), vcServer.URL.Host) + client, err := vCenters.Client(ctx, vcServer.URL.Host) Expect(err).To(Succeed()) host := object.NewHostSystem(client.Client, types.ManagedObjectReference{ Type: "HostSystem", - Value: "host-20", + Value: esxRef, }) - task, err := host.ExitMaintenanceMode(context.Background(), 1000) + task, err := host.ExitMaintenanceMode(ctx, 1000) Expect(err).To(Succeed()) - err = task.WaitEx(context.Background()) + err = task.WaitEx(ctx) Expect(err).To(Succeed()) }) - AfterEach(func() { + AfterEach(func(ctx SpecContext) { // set host out of maintenance - client, err := vCenters.Client(context.Background(), vcServer.URL.Host) + client, err := vCenters.Client(ctx, vcServer.URL.Host) Expect(err).To(Succeed()) host := object.NewHostSystem(client.Client, types.ManagedObjectReference{ Type: "HostSystem", - Value: "host-20", + Value: esxRef, }) - task, err := host.ExitMaintenanceMode(context.Background(), 1000) + task, err := host.ExitMaintenanceMode(ctx, 1000) Expect(err).To(Succeed()) - err = task.WaitEx(context.Background()) + err = task.WaitEx(ctx) Expect(err).To(Succeed()) }) - It("should return NoMaintenance if the host is not in maintenance", func() { - result, err := CheckForMaintenance(context.Background(), CheckParameters{vCenters, HostInfo{ + It("should return NoMaintenance if the host is not in maintenance", func(ctx SpecContext) { + result, err := CheckForMaintenance(ctx, CheckParameters{vCenters, HostInfo{ AvailabilityZone: vcServer.URL.Host, Name: HostSystemName, }, GinkgoLogr}) @@ -80,21 +78,21 @@ var _ = Describe("CheckForMaintenance", func() { Expect(result).To(Equal(NoMaintenance)) }) - It("should return InMaintenance if the host is in maintenance", func() { - client, err := vCenters.Client(context.Background(), vcServer.URL.Host) + It("should return InMaintenance if the host is in maintenance", func(ctx SpecContext) { + client, err := vCenters.Client(ctx, vcServer.URL.Host) Expect(err).To(Succeed()) // set host in maintenance host := object.NewHostSystem(client.Client, types.ManagedObjectReference{ Type: "HostSystem", - Value: "host-20", + Value: esxRef, }) - task, err := host.EnterMaintenanceMode(context.Background(), 1000, false, &types.HostMaintenanceSpec{}) + task, err := host.EnterMaintenanceMode(ctx, 1000, false, &types.HostMaintenanceSpec{}) Expect(err).To(Succeed()) - err = task.WaitEx(context.Background()) + err = task.WaitEx(ctx) Expect(err).To(Succeed()) - result, err := CheckForMaintenance(context.Background(), CheckParameters{vCenters, HostInfo{ + result, err := CheckForMaintenance(ctx, CheckParameters{vCenters, HostInfo{ AvailabilityZone: vcServer.URL.Host, Name: HostSystemName, }, GinkgoLogr}) @@ -119,8 +117,8 @@ var _ = Describe("FetchVersion", func() { } }) - It("should return the version", func() { - version, err := FetchVersion(context.Background(), CheckParameters{vCenters, HostInfo{ + It("should return the version", func(ctx SpecContext) { + version, err := FetchVersion(ctx, CheckParameters{vCenters, HostInfo{ AvailabilityZone: vcServer.URL.Host, Name: HostSystemName, }, GinkgoLogr}) diff --git a/esx/runnable_test.go b/esx/runnable_test.go index 6ced7a0..6d79fb8 100644 --- a/esx/runnable_test.go +++ b/esx/runnable_test.go @@ -47,7 +47,7 @@ var _ = Describe("The ESX controller", func() { var thirdNode *corev1.Node var fourthNode *corev1.Node - makeNode := func(name, esx string, schedulable, withPods bool) (*corev1.Node, error) { + makeNode := func(ctx context.Context, name, esx string, schedulable, withPods bool) (*corev1.Node, error) { node := &corev1.Node{} node.Name = name node.Namespace = DefaultNamespace @@ -55,7 +55,7 @@ var _ = Describe("The ESX controller", func() { node.Labels = make(map[string]string) node.Labels[constants.HostLabelKey] = esx node.Labels[constants.FailureDomainLabelKey] = "eu-nl-2a" - err := k8sClient.Create(context.Background(), node) + err := k8sClient.Create(ctx, node) if err != nil { return nil, err } @@ -76,62 +76,62 @@ var _ = Describe("The ESX controller", func() { } var gracePeriod int64 pod.Spec.TerminationGracePeriodSeconds = &gracePeriod - err = k8sClient.Create(context.Background(), pod) + err = k8sClient.Create(ctx, pod) if err != nil { return nil, err } return node, nil } - BeforeEach(func() { + BeforeEach(func(ctx SpecContext) { var err error - firstNode, err = makeNode("firstvm", ESXName, true, true) + firstNode, err = makeNode(ctx, "firstvm", ESXName, true, true) Expect(err).To(Succeed()) - secondNode, err = makeNode("secondvm", ESXName, true, true) + secondNode, err = makeNode(ctx, "secondvm", ESXName, true, true) Expect(err).To(Succeed()) - thirdNode, err = makeNode("thirdvm", "DC0_H1", true, false) + thirdNode, err = makeNode(ctx, "thirdvm", "DC0_H1", true, false) Expect(err).To(Succeed()) - fourthNode, err = makeNode("fourthvm", "DC0_H1", false, false) + fourthNode, err = makeNode(ctx, "fourthvm", "DC0_H1", false, false) Expect(err).To(Succeed()) }) - AfterEach(func() { - err := k8sClient.Delete(context.Background(), firstNode) + AfterEach(func(ctx SpecContext) { + err := k8sClient.Delete(ctx, firstNode) Expect(err).To(Succeed()) - err = k8sClient.Delete(context.Background(), secondNode) + err = k8sClient.Delete(ctx, secondNode) Expect(err).To(Succeed()) - err = k8sClient.Delete(context.Background(), thirdNode) + err = k8sClient.Delete(ctx, thirdNode) Expect(err).To(Succeed()) - err = k8sClient.Delete(context.Background(), fourthNode) + err = k8sClient.Delete(ctx, fourthNode) Expect(err).To(Succeed()) var podList corev1.PodList - err = k8sClient.List(context.Background(), &podList) + err = k8sClient.List(ctx, &podList) Expect(err).To(Succeed()) var gracePeriod int64 for i := range podList.Items { - err = k8sClient.Delete(context.Background(), &podList.Items[i], + err = k8sClient.Delete(ctx, &podList.Items[i], &client.DeleteOptions{GracePeriodSeconds: &gracePeriod}) Expect(err).To(Succeed()) } - vcClient, err := govmomi.NewClient(context.Background(), vcServer.URL, true) + vcClient, err := govmomi.NewClient(ctx, vcServer.URL, true) Expect(err).To(Succeed()) // set host out of maintenance host := object.NewHostSystem(vcClient.Client, vctypes.ManagedObjectReference{ Type: "HostSystem", - Value: "host-20", + Value: esxRef, }) - task, err := host.ExitMaintenanceMode(context.Background(), 1000) + task, err := host.ExitMaintenanceMode(ctx, 1000) Expect(err).To(Succeed()) - err = task.WaitEx(context.Background()) + err = task.WaitEx(ctx) Expect(err).To(Succeed()) }) - It("labels previously unlabeled nodes with maintenance state", func() { + It("labels previously unlabeled nodes with maintenance state", func(ctx SpecContext) { Eventually(func(g Gomega) string { var node corev1.Node - err := k8sClient.Get(context.Background(), client.ObjectKey{Name: "firstvm"}, &node) + err := k8sClient.Get(ctx, client.ObjectKey{Name: "firstvm"}, &node) g.Expect(err).To(Succeed()) val := node.Labels[constants.EsxMaintenanceLabelKey] @@ -139,7 +139,7 @@ var _ = Describe("The ESX controller", func() { }).Should(Equal(string(NoMaintenance))) Eventually(func(g Gomega) string { var node corev1.Node - err := k8sClient.Get(context.Background(), client.ObjectKey{Name: "secondvm"}, &node) + err := k8sClient.Get(ctx, client.ObjectKey{Name: "secondvm"}, &node) g.Expect(err).To(Succeed()) val := node.Labels[constants.EsxMaintenanceLabelKey] @@ -147,10 +147,10 @@ var _ = Describe("The ESX controller", func() { }).Should(Equal(string(NoMaintenance))) }) - It("labels previously unlabeled nodes with esx version", func() { + It("labels previously unlabeled nodes with esx version", func(ctx SpecContext) { Eventually(func(g Gomega) string { var node corev1.Node - err := k8sClient.Get(context.Background(), client.ObjectKey{Name: "firstvm"}, &node) + err := k8sClient.Get(ctx, client.ObjectKey{Name: "firstvm"}, &node) g.Expect(err).To(Succeed()) val := node.Labels[constants.EsxVersionLabelKey] @@ -158,7 +158,7 @@ var _ = Describe("The ESX controller", func() { }).Should(Equal("8.0.2")) Eventually(func(g Gomega) string { var node corev1.Node - err := k8sClient.Get(context.Background(), client.ObjectKey{Name: "secondvm"}, &node) + err := k8sClient.Get(ctx, client.ObjectKey{Name: "secondvm"}, &node) g.Expect(err).To(Succeed()) val := node.Labels[constants.EsxVersionLabelKey] @@ -166,23 +166,23 @@ var _ = Describe("The ESX controller", func() { }).Should(Equal("8.0.2")) }) - It("labels all nodes on a single EXS host in case of changes to the maintenance state", func() { - vcClient, err := govmomi.NewClient(context.Background(), vcServer.URL, true) + It("labels all nodes on a single EXS host in case of changes to the maintenance state", func(ctx SpecContext) { + vcClient, err := govmomi.NewClient(ctx, vcServer.URL, true) Expect(err).To(Succeed()) // set host in maintenance host := object.NewHostSystem(vcClient.Client, vctypes.ManagedObjectReference{ Type: "HostSystem", - Value: "host-20", + Value: esxRef, }) - task, err := host.EnterMaintenanceMode(context.Background(), 1000, false, &vctypes.HostMaintenanceSpec{}) + task, err := host.EnterMaintenanceMode(ctx, 1000, false, &vctypes.HostMaintenanceSpec{}) Expect(err).To(Succeed()) - err = task.WaitEx(context.Background()) + err = task.WaitEx(ctx) Expect(err).To(Succeed()) Eventually(func(g Gomega) string { var node corev1.Node - err := k8sClient.Get(context.Background(), client.ObjectKey{Name: "firstvm"}, &node) + err := k8sClient.Get(ctx, client.ObjectKey{Name: "firstvm"}, &node) g.Expect(err).To(Succeed()) val := node.Labels[constants.EsxMaintenanceLabelKey] @@ -190,7 +190,7 @@ var _ = Describe("The ESX controller", func() { }).Should(Equal(string(InMaintenance))) Eventually(func(g Gomega) string { var node corev1.Node - err := k8sClient.Get(context.Background(), client.ObjectKey{Name: "secondvm"}, &node) + err := k8sClient.Get(ctx, client.ObjectKey{Name: "secondvm"}, &node) g.Expect(err).To(Succeed()) val := node.Labels[constants.EsxMaintenanceLabelKey] @@ -198,64 +198,70 @@ var _ = Describe("The ESX controller", func() { }).Should(Equal(string(InMaintenance))) }) - fetchPowerState := func(vcClient *govmomi.Client) (vctypes.VirtualMachinePowerState, error) { + fetchPowerState := func(ctx context.Context, vcClient *govmomi.Client) (vctypes.VirtualMachinePowerState, error) { mgr := view.NewManager(vcClient.Client) - view, err := mgr.CreateContainerView(context.Background(), + view, err := mgr.CreateContainerView(ctx, vcClient.ServiceContent.RootFolder, []string{"VirtualMachine"}, true) if err != nil { return "", err } var vms []mo.VirtualMachine - err = view.RetrieveWithFilter(context.Background(), []string{"VirtualMachine"}, + err = view.RetrieveWithFilter(ctx, []string{"VirtualMachine"}, []string{"summary.runtime"}, &vms, property.Match{"name": "firstvm"}) if err != nil { + if err = view.Destroy(ctx); err != nil { + return "", err + } return "", nil } + if err = view.Destroy(ctx); err != nil { + return "", err + } return vms[0].Summary.Runtime.PowerState, nil } - It("shuts down nodes on an ESX host if it is in-maintenance and reboots are allowed", func() { - vcClient, err := govmomi.NewClient(context.Background(), vcServer.URL, true) + It("shuts down nodes on an ESX host if it is in-maintenance and reboots are allowed", func(ctx SpecContext) { + vcClient, err := govmomi.NewClient(ctx, vcServer.URL, true) Expect(err).To(Succeed()) // set host in maintenance host := object.NewHostSystem(vcClient.Client, vctypes.ManagedObjectReference{ Type: "HostSystem", - Value: "host-20", + Value: esxRef, }) - task, err := host.EnterMaintenanceMode(context.Background(), 1000, false, &vctypes.HostMaintenanceSpec{}) + task, err := host.EnterMaintenanceMode(ctx, 1000, false, &vctypes.HostMaintenanceSpec{}) Expect(err).To(Succeed()) - err = task.WaitEx(context.Background()) + err = task.WaitEx(ctx) Expect(err).To(Succeed()) allowMaintenance := func(node *corev1.Node) error { cloned := node.DeepCopy() node.Labels[constants.EsxRebootOkLabelKey] = constants.TrueStr - return k8sClient.Patch(context.Background(), node, client.MergeFrom(cloned)) + return k8sClient.Patch(ctx, node, client.MergeFrom(cloned)) } Expect(allowMaintenance(firstNode)).To(Succeed()) Expect(allowMaintenance(secondNode)).To(Succeed()) Eventually(func(g Gomega) map[string]string { node := &corev1.Node{} - err = k8sClient.Get(context.Background(), types.NamespacedName{Namespace: DefaultNamespace, Name: "firstvm"}, node) + err = k8sClient.Get(ctx, types.NamespacedName{Namespace: DefaultNamespace, Name: "firstvm"}, node) g.Expect(err).To(Succeed()) return node.Annotations }).Should(HaveKey(constants.EsxRebootInitiatedAnnotationKey)) Eventually(func(g Gomega) bool { node := &corev1.Node{} - err = k8sClient.Get(context.Background(), types.NamespacedName{Namespace: DefaultNamespace, Name: "firstvm"}, node) + err = k8sClient.Get(ctx, types.NamespacedName{Namespace: DefaultNamespace, Name: "firstvm"}, node) g.Expect(err).To(Succeed()) return node.Spec.Unschedulable }).Should(BeTrue()) Eventually(func(g Gomega) []corev1.Pod { var podList corev1.PodList - err = k8sClient.List(context.Background(), &podList) + err = k8sClient.List(ctx, &podList) g.Expect(err).To(Succeed()) return podList.Items }, 10*time.Second).Should(BeEmpty()) Eventually(func(g Gomega) bool { - powerState, err := fetchPowerState(vcClient) + powerState, err := fetchPowerState(ctx, vcClient) g.Expect(err).To(Succeed()) return powerState == vctypes.VirtualMachinePowerStatePoweredOff }).Should(BeTrue()) @@ -263,49 +269,52 @@ var _ = Describe("The ESX controller", func() { // ensure VM's on different host are not affected mgr := view.NewManager(vcClient.Client) Expect(err).To(Succeed()) - view, err := mgr.CreateContainerView(context.Background(), + view, err := mgr.CreateContainerView(ctx, vcClient.ServiceContent.RootFolder, []string{"VirtualMachine"}, true) Expect(err).To(Succeed()) + defer func() { + Expect(view.Destroy(ctx)).To(Succeed()) + }() var vms []mo.VirtualMachine - err = view.RetrieveWithFilter(context.Background(), []string{"VirtualMachine"}, + err = view.RetrieveWithFilter(ctx, []string{"VirtualMachine"}, []string{"summary.runtime"}, &vms, property.Match{"name": "thirdvm"}) Expect(err).To(Succeed()) result := vms[0].Summary.Runtime.PowerState == vctypes.VirtualMachinePowerStatePoweredOn Expect(result).To(BeTrue()) - err = view.RetrieveWithFilter(context.Background(), []string{"VirtualMachine"}, + err = view.RetrieveWithFilter(ctx, []string{"VirtualMachine"}, []string{"summary.runtime"}, &vms, property.Match{"name": "fourthvm"}) Expect(err).To(Succeed()) result = vms[0].Summary.Runtime.PowerState == vctypes.VirtualMachinePowerStatePoweredOn Expect(result).To(BeTrue()) }) - It("starts nodes on an ESX host if it is out of maintenance and the controller initiated the shutdown", func() { - vcClient, err := govmomi.NewClient(context.Background(), vcServer.URL, true) + It("starts nodes on an ESX host if it is out of maintenance and the controller initiated the shutdown", func(ctx SpecContext) { + vcClient, err := govmomi.NewClient(ctx, vcServer.URL, true) Expect(err).To(Succeed()) markInitiated := func(node *corev1.Node) error { cloned := node.DeepCopy() node.Spec.Unschedulable = true node.Annotations = map[string]string{constants.EsxRebootInitiatedAnnotationKey: constants.TrueStr} - return k8sClient.Patch(context.Background(), node, client.MergeFrom(cloned)) + return k8sClient.Patch(ctx, node, client.MergeFrom(cloned)) } Expect(markInitiated(firstNode)).To(Succeed()) Expect(markInitiated(secondNode)).To(Succeed()) Eventually(func(g Gomega) bool { node := &corev1.Node{} - err := k8sClient.Get(context.Background(), types.NamespacedName{Namespace: DefaultNamespace, Name: "firstvm"}, node) + err := k8sClient.Get(ctx, types.NamespacedName{Namespace: DefaultNamespace, Name: "firstvm"}, node) g.Expect(err).To(Succeed()) return node.Spec.Unschedulable }, 10*time.Second).Should(BeFalse()) Eventually(func(g Gomega) map[string]string { node := &corev1.Node{} - err := k8sClient.Get(context.Background(), types.NamespacedName{Namespace: DefaultNamespace, Name: "firstvm"}, node) + err := k8sClient.Get(ctx, types.NamespacedName{Namespace: DefaultNamespace, Name: "firstvm"}, node) g.Expect(err).To(Succeed()) return node.Annotations }).ShouldNot(HaveKey(constants.EsxRebootInitiatedAnnotationKey)) Eventually(func(g Gomega) bool { - powerState, err := fetchPowerState(vcClient) + powerState, err := fetchPowerState(ctx, vcClient) g.Expect(err).To(Succeed()) return powerState == vctypes.VirtualMachinePowerStatePoweredOn }).Should(BeTrue()) diff --git a/esx/shutdown_test.go b/esx/shutdown_test.go index 6a09184..f006c89 100644 --- a/esx/shutdown_test.go +++ b/esx/shutdown_test.go @@ -81,7 +81,7 @@ var _ = Describe("ShouldShutdown", func() { var _ = Describe("GetPodsForDeletion", func() { - makePod := func(podName, nodeName string, custom ...func(*corev1.Pod)) error { + makePod := func(ctx context.Context, podName, nodeName string, custom ...func(*corev1.Pod)) error { var pod corev1.Pod pod.Namespace = "default" pod.Name = podName @@ -95,18 +95,18 @@ var _ = Describe("GetPodsForDeletion", func() { for _, cust := range custom { cust(&pod) } - return k8sClient.Create(context.Background(), &pod) + return k8sClient.Create(ctx, &pod) } - AfterEach(func() { + AfterEach(func(ctx context.Context) { var podList corev1.PodList - Expect(k8sClient.List(context.Background(), &podList)).To(Succeed()) + Expect(k8sClient.List(ctx, &podList)).To(Succeed()) var gracePeriod int64 for i := range podList.Items { - Expect(k8sClient.Delete(context.Background(), &podList.Items[i], + Expect(k8sClient.Delete(ctx, &podList.Items[i], &client.DeleteOptions{GracePeriodSeconds: &gracePeriod})).To(Succeed()) } - err := common.WaitForPodDeletions(context.Background(), k8sClient, podList.Items, + err := common.WaitForPodDeletions(ctx, k8sClient, podList.Items, common.WaitParameters{ Period: 1 * time.Second, Timeout: 4 * time.Second, @@ -114,32 +114,32 @@ var _ = Describe("GetPodsForDeletion", func() { Expect(err).To(Succeed()) }) - It("filters for the correct node", func() { - Expect(makePod("firstpod", "firstnode")).To(Succeed()) - Expect(makePod("secondpod", "secondnode")).To(Succeed()) - deletable, err := common.GetPodsForDrain(context.Background(), k8sClient, "firstnode") + It("filters for the correct node", func(ctx SpecContext) { + Expect(makePod(ctx, "firstpod", "firstnode")).To(Succeed()) + Expect(makePod(ctx, "secondpod", "secondnode")).To(Succeed()) + deletable, err := common.GetPodsForDrain(ctx, k8sClient, "firstnode") Expect(err).To(Succeed()) Expect(deletable).To(HaveLen(1)) }) - It("filters DaemonSets", func() { - Expect(makePod("firstpod", "node")).To(Succeed()) - Expect(makePod("secondpod", "node")).To(Succeed()) - Expect(makePod("ds", "node", func(p *corev1.Pod) { + It("filters DaemonSets", func(ctx SpecContext) { + Expect(makePod(ctx, "firstpod", "node")).To(Succeed()) + Expect(makePod(ctx, "secondpod", "node")).To(Succeed()) + Expect(makePod(ctx, "ds", "node", func(p *corev1.Pod) { p.OwnerReferences = []v1.OwnerReference{{Kind: "DaemonSet", APIVersion: "apps/v1", Name: "ds", UID: types.UID("ds")}} })).To(Succeed()) - deletable, err := common.GetPodsForDrain(context.Background(), k8sClient, "node") + deletable, err := common.GetPodsForDrain(ctx, k8sClient, "node") Expect(err).To(Succeed()) Expect(deletable).To(HaveLen(2)) }) - It("filters MirrorPods", func() { - Expect(makePod("firstpod", "node")).To(Succeed()) - Expect(makePod("secondpod", "node")).To(Succeed()) - Expect(makePod("mirror", "node", func(p *corev1.Pod) { + It("filters MirrorPods", func(ctx SpecContext) { + Expect(makePod(ctx, "firstpod", "node")).To(Succeed()) + Expect(makePod(ctx, "secondpod", "node")).To(Succeed()) + Expect(makePod(ctx, "mirror", "node", func(p *corev1.Pod) { p.Annotations = map[string]string{corev1.MirrorPodAnnotationKey: constants.TrueStr} })).To(Succeed()) - deletable, err := common.GetPodsForDrain(context.Background(), k8sClient, "node") + deletable, err := common.GetPodsForDrain(ctx, k8sClient, "node") Expect(err).To(Succeed()) Expect(deletable).To(HaveLen(2)) }) @@ -160,28 +160,28 @@ var _ = Describe("ensureVmOff", func() { } }) - AfterEach(func() { + AfterEach(func(ctx SpecContext) { // power on VM - client, err := vCenters.Client(context.Background(), vcServer.URL.Host) + client, err := vCenters.Client(ctx, vcServer.URL.Host) Expect(err).To(Succeed()) mgr := view.NewManager(client.Client) Expect(err).To(Succeed()) - view, err := mgr.CreateContainerView(context.Background(), + view, err := mgr.CreateContainerView(ctx, client.ServiceContent.RootFolder, []string{"VirtualMachine"}, true) Expect(err).To(Succeed()) var vms []mo.VirtualMachine - err = view.RetrieveWithFilter(context.Background(), []string{"VirtualMachine"}, + err = view.RetrieveWithFilter(ctx, []string{"VirtualMachine"}, []string{"summary.runtime"}, &vms, property.Match{"name": "firstvm"}) Expect(err).To(Succeed()) vm := object.NewVirtualMachine(client.Client, vms[0].Self) - task, err := vm.PowerOn(context.Background()) + task, err := vm.PowerOn(ctx) Expect(err).To(Succeed()) - err = task.WaitEx(context.Background()) + err = task.WaitEx(ctx) Expect(err).To(Succeed()) }) - It("should shutdown a VM", func() { - err := EnsureVMOff(context.Background(), ShutdownParams{ + It("should shutdown a VM", func(ctx SpecContext) { + err := EnsureVMOff(ctx, ShutdownParams{ VCenters: vCenters, Info: HostInfo{ AvailabilityZone: vcServer.URL.Host, @@ -194,15 +194,15 @@ var _ = Describe("ensureVmOff", func() { }) Expect(err).To(Succeed()) - client, err := vCenters.Client(context.Background(), vcServer.URL.Host) + client, err := vCenters.Client(ctx, vcServer.URL.Host) Expect(err).To(Succeed()) mgr := view.NewManager(client.Client) Expect(err).To(Succeed()) - view, err := mgr.CreateContainerView(context.Background(), + view, err := mgr.CreateContainerView(ctx, client.ServiceContent.RootFolder, []string{"VirtualMachine"}, true) Expect(err).To(Succeed()) var vms []mo.VirtualMachine - err = view.RetrieveWithFilter(context.Background(), []string{"VirtualMachine"}, + err = view.RetrieveWithFilter(ctx, []string{"VirtualMachine"}, []string{"summary.runtime"}, &vms, property.Match{"name": "firstvm"}) Expect(err).To(Succeed()) result := vms[0].Summary.Runtime.PowerState == vctypes.VirtualMachinePowerStatePoweredOff diff --git a/esx/start_test.go b/esx/start_test.go index 2d0828a..16929ad 100644 --- a/esx/start_test.go +++ b/esx/start_test.go @@ -20,7 +20,6 @@ package esx import ( - "context" "time" . "github.com/onsi/ginkgo/v2" @@ -60,7 +59,7 @@ var _ = Describe("ShouldStart", func() { var _ = Describe("ensureVmOn", func() { - It("starts a VM", func() { + It("starts a VM", func(ctx SpecContext) { vCenters := &VCenters{ Template: TemplateURL, Credentials: map[string]Credential{ @@ -74,7 +73,7 @@ var _ = Describe("ensureVmOn", func() { AvailabilityZone: vcServer.URL.Host, Name: HostSystemName, } - err := EnsureVMOff(context.Background(), ShutdownParams{ + err := EnsureVMOff(ctx, ShutdownParams{ VCenters: vCenters, Info: hostInfo, NodeName: "firstvm", @@ -83,18 +82,18 @@ var _ = Describe("ensureVmOn", func() { Log: GinkgoLogr, }) Expect(err).To(Succeed()) - err = ensureVMOn(context.Background(), vCenters, hostInfo, "firstvm") + err = ensureVMOn(ctx, vCenters, hostInfo, "firstvm") Expect(err).To(Succeed()) - client, err := vCenters.Client(context.Background(), vcServer.URL.Host) + client, err := vCenters.Client(ctx, vcServer.URL.Host) Expect(err).To(Succeed()) mgr := view.NewManager(client.Client) Expect(err).To(Succeed()) - view, err := mgr.CreateContainerView(context.Background(), + view, err := mgr.CreateContainerView(ctx, client.ServiceContent.RootFolder, []string{"VirtualMachine"}, true) Expect(err).To(Succeed()) var vms []mo.VirtualMachine - err = view.RetrieveWithFilter(context.Background(), []string{"VirtualMachine"}, + err = view.RetrieveWithFilter(ctx, []string{"VirtualMachine"}, []string{"summary.runtime"}, &vms, property.Match{"name": "firstvm"}) Expect(err).To(Succeed()) result := vms[0].Summary.Runtime.PowerState == vctypes.VirtualMachinePowerStatePoweredOn diff --git a/esx/suite_test.go b/esx/suite_test.go index b57b738..f893dbb 100644 --- a/esx/suite_test.go +++ b/esx/suite_test.go @@ -89,6 +89,7 @@ var testEnv *envtest.Environment var vCenter *simulator.Model var vcServer *simulator.Server var stopController context.CancelFunc +var esxRef string func TestESX(t *testing.T) { RegisterFailHandler(Fail) @@ -142,11 +143,25 @@ var _ = BeforeSuite(func() { view, err := mgr.CreateContainerView(context.Background(), vcClient.ServiceContent.RootFolder, []string{"VirtualMachine"}, true) Expect(err).To(Succeed()) + defer func() { + Expect(view.Destroy(context.Background())).To(Succeed()) + }() renameVM(view, "DC0_H0_VM0", "firstvm") renameVM(view, "DC0_H0_VM1", "secondvm") renameVM(view, "DC0_H1_VM0", "thirdvm") renameVM(view, "DC0_H1_VM1", "fourthvm") + containerView, err := mgr.CreateContainerView(context.Background(), vcClient.ServiceContent.RootFolder, []string{"HostSystem"}, true) + Expect(err).To(Succeed()) + var hss []mo.HostSystem + err = containerView.Retrieve(context.Background(), []string{"HostSystem"}, []string{"name"}, &hss) + Expect(err).To(Succeed()) + for _, hs := range hss { + if hs.Name == ESXName { + esxRef = hs.Reference().Value + } + } + By("bootstrapping test environment") testEnv = &envtest.Environment{ CRDDirectoryPaths: []string{filepath.Join("..", "config", "crd", "bases")}, diff --git a/go.mod b/go.mod index b018396..829e170 100644 --- a/go.mod +++ b/go.mod @@ -15,7 +15,7 @@ require ( github.com/prometheus/common v0.54.0 github.com/sapcc/ucfgwrap v0.0.0-20221123134804-375b5fb88359 github.com/slack-go/slack v0.13.0 - github.com/vmware/govmomi v0.37.3 + github.com/vmware/govmomi v0.38.0 go.uber.org/zap v1.27.0 golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8 gopkg.in/ini.v1 v1.67.0 diff --git a/go.sum b/go.sum index 43a6c24..f9ba93f 100644 --- a/go.sum +++ b/go.sum @@ -132,8 +132,8 @@ github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= -github.com/vmware/govmomi v0.37.3 h1:L2y2Ba09tYiZwdPtdF64Ox9QZeJ8vlCUGcAF9SdODn4= -github.com/vmware/govmomi v0.37.3/go.mod h1:mtGWtM+YhTADHlCgJBiskSRPOZRsN9MSjPzaZLte/oQ= +github.com/vmware/govmomi v0.38.0 h1:UvQpLAOjDpO0JUxoPCXnEzOlEa/9kejO6K58qOFr6cM= +github.com/vmware/govmomi v0.38.0/go.mod h1:mtGWtM+YhTADHlCgJBiskSRPOZRsN9MSjPzaZLte/oQ= github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=