Skip to content

Commit

Permalink
tests,vm: Rename import of virtctl
Browse files Browse the repository at this point in the history
This allows to name variables 'vm' in subsequent commits, which makes the
code easier to read.

Signed-off-by: Felix Matouschek <[email protected]>
  • Loading branch information
0xFelix committed Apr 20, 2023
1 parent 21539e7 commit b9d33dc
Showing 1 changed file with 39 additions and 39 deletions.
78 changes: 39 additions & 39 deletions tests/vm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ import (
cdiv1 "kubevirt.io/containerized-data-importer-api/pkg/apis/core/v1beta1"

"kubevirt.io/kubevirt/pkg/controller"
"kubevirt.io/kubevirt/pkg/virtctl/vm"
virtctl "kubevirt.io/kubevirt/pkg/virtctl/vm"
"kubevirt.io/kubevirt/tests"
"kubevirt.io/kubevirt/tests/clientcmd"
"kubevirt.io/kubevirt/tests/console"
Expand Down Expand Up @@ -954,7 +954,7 @@ var _ = Describe("[rfe_id:1177][crit:medium][vendor:[email protected]][level:com
newVM := createVirtualMachine(false, libvmi.NewCirros())

By("Invoking virtctl start")
startCommand := clientcmd.NewRepeatableVirtctlCommand(vm.COMMAND_START, "--namespace", newVM.Namespace, newVM.Name)
startCommand := clientcmd.NewRepeatableVirtctlCommand(virtctl.COMMAND_START, "--namespace", newVM.Namespace, newVM.Name)
Expect(startCommand()).To(Succeed())

By("Getting the status of the VM")
Expand Down Expand Up @@ -989,7 +989,7 @@ var _ = Describe("[rfe_id:1177][crit:medium][vendor:[email protected]][level:com
}, 360*time.Second, 1*time.Second).Should(BeTrue())

By("Invoking virtctl stop")
stopCommand := clientcmd.NewRepeatableVirtctlCommand(vm.COMMAND_STOP, "--namespace", newVM.Namespace, newVM.Name)
stopCommand := clientcmd.NewRepeatableVirtctlCommand(virtctl.COMMAND_STOP, "--namespace", newVM.Namespace, newVM.Name)
Expect(stopCommand()).To(Succeed())

By("Ensuring VM is not running")
Expand Down Expand Up @@ -1017,7 +1017,7 @@ var _ = Describe("[rfe_id:1177][crit:medium][vendor:[email protected]][level:com
newVM := createVirtualMachine(false, libvmi.NewCirros())

By("Invoking virtctl start")
startCommand := clientcmd.NewRepeatableVirtctlCommand(vm.COMMAND_START, "--namespace", newVM.Namespace, newVM.Name, "--paused")
startCommand := clientcmd.NewRepeatableVirtctlCommand(virtctl.COMMAND_START, "--namespace", newVM.Namespace, newVM.Name, "--paused")
Expect(startCommand()).To(Succeed())

By("Getting the status of the VM")
Expand Down Expand Up @@ -1053,7 +1053,7 @@ var _ = Describe("[rfe_id:1177][crit:medium][vendor:[email protected]][level:com
oldVMIUuid := newVM.ObjectMeta.UID

By("Invoking virtctl --force restart")
forceRestart := clientcmd.NewRepeatableVirtctlCommand(vm.COMMAND_RESTART, "--namespace", newVM.Namespace, "--force", newVM.Name, "--grace-period=0")
forceRestart := clientcmd.NewRepeatableVirtctlCommand(virtctl.COMMAND_RESTART, "--namespace", newVM.Namespace, "--force", newVM.Name, "--grace-period=0")
err = forceRestart()
Expect(err).ToNot(HaveOccurred())

Expand Down Expand Up @@ -1125,7 +1125,7 @@ var _ = Describe("[rfe_id:1177][crit:medium][vendor:[email protected]][level:com
go terminatationGracePeriodUpdated(stopCn, lw.ResultChan(), updated)

By("Invoking virtctl --force stop")
forceStop := clientcmd.NewRepeatableVirtctlCommand(vm.COMMAND_STOP, newVM.Name, "--namespace", newVM.Namespace, "--force", "--grace-period=0")
forceStop := clientcmd.NewRepeatableVirtctlCommand(virtctl.COMMAND_STOP, newVM.Name, "--namespace", newVM.Namespace, "--force", "--grace-period=0")
Expect(forceStop()).ToNot(HaveOccurred())

By("Ensuring the VirtualMachineInstance is removed")
Expand All @@ -1152,7 +1152,7 @@ var _ = Describe("[rfe_id:1177][crit:medium][vendor:[email protected]][level:com
}, 360*time.Second, 1*time.Second).Should(BeTrue())

By("Invoking virtctl stop")
stopCommand := clientcmd.NewRepeatableVirtctlCommand(vm.COMMAND_STOP, "--namespace", virtualMachine.Namespace, virtualMachine.Name)
stopCommand := clientcmd.NewRepeatableVirtctlCommand(virtctl.COMMAND_STOP, "--namespace", virtualMachine.Namespace, virtualMachine.Name)
Expect(stopCommand()).To(Succeed())

By("Ensuring the VirtualMachineInstance is removed")
Expand Down Expand Up @@ -1186,7 +1186,7 @@ var _ = Describe("[rfe_id:1177][crit:medium][vendor:[email protected]][level:com
currentUUID := virtualMachine.UID

By("Invoking virtctl restart")
restartCommand := clientcmd.NewRepeatableVirtctlCommand(vm.COMMAND_RESTART, "--namespace", virtualMachine.Namespace, virtualMachine.Name)
restartCommand := clientcmd.NewRepeatableVirtctlCommand(virtctl.COMMAND_RESTART, "--namespace", virtualMachine.Namespace, virtualMachine.Name)
Expect(restartCommand()).To(Succeed())

By("Ensuring the VirtualMachineInstance is restarted")
Expand Down Expand Up @@ -1277,7 +1277,7 @@ var _ = Describe("[rfe_id:1177][crit:medium][vendor:[email protected]][level:com
}, 360*time.Second, 1*time.Second).Should(BeTrue())

By("Invoking virtctl migrate")
migrateCommand := clientcmd.NewRepeatableVirtctlCommand(vm.COMMAND_MIGRATE, "--namespace", virtualMachine.Namespace, virtualMachine.Name)
migrateCommand := clientcmd.NewRepeatableVirtctlCommand(virtctl.COMMAND_MIGRATE, "--namespace", virtualMachine.Namespace, virtualMachine.Name)
Expect(migrateCommand()).To(Succeed())

By("Ensuring the VirtualMachineInstance is migrated")
Expand All @@ -1304,7 +1304,7 @@ var _ = Describe("[rfe_id:1177][crit:medium][vendor:[email protected]][level:com
}, 360*time.Second, 1*time.Second).Should(BeTrue())

By("Invoking virtctl migrate with dry-run option")
migrateCommand := clientcmd.NewRepeatableVirtctlCommand(vm.COMMAND_MIGRATE, "--dry-run", "--namespace", virtualMachine.Namespace, virtualMachine.Name)
migrateCommand := clientcmd.NewRepeatableVirtctlCommand(virtctl.COMMAND_MIGRATE, "--dry-run", "--namespace", virtualMachine.Namespace, virtualMachine.Name)
Expect(migrateCommand()).To(Succeed())

By("Check that no migration was actually created")
Expand All @@ -1320,7 +1320,7 @@ var _ = Describe("[rfe_id:1177][crit:medium][vendor:[email protected]][level:com
By("creating a VM with RunStrategyRerunOnFailure")
virtualMachine := newVirtualMachineWithRunStrategy(v1.RunStrategyRerunOnFailure)

stopCommand := clientcmd.NewRepeatableVirtctlCommand(vm.COMMAND_STOP, "--namespace", virtualMachine.Namespace, virtualMachine.Name)
stopCommand := clientcmd.NewRepeatableVirtctlCommand(virtctl.COMMAND_STOP, "--namespace", virtualMachine.Namespace, virtualMachine.Name)

By("Waiting for VM to be ready")
Eventually(func() bool {
Expand Down Expand Up @@ -1352,7 +1352,7 @@ var _ = Describe("[rfe_id:1177][crit:medium][vendor:[email protected]][level:com
By("creating a VM with RunStrategyRerunOnFailure")
virtualMachine := newVirtualMachineWithRunStrategy(v1.RunStrategyRerunOnFailure)

restartCommand := clientcmd.NewRepeatableVirtctlCommand(vm.COMMAND_RESTART, "--namespace", virtualMachine.Namespace, virtualMachine.Name)
restartCommand := clientcmd.NewRepeatableVirtctlCommand(virtctl.COMMAND_RESTART, "--namespace", virtualMachine.Namespace, virtualMachine.Name)

By("Waiting for VM to be ready")
Eventually(func() bool {
Expand Down Expand Up @@ -1432,7 +1432,7 @@ var _ = Describe("[rfe_id:1177][crit:medium][vendor:[email protected]][level:com
// At this point, explicitly test that a start command will delete an existing
// VMI in the Succeeded phase.
By("Invoking virtctl start")
restartCommand := clientcmd.NewRepeatableVirtctlCommand(vm.COMMAND_START, "--namespace", virtualMachine.Namespace, virtualMachine.Name)
restartCommand := clientcmd.NewRepeatableVirtctlCommand(virtctl.COMMAND_START, "--namespace", virtualMachine.Namespace, virtualMachine.Name)
err = restartCommand()
Expect(err).ToNot(HaveOccurred())

Expand All @@ -1457,7 +1457,7 @@ var _ = Describe("[rfe_id:1177][crit:medium][vendor:[email protected]][level:com
By("creating a VM with RunStrategyHalted")
virtualMachine := newVirtualMachineWithRunStrategy(v1.RunStrategyHalted)

startCommand := clientcmd.NewRepeatableVirtctlCommand(vm.COMMAND_START, "--namespace", virtualMachine.Namespace, virtualMachine.Name)
startCommand := clientcmd.NewRepeatableVirtctlCommand(virtctl.COMMAND_START, "--namespace", virtualMachine.Namespace, virtualMachine.Name)
err = startCommand()
Expect(err).ToNot(HaveOccurred())

Expand Down Expand Up @@ -1565,7 +1565,7 @@ var _ = Describe("[rfe_id:1177][crit:medium][vendor:[email protected]][level:com
By("creating a VM with RunStrategyManual")
virtualMachine := newVirtualMachineWithRunStrategy(v1.RunStrategyManual)

startCommand := clientcmd.NewRepeatableVirtctlCommand(vm.COMMAND_START, "--namespace", virtualMachine.Namespace, virtualMachine.Name)
startCommand := clientcmd.NewRepeatableVirtctlCommand(virtctl.COMMAND_START, "--namespace", virtualMachine.Namespace, virtualMachine.Name)
err = startCommand()
Expect(err).ToNot(HaveOccurred())

Expand All @@ -1588,7 +1588,7 @@ var _ = Describe("[rfe_id:1177][crit:medium][vendor:[email protected]][level:com
By("creating a VM with RunStrategyManual")
virtualMachine := newVirtualMachineWithRunStrategy(v1.RunStrategyManual)

startCommand := clientcmd.NewRepeatableVirtctlCommand(vm.COMMAND_START, "--namespace", virtualMachine.Namespace, virtualMachine.Name)
startCommand := clientcmd.NewRepeatableVirtctlCommand(virtctl.COMMAND_START, "--namespace", virtualMachine.Namespace, virtualMachine.Name)
err = startCommand()
Expect(err).ToNot(HaveOccurred())

Expand All @@ -1599,7 +1599,7 @@ var _ = Describe("[rfe_id:1177][crit:medium][vendor:[email protected]][level:com
return virtualMachine.Status.Ready
}, 360*time.Second, 1*time.Second).Should(BeTrue())

stopCommand := clientcmd.NewRepeatableVirtctlCommand(vm.COMMAND_STOP, "--namespace", virtualMachine.Namespace, virtualMachine.Name)
stopCommand := clientcmd.NewRepeatableVirtctlCommand(virtctl.COMMAND_STOP, "--namespace", virtualMachine.Namespace, virtualMachine.Name)
err = stopCommand()
Expect(err).ToNot(HaveOccurred())

Expand Down Expand Up @@ -1628,7 +1628,7 @@ var _ = Describe("[rfe_id:1177][crit:medium][vendor:[email protected]][level:com
virtualMachine := newVirtualMachineWithRunStrategy(v1.RunStrategyManual)

By("Invoking virtctl start")
startCommand := clientcmd.NewRepeatableVirtctlCommand(vm.COMMAND_START, "--namespace", virtualMachine.Namespace, virtualMachine.Name, "--paused")
startCommand := clientcmd.NewRepeatableVirtctlCommand(virtctl.COMMAND_START, "--namespace", virtualMachine.Namespace, virtualMachine.Name, "--paused")
Expect(startCommand()).To(Succeed())

By("Getting the status of the VM")
Expand All @@ -1652,9 +1652,9 @@ var _ = Describe("[rfe_id:1177][crit:medium][vendor:[email protected]][level:com
By("creating a VM with RunStrategyManual")
virtualMachine := newVirtualMachineWithRunStrategy(v1.RunStrategyManual)

startCommand := clientcmd.NewRepeatableVirtctlCommand(vm.COMMAND_START, "--namespace", virtualMachine.Namespace, virtualMachine.Name)
stopCommand := clientcmd.NewRepeatableVirtctlCommand(vm.COMMAND_STOP, "--namespace", virtualMachine.Namespace, virtualMachine.Name)
restartCommand := clientcmd.NewRepeatableVirtctlCommand(vm.COMMAND_RESTART, "--namespace", virtualMachine.Namespace, virtualMachine.Name)
startCommand := clientcmd.NewRepeatableVirtctlCommand(virtctl.COMMAND_START, "--namespace", virtualMachine.Namespace, virtualMachine.Name)
stopCommand := clientcmd.NewRepeatableVirtctlCommand(virtctl.COMMAND_STOP, "--namespace", virtualMachine.Namespace, virtualMachine.Name)
restartCommand := clientcmd.NewRepeatableVirtctlCommand(virtctl.COMMAND_RESTART, "--namespace", virtualMachine.Namespace, virtualMachine.Name)

By("Invoking virtctl restart should fail")
err = restartCommand()
Expand Down Expand Up @@ -1745,7 +1745,7 @@ var _ = Describe("[rfe_id:1177][crit:medium][vendor:[email protected]][level:com
By("creating a VM with RunStrategyManual")
virtualMachine := newVirtualMachineWithRunStrategy(v1.RunStrategyManual)

startCommand := clientcmd.NewRepeatableVirtctlCommand(vm.COMMAND_START, "--namespace", virtualMachine.Namespace, virtualMachine.Name)
startCommand := clientcmd.NewRepeatableVirtctlCommand(virtctl.COMMAND_START, "--namespace", virtualMachine.Namespace, virtualMachine.Name)
err = startCommand()
Expect(err).ToNot(HaveOccurred())

Expand Down Expand Up @@ -1778,7 +1778,7 @@ var _ = Describe("[rfe_id:1177][crit:medium][vendor:[email protected]][level:com
// At this point, explicitly test that a start command will delete an existing
// VMI in the Succeeded phase.
By("Invoking virtctl start")
restartCommand := clientcmd.NewRepeatableVirtctlCommand(vm.COMMAND_START, "--namespace", virtualMachine.Namespace, virtualMachine.Name)
restartCommand := clientcmd.NewRepeatableVirtctlCommand(virtctl.COMMAND_START, "--namespace", virtualMachine.Namespace, virtualMachine.Name)
err = restartCommand()
Expect(err).ToNot(HaveOccurred())

Expand Down Expand Up @@ -1815,7 +1815,7 @@ var _ = Describe("[rfe_id:1177][crit:medium][vendor:[email protected]][level:com
Expect(err).ToNot(HaveOccurred())

By("Starting the VMI with virtctl")
startCommand := clientcmd.NewRepeatableVirtctlCommand(vm.COMMAND_START, "--namespace", newVM.Namespace, newVM.Name)
startCommand := clientcmd.NewRepeatableVirtctlCommand(virtctl.COMMAND_START, "--namespace", newVM.Namespace, newVM.Name)
err = startCommand()
Expect(err).ToNot(HaveOccurred())

Expand Down Expand Up @@ -1865,36 +1865,36 @@ var _ = Describe("[rfe_id:1177][crit:medium][vendor:[email protected]][level:com
})

It("should fail without arguments", func() {
expandCommand := clientcmd.NewRepeatableVirtctlCommand(vm.COMMAND_EXPAND)
expandCommand := clientcmd.NewRepeatableVirtctlCommand(virtctl.COMMAND_EXPAND)
err = expandCommand()

Expect(err).To(HaveOccurred())
Expect(err).Should(MatchError("error invalid arguments - VirtualMachine name or file must be provided"))
})

It("should expand vm", func() {
expandCommand := clientcmd.NewRepeatableVirtctlCommand(vm.COMMAND_EXPAND, "--namespace", newVM.Namespace, "--vm", newVM.Name)
expandCommand := clientcmd.NewRepeatableVirtctlCommand(virtctl.COMMAND_EXPAND, "--namespace", newVM.Namespace, "--vm", newVM.Name)
Expect(expandCommand()).To(Succeed())
})

It("should fail with non existing vm", func() {
expandCommand := clientcmd.NewRepeatableVirtctlCommand(vm.COMMAND_EXPAND, "--vm", "non-existing-vm")
expandCommand := clientcmd.NewRepeatableVirtctlCommand(virtctl.COMMAND_EXPAND, "--vm", "non-existing-vm")

err := expandCommand()
Expect(err).To(HaveOccurred())
Expect(err).Should(MatchError("error expanding VirtualMachine - non-existing-vm in namespace - default: virtualmachine.kubevirt.io \"non-existing-vm\" not found"))
})

DescribeTable("should expand vm with", func(formatName string) {
expandCommand := clientcmd.NewRepeatableVirtctlCommand(vm.COMMAND_EXPAND, "--namespace", newVM.Namespace, "--output", formatName, "--vm", newVM.Name)
expandCommand := clientcmd.NewRepeatableVirtctlCommand(virtctl.COMMAND_EXPAND, "--namespace", newVM.Namespace, "--output", formatName, "--vm", newVM.Name)
Expect(expandCommand()).To(Succeed())
},
Entry("supported format output json", vm.JSON),
Entry("supported format output yaml", vm.YAML),
Entry("supported format output json", virtctl.JSON),
Entry("supported format output yaml", virtctl.YAML),
)

It("should fail with unsupported output format", func() {
expandCommand := clientcmd.NewRepeatableVirtctlCommand(vm.COMMAND_EXPAND, "--namespace", newVM.Namespace, "--output", "fakeJson", "--vm", newVM.Name)
expandCommand := clientcmd.NewRepeatableVirtctlCommand(virtctl.COMMAND_EXPAND, "--namespace", newVM.Namespace, "--output", "fakeJson", "--vm", newVM.Name)
err := expandCommand()

fmt.Printf("%+v\n", err.Error())
Expand Down Expand Up @@ -1940,21 +1940,21 @@ status:
It("should expand vm defined in file", func() {
Expect(os.WriteFile(file.Name(), []byte(vmSpec), 0777)).To(Succeed())

expandCommand := clientcmd.NewRepeatableVirtctlCommand(vm.COMMAND_EXPAND, "--file", file.Name())
expandCommand := clientcmd.NewRepeatableVirtctlCommand(virtctl.COMMAND_EXPAND, "--file", file.Name())
Expect(expandCommand()).To(Succeed())
})

It("should fail expanding invalid vm defined in file", func() {
Expect(os.WriteFile(file.Name(), []byte(invalidVmSpec), 0777)).To(Succeed())
expandCommand := clientcmd.NewRepeatableVirtctlCommand(vm.COMMAND_EXPAND, "--file", file.Name())
expandCommand := clientcmd.NewRepeatableVirtctlCommand(virtctl.COMMAND_EXPAND, "--file", file.Name())

err := expandCommand()
Expect(err).To(HaveOccurred())
Expect(err).Should(MatchError("error expanding VirtualMachine - testvm in namespace - default: Object is not a valid VirtualMachine"))
})

It("should fail expanding vm when input file does not exist", func() {
expandCommand := clientcmd.NewRepeatableVirtctlCommand(vm.COMMAND_EXPAND, "--file", "invalid/path")
expandCommand := clientcmd.NewRepeatableVirtctlCommand(virtctl.COMMAND_EXPAND, "--file", "invalid/path")

err := expandCommand()
Expect(err).To(HaveOccurred())
Expand Down Expand Up @@ -2054,7 +2054,7 @@ status:
Expect(err).ToNot(HaveOccurred())

By("Invoking virtctl start")
virtctl := clientcmd.NewRepeatableVirtctlCommand(vm.COMMAND_START, "--namespace", thisVm.Namespace, thisVm.Name)
virtctl := clientcmd.NewRepeatableVirtctlCommand(virtctl.COMMAND_START, "--namespace", thisVm.Namespace, thisVm.Name)
err = virtctl()
Expect(err).ToNot(HaveOccurred())

Expand Down Expand Up @@ -2092,7 +2092,7 @@ status:
Expect(err).ToNot(HaveOccurred())

By("Invoking virtctl start with dry-run option")
virtctl := clientcmd.NewRepeatableVirtctlCommand(vm.COMMAND_START, "--namespace", thisVm.Namespace, "--dry-run", thisVm.Name)
virtctl := clientcmd.NewRepeatableVirtctlCommand(virtctl.COMMAND_START, "--namespace", thisVm.Namespace, "--dry-run", thisVm.Name)
err = virtctl()
Expect(err).ToNot(HaveOccurred())

Expand Down Expand Up @@ -2124,7 +2124,7 @@ status:
originalVM, err := virtClient.VirtualMachine(thisVm.Namespace).Get(context.Background(), thisVm.Name, &k8smetav1.GetOptions{})
Expect(err).ToNot(HaveOccurred())

var args = []string{vm.COMMAND_STOP, "--namespace", thisVm.Namespace, thisVm.Name, "--dry-run"}
var args = []string{virtctl.COMMAND_STOP, "--namespace", thisVm.Namespace, thisVm.Name, "--dry-run"}
if flags != nil {
args = append(args, flags...)
}
Expand Down Expand Up @@ -2181,7 +2181,7 @@ status:
VMIUuid := currentVmi.ObjectMeta.UID

By("Invoking virtctl restart with dry-run option")
virtctl := clientcmd.NewRepeatableVirtctlCommand(vm.COMMAND_RESTART, "--namespace", thisVm.Namespace, "--dry-run", thisVm.Name)
virtctl := clientcmd.NewRepeatableVirtctlCommand(virtctl.COMMAND_RESTART, "--namespace", thisVm.Namespace, "--dry-run", thisVm.Name)
err = virtctl()
Expect(err).ToNot(HaveOccurred())

Expand Down Expand Up @@ -2390,7 +2390,7 @@ status:
}, 1*time.Minute, 5*time.Second).Should(BeNil())

By("Invoking virtctl stop while in a crash loop")
stopCmd := clientcmd.NewRepeatableVirtctlCommand(vm.COMMAND_STOP, newVM.Name, "--namespace", newVM.Namespace)
stopCmd := clientcmd.NewRepeatableVirtctlCommand(virtctl.COMMAND_STOP, newVM.Name, "--namespace", newVM.Namespace)
Expect(stopCmd()).ToNot(HaveOccurred())

By("Waiting on crash loop status to be removed.")
Expand Down

0 comments on commit b9d33dc

Please sign in to comment.