Skip to content
This repository was archived by the owner on Nov 27, 2023. It is now read-only.

Commit b24a3e1

Browse files
authored
Merge pull request #1455 from ulyssessouza/cache-container-list-on-down
Reuse container list on command down
2 parents badf822 + 31e59f6 commit b24a3e1

File tree

4 files changed

+28
-29
lines changed

4 files changed

+28
-29
lines changed

local/compose/down.go

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -38,22 +38,23 @@ func (s *composeService) Down(ctx context.Context, projectName string, options c
3838
w := progress.ContextWriter(ctx)
3939
resourceToRemove := false
4040

41-
if options.Project == nil {
42-
project, err := s.projectFromContainerLabels(ctx, projectName)
43-
if err != nil {
44-
return err
45-
}
46-
options.Project = project
47-
}
48-
4941
var containers Containers
5042
containers, err := s.apiClient.ContainerList(ctx, moby.ContainerListOptions{
51-
Filters: filters.NewArgs(projectFilter(options.Project.Name)),
43+
Filters: filters.NewArgs(projectFilter(projectName)),
5244
All: true,
5345
})
5446
if err != nil {
5547
return err
5648
}
49+
50+
if options.Project == nil {
51+
project, err := s.projectFromContainerLabels(containers, projectName)
52+
if err != nil {
53+
return err
54+
}
55+
options.Project = project
56+
}
57+
5758
if len(containers) > 0 {
5859
resourceToRemove = true
5960
}
@@ -176,18 +177,7 @@ func (s *composeService) removeContainers(ctx context.Context, w progress.Writer
176177
return eg.Wait()
177178
}
178179

179-
func projectFilterListOpt(projectName string) moby.ContainerListOptions {
180-
return moby.ContainerListOptions{
181-
Filters: filters.NewArgs(projectFilter(projectName)),
182-
All: true,
183-
}
184-
}
185-
186-
func (s *composeService) projectFromContainerLabels(ctx context.Context, projectName string) (*types.Project, error) {
187-
containers, err := s.apiClient.ContainerList(ctx, projectFilterListOpt(projectName))
188-
if err != nil {
189-
return nil, err
190-
}
180+
func (s *composeService) projectFromContainerLabels(containers Containers, projectName string) (*types.Project, error) {
191181
fakeProject := &types.Project{
192182
Name: projectName,
193183
}

local/compose/down_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ func TestDown(t *testing.T) {
3636
tested.apiClient = api
3737

3838
ctx := context.Background()
39-
api.EXPECT().ContainerList(ctx, projectFilterListOpt(testProject)).Return(
40-
[]apitypes.Container{testContainer("service1", "123"), testContainer("service1", "456"), testContainer("service2", "789"), testContainer("service_orphan", "321")}, nil).Times(2)
39+
api.EXPECT().ContainerList(ctx, projectFilterListOpt()).Return(
40+
[]apitypes.Container{testContainer("service1", "123"), testContainer("service1", "456"), testContainer("service2", "789"), testContainer("service_orphan", "321")}, nil)
4141

4242
api.EXPECT().ContainerStop(ctx, "123", nil).Return(nil)
4343
api.EXPECT().ContainerStop(ctx, "456", nil).Return(nil)
@@ -62,8 +62,8 @@ func TestDownRemoveOrphans(t *testing.T) {
6262
tested.apiClient = api
6363

6464
ctx := context.Background()
65-
api.EXPECT().ContainerList(ctx, projectFilterListOpt(testProject)).Return(
66-
[]apitypes.Container{testContainer("service1", "123"), testContainer("service2", "789"), testContainer("service_orphan", "321")}, nil).Times(2)
65+
api.EXPECT().ContainerList(ctx, projectFilterListOpt()).Return(
66+
[]apitypes.Container{testContainer("service1", "123"), testContainer("service2", "789"), testContainer("service_orphan", "321")}, nil)
6767

6868
api.EXPECT().ContainerStop(ctx, "123", nil).Return(nil)
6969
api.EXPECT().ContainerStop(ctx, "789", nil).Return(nil)

local/compose/kill_test.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"context"
2121
"testing"
2222

23+
"github.com/docker/docker/api/types/filters"
2324
"github.com/golang/mock/gomock"
2425
"gotest.tools/v3/assert"
2526

@@ -43,7 +44,7 @@ func TestKillAll(t *testing.T) {
4344
project := types.Project{Name: testProject, Services: []types.ServiceConfig{testService("service1"), testService("service2")}}
4445

4546
ctx := context.Background()
46-
api.EXPECT().ContainerList(ctx, projectFilterListOpt(testProject)).Return(
47+
api.EXPECT().ContainerList(ctx, projectFilterListOpt()).Return(
4748
[]apitypes.Container{testContainer("service1", "123"), testContainer("service1", "456"), testContainer("service2", "789")}, nil)
4849
api.EXPECT().ContainerKill(anyCancellableContext(), "123", "").Return(nil)
4950
api.EXPECT().ContainerKill(anyCancellableContext(), "456", "").Return(nil)
@@ -62,7 +63,7 @@ func TestKillSignal(t *testing.T) {
6263
project := types.Project{Name: testProject, Services: []types.ServiceConfig{testService("service1")}}
6364

6465
ctx := context.Background()
65-
api.EXPECT().ContainerList(ctx, projectFilterListOpt(testProject)).Return([]apitypes.Container{testContainer("service1", "123")}, nil)
66+
api.EXPECT().ContainerList(ctx, projectFilterListOpt()).Return([]apitypes.Container{testContainer("service1", "123")}, nil)
6667
api.EXPECT().ContainerKill(anyCancellableContext(), "123", "SIGTERM").Return(nil)
6768

6869
err := tested.Kill(ctx, &project, compose.KillOptions{Signal: "SIGTERM"})
@@ -90,3 +91,10 @@ func anyCancellableContext() gomock.Matcher {
9091
cancel()
9192
return gomock.AssignableToTypeOf(ctxWithCancel)
9293
}
94+
95+
func projectFilterListOpt() apitypes.ContainerListOptions {
96+
return apitypes.ContainerListOptions{
97+
Filters: filters.NewArgs(projectFilter(testProject)),
98+
All: true,
99+
}
100+
}

local/compose/stop_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,10 @@ import (
2121
"testing"
2222
"time"
2323

24+
moby "github.com/docker/docker/api/types"
25+
2426
"github.com/docker/compose-cli/api/compose"
2527
"github.com/docker/compose-cli/local/mocks"
26-
moby "github.com/docker/docker/api/types"
2728

2829
"github.com/compose-spec/compose-go/types"
2930
"github.com/golang/mock/gomock"
@@ -37,7 +38,7 @@ func TestStopTimeout(t *testing.T) {
3738
tested.apiClient = api
3839

3940
ctx := context.Background()
40-
api.EXPECT().ContainerList(ctx, projectFilterListOpt(testProject)).Return(
41+
api.EXPECT().ContainerList(ctx, projectFilterListOpt()).Return(
4142
[]moby.Container{
4243
testContainer("service1", "123"),
4344
testContainer("service1", "456"),

0 commit comments

Comments
 (0)