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

Commit b4fa7b2

Browse files
committed
PS unit test
Signed-off-by: Guillaume Tardif <[email protected]>
1 parent 4140196 commit b4fa7b2

File tree

4 files changed

+119
-22
lines changed

4 files changed

+119
-22
lines changed

local/compose/kill_test.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,14 @@ func testContainer(service string, id string) apitypes.Container {
7777
return apitypes.Container{
7878
ID: id,
7979
Names: []string{id},
80-
Labels: map[string]string{serviceLabel: service, configFilesLabel: "testdata/docker-compose.yml", workingDirLabel: "testdata", projectLabel: testProject},
80+
Labels: containerLabels(service),
8181
}
8282
}
8383

84+
func containerLabels(service string) map[string]string {
85+
return map[string]string{serviceLabel: service, configFilesLabel: "testdata/docker-compose.yml", workingDirLabel: "testdata", projectLabel: testProject}
86+
}
87+
8488
func anyCancellableContext() gomock.Matcher {
8589
ctxWithCancel, cancel := context.WithCancel(context.Background())
8690
cancel()

local/compose/ls.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,23 @@ func combinedStatus(statuses []string) string {
8484
}
8585
return result
8686
}
87+
88+
func groupContainerByLabel(containers []moby.Container, labelName string) (map[string][]moby.Container, []string, error) {
89+
containersByLabel := map[string][]moby.Container{}
90+
keys := []string{}
91+
for _, c := range containers {
92+
label, ok := c.Labels[labelName]
93+
if !ok {
94+
return nil, nil, fmt.Errorf("No label %q set on container %q of compose project", labelName, c.ID)
95+
}
96+
labelContainers, ok := containersByLabel[label]
97+
if !ok {
98+
labelContainers = []moby.Container{}
99+
keys = append(keys, label)
100+
}
101+
labelContainers = append(labelContainers, c)
102+
containersByLabel[label] = labelContainers
103+
}
104+
sort.Strings(keys)
105+
return containersByLabel, keys, nil
106+
}

local/compose/ps.go

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ package compose
1919
import (
2020
"context"
2121
"fmt"
22-
"sort"
2322

2423
moby "github.com/docker/docker/api/types"
2524
"github.com/docker/docker/api/types/filters"
@@ -81,23 +80,3 @@ func (s *composeService) Ps(ctx context.Context, projectName string, options com
8180
}
8281
return summary, eg.Wait()
8382
}
84-
85-
func groupContainerByLabel(containers []moby.Container, labelName string) (map[string][]moby.Container, []string, error) {
86-
containersByLabel := map[string][]moby.Container{}
87-
keys := []string{}
88-
for _, c := range containers {
89-
label, ok := c.Labels[labelName]
90-
if !ok {
91-
return nil, nil, fmt.Errorf("No label %q set on container %q of compose project", labelName, c.ID)
92-
}
93-
labelContainers, ok := containersByLabel[label]
94-
if !ok {
95-
labelContainers = []moby.Container{}
96-
keys = append(keys, label)
97-
}
98-
labelContainers = append(labelContainers, c)
99-
containersByLabel[label] = labelContainers
100-
}
101-
sort.Strings(keys)
102-
return containersByLabel, keys, nil
103-
}

local/compose/ps_test.go

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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

Comments
 (0)