6
6
package vm
7
7
8
8
import (
9
+ "runtime"
10
+ "time"
11
+
9
12
"github.com/onsi/ginkgo/v2"
10
13
"github.com/onsi/gomega"
11
14
"github.com/runfinch/common-tests/command"
12
15
"github.com/runfinch/common-tests/option"
13
16
)
14
17
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
+
15
35
var testVMLifecycle = func (o * option.Option ) {
16
- // These tests are run in serial because we only define one virtual machine instance.
17
36
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
+
19
42
ginkgo .It ("should fail to init/remove the virtual machine" , func () {
20
43
command .New (o , virtualMachineRootCmd , "init" ).WithoutSuccessfulExit ().Run ()
21
44
command .New (o , virtualMachineRootCmd , "remove" ).WithoutSuccessfulExit ().Run ()
@@ -36,7 +59,6 @@ var testVMLifecycle = func(o *option.Option) {
36
59
command .Run (o , "images" )
37
60
command .New (o , virtualMachineRootCmd , "remove" , "--force" ).WithTimeoutInSeconds (160 ).Run ()
38
61
command .RunWithoutSuccessfulExit (o , "images" )
39
- command .New (o , virtualMachineRootCmd , "init" ).WithTimeoutInSeconds (160 ).Run ()
40
62
})
41
63
42
64
ginkgo .It ("should be able to stop the virtual machine" , func () {
@@ -46,7 +68,15 @@ var testVMLifecycle = func(o *option.Option) {
46
68
})
47
69
})
48
70
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
+
50
80
ginkgo .It ("should fail to init/stop" , func () {
51
81
command .New (o , virtualMachineRootCmd , "stop" ).WithoutSuccessfulExit ().Run ()
52
82
command .New (o , virtualMachineRootCmd , "init" ).WithoutSuccessfulExit ().Run ()
@@ -63,20 +93,25 @@ var testVMLifecycle = func(o *option.Option) {
63
93
})
64
94
65
95
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 ()
68
96
command .New (o , virtualMachineRootCmd , "remove" ).WithTimeoutInSeconds (160 ).Run ()
69
97
command .RunWithoutSuccessfulExit (o , "images" )
70
- command .New (o , virtualMachineRootCmd , "init" ).WithTimeoutInSeconds (160 ).Run ()
71
- command .New (o , virtualMachineRootCmd , "stop" ).WithTimeoutInSeconds (90 ).Run ()
72
98
})
99
+
73
100
ginkgo .It ("should be able to force remove the virtual machine" , func () {
74
101
command .New (o , virtualMachineRootCmd , "remove" , "--force" ).WithTimeoutInSeconds (160 ).Run ()
75
102
command .RunWithoutSuccessfulExit (o , "images" )
76
103
})
77
104
})
78
105
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
+
80
115
ginkgo .It ("should fail to start/stop" , func () {
81
116
command .New (o , virtualMachineRootCmd , "start" ).WithoutSuccessfulExit ().Run ()
82
117
command .New (o , virtualMachineRootCmd , "stop" ).WithoutSuccessfulExit ().Run ()
0 commit comments