|
| 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 | +} |
0 commit comments