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

Commit 4140196

Browse files
committed
Add down unit tests
Signed-off-by: Guillaume Tardif <[email protected]>
1 parent b8093e6 commit 4140196

File tree

6 files changed

+117
-39
lines changed

6 files changed

+117
-39
lines changed

local/compose/create.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,8 @@ func (s *composeService) Create(ctx context.Context, project *types.Project, opt
6464

6565
var observedState Containers
6666
observedState, err = s.apiClient.ContainerList(ctx, moby.ContainerListOptions{
67-
Filters: filters.NewArgs(
68-
projectFilter(project.Name),
69-
),
70-
All: true,
67+
Filters: filters.NewArgs(projectFilter(project.Name)),
68+
All: true,
7169
})
7270
if err != nil {
7371
return err

local/compose/down.go

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,7 @@ func (s *composeService) Down(ctx context.Context, projectName string, options c
7373
}
7474
}
7575

76-
networks, err := s.apiClient.NetworkList(ctx, moby.NetworkListOptions{
77-
Filters: filters.NewArgs(
78-
projectFilter(projectName),
79-
),
80-
})
76+
networks, err := s.apiClient.NetworkList(ctx, moby.NetworkListOptions{Filters: filters.NewArgs(projectFilter(projectName))})
8177
if err != nil {
8278
return err
8379
}
@@ -137,13 +133,15 @@ func (s *composeService) removeContainers(ctx context.Context, w progress.Writer
137133
return eg.Wait()
138134
}
139135

136+
func projectFilterListOpt(projectName string) moby.ContainerListOptions {
137+
return moby.ContainerListOptions{
138+
Filters: filters.NewArgs(projectFilter(projectName)),
139+
All: true,
140+
}
141+
}
142+
140143
func (s *composeService) projectFromContainerLabels(ctx context.Context, projectName string) (*types.Project, error) {
141-
containers, err := s.apiClient.ContainerList(ctx, moby.ContainerListOptions{
142-
Filters: filters.NewArgs(
143-
projectFilter(projectName),
144-
),
145-
All: true,
146-
})
144+
containers, err := s.apiClient.ContainerList(ctx, projectFilterListOpt(projectName))
147145
if err != nil {
148146
return nil, err
149147
}

local/compose/down_test.go

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
Copyright 2020 Docker Compose CLI authors
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package compose
18+
19+
import (
20+
"context"
21+
"testing"
22+
23+
"github.com/golang/mock/gomock"
24+
"gotest.tools/v3/assert"
25+
26+
apitypes "github.com/docker/docker/api/types"
27+
"github.com/docker/docker/api/types/filters"
28+
29+
"github.com/docker/compose-cli/api/compose"
30+
"github.com/docker/compose-cli/local/mocks"
31+
)
32+
33+
func TestDown(t *testing.T) {
34+
mockCtrl := gomock.NewController(t)
35+
defer mockCtrl.Finish()
36+
api := mocks.NewMockAPIClient(mockCtrl)
37+
tested.apiClient = api
38+
39+
ctx := context.Background()
40+
api.EXPECT().ContainerList(ctx, projectFilterListOpt(testProject)).Return(
41+
[]apitypes.Container{testContainer("service1", "123"), testContainer("service1", "456"), testContainer("service2", "789"), testContainer("service_orphan", "321")}, nil).Times(2)
42+
43+
api.EXPECT().ContainerStop(ctx, "123", nil).Return(nil)
44+
api.EXPECT().ContainerStop(ctx, "456", nil).Return(nil)
45+
api.EXPECT().ContainerStop(ctx, "789", nil).Return(nil)
46+
47+
api.EXPECT().ContainerRemove(ctx, "123", apitypes.ContainerRemoveOptions{Force: true}).Return(nil)
48+
api.EXPECT().ContainerRemove(ctx, "456", apitypes.ContainerRemoveOptions{Force: true}).Return(nil)
49+
api.EXPECT().ContainerRemove(ctx, "789", apitypes.ContainerRemoveOptions{Force: true}).Return(nil)
50+
51+
api.EXPECT().NetworkList(ctx, apitypes.NetworkListOptions{Filters: filters.NewArgs(projectFilter(testProject))}).Return([]apitypes.NetworkResource{{ID: "myProject_default"}}, nil)
52+
53+
api.EXPECT().NetworkRemove(ctx, "myProject_default").Return(nil)
54+
55+
err := tested.Down(ctx, testProject, compose.DownOptions{})
56+
assert.NilError(t, err)
57+
}
58+
59+
func TestDownRemoveOrphans(t *testing.T) {
60+
mockCtrl := gomock.NewController(t)
61+
defer mockCtrl.Finish()
62+
api := mocks.NewMockAPIClient(mockCtrl)
63+
tested.apiClient = api
64+
65+
ctx := context.Background()
66+
api.EXPECT().ContainerList(ctx, projectFilterListOpt(testProject)).Return(
67+
[]apitypes.Container{testContainer("service1", "123"), testContainer("service2", "789"), testContainer("service_orphan", "321")}, nil).Times(2)
68+
69+
api.EXPECT().ContainerStop(ctx, "123", nil).Return(nil)
70+
api.EXPECT().ContainerStop(ctx, "789", nil).Return(nil)
71+
api.EXPECT().ContainerStop(ctx, "321", nil).Return(nil)
72+
73+
api.EXPECT().ContainerRemove(ctx, "123", apitypes.ContainerRemoveOptions{Force: true}).Return(nil)
74+
api.EXPECT().ContainerRemove(ctx, "789", apitypes.ContainerRemoveOptions{Force: true}).Return(nil)
75+
api.EXPECT().ContainerRemove(ctx, "321", apitypes.ContainerRemoveOptions{Force: true}).Return(nil)
76+
77+
api.EXPECT().NetworkList(ctx, apitypes.NetworkListOptions{Filters: filters.NewArgs(projectFilter(testProject))}).Return([]apitypes.NetworkResource{{ID: "myProject_default"}}, nil)
78+
79+
api.EXPECT().NetworkRemove(ctx, "myProject_default").Return(nil)
80+
81+
err := tested.Down(ctx, testProject, compose.DownOptions{RemoveOrphans: true})
82+
assert.NilError(t, err)
83+
}

local/compose/kill_test.go

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,56 +20,52 @@ import (
2020
"context"
2121
"testing"
2222

23-
"github.com/compose-spec/compose-go/types"
24-
apitypes "github.com/docker/docker/api/types"
25-
"github.com/docker/docker/api/types/filters"
26-
2723
"github.com/golang/mock/gomock"
2824
"gotest.tools/v3/assert"
2925

26+
"github.com/compose-spec/compose-go/types"
27+
apitypes "github.com/docker/docker/api/types"
28+
3029
"github.com/docker/compose-cli/api/compose"
3130
"github.com/docker/compose-cli/local/mocks"
3231
)
3332

34-
var (
35-
s = composeService{}
36-
projectListOpts = apitypes.ContainerListOptions{
37-
Filters: filters.NewArgs(projectFilter("myProject")),
38-
All: true,
39-
}
40-
)
33+
const testProject = "testProject"
34+
35+
var tested = composeService{}
4136

4237
func TestKillAll(t *testing.T) {
4338
mockCtrl := gomock.NewController(t)
4439
defer mockCtrl.Finish()
4540
api := mocks.NewMockAPIClient(mockCtrl)
46-
s.apiClient = api
41+
tested.apiClient = api
4742

48-
project := types.Project{Name: "myProject", Services: []types.ServiceConfig{testService("service1"), testService("service2")}}
43+
project := types.Project{Name: testProject, Services: []types.ServiceConfig{testService("service1"), testService("service2")}}
4944

5045
ctx := context.Background()
51-
api.EXPECT().ContainerList(ctx, projectListOpts).Return([]apitypes.Container{testContainer("service1", "123"), testContainer("service1", "456"), testContainer("service2", "789")}, nil)
46+
api.EXPECT().ContainerList(ctx, projectFilterListOpt(testProject)).Return(
47+
[]apitypes.Container{testContainer("service1", "123"), testContainer("service1", "456"), testContainer("service2", "789")}, nil)
5248
api.EXPECT().ContainerKill(anyCancellableContext(), "123", "").Return(nil)
5349
api.EXPECT().ContainerKill(anyCancellableContext(), "456", "").Return(nil)
5450
api.EXPECT().ContainerKill(anyCancellableContext(), "789", "").Return(nil)
5551

56-
err := s.Kill(ctx, &project, compose.KillOptions{})
52+
err := tested.Kill(ctx, &project, compose.KillOptions{})
5753
assert.NilError(t, err)
5854
}
5955

6056
func TestKillSignal(t *testing.T) {
6157
mockCtrl := gomock.NewController(t)
6258
defer mockCtrl.Finish()
6359
api := mocks.NewMockAPIClient(mockCtrl)
64-
s.apiClient = api
60+
tested.apiClient = api
6561

66-
project := types.Project{Name: "myProject", Services: []types.ServiceConfig{testService("service1")}}
62+
project := types.Project{Name: testProject, Services: []types.ServiceConfig{testService("service1")}}
6763

6864
ctx := context.Background()
69-
api.EXPECT().ContainerList(ctx, projectListOpts).Return([]apitypes.Container{testContainer("service1", "123")}, nil)
65+
api.EXPECT().ContainerList(ctx, projectFilterListOpt(testProject)).Return([]apitypes.Container{testContainer("service1", "123")}, nil)
7066
api.EXPECT().ContainerKill(anyCancellableContext(), "123", "SIGTERM").Return(nil)
7167

72-
err := s.Kill(ctx, &project, compose.KillOptions{Signal: "SIGTERM"})
68+
err := tested.Kill(ctx, &project, compose.KillOptions{Signal: "SIGTERM"})
7369
assert.NilError(t, err)
7470
}
7571

@@ -81,7 +77,7 @@ func testContainer(service string, id string) apitypes.Container {
8177
return apitypes.Container{
8278
ID: id,
8379
Names: []string{id},
84-
Labels: map[string]string{compose.ServiceTag: service},
80+
Labels: map[string]string{serviceLabel: service, configFilesLabel: "testdata/docker-compose.yml", workingDirLabel: "testdata", projectLabel: testProject},
8581
}
8682
}
8783

local/compose/ps.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,8 @@ import (
3030

3131
func (s *composeService) Ps(ctx context.Context, projectName string, options compose.PsOptions) ([]compose.ContainerSummary, error) {
3232
containers, err := s.apiClient.ContainerList(ctx, moby.ContainerListOptions{
33-
Filters: filters.NewArgs(
34-
projectFilter(projectName),
35-
),
36-
All: options.All,
33+
Filters: filters.NewArgs(projectFilter(projectName)),
34+
All: options.All,
3735
})
3836
if err != nil {
3937
return nil, err
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
services:
2+
service1:
3+
image: nginx
4+
service2:
5+
image: mysql

0 commit comments

Comments
 (0)