Skip to content

Commit

Permalink
Add functional test coverage
Browse files Browse the repository at this point in the history
This commit adds func test coverage for the "velero.kubevirt.io/restore-power-on" and "velero.kubevirt.io/restore-power-off" labels.

Signed-off-by: Alvaro Romero <[email protected]>
  • Loading branch information
alromeros committed Dec 17, 2024
1 parent 8e66c89 commit 4db42dc
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 1 deletion.
13 changes: 12 additions & 1 deletion tests/framework/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ func CreateSnapshotLocation(ctx context.Context, locationName, provider, region
return nil
}

func CreateRestoreForBackup(ctx context.Context, backupName, restoreName string, backupNamespace string, wait bool) error {
func CreateRestoreWithLabels(ctx context.Context, backupName, restoreName, backupNamespace string, wait bool, labels map[string]string) error {
args := []string{
"restore", "create", restoreName,
"--from-backup", backupName,
Expand All @@ -199,6 +199,13 @@ func CreateRestoreForBackup(ctx context.Context, backupName, restoreName string,
if wait {
args = append(args, "--wait")
}
if len(labels) > 0 {
labelPairs := []string{}
for key, value := range labels {
labelPairs = append(labelPairs, fmt.Sprintf("%s=%s", key, value))
}
args = append(args, "--labels", strings.Join(labelPairs, ","))
}

restoreCmd := exec.CommandContext(ctx, veleroCLI, args...)
restoreCmd.Stdout = os.Stdout
Expand All @@ -212,6 +219,10 @@ func CreateRestoreForBackup(ctx context.Context, backupName, restoreName string,
return nil
}

func CreateRestoreForBackup(ctx context.Context, backupName, restoreName, backupNamespace string, wait bool) error {
return CreateRestoreWithLabels(ctx, backupName, restoreName, backupNamespace, wait, nil)
}

func GetRestore(ctx context.Context, restoreName string, backupNamespace string) (*v1.Restore, error) {
checkCMD := exec.CommandContext(ctx, veleroCLI, "restore", "get", "-n", backupNamespace, "-o", "json", restoreName)

Expand Down
61 changes: 61 additions & 0 deletions tests/vm_backup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,67 @@ var _ = Describe("[smoke] VM Backup", func() {
Expect(err).ToNot(HaveOccurred())
})

DescribeTable("should respect power state configuration after restore", func(startVM bool, restoreLabel map[string]string, expectedState kvv1.VirtualMachinePrintableStatus) {
By("Creating a VM")
var err error
vm, err = framework.CreateStartedVirtualMachine(f.KvClient, f.Namespace.Name, framework.CreateVmWithGuestAgent("test-vm", f.StorageClass))
Expect(err).ToNot(HaveOccurred())

err = framework.WaitForVirtualMachineStatus(f.KvClient, f.Namespace.Name, vm.Name, kvv1.VirtualMachineStatusRunning)
Expect(err).ToNot(HaveOccurred())

if !startVM {
By("Stopping VM")
err = framework.StopVirtualMachine(f.KvClient, f.Namespace.Name, vm.Name)
Expect(err).ToNot(HaveOccurred())
err = framework.WaitForVirtualMachineStatus(f.KvClient, f.Namespace.Name, vm.Name, kvv1.VirtualMachineStatusStopped)
Expect(err).ToNot(HaveOccurred())
}

By("Creating a backup for the VM")
err = framework.CreateBackupForNamespace(timeout, backupName, f.Namespace.Name, snapshotLocation, f.BackupNamespace, true)
Expect(err).ToNot(HaveOccurred())

phase, err := framework.GetBackupPhase(timeout, backupName, f.BackupNamespace)
Expect(err).ToNot(HaveOccurred())
Expect(phase).To(Equal(velerov1api.BackupPhaseCompleted))

if startVM {
By("Stopping VM")
err = framework.StopVirtualMachine(f.KvClient, f.Namespace.Name, vm.Name)
Expect(err).ToNot(HaveOccurred())
err = framework.WaitForVirtualMachineStatus(f.KvClient, f.Namespace.Name, vm.Name, kvv1.VirtualMachineStatusStopped)
Expect(err).ToNot(HaveOccurred())
}

By("Deleting the VM")
err = framework.DeleteVirtualMachine(f.KvClient, f.Namespace.Name, vm.Name)
Expect(err).ToNot(HaveOccurred())

By("Creating restore with specific label")
err = framework.CreateRestoreWithLabels(timeout, backupName, restoreName, f.BackupNamespace, true, restoreLabel)
Expect(err).ToNot(HaveOccurred())

rPhase, err := framework.GetRestorePhase(timeout, restoreName, f.BackupNamespace)
Expect(err).ToNot(HaveOccurred())
Expect(rPhase).To(Equal(velerov1api.RestorePhaseCompleted))

By("Validating the restored VM state")
err = framework.WaitForVirtualMachineStatus(f.KvClient, f.Namespace.Name, vm.Name, expectedState)
Expect(err).ToNot(HaveOccurred())
},
Entry("Restore with Always run strategy label should start the VM",
false,
map[string]string{"velero.kubevirt.io/restore-run-strategy": "Always"},
kvv1.VirtualMachineStatusRunning,
),
Entry("Restore with Halted run strategy label should stop the VM",
true,
map[string]string{"velero.kubevirt.io/restore-run-strategy": "Halted"},
kvv1.VirtualMachineStatusStopped,
),
)

Context("VM and VMI object graph backup", func() {
Context("with instancetypes and preferences", func() {
nsDelFunc := func() {
Expand Down

0 comments on commit 4db42dc

Please sign in to comment.