Skip to content

Commit

Permalink
fix: add retry to assert containers do not exist for compose down (#73)
Browse files Browse the repository at this point in the history
runfinch/finch#460



- [ X ] I've reviewed the guidance in CONTRIBUTING.md


#### License Acceptance

By submitting this pull request, I confirm that my contribution is made
under the terms of the Apache 2.0 license.

Signed-off-by: Ziwen Ning <ningziwe@amazon.com>
  • Loading branch information
ningziwen committed Jun 26, 2023
1 parent 6c72750 commit 88f732f
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 10 deletions.
11 changes: 9 additions & 2 deletions tests/compose_down.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"os"
"regexp"
"time"

"github.com/onsi/ginkgo/v2"
"github.com/onsi/gomega"
Expand Down Expand Up @@ -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")
})

Expand All @@ -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")
}
Expand Down
13 changes: 9 additions & 4 deletions tests/rm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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() {
Expand All @@ -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())
})
}
})
Expand All @@ -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)
},
)
Expand All @@ -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")
},
)
Expand Down
3 changes: 2 additions & 1 deletion tests/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
10 changes: 7 additions & 3 deletions tests/tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 88f732f

Please sign in to comment.