Refactor Down to avoid double ContainerList

Signed-off-by: Ulysses Souza <ulyssessouza@gmail.com>
This commit is contained in:
Ulysses Souza 2021-03-24 17:48:23 -03:00
parent 9f81314124
commit 31e59f6919
4 changed files with 28 additions and 29 deletions

View File

@ -38,22 +38,23 @@ func (s *composeService) Down(ctx context.Context, projectName string, options c
w := progress.ContextWriter(ctx)
resourceToRemove := false
var containers Containers
containers, err := s.apiClient.ContainerList(ctx, moby.ContainerListOptions{
Filters: filters.NewArgs(projectFilter(projectName)),
All: true,
})
if err != nil {
return err
}
if options.Project == nil {
project, err := s.projectFromContainerLabels(ctx, projectName)
project, err := s.projectFromContainerLabels(containers, projectName)
if err != nil {
return err
}
options.Project = project
}
var containers Containers
containers, err := s.apiClient.ContainerList(ctx, moby.ContainerListOptions{
Filters: filters.NewArgs(projectFilter(options.Project.Name)),
All: true,
})
if err != nil {
return err
}
if len(containers) > 0 {
resourceToRemove = true
}
@ -176,18 +177,7 @@ func (s *composeService) removeContainers(ctx context.Context, w progress.Writer
return eg.Wait()
}
func projectFilterListOpt(projectName string) moby.ContainerListOptions {
return moby.ContainerListOptions{
Filters: filters.NewArgs(projectFilter(projectName)),
All: true,
}
}
func (s *composeService) projectFromContainerLabels(ctx context.Context, projectName string) (*types.Project, error) {
containers, err := s.apiClient.ContainerList(ctx, projectFilterListOpt(projectName))
if err != nil {
return nil, err
}
func (s *composeService) projectFromContainerLabels(containers Containers, projectName string) (*types.Project, error) {
fakeProject := &types.Project{
Name: projectName,
}

View File

@ -36,8 +36,8 @@ func TestDown(t *testing.T) {
tested.apiClient = api
ctx := context.Background()
api.EXPECT().ContainerList(ctx, projectFilterListOpt(testProject)).Return(
[]apitypes.Container{testContainer("service1", "123"), testContainer("service1", "456"), testContainer("service2", "789"), testContainer("service_orphan", "321")}, nil).Times(2)
api.EXPECT().ContainerList(ctx, projectFilterListOpt()).Return(
[]apitypes.Container{testContainer("service1", "123"), testContainer("service1", "456"), testContainer("service2", "789"), testContainer("service_orphan", "321")}, nil)
api.EXPECT().ContainerStop(ctx, "123", nil).Return(nil)
api.EXPECT().ContainerStop(ctx, "456", nil).Return(nil)
@ -62,8 +62,8 @@ func TestDownRemoveOrphans(t *testing.T) {
tested.apiClient = api
ctx := context.Background()
api.EXPECT().ContainerList(ctx, projectFilterListOpt(testProject)).Return(
[]apitypes.Container{testContainer("service1", "123"), testContainer("service2", "789"), testContainer("service_orphan", "321")}, nil).Times(2)
api.EXPECT().ContainerList(ctx, projectFilterListOpt()).Return(
[]apitypes.Container{testContainer("service1", "123"), testContainer("service2", "789"), testContainer("service_orphan", "321")}, nil)
api.EXPECT().ContainerStop(ctx, "123", nil).Return(nil)
api.EXPECT().ContainerStop(ctx, "789", nil).Return(nil)

View File

@ -20,6 +20,7 @@ import (
"context"
"testing"
"github.com/docker/docker/api/types/filters"
"github.com/golang/mock/gomock"
"gotest.tools/v3/assert"
@ -43,7 +44,7 @@ func TestKillAll(t *testing.T) {
project := types.Project{Name: testProject, Services: []types.ServiceConfig{testService("service1"), testService("service2")}}
ctx := context.Background()
api.EXPECT().ContainerList(ctx, projectFilterListOpt(testProject)).Return(
api.EXPECT().ContainerList(ctx, projectFilterListOpt()).Return(
[]apitypes.Container{testContainer("service1", "123"), testContainer("service1", "456"), testContainer("service2", "789")}, nil)
api.EXPECT().ContainerKill(anyCancellableContext(), "123", "").Return(nil)
api.EXPECT().ContainerKill(anyCancellableContext(), "456", "").Return(nil)
@ -62,7 +63,7 @@ func TestKillSignal(t *testing.T) {
project := types.Project{Name: testProject, Services: []types.ServiceConfig{testService("service1")}}
ctx := context.Background()
api.EXPECT().ContainerList(ctx, projectFilterListOpt(testProject)).Return([]apitypes.Container{testContainer("service1", "123")}, nil)
api.EXPECT().ContainerList(ctx, projectFilterListOpt()).Return([]apitypes.Container{testContainer("service1", "123")}, nil)
api.EXPECT().ContainerKill(anyCancellableContext(), "123", "SIGTERM").Return(nil)
err := tested.Kill(ctx, &project, compose.KillOptions{Signal: "SIGTERM"})
@ -90,3 +91,10 @@ func anyCancellableContext() gomock.Matcher {
cancel()
return gomock.AssignableToTypeOf(ctxWithCancel)
}
func projectFilterListOpt() apitypes.ContainerListOptions {
return apitypes.ContainerListOptions{
Filters: filters.NewArgs(projectFilter(testProject)),
All: true,
}
}

View File

@ -21,9 +21,10 @@ import (
"testing"
"time"
moby "github.com/docker/docker/api/types"
"github.com/docker/compose-cli/api/compose"
"github.com/docker/compose-cli/local/mocks"
moby "github.com/docker/docker/api/types"
"github.com/compose-spec/compose-go/types"
"github.com/golang/mock/gomock"
@ -37,7 +38,7 @@ func TestStopTimeout(t *testing.T) {
tested.apiClient = api
ctx := context.Background()
api.EXPECT().ContainerList(ctx, projectFilterListOpt(testProject)).Return(
api.EXPECT().ContainerList(ctx, projectFilterListOpt()).Return(
[]moby.Container{
testContainer("service1", "123"),
testContainer("service1", "456"),