Skip to content

Commit

Permalink
fix: Refactor VM lifecycle test for flakiness (#1187)
Browse files Browse the repository at this point in the history
Signed-off-by: Shubharanshu Mahapatra <[email protected]>
  • Loading branch information
Shubhranshu153 authored Nov 18, 2024
1 parent 7649623 commit 2978275
Showing 1 changed file with 44 additions and 9 deletions.
53 changes: 44 additions & 9 deletions e2e/vm/lifecycle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,39 @@
package vm

import (
"runtime"
"time"

"github.com/onsi/ginkgo/v2"
"github.com/onsi/gomega"
"github.com/runfinch/common-tests/command"
"github.com/runfinch/common-tests/option"
)

var restoreVM = func(o *option.Option) {
origFinchCfg := readFile(finchConfigFilePath)
writeFile(finchConfigFilePath, origFinchCfg)
command.New(o, virtualMachineRootCmd, "stop", "-f").WithoutCheckingExitCode().WithTimeoutInSeconds(20).Run()
time.Sleep(1 * time.Second)
command.New(o, virtualMachineRootCmd, "remove", "-f").WithoutCheckingExitCode().WithTimeoutInSeconds(10).Run()
time.Sleep(1 * time.Second)
if runtime.GOOS == "windows" {
// clean up iptables
//nolint:lll // link to explanation
// 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
gomega.Expect(shutdownWSL()).Should(gomega.BeNil())
}
time.Sleep(1 * time.Second)
command.New(o, virtualMachineRootCmd, "init").WithoutCheckingExitCode().WithTimeoutInSeconds(160).Run()
}

var testVMLifecycle = func(o *option.Option) {
// These tests are run in serial because we only define one virtual machine instance.
ginkgo.Describe("virtual machine lifecycle", ginkgo.Serial, func() {
ginkgo.When("the virtual machine is in running status", func() {
ginkgo.Describe("when the virtual machine is in running status", func() {
ginkgo.AfterEach(func() {
restoreVM(o)
})

ginkgo.It("should fail to init/remove the virtual machine", func() {
command.New(o, virtualMachineRootCmd, "init").WithoutSuccessfulExit().Run()
command.New(o, virtualMachineRootCmd, "remove").WithoutSuccessfulExit().Run()
Expand All @@ -36,7 +59,6 @@ var testVMLifecycle = func(o *option.Option) {
command.Run(o, "images")
command.New(o, virtualMachineRootCmd, "remove", "--force").WithTimeoutInSeconds(160).Run()
command.RunWithoutSuccessfulExit(o, "images")
command.New(o, virtualMachineRootCmd, "init").WithTimeoutInSeconds(160).Run()
})

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

ginkgo.When("the virtual machine is in stopped status", func() {
ginkgo.Describe("when the virtual machine is in stopped status", func() {
ginkgo.BeforeEach(func() {
command.New(o, virtualMachineRootCmd, "stop", "--force").WithTimeoutInSeconds(90).Run()
})

ginkgo.AfterEach(func() {
restoreVM(o)
})

ginkgo.It("should fail to init/stop", func() {
command.New(o, virtualMachineRootCmd, "stop").WithoutSuccessfulExit().Run()
command.New(o, virtualMachineRootCmd, "init").WithoutSuccessfulExit().Run()
Expand All @@ -63,20 +93,25 @@ var testVMLifecycle = func(o *option.Option) {
})

ginkgo.It("should be able to remove the virtual machine", func() {
// don't asssume the VM will be in a stopped state (e.g. if the previous test fails)
command.New(o, virtualMachineRootCmd, "stop", "--force").WithTimeoutInSeconds(90).Run()
command.New(o, virtualMachineRootCmd, "remove").WithTimeoutInSeconds(160).Run()
command.RunWithoutSuccessfulExit(o, "images")
command.New(o, virtualMachineRootCmd, "init").WithTimeoutInSeconds(160).Run()
command.New(o, virtualMachineRootCmd, "stop").WithTimeoutInSeconds(90).Run()
})

ginkgo.It("should be able to force remove the virtual machine", func() {
command.New(o, virtualMachineRootCmd, "remove", "--force").WithTimeoutInSeconds(160).Run()
command.RunWithoutSuccessfulExit(o, "images")
})
})

ginkgo.When("the virtual machine instance does not exist", func() {
ginkgo.Describe("when the virtual machine instance does not exist", func() {
ginkgo.BeforeEach(func() {
command.New(o, virtualMachineRootCmd, "remove", "--force").WithTimeoutInSeconds(160).Run()
})

ginkgo.AfterEach(func() {
restoreVM(o)
})

ginkgo.It("should fail to start/stop", func() {
command.New(o, virtualMachineRootCmd, "start").WithoutSuccessfulExit().Run()
command.New(o, virtualMachineRootCmd, "stop").WithoutSuccessfulExit().Run()
Expand Down

0 comments on commit 2978275

Please sign in to comment.