diff --git a/controllers/infra/secret/infra_secret_controller_intg_test.go b/controllers/infra/secret/infra_secret_controller_intg_test.go index 32192f1b2..b07c7ec6e 100644 --- a/controllers/infra/secret/infra_secret_controller_intg_test.go +++ b/controllers/infra/secret/infra_secret_controller_intg_test.go @@ -23,6 +23,7 @@ import ( func intgTests() { Describe( "Reconcile", + Serial, Label( testlabels.Controller, testlabels.EnvTest, diff --git a/controllers/virtualmachine/storagepolicyusage/storagepolicyusage_controller_unit_test.go b/controllers/virtualmachine/storagepolicyusage/storagepolicyusage_controller_unit_test.go index 0c7a3cf94..195755411 100644 --- a/controllers/virtualmachine/storagepolicyusage/storagepolicyusage_controller_unit_test.go +++ b/controllers/virtualmachine/storagepolicyusage/storagepolicyusage_controller_unit_test.go @@ -448,6 +448,8 @@ func unitTestsReconcileSPUForVM() { ) BeforeEach(func() { + vm1 = nil + vm2 = nil withObjects = nil withFuncs = interceptor.Funcs{} inNamespace = namespace @@ -1010,6 +1012,11 @@ func unitTestsReconcileSPUForVMSnapshot() { withObjects = nil inNamespace = namespace inName = name + vm1 = nil + vm2 = nil + vmSnapshot1 = nil + vmSnapshot2 = nil + }) JustBeforeEach(func() { diff --git a/controllers/virtualmachineclass/virtualmachineclass_controller_intg_test.go b/controllers/virtualmachineclass/virtualmachineclass_controller_intg_test.go index b5a26ee36..8cd4d12de 100644 --- a/controllers/virtualmachineclass/virtualmachineclass_controller_intg_test.go +++ b/controllers/virtualmachineclass/virtualmachineclass_controller_intg_test.go @@ -44,7 +44,7 @@ func intgTestsReconcile() { vmClass = &vmopv1.VirtualMachineClass{ ObjectMeta: metav1.ObjectMeta{ Name: "small", - Namespace: "default", + Namespace: ctx.Namespace, }, Spec: vmopv1.VirtualMachineClassSpec{ Hardware: vmopv1.VirtualMachineClassHardware{ diff --git a/controllers/virtualmachinesnapshot/virtualmachinesnapshot_controller_intg_test.go b/controllers/virtualmachinesnapshot/virtualmachinesnapshot_controller_intg_test.go index ef7a97b4f..722e2db64 100644 --- a/controllers/virtualmachinesnapshot/virtualmachinesnapshot_controller_intg_test.go +++ b/controllers/virtualmachinesnapshot/virtualmachinesnapshot_controller_intg_test.go @@ -92,11 +92,6 @@ func intgTestsReconcile() { Expect(vcSimCtx).ToNot(BeNil()) vcSimCtx.BeforeEach() - - snapshotObjKey = types.NamespacedName{ - Name: vmSnapshot.Name, - Namespace: vmSnapshot.Namespace, - } }) AfterEach(func() { @@ -108,13 +103,17 @@ func intgTestsReconcile() { BeforeEach(func() { initEnvFn = func(ctx *builder.IntegrationTestContextForVCSim) { By("create vm in k8s") - vm = builder.DummyBasicVirtualMachine("dummy-vm", vcSimCtx.NSInfo.Namespace) - Expect(vcSimCtx.Client.Create(ctx, vm)).To(Succeed()) + vm = builder.DummyBasicVirtualMachine("dummy-vm", ctx.NSInfo.Namespace) + Expect(ctx.Client.Create(ctx, vm)).To(Succeed()) vm.Status.UniqueID = uniqueVMID - Expect(vcSimCtx.Client.Status().Update(ctx, vm)).To(Succeed()) + Expect(ctx.Client.Status().Update(ctx, vm)).To(Succeed()) By("create snapshot in k8s") - vmSnapshot = builder.DummyVirtualMachineSnapshot(vcSimCtx.NSInfo.Namespace, "snap-1", vm.Name) - Expect(vcSimCtx.Client.Create(ctx, vmSnapshot.DeepCopy())).To(Succeed()) + vmSnapshot = builder.DummyVirtualMachineSnapshot(ctx.NSInfo.Namespace, "snap-1", vm.Name) + snapshotObjKey = types.NamespacedName{ + Name: vmSnapshot.Name, + Namespace: vmSnapshot.Namespace, + } + Expect(ctx.Client.Create(ctx, vmSnapshot.DeepCopy())).To(Succeed()) } }) @@ -139,13 +138,17 @@ func intgTestsReconcile() { BeforeEach(func() { initEnvFn = func(ctx *builder.IntegrationTestContextForVCSim) { By("create vm in k8s") - vm = builder.DummyBasicVirtualMachine("dummy-vm", vcSimCtx.NSInfo.Namespace) - Expect(vcSimCtx.Client.Create(ctx, vm)).To(Succeed()) + vm = builder.DummyBasicVirtualMachine("dummy-vm", ctx.NSInfo.Namespace) + Expect(ctx.Client.Create(ctx, vm)).To(Succeed()) vm.Status.UniqueID = uniqueVMID - Expect(vcSimCtx.Client.Status().Update(ctx, vm)).To(Succeed()) + Expect(ctx.Client.Status().Update(ctx, vm)).To(Succeed()) By("create snapshot in k8s") - vmSnapshot = builder.DummyVirtualMachineSnapshot(vcSimCtx.NSInfo.Namespace, "snap-1", vm.Name) - Expect(vcSimCtx.Client.Create(ctx, vmSnapshot.DeepCopy())).To(Succeed()) + vmSnapshot = builder.DummyVirtualMachineSnapshot(ctx.NSInfo.Namespace, "snap-1", vm.Name) + snapshotObjKey = types.NamespacedName{ + Name: vmSnapshot.Name, + Namespace: vmSnapshot.Namespace, + } + Expect(ctx.Client.Create(ctx, vmSnapshot.DeepCopy())).To(Succeed()) } }) JustBeforeEach(func() { @@ -158,7 +161,7 @@ func intgTestsReconcile() { It("returns success, and set the csi volume sync annotation to requested", func() { Eventually(func(g Gomega) { vmSnapshotObj := &vmopv1.VirtualMachineSnapshot{} - Expect(vcSimCtx.Client.Get(ctx, snapshotObjKey, vmSnapshotObj)).To(Succeed()) + g.Expect(vcSimCtx.Client.Get(ctx, snapshotObjKey, vmSnapshotObj)).To(Succeed()) g.Expect(vmSnapshotObj.Annotations[constants.CSIVSphereVolumeSyncAnnotationKey]). To(Equal(constants.CSIVSphereVolumeSyncAnnotationValueRequested)) g.Expect(conditions.IsFalse(vmSnapshotObj, @@ -201,13 +204,17 @@ func intgTestsReconcile() { BeforeEach(func() { initEnvFn = func(ctx *builder.IntegrationTestContextForVCSim) { By("create vm and snapshot in k8s") - vm = builder.DummyBasicVirtualMachine("dummy-vm", vcSimCtx.NSInfo.Namespace) - vmSnapshot = builder.DummyVirtualMachineSnapshot(vcSimCtx.NSInfo.Namespace, "snap-1", vm.Name) - Expect(vcSimCtx.Client.Create(ctx, vmSnapshot.DeepCopy())).To(Succeed()) - Expect(vcSimCtx.Client.Create(ctx, vm)).To(Succeed()) + vm = builder.DummyBasicVirtualMachine("dummy-vm", ctx.NSInfo.Namespace) + vmSnapshot = builder.DummyVirtualMachineSnapshot(ctx.NSInfo.Namespace, "snap-1", vm.Name) + snapshotObjKey = types.NamespacedName{ + Name: vmSnapshot.Name, + Namespace: vmSnapshot.Namespace, + } + Expect(ctx.Client.Create(ctx, vmSnapshot.DeepCopy())).To(Succeed()) + Expect(ctx.Client.Create(ctx, vm)).To(Succeed()) vm.Status.UniqueID = uniqueVMID vm.Status.CurrentSnapshot = newManagedSnapshotRefWithSnapshotName(vmSnapshot.Name) - Expect(vcSimCtx.Client.Status().Update(ctx, vm)).To(Succeed()) + Expect(ctx.Client.Status().Update(ctx, vm)).To(Succeed()) } provider.Lock() @@ -311,8 +318,13 @@ func intgTestsReconcile() { BeforeEach(func() { initEnvFn = func(ctx *builder.IntegrationTestContextForVCSim) { By("only create snapshot in k8s") - vmSnapshot = builder.DummyVirtualMachineSnapshot(vcSimCtx.NSInfo.Namespace, "snap-1", vm.Name) - Expect(vcSimCtx.Client.Create(ctx, vmSnapshot.DeepCopy())).To(Succeed()) + vm = builder.DummyBasicVirtualMachine("dummy-vm", ctx.NSInfo.Namespace) + vmSnapshot = builder.DummyVirtualMachineSnapshot(ctx.NSInfo.Namespace, "snap-1", vm.Name) + snapshotObjKey = types.NamespacedName{ + Name: vmSnapshot.Name, + Namespace: vmSnapshot.Namespace, + } + Expect(ctx.Client.Create(ctx, vmSnapshot.DeepCopy())).To(Succeed()) } }) It("snapshot is deleted", func() { @@ -335,15 +347,19 @@ func intgTestsReconcile() { BeforeEach(func() { initEnvFn = func(ctx *builder.IntegrationTestContextForVCSim) { By("create vm and snapshot in k8s") - vm = builder.DummyBasicVirtualMachine("dummy-vm", vcSimCtx.NSInfo.Namespace) - vmSnapshot = builder.DummyVirtualMachineSnapshot(vcSimCtx.NSInfo.Namespace, "snap-1", vm.Name) + vm = builder.DummyBasicVirtualMachine("dummy-vm", ctx.NSInfo.Namespace) + vmSnapshot = builder.DummyVirtualMachineSnapshot(ctx.NSInfo.Namespace, "snap-1", vm.Name) + snapshotObjKey = types.NamespacedName{ + Name: vmSnapshot.Name, + Namespace: vmSnapshot.Namespace, + } By("set snapshot's vmref to nil") vmSnapshot.Spec.VMRef = nil - Expect(vcSimCtx.Client.Create(ctx, vmSnapshot.DeepCopy())).To(Succeed()) - Expect(vcSimCtx.Client.Create(ctx, vm)).To(Succeed()) + Expect(ctx.Client.Create(ctx, vmSnapshot.DeepCopy())).To(Succeed()) + Expect(ctx.Client.Create(ctx, vm)).To(Succeed()) vm.Status.UniqueID = uniqueVMID vm.Status.CurrentSnapshot = newManagedSnapshotRefWithSnapshotName(vmSnapshot.Name) - Expect(vcSimCtx.Client.Status().Update(ctx, vm)).To(Succeed()) + Expect(ctx.Client.Status().Update(ctx, vm)).To(Succeed()) vmObjKey := types.NamespacedName{Name: vm.Name, Namespace: vm.Namespace} Eventually(func(g Gomega) { @@ -412,18 +428,18 @@ func intgTestsReconcile() { vmSnapshotL3Node1 = builder.DummyVirtualMachineSnapshot(ctx.NSInfo.Namespace, vmSnapshotL3Node1Name, vm.Name) vmSnapshotL3Node2 = builder.DummyVirtualMachineSnapshot(ctx.NSInfo.Namespace, vmSnapshotL3Node2Name, vm.Name) - Expect(vcSimCtx.Client.Create(ctx, vm)).To(Succeed()) + Expect(ctx.Client.Create(ctx, vm)).To(Succeed()) // Update the current snapshot after creation. Otherwise will run into "failed to get informer from cache" vm.Status.UniqueID = uniqueVMID // Update the root snapshots vm.Status.RootSnapshots = []vmopv1.VirtualMachineSnapshotReference{*newManagedSnapshotRefWithSnapshotName(vmSnapshotL1.Name)} - Expect(vcSimCtx.Client.Status().Update(ctx, vm)).To(Succeed()) + Expect(ctx.Client.Status().Update(ctx, vm)).To(Succeed()) // // Create the object here so that it can be customized in each BeforeEach - Expect(vcSimCtx.Client.Create(ctx, vmSnapshotL1)).To(Succeed()) - Expect(vcSimCtx.Client.Create(ctx, vmSnapshotL2)).To(Succeed()) - Expect(vcSimCtx.Client.Create(ctx, vmSnapshotL3Node1.DeepCopy())).To(Succeed()) - Expect(vcSimCtx.Client.Create(ctx, vmSnapshotL3Node2.DeepCopy())).To(Succeed()) + Expect(ctx.Client.Create(ctx, vmSnapshotL1)).To(Succeed()) + Expect(ctx.Client.Create(ctx, vmSnapshotL2)).To(Succeed()) + Expect(ctx.Client.Create(ctx, vmSnapshotL3Node1.DeepCopy())).To(Succeed()) + Expect(ctx.Client.Create(ctx, vmSnapshotL3Node2.DeepCopy())).To(Succeed()) // Mark the snapshot as ready so that they won't update CurrentSnapshot markVMSnapshotReady(vcSimCtx, vmSnapshotL1) markVMSnapshotReady(vcSimCtx, vmSnapshotL2) diff --git a/hack/test.sh b/hack/test.sh index 07b0a8bda..3f60660dc 100755 --- a/hack/test.sh +++ b/hack/test.sh @@ -13,10 +13,18 @@ set -x # script is located. cd "$(dirname "${BASH_SOURCE[0]}")/.." -GO_TEST_FLAGS+=("-v") # verbose -GO_TEST_FLAGS+=("-r") # recursive -GO_TEST_FLAGS+=("--race") # check for possible races -GO_TEST_FLAGS+=("--keep-going") # do not fail on the first error +GO_TEST_FLAGS+=("-p") # parallel +GO_TEST_FLAGS+=("-r") # recursive +GO_TEST_FLAGS+=("-v") # verbose +GO_TEST_FLAGS+=("--race") # check for possible races +GO_TEST_FLAGS+=("--keep-going") # do not fail on the first error +GO_TEST_FLAGS+=("--randomize-all") # random order +GO_TEST_FLAGS+=("--force-newlines") # always print empty line after test cases + +# Use the GitHub output option if the tests are run as part of a GitHub action. +if [ -n "${GITHUB_RUN_ID:-}" ]; then + GO_TEST_FLAGS+=("--github-output") +fi # Only run tests that match given labels if LABEL_FILTER is non-empty. if [ -n "${LABEL_FILTER:-}" ]; then diff --git a/pkg/builder/auth_test.go b/pkg/builder/auth_test.go index dca849a9a..36d1801ee 100644 --- a/pkg/builder/auth_test.go +++ b/pkg/builder/auth_test.go @@ -81,7 +81,7 @@ var _ = DescribeTable("IsPrivilegedAccount", ), ) -var _ = Describe("VerifyWebhookRequest", func() { +var _ = Describe("VerifyWebhookRequest", Serial, func() { BeforeEach(func() { dir := filepath.Dir(caFilePath) Expect(os.MkdirAll(dir, 0755)).To(Succeed()) @@ -89,7 +89,7 @@ var _ = Describe("VerifyWebhookRequest", func() { }) AfterEach(func() { - Expect(os.Remove(caFilePath)).To(Succeed()) + Expect(os.RemoveAll(caFilePath)).To(Succeed()) Expect(os.RemoveAll("/tmp/k8s-webhook-server")).To(Succeed()) }) diff --git a/pkg/builder/mutating_webhook_test.go b/pkg/builder/mutating_webhook_test.go index ecd2759bf..01b94e6bb 100644 --- a/pkg/builder/mutating_webhook_test.go +++ b/pkg/builder/mutating_webhook_test.go @@ -28,7 +28,7 @@ import ( pkgctxfake "github.com/vmware-tanzu/vm-operator/pkg/context/fake" ) -var _ = Describe("NewMutatingWebhook", func() { +var _ = Describe("NewMutatingWebhook", Serial, func() { var ( err error @@ -135,7 +135,7 @@ var _ = Describe("NewMutatingWebhook", func() { }) AfterEach(func() { - err := os.Remove(caFilePath) + err := os.RemoveAll(caFilePath) Expect(err).NotTo(HaveOccurred()) err = os.RemoveAll("/tmp/k8s-webhook-server") diff --git a/pkg/builder/validating_webhook_test.go b/pkg/builder/validating_webhook_test.go index c084f5189..1a3bbc6f6 100644 --- a/pkg/builder/validating_webhook_test.go +++ b/pkg/builder/validating_webhook_test.go @@ -29,7 +29,7 @@ import ( pkgctxfake "github.com/vmware-tanzu/vm-operator/pkg/context/fake" ) -var _ = Describe("NewValidatingWebhook", func() { +var _ = Describe("NewValidatingWebhook", Serial, func() { var ( err error @@ -153,7 +153,7 @@ var _ = Describe("NewValidatingWebhook", func() { }) AfterEach(func() { - err := os.Remove(caFilePath) + err := os.RemoveAll(caFilePath) Expect(err).NotTo(HaveOccurred()) err = os.RemoveAll("/tmp/k8s-webhook-server") diff --git a/pkg/providers/vsphere/session/session_vm_update_test.go b/pkg/providers/vsphere/session/session_vm_update_test.go index 51e73b77e..66cba1b6a 100644 --- a/pkg/providers/vsphere/session/session_vm_update_test.go +++ b/pkg/providers/vsphere/session/session_vm_update_test.go @@ -109,6 +109,7 @@ var _ = Describe("Update ConfigSpec", func() { }, } globalExtraConfig = make(map[string]string) + ecMap = nil }) JustBeforeEach(func() { @@ -232,6 +233,7 @@ var _ = Describe("Update ConfigSpec", func() { var vmSpec vmopv1.VirtualMachineSpec BeforeEach(func() { + vmSpec = vmopv1.VirtualMachineSpec{} config.ChangeTrackingEnabled = nil }) diff --git a/pkg/providers/vsphere/virtualmachine/cdrom_test.go b/pkg/providers/vsphere/virtualmachine/cdrom_test.go index 0c9c4320a..9ede619c7 100644 --- a/pkg/providers/vsphere/virtualmachine/cdrom_test.go +++ b/pkg/providers/vsphere/virtualmachine/cdrom_test.go @@ -1089,14 +1089,14 @@ func cdromTests() { Context("UpdateConfigSpecCdromDeviceConnection", func() { var ( - ctx *builder.TestContextForVCSim - vmCtx pkgctx.VirtualMachineContext - restClient *rest.Client - k8sClient ctrlclient.Client - configInfo *vimtypes.VirtualMachineConfigInfo - configSpec *vimtypes.VirtualMachineConfigSpec - - updateErr error + ctx *builder.TestContextForVCSim + vmCtx pkgctx.VirtualMachineContext + restClient *rest.Client + k8sClient ctrlclient.Client + configInfo *vimtypes.VirtualMachineConfigInfo + configSpec *vimtypes.VirtualMachineConfigSpec + k8sInitObjs []ctrlclient.Object + updateErr error ) BeforeEach(func() { @@ -1118,6 +1118,7 @@ func cdromTests() { }) JustBeforeEach(func() { + k8sClient = builder.NewFakeClient(k8sInitObjs...) updateErr = virtualmachine.UpdateConfigSpecCdromDeviceConnection(vmCtx, restClient, k8sClient, configInfo, configSpec) }) @@ -1125,9 +1126,8 @@ func cdromTests() { BeforeEach(func() { // Create a fake K8s client with both namespace & cluster scope ISO type images and their content library item objects. - k8sInitObjs := builder.DummyImageAndItemObjectsForCdromBacking(vmiName, ns, vmiKind, vmiFileName, ctx.ContentLibraryIsoItemID, true, true, resource.MustParse("100Mi"), true, true, imgregv1a1.ContentLibraryItemTypeIso) + k8sInitObjs = builder.DummyImageAndItemObjectsForCdromBacking(vmiName, ns, vmiKind, vmiFileName, ctx.ContentLibraryIsoItemID, true, true, resource.MustParse("100Mi"), true, true, imgregv1a1.ContentLibraryItemTypeIso) k8sInitObjs = append(k8sInitObjs, builder.DummyImageAndItemObjectsForCdromBacking(cvmiName, "", cvmiKind, cvmiFileName, ctx.ContentLibraryIsoItemID, true, true, resource.MustParse("100Mi"), true, true, imgregv1a1.ContentLibraryItemTypeIso)...) - k8sClient = builder.NewFakeClient(k8sInitObjs...) }) When("VM.Spec.Cdrom has no changes", func() { diff --git a/pkg/providers/vsphere/virtualmachine/virtualmachine_suite_test.go b/pkg/providers/vsphere/virtualmachine/virtualmachine_suite_test.go index 712b13a52..f073da916 100644 --- a/pkg/providers/vsphere/virtualmachine/virtualmachine_suite_test.go +++ b/pkg/providers/vsphere/virtualmachine/virtualmachine_suite_test.go @@ -19,7 +19,7 @@ func vcSimTests() { Describe("Publish", Label(testlabels.VCSim), publishTests) Describe("Backup", Label(testlabels.VCSim), backupTests) Describe("GuestInfo", Label(testlabels.VCSim), guestInfoTests) - Describe("CD-ROM", Label(testlabels.VCSim), cdromTests) + Describe("CD-ROM", Serial, Label(testlabels.VCSim), cdromTests) Describe("Snapshot", Label(testlabels.VCSim), snapShotTests) Describe("ExtraConfig", Label(testlabels.VCSim), extraConfigTests) } diff --git a/pkg/util/cloudinit/cloudconfig_secret_test.go b/pkg/util/cloudinit/cloudconfig_secret_test.go index 190144bd8..6a7b6af90 100644 --- a/pkg/util/cloudinit/cloudconfig_secret_test.go +++ b/pkg/util/cloudinit/cloudconfig_secret_test.go @@ -184,7 +184,14 @@ var _ = Describe("CloudConfig GetCloudConfigSecretData", func() { When("CloudConfig has a file that references data from a secret", func() { BeforeEach(func() { - cloudConfig.WriteFiles[0].Content = []byte(`{"name":"my-bootstrap-data","key":"file-hello"}`) + cloudConfig = vmopv1cloudinit.CloudConfig{ + WriteFiles: []vmopv1cloudinit.WriteFile{ + { + Path: "/hello", + Content: []byte(`{"name":"my-bootstrap-data","key":"file-hello"}`), + }, + }, + } }) When("The secret does not exist", func() { It("Should return an error", func() { diff --git a/webhooks/unifiedstoragequota/validation/unifiedstoragequota_validator_unit_test.go b/webhooks/unifiedstoragequota/validation/unifiedstoragequota_validator_unit_test.go index e3da97476..fe9e70850 100644 --- a/webhooks/unifiedstoragequota/validation/unifiedstoragequota_validator_unit_test.go +++ b/webhooks/unifiedstoragequota/validation/unifiedstoragequota_validator_unit_test.go @@ -483,6 +483,15 @@ func testVMRequestedCapacityHandlerHandle() { operation admissionv1.Operation ) + BeforeEach(func() { + vm, oldVM = nil, nil + obj, oldObj = nil, nil + handler = nil + req = admission.Request{} + resp = validation.CapacityResponse{} + operation = admissionv1.Operation("") + }) + Context("Handle", func() { BeforeEach(func() { sc := builder.DummyStorageClass() @@ -1098,6 +1107,16 @@ func testVMRequestedCapacityHandlerHandleUpdate() { resp, expected validation.CapacityResponse ) + BeforeEach(func() { + vm, oldVM = nil, nil + obj, oldObj = nil, nil + interceptors = interceptor.Funcs{} + dummyConverter = nil + dummyDecoder = nil + resp = validation.CapacityResponse{} + expected = validation.CapacityResponse{} + }) + When("HandleUpdate is called", func() { BeforeEach(func() {