diff --git a/tests/compose_down.go b/tests/compose_down.go index 5e1e720..d1a92df 100644 --- a/tests/compose_down.go +++ b/tests/compose_down.go @@ -5,12 +5,14 @@ package tests import ( "fmt" + "os" + "regexp" + "github.com/onsi/ginkgo/v2" "github.com/onsi/gomega" "github.com/runfinch/common-tests/command" "github.com/runfinch/common-tests/ffs" "github.com/runfinch/common-tests/option" - "os" ) // ComposeDown tests functionality of `compose down` command. @@ -37,9 +39,33 @@ func ComposeDown(o *option.Option) { containerShouldNotExist(o, containerNames...) volumeShouldExist(o, "compose_data_volume") }) + + for _, volumes := range []string{"-v", "--volumes"} { + volumes := volumes + 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...) + 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))