Skip to content

Commit

Permalink
Add func testing for clearing MAC address
Browse files Browse the repository at this point in the history
Signed-off-by: Alvaro Romero <[email protected]>
  • Loading branch information
alromeros committed Dec 16, 2024
1 parent 9b0e212 commit 2a82153
Show file tree
Hide file tree
Showing 2 changed files with 70 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 createRestore(ctx context.Context, backupName, restoreName, backupNamespace string, wait, clearMacAddress bool) error {
args := []string{
"restore", "create", restoreName,
"--from-backup", backupName,
Expand All @@ -199,6 +199,9 @@ func CreateRestoreForBackup(ctx context.Context, backupName, restoreName string,
if wait {
args = append(args, "--wait")
}
if clearMacAddress {
args = append(args, "--labels", "velero.kubevirt.io/clear-mac-address=true")
}

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

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

func CreateRestoreWithClearedMACAddress(ctx context.Context, backupName, restoreName, backupNamespace string, wait bool) error {
return createRestore(ctx, backupName, restoreName, backupNamespace, wait, true)
}

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
58 changes: 58 additions & 0 deletions tests/vm_backup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,64 @@ var _ = Describe("[smoke] VM Backup", func() {
Expect(err).ToNot(HaveOccurred())
})

It("started VM should be restored with new MAC address", func() {
// creating a started VM, so it works correctly also on WFFC storage
var err error
By("Starting a VM")
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())

By("Retrieving the original MAC address")
vm, err = f.KvClient.VirtualMachine(f.Namespace.Name).Get(context.TODO(), vm.Name, metav1.GetOptions{})
Expect(err).ToNot(HaveOccurred())
originalMAC := vm.Spec.Template.Spec.Domain.Devices.Interfaces[0].MacAddress
if originalMAC == "" {
// This means there is no KubeMacPool running. We can simply choose a random address
originalMAC = "DE-AD-00-00-BE-AF"
update := func(vm *kvv1.VirtualMachine) *kvv1.VirtualMachine {
vm.Spec.Template.Spec.Domain.Devices.Interfaces[0].MacAddress = originalMAC
return vm
}
retryOnceOnErr(updateVm(f.KvClient, f.Namespace.Name, vm.Name, update)).Should(BeNil())

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

By("Creating backup")
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))

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

By("Creating restore")
err = framework.CreateRestoreWithClearedMACAddress(timeout, backupName, restoreName, f.BackupNamespace, true)
Expect(err).ToNot(HaveOccurred())

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

By("Verifying restored VM")
err = framework.WaitForVirtualMachineStatus(f.KvClient, f.Namespace.Name, vm.Name, kvv1.VirtualMachineStatusRunning)
Expect(err).ToNot(HaveOccurred())

By("Retrieving the restored MAC address")
vm, err = f.KvClient.VirtualMachine(f.Namespace.Name).Get(context.TODO(), vm.Name, metav1.GetOptions{})
Expect(err).ToNot(HaveOccurred())
restoredMAC := vm.Spec.Template.Spec.Domain.Devices.Interfaces[0].MacAddress
Expect(restoredMAC).ToNot(Equal(originalMAC))
})

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

0 comments on commit 2a82153

Please sign in to comment.