Skip to content

Commit

Permalink
Unit tests to verify new RunStrategyOnce option
Browse files Browse the repository at this point in the history
Signed-off-by: David Vossel <[email protected]>
  • Loading branch information
davidvossel committed Feb 11, 2022
1 parent 62bbcd4 commit 3648ac1
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 3 deletions.
7 changes: 7 additions & 0 deletions pkg/virt-api/rest/subresource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -697,11 +697,13 @@ var _ = Describe("VirtualMachineInstance Subresources", func() {
table.Entry("Always", v1.RunStrategyAlways, "VM is not running", &v1.RestartOptions{}),
table.Entry("Manual", v1.RunStrategyManual, "VM is not running", &v1.RestartOptions{}),
table.Entry("RerunOnFailure", v1.RunStrategyRerunOnFailure, "VM is not running", &v1.RestartOptions{}),
table.Entry("Once", v1.RunStrategyOnce, "Once does not support manual restart requests", &v1.RestartOptions{}),
table.Entry("Halted", v1.RunStrategyHalted, "Halted does not support manual restart requests", &v1.RestartOptions{}),

table.Entry("Always with dry-run option", v1.RunStrategyAlways, "VM is not running", &v1.RestartOptions{DryRun: getDryRunOption()}),
table.Entry("Manual with dry-run option", v1.RunStrategyManual, "VM is not running", &v1.RestartOptions{DryRun: getDryRunOption()}),
table.Entry("RerunOnFailure with dry-run option", v1.RunStrategyRerunOnFailure, "VM is not running", &v1.RestartOptions{DryRun: getDryRunOption()}),
table.Entry("Once with dry-run option", v1.RunStrategyOnce, "Once does not support manual restart requests", &v1.RestartOptions{DryRun: getDryRunOption()}),
table.Entry("Halted with dry-run option", v1.RunStrategyHalted, "Halted does not support manual restart requests", &v1.RestartOptions{DryRun: getDryRunOption()}),
)
})
Expand Down Expand Up @@ -1091,10 +1093,12 @@ var _ = Describe("VirtualMachineInstance Subresources", func() {
},
table.Entry("Always without VMI", v1.RunStrategyAlways, v1.VmPhaseUnset, http.StatusNotFound, "Always does not support manual start requests", &v1.StartOptions{}),
table.Entry("Always with VMI in phase Running", v1.RunStrategyAlways, v1.Running, http.StatusOK, "VM is already running", &v1.StartOptions{}),
table.Entry("Once", v1.RunStrategyOnce, v1.VmPhaseUnset, http.StatusNotFound, "Once does not support manual start requests", &v1.StartOptions{}),
table.Entry("RerunOnFailure with VMI in phase Failed", v1.RunStrategyRerunOnFailure, v1.Failed, http.StatusOK, "RerunOnFailure does not support starting VM from failed state", &v1.StartOptions{}),

table.Entry("Always without VMI and with dry-run option", v1.RunStrategyAlways, v1.VmPhaseUnset, http.StatusNotFound, "Always does not support manual start requests", &v1.StartOptions{DryRun: getDryRunOption()}),
table.Entry("Always with VMI in phase Running and with dry-run option", v1.RunStrategyAlways, v1.Running, http.StatusOK, "VM is already running", &v1.StartOptions{DryRun: getDryRunOption()}),
table.Entry("Once with dry-run option", v1.RunStrategyOnce, v1.VmPhaseUnset, http.StatusNotFound, "Once does not support manual start requests", &v1.StartOptions{DryRun: getDryRunOption()}),
table.Entry("RerunOnFailure with VMI in phase Failed and with dry-run option", v1.RunStrategyRerunOnFailure, v1.Failed, http.StatusOK, "RerunOnFailure does not support starting VM from failed state", &v1.StartOptions{DryRun: getDryRunOption()}),
)

Expand Down Expand Up @@ -1160,11 +1164,13 @@ var _ = Describe("VirtualMachineInstance Subresources", func() {

},
table.Entry("RunStrategyAlways", v1.RunStrategyAlways, "", false, &v1.StopOptions{}),
table.Entry("RunStrategyOnce", v1.RunStrategyOnce, "", false, &v1.StopOptions{}),
table.Entry("RunStrategyRerunOnFailure", v1.RunStrategyRerunOnFailure, "", false, &v1.StopOptions{}),
table.Entry("RunStrategyManual", v1.RunStrategyManual, "VM is not running", true, &v1.StopOptions{}),
table.Entry("RunStrategyHalted", v1.RunStrategyHalted, "VM is not running", true, &v1.StopOptions{}),

table.Entry("RunStrategyAlways with dry-run option", v1.RunStrategyAlways, "", false, &v1.StopOptions{DryRun: getDryRunOption()}),
table.Entry("RunStrategyOnce with dry-run option", v1.RunStrategyOnce, "", false, &v1.StopOptions{DryRun: getDryRunOption()}),
table.Entry("RunStrategyRerunOnFailure with dry-run option", v1.RunStrategyRerunOnFailure, "", false, &v1.StopOptions{DryRun: getDryRunOption()}),
table.Entry("RunStrategyManual with dry-run option", v1.RunStrategyManual, "VM is not running", true, &v1.StopOptions{DryRun: getDryRunOption()}),
table.Entry("RunStrategyHalted with dry-run option", v1.RunStrategyHalted, "VM is not running", true, &v1.StopOptions{DryRun: getDryRunOption()}),
Expand Down Expand Up @@ -1217,6 +1223,7 @@ var _ = Describe("VirtualMachineInstance Subresources", func() {
},
table.Entry("Always", v1.RunStrategyAlways),
table.Entry("RerunOnFailure", v1.RunStrategyRerunOnFailure),
table.Entry("Once", v1.RunStrategyOnce),
table.Entry("Manual", v1.RunStrategyManual),
)
})
Expand Down
48 changes: 45 additions & 3 deletions pkg/virt-controller/watch/vm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -958,7 +958,7 @@ var _ = Describe("VirtualMachine", func() {

controller.Execute()

if runStrategy != virtv1.RunStrategyManual {
if runStrategy != virtv1.RunStrategyManual && runStrategy != virtv1.RunStrategyOnce {
testutils.ExpectEvent(recorder, SuccessfulDeleteVirtualMachineReason)
}
},
Expand All @@ -967,6 +967,7 @@ var _ = Describe("VirtualMachine", func() {
table.Entry("always", virtv1.RunStrategyAlways),
table.Entry("manual", virtv1.RunStrategyManual),
table.Entry("rerunOnFailure", virtv1.RunStrategyRerunOnFailure),
table.Entry("once", virtv1.RunStrategyOnce),
)

table.DescribeTable("should calculated expected backoff delay", func(failCount, minExpectedDelay int, maxExpectedDelay int) {
Expand Down Expand Up @@ -1249,9 +1250,11 @@ var _ = Describe("VirtualMachine", func() {
testutils.ExpectEvent(recorder, SuccessfulCreateVirtualMachineReason)
})

It("should create missing VirtualMachineInstance", func() {
table.DescribeTable("should create missing VirtualMachineInstance", func(runStrategy virtv1.VirtualMachineRunStrategy) {
vm, vmi := DefaultVirtualMachine(true)

vm.Spec.Running = nil
vm.Spec.RunStrategy = &runStrategy
addVirtualMachine(vm)

// expect creation called
Expand All @@ -1268,7 +1271,12 @@ var _ = Describe("VirtualMachine", func() {
controller.Execute()

testutils.ExpectEvent(recorder, SuccessfulCreateVirtualMachineReason)
})
},

table.Entry("with run strategy Always", virtv1.RunStrategyAlways),
table.Entry("with run strategy Once", virtv1.RunStrategyOnce),
table.Entry("with run strategy RerunOnFailure", virtv1.RunStrategyRerunOnFailure),
)

It("should ignore the name of a VirtualMachineInstance templates", func() {
vm, vmi := DefaultVirtualMachineWithNames(true, "vmname", "vminame")
Expand Down Expand Up @@ -1364,6 +1372,28 @@ var _ = Describe("VirtualMachine", func() {
testutils.ExpectEvent(recorder, SuccessfulDeleteVirtualMachineReason)
})

table.DescribeTable("should not delete VirtualMachineInstance when vmi failed", func(runStrategy virtv1.VirtualMachineRunStrategy) {
vm, vmi := DefaultVirtualMachine(true)

vm.Spec.Running = nil
vm.Spec.RunStrategy = &runStrategy

vmi.Status.Phase = virtv1.Failed

addVirtualMachine(vm)
vmiFeeder.Add(vmi)

shouldExpectVMIFinalizerRemoval(vmi)
vmInterface.EXPECT().UpdateStatus(gomock.Any()).Times(1).Return(vm, nil)

controller.Execute()

},

table.Entry("with run strategy Once", virtv1.RunStrategyOnce),
table.Entry("with run strategy Manual", virtv1.RunStrategyManual),
)

It("should not delete the VirtualMachineInstance again if it is already marked for deletion", func() {
vm, vmi := DefaultVirtualMachine(false)
vmi.DeletionTimestamp = now()
Expand Down Expand Up @@ -1912,6 +1942,18 @@ var _ = Describe("VirtualMachine", func() {
virtv1.RunStrategyManual,
false,
false),
table.Entry("vm with runStrategy once should not report crash loop",
virtv1.VirtualMachineStatus{
StartFailure: &virtv1.VirtualMachineStartFailure{
ConsecutiveFailCount: 1,
RetryAfterTimestamp: &metav1.Time{
Time: time.Now().Add(300 * time.Second),
},
},
},
virtv1.RunStrategyOnce,
true,
false),
table.Entry("vm with runStrategy always and VMI still exists should not report crash loop",
virtv1.VirtualMachineStatus{
StartFailure: &virtv1.VirtualMachineStartFailure{
Expand Down

0 comments on commit 3648ac1

Please sign in to comment.