diff --git a/tests/compose_down.go b/tests/compose_down.go index a875a3d..d1a92df 100644 --- a/tests/compose_down.go +++ b/tests/compose_down.go @@ -6,7 +6,7 @@ package tests import ( "fmt" "os" - "time" + "regexp" "github.com/onsi/ginkgo/v2" "github.com/onsi/gomega" @@ -44,19 +44,28 @@ func ComposeDown(o *option.Option) { volumes := volumes ginkgo.It(fmt.Sprintf("should stop compose services and delete volumes by specifying %s flag", volumes), func() { volumes := volumes - // Wait 10 sec before calling compose down since compose down cmd sometimes fails to delete the volume - // due to concurrent access of the volume. - // For more details - https://github.com/runfinch/finch/issues/261 - time.Sleep(10 * time.Second) - - command.Run(o, "compose", "down", volumes, "--file", composeFilePath) + output := command.StdoutStr(o, "compose", "down", volumes, "--file", composeFilePath) containerShouldNotExist(o, containerNames...) - volumeShouldNotExist(o, "compose_data_volume") + if !isVolumeInUse(output) { + volumeShouldNotExist(o, "compose_data_volume") + } }) } }) } +// sometimes nerdctl fails to delete the volume due to concurrent usage of the volume by the container. +// For more details - https://github.com/runfinch/finch/issues/261 +func isVolumeInUse(output string) bool { + if len(output) < 1 { + return false + } + // Error msg is generated from nerdctl volume rm cmd. + // see: https://github.com/containerd/nerdctl/blob/main/pkg/cmd/volume/rm.go#L52 + re := regexp.MustCompile(`volume.*in use`) + return re.MatchString(output) +} + func createComposeYmlForDownCmd(serviceNames []string, containerNames []string) (string, string) { gomega.Expect(serviceNames).Should(gomega.HaveLen(2)) gomega.Expect(containerNames).Should(gomega.HaveLen(2))