Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add retry to assert containers do not exist for compose down #73

Merged
merged 1 commit into from
Jun 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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