down --volume to remove volume

Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
This commit is contained in:
Nicolas De Loof 2021-04-29 21:54:54 +02:00
parent b46015f2f8
commit 98187b2f62
No known key found for this signature in database
GPG Key ID: 9858809D6F8F6E7E
3 changed files with 56 additions and 4 deletions

View File

@ -98,6 +98,20 @@ func (s *composeService) Down(ctx context.Context, projectName string, options c
}
}
if options.Volumes {
networks, err := s.apiClient.VolumeList(ctx, filters.NewArgs(projectFilter(projectName)))
if err != nil {
return err
}
for _, vol := range networks.Volumes {
id := vol.Name
eg.Go(func() error {
resourceToRemove = true
return s.removeVolume(ctx, id, w)
})
}
}
if !resourceToRemove {
w.Event(progress.NewEvent(projectName, progress.Done, "Warning: No resource found to remove"))
}
@ -134,6 +148,21 @@ func (s *composeService) removeImage(ctx context.Context, image string, w progre
return err
}
func (s *composeService) removeVolume(ctx context.Context, id string, w progress.Writer) error {
resource := fmt.Sprintf("Volume %s", id)
w.Event(progress.NewEvent(resource, progress.Working, "Removing"))
err := s.apiClient.VolumeRemove(ctx, id, true)
if err == nil {
w.Event(progress.NewEvent(resource, progress.Done, "Removed"))
return nil
}
if errdefs.IsNotFound(err) {
w.Event(progress.NewEvent(resource, progress.Done, "Warning: No resource found to remove"))
return nil
}
return err
}
func (s *composeService) stopContainers(ctx context.Context, w progress.Writer, containers []moby.Container, timeout *time.Duration) error {
for _, container := range containers {
toStop := container

View File

@ -20,12 +20,12 @@ import (
"context"
"testing"
"github.com/docker/docker/api/types/filters"
"github.com/docker/compose-cli/api/compose"
"github.com/docker/compose-cli/local/mocks"
apitypes "github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/api/types/volume"
"github.com/golang/mock/gomock"
"gotest.tools/v3/assert"
)
@ -79,3 +79,24 @@ func TestDownRemoveOrphans(t *testing.T) {
err := tested.Down(context.Background(), testProject, compose.DownOptions{RemoveOrphans: true})
assert.NilError(t, err)
}
func TestDownRemoveVolumes(t *testing.T) {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()
api := mocks.NewMockAPIClient(mockCtrl)
tested.apiClient = api
api.EXPECT().ContainerList(gomock.Any(), projectFilterListOpt()).Return(
[]apitypes.Container{testContainer("service1", "123")}, nil)
api.EXPECT().ContainerStop(gomock.Any(), "123", nil).Return(nil)
api.EXPECT().ContainerRemove(gomock.Any(), "123", apitypes.ContainerRemoveOptions{Force: true}).Return(nil)
api.EXPECT().NetworkList(gomock.Any(), apitypes.NetworkListOptions{Filters: filters.NewArgs(projectFilter(testProject))}).Return(nil, nil)
api.EXPECT().VolumeList(gomock.Any(), filters.NewArgs(projectFilter(testProject))).Return(volume.VolumeListOKBody{Volumes: []*apitypes.Volume{{Name: "myProject_volume"}}}, nil)
api.EXPECT().VolumeRemove(gomock.Any(), "myProject_volume", true).Return(nil)
err := tested.Down(context.Background(), testProject, compose.DownOptions{Volumes: true})
assert.NilError(t, err)
}

View File

@ -72,7 +72,9 @@ func TestLocalComposeVolume(t *testing.T) {
})
t.Run("cleanup volume project", func(t *testing.T) {
c.RunDockerCmd("compose", "--project-name", projectName, "down")
c.RunDockerCmd("volume", "rm", projectName+"_staticVol")
c.RunDockerCmd("compose", "--project-name", projectName, "down", "--volumes")
res := c.RunDockerCmd("volume", "ls")
assert.Assert(t, !strings.Contains(res.Stdout(), projectName+"_staticVol"))
assert.Assert(t, !strings.Contains(res.Stdout(), "myvolume"))
})
}