|
| 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 TestPs(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 | + listOpts := apitypes.ContainerListOptions{Filters: filters.NewArgs(projectFilter(testProject)), All: false} |
| 41 | + c1, inspect1 := containerDetails("service1", "123", "Running", "healthy") |
| 42 | + c2, inspect2 := containerDetails("service1", "456", "Running", "") |
| 43 | + c2.Ports = []apitypes.Port{{PublicPort: 80, PrivatePort: 90, IP: "localhost"}} |
| 44 | + c3, inspect3 := containerDetails("service2", "789", "Running", "") |
| 45 | + api.EXPECT().ContainerList(ctx, listOpts).Return([]apitypes.Container{c1, c2, c3}, nil) |
| 46 | + api.EXPECT().ContainerInspect(anyCancellableContext(), "123").Return(inspect1, nil) |
| 47 | + api.EXPECT().ContainerInspect(anyCancellableContext(), "456").Return(inspect2, nil) |
| 48 | + api.EXPECT().ContainerInspect(anyCancellableContext(), "789").Return(inspect3, nil) |
| 49 | + |
| 50 | + containers, err := tested.Ps(ctx, testProject, compose.PsOptions{}) |
| 51 | + |
| 52 | + expected := []compose.ContainerSummary{ |
| 53 | + {ID: "123", Name: "123", Project: testProject, Service: "service1", State: "Running", Health: "healthy", Publishers: nil}, |
| 54 | + {ID: "456", Name: "456", Project: testProject, Service: "service1", State: "Running", Health: "", Publishers: []compose.PortPublisher{{URL: "localhost:80", TargetPort: 90, PublishedPort: 80}}}, |
| 55 | + {ID: "789", Name: "789", Project: testProject, Service: "service2", State: "Running", Health: "", Publishers: nil}, |
| 56 | + } |
| 57 | + assert.NilError(t, err) |
| 58 | + assert.DeepEqual(t, containers, expected) |
| 59 | +} |
| 60 | + |
| 61 | +func TestPsAll(t *testing.T) { |
| 62 | + mockCtrl := gomock.NewController(t) |
| 63 | + defer mockCtrl.Finish() |
| 64 | + api := mocks.NewMockAPIClient(mockCtrl) |
| 65 | + tested.apiClient = api |
| 66 | + |
| 67 | + ctx := context.Background() |
| 68 | + listOpts := apitypes.ContainerListOptions{Filters: filters.NewArgs(projectFilter(testProject)), All: true} |
| 69 | + c1, inspect1 := containerDetails("service1", "123", "Running", "healthy") |
| 70 | + c2, inspect2 := containerDetails("service1", "456", "Stopped", "") |
| 71 | + api.EXPECT().ContainerList(ctx, listOpts).Return([]apitypes.Container{c1, c2}, nil) |
| 72 | + api.EXPECT().ContainerInspect(anyCancellableContext(), "123").Return(inspect1, nil) |
| 73 | + api.EXPECT().ContainerInspect(anyCancellableContext(), "456").Return(inspect2, nil) |
| 74 | + |
| 75 | + containers, err := tested.Ps(ctx, testProject, compose.PsOptions{All: true}) |
| 76 | + |
| 77 | + expected := []compose.ContainerSummary{ |
| 78 | + {ID: "123", Name: "123", Project: testProject, Service: "service1", State: "Running", Health: "healthy", Publishers: nil}, |
| 79 | + {ID: "456", Name: "456", Project: testProject, Service: "service1", State: "Stopped", Health: "", Publishers: nil}, |
| 80 | + } |
| 81 | + assert.NilError(t, err) |
| 82 | + assert.DeepEqual(t, containers, expected) |
| 83 | +} |
| 84 | + |
| 85 | +func containerDetails(service string, id string, status string, health string) (apitypes.Container, apitypes.ContainerJSON) { |
| 86 | + container := apitypes.Container{ |
| 87 | + ID: id, |
| 88 | + Names: []string{"/" + id}, |
| 89 | + Labels: containerLabels(service), |
| 90 | + State: status, |
| 91 | + } |
| 92 | + inspect := apitypes.ContainerJSON{ContainerJSONBase: &apitypes.ContainerJSONBase{State: &apitypes.ContainerState{Status: status, Health: &apitypes.Health{Status: health}}}} |
| 93 | + return container, inspect |
| 94 | +} |
0 commit comments