Skip to content

Commit 2978275

Browse files
fix: Refactor VM lifecycle test for flakiness (runfinch#1187)
Signed-off-by: Shubharanshu Mahapatra <[email protected]>
1 parent 7649623 commit 2978275

File tree

1 file changed

+44
-9
lines changed

1 file changed

+44
-9
lines changed

Diff for: e2e/vm/lifecycle_test.go

+44-9
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,39 @@
66
package vm
77

88
import (
9+
"runtime"
10+
"time"
11+
912
"github.com/onsi/ginkgo/v2"
1013
"github.com/onsi/gomega"
1114
"github.com/runfinch/common-tests/command"
1215
"github.com/runfinch/common-tests/option"
1316
)
1417

18+
var restoreVM = func(o *option.Option) {
19+
origFinchCfg := readFile(finchConfigFilePath)
20+
writeFile(finchConfigFilePath, origFinchCfg)
21+
command.New(o, virtualMachineRootCmd, "stop", "-f").WithoutCheckingExitCode().WithTimeoutInSeconds(20).Run()
22+
time.Sleep(1 * time.Second)
23+
command.New(o, virtualMachineRootCmd, "remove", "-f").WithoutCheckingExitCode().WithTimeoutInSeconds(10).Run()
24+
time.Sleep(1 * time.Second)
25+
if runtime.GOOS == "windows" {
26+
// clean up iptables
27+
//nolint:lll // link to explanation
28+
// https://docs.rancherdesktop.io/troubleshooting-tips/#q-how-do-i-fix-fata0005-subnet-1040024-overlaps-with-other-one-on-this-address-space-when-running-a-container-using-nerdctl-run
29+
gomega.Expect(shutdownWSL()).Should(gomega.BeNil())
30+
}
31+
time.Sleep(1 * time.Second)
32+
command.New(o, virtualMachineRootCmd, "init").WithoutCheckingExitCode().WithTimeoutInSeconds(160).Run()
33+
}
34+
1535
var testVMLifecycle = func(o *option.Option) {
16-
// These tests are run in serial because we only define one virtual machine instance.
1736
ginkgo.Describe("virtual machine lifecycle", ginkgo.Serial, func() {
18-
ginkgo.When("the virtual machine is in running status", func() {
37+
ginkgo.Describe("when the virtual machine is in running status", func() {
38+
ginkgo.AfterEach(func() {
39+
restoreVM(o)
40+
})
41+
1942
ginkgo.It("should fail to init/remove the virtual machine", func() {
2043
command.New(o, virtualMachineRootCmd, "init").WithoutSuccessfulExit().Run()
2144
command.New(o, virtualMachineRootCmd, "remove").WithoutSuccessfulExit().Run()
@@ -36,7 +59,6 @@ var testVMLifecycle = func(o *option.Option) {
3659
command.Run(o, "images")
3760
command.New(o, virtualMachineRootCmd, "remove", "--force").WithTimeoutInSeconds(160).Run()
3861
command.RunWithoutSuccessfulExit(o, "images")
39-
command.New(o, virtualMachineRootCmd, "init").WithTimeoutInSeconds(160).Run()
4062
})
4163

4264
ginkgo.It("should be able to stop the virtual machine", func() {
@@ -46,7 +68,15 @@ var testVMLifecycle = func(o *option.Option) {
4668
})
4769
})
4870

49-
ginkgo.When("the virtual machine is in stopped status", func() {
71+
ginkgo.Describe("when the virtual machine is in stopped status", func() {
72+
ginkgo.BeforeEach(func() {
73+
command.New(o, virtualMachineRootCmd, "stop", "--force").WithTimeoutInSeconds(90).Run()
74+
})
75+
76+
ginkgo.AfterEach(func() {
77+
restoreVM(o)
78+
})
79+
5080
ginkgo.It("should fail to init/stop", func() {
5181
command.New(o, virtualMachineRootCmd, "stop").WithoutSuccessfulExit().Run()
5282
command.New(o, virtualMachineRootCmd, "init").WithoutSuccessfulExit().Run()
@@ -63,20 +93,25 @@ var testVMLifecycle = func(o *option.Option) {
6393
})
6494

6595
ginkgo.It("should be able to remove the virtual machine", func() {
66-
// don't asssume the VM will be in a stopped state (e.g. if the previous test fails)
67-
command.New(o, virtualMachineRootCmd, "stop", "--force").WithTimeoutInSeconds(90).Run()
6896
command.New(o, virtualMachineRootCmd, "remove").WithTimeoutInSeconds(160).Run()
6997
command.RunWithoutSuccessfulExit(o, "images")
70-
command.New(o, virtualMachineRootCmd, "init").WithTimeoutInSeconds(160).Run()
71-
command.New(o, virtualMachineRootCmd, "stop").WithTimeoutInSeconds(90).Run()
7298
})
99+
73100
ginkgo.It("should be able to force remove the virtual machine", func() {
74101
command.New(o, virtualMachineRootCmd, "remove", "--force").WithTimeoutInSeconds(160).Run()
75102
command.RunWithoutSuccessfulExit(o, "images")
76103
})
77104
})
78105

79-
ginkgo.When("the virtual machine instance does not exist", func() {
106+
ginkgo.Describe("when the virtual machine instance does not exist", func() {
107+
ginkgo.BeforeEach(func() {
108+
command.New(o, virtualMachineRootCmd, "remove", "--force").WithTimeoutInSeconds(160).Run()
109+
})
110+
111+
ginkgo.AfterEach(func() {
112+
restoreVM(o)
113+
})
114+
80115
ginkgo.It("should fail to start/stop", func() {
81116
command.New(o, virtualMachineRootCmd, "start").WithoutSuccessfulExit().Run()
82117
command.New(o, virtualMachineRootCmd, "stop").WithoutSuccessfulExit().Run()

0 commit comments

Comments
 (0)