Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
func intgTests() {
Describe(
"Reconcile",
Serial,
Label(
testlabels.Controller,
testlabels.EnvTest,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,8 @@ func unitTestsReconcileSPUForVM() {
)

BeforeEach(func() {
vm1 = nil
vm2 = nil
withObjects = nil
withFuncs = interceptor.Funcs{}
inNamespace = namespace
Expand Down Expand Up @@ -1010,6 +1012,11 @@ func unitTestsReconcileSPUForVMSnapshot() {
withObjects = nil
inNamespace = namespace
inName = name
vm1 = nil
vm2 = nil
vmSnapshot1 = nil
vmSnapshot2 = nil

})

JustBeforeEach(func() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,6 @@ func intgTestsReconcile() {
Expect(vcSimCtx).ToNot(BeNil())

vcSimCtx.BeforeEach()

snapshotObjKey = types.NamespacedName{
Name: vmSnapshot.Name,
Namespace: vmSnapshot.Namespace,
}
})

AfterEach(func() {
Expand All @@ -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())
}
})

Expand All @@ -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() {
Expand All @@ -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,
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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() {
Expand All @@ -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) {
Expand Down Expand Up @@ -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)
Expand Down
16 changes: 12 additions & 4 deletions hack/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions pkg/builder/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,15 @@ 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())
Expect(os.WriteFile(caFilePath, []byte(sampleCert), 0644)).To(Succeed())
})

AfterEach(func() {
Expect(os.Remove(caFilePath)).To(Succeed())
Expect(os.RemoveAll(caFilePath)).To(Succeed())
Expect(os.RemoveAll("/tmp/k8s-webhook-server")).To(Succeed())
})

Expand Down
4 changes: 2 additions & 2 deletions pkg/builder/mutating_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand Down
4 changes: 2 additions & 2 deletions pkg/builder/validating_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand Down
2 changes: 2 additions & 0 deletions pkg/providers/vsphere/session/session_vm_update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ var _ = Describe("Update ConfigSpec", func() {
},
}
globalExtraConfig = make(map[string]string)
ecMap = nil
})

JustBeforeEach(func() {
Expand Down Expand Up @@ -232,6 +233,7 @@ var _ = Describe("Update ConfigSpec", func() {
var vmSpec vmopv1.VirtualMachineSpec

BeforeEach(func() {
vmSpec = vmopv1.VirtualMachineSpec{}
config.ChangeTrackingEnabled = nil
})

Expand Down
20 changes: 10 additions & 10 deletions pkg/providers/vsphere/virtualmachine/cdrom_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -1118,16 +1118,16 @@ func cdromTests() {
})

JustBeforeEach(func() {
k8sClient = builder.NewFakeClient(k8sInitObjs...)
updateErr = virtualmachine.UpdateConfigSpecCdromDeviceConnection(vmCtx, restClient, k8sClient, configInfo, configSpec)
})

Context("Happy Path (no error occurs)", func() {

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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
9 changes: 8 additions & 1 deletion pkg/util/cloudinit/cloudconfig_secret_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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() {
Expand Down
Loading