|
| 1 | +/* |
| 2 | + Copyright 2024 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 | + "strings" |
| 22 | + "testing" |
| 23 | + |
| 24 | + containerType "github.com/docker/docker/api/types/container" |
| 25 | + "go.uber.org/mock/gomock" |
| 26 | + "gotest.tools/v3/assert" |
| 27 | + |
| 28 | + compose "github.com/docker/compose/v2/pkg/api" |
| 29 | + moby "github.com/docker/docker/api/types" |
| 30 | + "github.com/docker/docker/api/types/filters" |
| 31 | +) |
| 32 | + |
| 33 | +func TestImages(t *testing.T) { |
| 34 | + mockCtrl := gomock.NewController(t) |
| 35 | + defer mockCtrl.Finish() |
| 36 | + |
| 37 | + api, cli := prepareMocks(mockCtrl) |
| 38 | + tested := composeService{ |
| 39 | + dockerCli: cli, |
| 40 | + } |
| 41 | + |
| 42 | + ctx := context.Background() |
| 43 | + args := filters.NewArgs(projectFilter(strings.ToLower(testProject))) |
| 44 | + listOpts := containerType.ListOptions{All: true, Filters: args} |
| 45 | + image1 := imageInspect("image1", "foo:1", 12345) |
| 46 | + image2 := imageInspect("image2", "bar:2", 67890) |
| 47 | + api.EXPECT().ImageInspectWithRaw(anyCancellableContext(), "foo:1").Return(image1, nil, nil) |
| 48 | + api.EXPECT().ImageInspectWithRaw(anyCancellableContext(), "bar:2").Return(image2, nil, nil) |
| 49 | + c1 := containerDetail("service1", "123", "running", "foo:1") |
| 50 | + c2 := containerDetail("service1", "456", "running", "bar:2") |
| 51 | + c2.Ports = []moby.Port{{PublicPort: 80, PrivatePort: 90, IP: "localhost"}} |
| 52 | + c3 := containerDetail("service2", "789", "exited", "foo:1") |
| 53 | + api.EXPECT().ContainerList(ctx, listOpts).Return([]moby.Container{c1, c2, c3}, nil) |
| 54 | + |
| 55 | + images, err := tested.Images(ctx, strings.ToLower(testProject), compose.ImagesOptions{}) |
| 56 | + |
| 57 | + expected := []compose.ImageSummary{ |
| 58 | + { |
| 59 | + ID: "image1", |
| 60 | + ContainerName: "123", |
| 61 | + Repository: "foo", |
| 62 | + Tag: "1", |
| 63 | + Size: 12345, |
| 64 | + }, |
| 65 | + { |
| 66 | + ID: "image2", |
| 67 | + ContainerName: "456", |
| 68 | + Repository: "bar", |
| 69 | + Tag: "2", |
| 70 | + Size: 67890, |
| 71 | + }, |
| 72 | + { |
| 73 | + ID: "image1", |
| 74 | + ContainerName: "789", |
| 75 | + Repository: "foo", |
| 76 | + Tag: "1", |
| 77 | + Size: 12345, |
| 78 | + }, |
| 79 | + } |
| 80 | + assert.NilError(t, err) |
| 81 | + assert.DeepEqual(t, images, expected) |
| 82 | +} |
| 83 | + |
| 84 | +func imageInspect(id string, image string, size int64) moby.ImageInspect { |
| 85 | + return moby.ImageInspect{ |
| 86 | + ID: id, |
| 87 | + RepoTags: []string{ |
| 88 | + "someRepo:someTag", |
| 89 | + image, |
| 90 | + }, |
| 91 | + Size: size, |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +func containerDetail(service string, id string, status string, image string) moby.Container { |
| 96 | + return moby.Container{ |
| 97 | + ID: id, |
| 98 | + Names: []string{"/" + id}, |
| 99 | + Image: image, |
| 100 | + Labels: containerLabels(service, false), |
| 101 | + State: status, |
| 102 | + } |
| 103 | +} |
0 commit comments