|
| 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 | + "testing" |
| 21 | + |
| 22 | + "github.com/compose-spec/compose-go/v2/types" |
| 23 | + "github.com/moby/moby/api/types/container" |
| 24 | + "github.com/moby/moby/api/types/network" |
| 25 | + "github.com/moby/moby/api/types/volume" |
| 26 | + "github.com/moby/moby/client" |
| 27 | + "go.uber.org/mock/gomock" |
| 28 | + "gotest.tools/v3/assert" |
| 29 | + |
| 30 | + "github.com/docker/compose/v5/pkg/api" |
| 31 | + "github.com/docker/compose/v5/pkg/mocks" |
| 32 | +) |
| 33 | + |
| 34 | +func TestToObservedContainer(t *testing.T) { |
| 35 | + c := container.Summary{ |
| 36 | + ID: "abc123", |
| 37 | + Names: []string{"/testProject-web-1"}, |
| 38 | + State: container.StateRunning, |
| 39 | + Labels: map[string]string{ |
| 40 | + api.ServiceLabel: "web", |
| 41 | + api.ConfigHashLabel: "sha256:aaa", |
| 42 | + api.ImageDigestLabel: "sha256:bbb", |
| 43 | + api.ContainerNumberLabel: "1", |
| 44 | + api.ProjectLabel: "testproject", |
| 45 | + }, |
| 46 | + NetworkSettings: &container.NetworkSettingsSummary{ |
| 47 | + Networks: map[string]*network.EndpointSettings{ |
| 48 | + "mynet": {NetworkID: "net123"}, |
| 49 | + }, |
| 50 | + }, |
| 51 | + } |
| 52 | + |
| 53 | + oc := toObservedContainer(c) |
| 54 | + |
| 55 | + assert.Equal(t, oc.ID, "abc123") |
| 56 | + assert.Equal(t, oc.Name, "testProject-web-1") |
| 57 | + assert.Equal(t, oc.State, container.StateRunning) |
| 58 | + assert.Equal(t, oc.ConfigHash, "sha256:aaa") |
| 59 | + assert.Equal(t, oc.ImageDigest, "sha256:bbb") |
| 60 | + assert.Equal(t, oc.Number, 1) |
| 61 | + assert.Equal(t, oc.ConnectedNetworks["mynet"], "net123") |
| 62 | + assert.Equal(t, oc.Summary.ID, "abc123") |
| 63 | +} |
| 64 | + |
| 65 | +func TestToObservedContainerNoNetworkSettings(t *testing.T) { |
| 66 | + c := container.Summary{ |
| 67 | + ID: "def456", |
| 68 | + Names: []string{"/testProject-db-1"}, |
| 69 | + State: container.StateExited, |
| 70 | + Labels: map[string]string{}, |
| 71 | + } |
| 72 | + |
| 73 | + oc := toObservedContainer(c) |
| 74 | + |
| 75 | + assert.Equal(t, oc.ID, "def456") |
| 76 | + assert.Equal(t, oc.Number, 0) |
| 77 | + assert.Equal(t, oc.ConfigHash, "") |
| 78 | + assert.Equal(t, oc.ImageDigest, "") |
| 79 | + assert.Equal(t, len(oc.ConnectedNetworks), 0) |
| 80 | +} |
| 81 | + |
| 82 | +func TestCollectObservedState(t *testing.T) { |
| 83 | + mockCtrl := gomock.NewController(t) |
| 84 | + |
| 85 | + apiClient := mocks.NewMockAPIClient(mockCtrl) |
| 86 | + cli := mocks.NewMockCli(mockCtrl) |
| 87 | + tested, err := NewComposeService(cli) |
| 88 | + assert.NilError(t, err) |
| 89 | + cli.EXPECT().Client().Return(apiClient).AnyTimes() |
| 90 | + |
| 91 | + project := &types.Project{ |
| 92 | + Name: "myproject", |
| 93 | + Services: types.Services{ |
| 94 | + "web": {Name: "web"}, |
| 95 | + "db": {Name: "db"}, |
| 96 | + }, |
| 97 | + Networks: types.Networks{ |
| 98 | + "frontend": {Name: "myproject_frontend"}, |
| 99 | + }, |
| 100 | + Volumes: types.Volumes{ |
| 101 | + "data": {Name: "myproject_data"}, |
| 102 | + }, |
| 103 | + } |
| 104 | + |
| 105 | + // Mock ContainerList |
| 106 | + apiClient.EXPECT().ContainerList(gomock.Any(), gomock.Any()).Return(client.ContainerListResult{ |
| 107 | + Items: []container.Summary{ |
| 108 | + { |
| 109 | + ID: "c1", |
| 110 | + Names: []string{"/myproject-web-1"}, |
| 111 | + State: container.StateRunning, |
| 112 | + Labels: map[string]string{ |
| 113 | + api.ServiceLabel: "web", |
| 114 | + api.ProjectLabel: "myproject", |
| 115 | + api.ConfigHashLabel: "hash1", |
| 116 | + api.ContainerNumberLabel: "1", |
| 117 | + api.OneoffLabel: "False", |
| 118 | + }, |
| 119 | + }, |
| 120 | + { |
| 121 | + ID: "c2", |
| 122 | + Names: []string{"/myproject-db-1"}, |
| 123 | + State: container.StateRunning, |
| 124 | + Labels: map[string]string{ |
| 125 | + api.ServiceLabel: "db", |
| 126 | + api.ProjectLabel: "myproject", |
| 127 | + api.ConfigHashLabel: "hash2", |
| 128 | + api.ContainerNumberLabel: "1", |
| 129 | + api.OneoffLabel: "False", |
| 130 | + }, |
| 131 | + }, |
| 132 | + { |
| 133 | + ID: "c3", |
| 134 | + Names: []string{"/myproject-old-1"}, |
| 135 | + State: container.StateExited, |
| 136 | + Labels: map[string]string{ |
| 137 | + api.ServiceLabel: "old", |
| 138 | + api.ProjectLabel: "myproject", |
| 139 | + api.ConfigHashLabel: "hash3", |
| 140 | + api.ContainerNumberLabel: "1", |
| 141 | + api.OneoffLabel: "False", |
| 142 | + }, |
| 143 | + }, |
| 144 | + }, |
| 145 | + }, nil) |
| 146 | + |
| 147 | + // Mock NetworkList |
| 148 | + apiClient.EXPECT().NetworkList(gomock.Any(), gomock.Any()).Return(client.NetworkListResult{ |
| 149 | + Items: []network.Summary{ |
| 150 | + {Network: network.Network{ |
| 151 | + ID: "net1", |
| 152 | + Name: "myproject_frontend", |
| 153 | + Labels: map[string]string{ |
| 154 | + api.NetworkLabel: "frontend", |
| 155 | + api.ProjectLabel: "myproject", |
| 156 | + api.ConfigHashLabel: "nethash1", |
| 157 | + }, |
| 158 | + }}, |
| 159 | + }, |
| 160 | + }, nil) |
| 161 | + |
| 162 | + // Mock VolumeList |
| 163 | + apiClient.EXPECT().VolumeList(gomock.Any(), gomock.Any()).Return(client.VolumeListResult{ |
| 164 | + Items: []volume.Volume{ |
| 165 | + { |
| 166 | + Name: "myproject_data", |
| 167 | + Driver: "local", |
| 168 | + Labels: map[string]string{ |
| 169 | + api.VolumeLabel: "data", |
| 170 | + api.ProjectLabel: "myproject", |
| 171 | + api.ConfigHashLabel: "volhash1", |
| 172 | + }, |
| 173 | + }, |
| 174 | + }, |
| 175 | + }, nil) |
| 176 | + |
| 177 | + state, err := tested.(*composeService).collectObservedState(t.Context(), project) |
| 178 | + assert.NilError(t, err) |
| 179 | + |
| 180 | + // Containers classified by service |
| 181 | + assert.Equal(t, len(state.Containers["web"]), 1) |
| 182 | + assert.Equal(t, state.Containers["web"][0].ID, "c1") |
| 183 | + assert.Equal(t, len(state.Containers["db"]), 1) |
| 184 | + assert.Equal(t, state.Containers["db"][0].ID, "c2") |
| 185 | + |
| 186 | + // Orphan container (service "old" not in project) |
| 187 | + assert.Equal(t, len(state.Orphans), 1) |
| 188 | + assert.Equal(t, state.Orphans[0].ID, "c3") |
| 189 | + |
| 190 | + // Networks |
| 191 | + assert.Equal(t, len(state.Networks), 1) |
| 192 | + nw := state.Networks["frontend"] |
| 193 | + assert.Equal(t, nw.ID, "net1") |
| 194 | + assert.Equal(t, nw.Name, "myproject_frontend") |
| 195 | + assert.Equal(t, nw.ConfigHash, "nethash1") |
| 196 | + |
| 197 | + // Volumes |
| 198 | + assert.Equal(t, len(state.Volumes), 1) |
| 199 | + vol := state.Volumes["data"] |
| 200 | + assert.Equal(t, vol.Name, "myproject_data") |
| 201 | + assert.Equal(t, vol.Driver, "local") |
| 202 | + assert.Equal(t, vol.ConfigHash, "volhash1") |
| 203 | +} |
0 commit comments