From 0a3bc8fb5ef3ad146ed7d494ba4ab68296fda318 Mon Sep 17 00:00:00 2001 From: Ziwen Ning Date: Sun, 25 Jun 2023 23:01:32 -0700 Subject: [PATCH] fix: add retry to assert containers do not exist for compose down Signed-off-by: Ziwen Ning --- tests/compose_down.go | 11 +++++++++-- tests/rm.go | 13 +++++++++---- tests/run.go | 3 ++- tests/tests.go | 10 +++++++--- 4 files changed, 27 insertions(+), 10 deletions(-) diff --git a/tests/compose_down.go b/tests/compose_down.go index d1a92df..c178eff 100644 --- a/tests/compose_down.go +++ b/tests/compose_down.go @@ -7,6 +7,7 @@ import ( "fmt" "os" "regexp" + "time" "github.com/onsi/ginkgo/v2" "github.com/onsi/gomega" @@ -36,7 +37,11 @@ func ComposeDown(o *option.Option) { }) ginkgo.It("should stop services defined in compose file without deleting volumes", func() { command.Run(o, "compose", "down", "--file", composeFilePath) - containerShouldNotExist(o, containerNames...) + // Container removing is asynchronous in compose down. + // https://github.com/containerd/nerdctl/blob/242c6b92bcb6a6d1522104dc7206d2886b7e9cc8/pkg/composer/rm.go#L89. + gomega.Eventually(func() error { + return containerShouldNotExist(o, containerNames...) + }, 10*time.Second, 5*time.Second).Should(gomega.BeNil()) volumeShouldExist(o, "compose_data_volume") }) @@ -45,7 +50,9 @@ func ComposeDown(o *option.Option) { ginkgo.It(fmt.Sprintf("should stop compose services and delete volumes by specifying %s flag", volumes), func() { volumes := volumes output := command.StdoutStr(o, "compose", "down", volumes, "--file", composeFilePath) - containerShouldNotExist(o, containerNames...) + gomega.Eventually(func() error { + return containerShouldNotExist(o, containerNames...) + }, 10*time.Second, 5*time.Second).Should(gomega.BeNil()) if !isVolumeInUse(output) { volumeShouldNotExist(o, "compose_data_volume") } diff --git a/tests/rm.go b/tests/rm.go index 5019bc7..43eeba7 100644 --- a/tests/rm.go +++ b/tests/rm.go @@ -7,6 +7,7 @@ import ( "fmt" "github.com/onsi/ginkgo/v2" + "github.com/onsi/gomega" "github.com/runfinch/common-tests/command" "github.com/runfinch/common-tests/option" @@ -27,7 +28,8 @@ func Rm(o *option.Option) { containerShouldExist(o, testContainerName) command.Run(o, "rm", testContainerName) - containerShouldNotExist(o, testContainerName) + err := containerShouldNotExist(o, testContainerName) + gomega.Expect(err).NotTo(gomega.HaveOccurred()) }) ginkgo.Context("when the container is running", func() { @@ -44,7 +46,8 @@ func Rm(o *option.Option) { force := force ginkgo.It(fmt.Sprintf("should be able to remove the container with %s flag", force), func() { command.Run(o, "rm", force, testContainerName) - containerShouldNotExist(o, testContainerName) + err := containerShouldNotExist(o, testContainerName) + gomega.Expect(err).NotTo(gomega.HaveOccurred()) }) } }) @@ -60,7 +63,8 @@ func Rm(o *option.Option) { containerShouldExist(o, testContainerName) volumeShouldExist(o, anonymousVolume) command.Run(o, "rm", volumes, testContainerName) - containerShouldNotExist(o, testContainerName) + err := containerShouldNotExist(o, testContainerName) + gomega.Expect(err).NotTo(gomega.HaveOccurred()) volumeShouldNotExist(o, anonymousVolume) }, ) @@ -71,7 +75,8 @@ func Rm(o *option.Option) { volumeShouldExist(o, "foo") command.Run(o, "rm", volumes, testContainerName) - containerShouldNotExist(o, testContainerName) + err := containerShouldNotExist(o, testContainerName) + gomega.Expect(err).NotTo(gomega.HaveOccurred()) volumeShouldExist(o, "foo") }, ) diff --git a/tests/run.go b/tests/run.go index 42b284b..869cf3c 100644 --- a/tests/run.go +++ b/tests/run.go @@ -69,7 +69,8 @@ func Run(o *RunOption) { ginkgo.It("with --rm flag, container should be removed when it exits", func() { command.Run(o.BaseOpt, "run", "--rm", "--name", testContainerName, defaultImage) - containerShouldNotExist(o.BaseOpt, testContainerName) + err := containerShouldNotExist(o.BaseOpt, testContainerName) + gomega.Expect(err).NotTo(gomega.HaveOccurred()) }) ginkgo.When("running a container with metadata related flags", func() { diff --git a/tests/tests.go b/tests/tests.go index f1c2823..d316785 100644 --- a/tests/tests.go +++ b/tests/tests.go @@ -131,11 +131,15 @@ func containerShouldExist(o *option.Option, containerNames ...string) { } } -func containerShouldNotExist(o *option.Option, containerNames ...string) { +func containerShouldNotExist(o *option.Option, containerNames ...string) error { for _, containerName := range containerNames { - gomega.Expect(command.Stdout(o, "ps", "-a", "-q", "--filter", - fmt.Sprintf("name=%s", containerName))).To(gomega.BeEmpty()) + containerExists := command.Stdout(o, "ps", "-a", "-q", "--filter", + fmt.Sprintf("name=%s", containerName)) + if len(containerExists) > 0 { + return fmt.Errorf("containerd '%s' exists but should not", containerName) + } } + return nil } func imageShouldExist(o *option.Option, imageName string) {