Skip to content

Commit

Permalink
Merge pull request #381 from peter-evans/compose-volumes
Browse files Browse the repository at this point in the history
feat: additionally remove volumes by default on compose down
  • Loading branch information
mdelapenya authored Dec 3, 2021
2 parents 79476d7 + 4bb8975 commit 02a0f97
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 2 deletions.
2 changes: 1 addition & 1 deletion compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func NewLocalDockerCompose(filePaths []string, identifier string) *LocalDockerCo

// Down executes docker-compose down
func (dc *LocalDockerCompose) Down() ExecError {
return executeCompose(dc, []string{"down", "--remove-orphans"})
return executeCompose(dc, []string{"down", "--remove-orphans", "--volumes"})
}

func (dc *LocalDockerCompose) getDockerComposeEnvironment() map[string]string {
Expand Down
30 changes: 29 additions & 1 deletion compose_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,34 @@ func TestLocalDockerComposeWithMultipleComposeFiles(t *testing.T) {
assertContainerEnvironmentVariables(t, containerNameNginx, present, absent)
}

func TestLocalDockerComposeWithVolume(t *testing.T) {
path := "./testresources/docker-compose-volume.yml"

identifier := strings.ToLower(uuid.New().String())

compose := NewLocalDockerCompose([]string{path}, identifier)
destroyFn := func() {
err := compose.Down()
checkIfError(t, err)
assertVolumeDoesNotExist(t, fmt.Sprintf("%s_mydata", identifier))
}
defer destroyFn()

err := compose.
WithCommand([]string{"up", "-d"}).
Invoke()
checkIfError(t, err)
}

func assertVolumeDoesNotExist(t *testing.T, volume string) {
args := []string{"volume", "inspect", volume}

output, _ := executeAndGetOutput("docker", args)
if !strings.Contains(output, "No such volume") {
t.Fatalf("Expected volume %q to not exist", volume)
}
}

func assertContainerEnvironmentVariables(t *testing.T, containerName string, present map[string]string, absent map[string]string) {
args := []string{"exec", containerName, "env"}

Expand Down Expand Up @@ -403,7 +431,7 @@ func executeAndGetOutput(command string, args []string) (string, ExecError) {
cmd := exec.Command(command, args...)
out, err := cmd.CombinedOutput()
if err != nil {
return "", ExecError{Error: err}
return string(out), ExecError{Error: err}
}

return string(out), ExecError{Error: nil}
Expand Down
17 changes: 17 additions & 0 deletions testresources/docker-compose-volume.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
version: '3'
services:
nginx:
image: nginx:stable-alpine
volumes:
- type: volume
source: mydata
target: /data
volume:
nocopy: true
environment:
bar: ${bar}
ports:
- "9080:80"

volumes:
mydata:

0 comments on commit 02a0f97

Please sign in to comment.