virtctl: add a migrate-cancel test to vm_test
@@ -28,6 +28,7 @@ var _ = Describe("VirtualMachine", func() {
const vmName = "testvm"
var vmInterface *kubecli.MockVirtualMachineInterface
var vmiInterface *kubecli.MockVirtualMachineInstanceInterface
+ var migrationInterface *kubecli.MockVirtualMachineInstanceMigrationInterface
var ctrl *gomock.Controller
running := true
@@ -46,6 +47,7 @@ var _ = Describe("VirtualMachine", func() {
kubecli.MockKubevirtClientInstance = kubecli.NewMockKubevirtClient(ctrl)
vmInterface = kubecli.NewMockVirtualMachineInterface(ctrl)
vmiInterface = kubecli.NewMockVirtualMachineInstanceInterface(ctrl)
+ migrationInterface = kubecli.NewMockVirtualMachineInstanceMigrationInterface(ctrl)
})
Context("With missing input parameters", func() {
@@ -65,6 +67,10 @@ var _ = Describe("VirtualMachine", func() {
cmd := tests.NewRepeatableVirtctlCommand("migrate")
Expect(cmd()).NotTo(BeNil())
})
+ It("should fail a migrate-cancel", func() {
+ cmd := tests.NewRepeatableVirtctlCommand("migrate-cancel")
+ Expect(cmd()).NotTo(BeNil())
+ })
})
Context("should patch VM", func() {
@@ -178,6 +184,51 @@ var _ = Describe("VirtualMachine", func() {
)
})
+ Context("with migrate-cancel VM cmd", func() {
+ vm := kubecli.NewMinimalVM(vmName)
+ migname := fmt.Sprintf("%s-%s", vm.Name, "migration") // "testvm-migration"
+ mig := kubecli.NewMinimalMigration(migname)
+ mig.Spec.VMIName = vm.Name // likely not required
+ labelselector := fmt.Sprintf("%s==%s", v1.MigrationSelectorLabel, vm.Name)
+ listoptions := k8smetav1.ListOptions{LabelSelector: labelselector}
+ cmd := tests.NewVirtctlCommand("migrate-cancel", vm.Name)
+ It("should cancel the vm migration", func() {
+ mig.Status.Phase = v1.MigrationRunning
+ migList := v1.VirtualMachineInstanceMigrationList{
+ Items: []v1.VirtualMachineInstanceMigration{
+ *mig,
+ },
+ }
+
+ kubecli.MockKubevirtClientInstance.EXPECT().
+ VirtualMachineInstanceMigration(k8smetav1.NamespaceDefault).
+ Return(migrationInterface).Times(2)
+
+ migrationInterface.EXPECT().List(&listoptions).Return(&migList, nil).Times(1)
+ migrationInterface.EXPECT().Delete(migname, &k8smetav1.DeleteOptions{}).Return(nil).Times(1)
+ Expect(cmd.Execute()).To(BeNil())
+ })
+ It("Should fail if no active migration is found", func() {
+ mig.Status.Phase = v1.MigrationSucceeded
+ migList := v1.VirtualMachineInstanceMigrationList{
+ Items: []v1.VirtualMachineInstanceMigration{
+ *mig,
+ },
+ }
+
+ kubecli.MockKubevirtClientInstance.EXPECT().
+ VirtualMachineInstanceMigration(k8smetav1.NamespaceDefault).
+ Return(migrationInterface).Times(1)
+
+ migrationInterface.EXPECT().List(&listoptions).Return(&migList, nil).Times(1)
+
+ res := cmd.Execute()
+ Expect(res).ToNot(BeNil())
+ errstr := fmt.Sprintf("Found no migration to cancel for %s", vm.Name)
+ Expect(res.Error()).To(ContainSubstring(errstr))
+ })
+ })
+
Context("with restart VM cmd", func() {
It("should restart vm", func() {
vm := kubecli.NewMinimalVM(vmName)
