mirror of https://github.com/docker/compose.git
down --volume to remove volume
Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
This commit is contained in:
parent
b46015f2f8
commit
98187b2f62
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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"))
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue