mirror of
https://github.com/docker/compose.git
synced 2026-11-21 23:50:17 +00:00
internal/inspect and internal/containerops declared the same engine methods and one-off enum, and pkg/compose adapted them twice. Declare them in internal/engine, embed them in both Backend interfaces and keep a single adapter. Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
109 lines
3.6 KiB
Go
109 lines
3.6 KiB
Go
/*
|
|
Copyright 2020 Docker Compose CLI authors
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
you may not use this file except in compliance with the License.
|
|
You may obtain a copy of the License at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
See the License for the specific language governing permissions and
|
|
limitations under the License.
|
|
*/
|
|
|
|
package inspect
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"github.com/moby/moby/api/types/container"
|
|
"github.com/moby/moby/client"
|
|
"go.uber.org/mock/gomock"
|
|
"gotest.tools/v3/assert"
|
|
|
|
"github.com/docker/compose/v5/internal/engine"
|
|
"github.com/docker/compose/v5/pkg/api"
|
|
"github.com/docker/compose/v5/pkg/mocks"
|
|
)
|
|
|
|
// fakeBackend serves canned containers and records how it was asked for them.
|
|
type fakeBackend struct {
|
|
apiClient client.APIClient
|
|
listed []container.Summary
|
|
|
|
oneOff engine.OneOff
|
|
all bool
|
|
services []string
|
|
index int
|
|
}
|
|
|
|
func (f *fakeBackend) APIClient() client.APIClient { return f.apiClient }
|
|
func (f *fakeBackend) MaxConcurrency() int { return 2 }
|
|
|
|
func (f *fakeBackend) Containers(_ context.Context, _ string, oneOff engine.OneOff, all bool, services ...string) ([]container.Summary, error) {
|
|
f.oneOff, f.all, f.services = oneOff, all, services
|
|
return f.listed, nil
|
|
}
|
|
|
|
func (f *fakeBackend) SpecifiedContainer(_ context.Context, _ string, oneOff engine.OneOff, all bool, serviceName string, index int) (container.Summary, error) {
|
|
f.oneOff, f.all, f.services, f.index = oneOff, all, []string{serviceName}, index
|
|
return f.listed[0], nil
|
|
}
|
|
|
|
func summary(service, id string) container.Summary {
|
|
return container.Summary{
|
|
ID: id,
|
|
Names: []string{"/" + id},
|
|
Labels: map[string]string{api.ServiceLabel: service},
|
|
}
|
|
}
|
|
|
|
func TestPsOneOffContainersOnlyWithAll(t *testing.T) {
|
|
for _, all := range []bool{false, true} {
|
|
ctrl := gomock.NewController(t)
|
|
apiClient := mocks.NewMockAPIClient(ctrl)
|
|
apiClient.EXPECT().ContainerInspect(gomock.Any(), "web1", gomock.Any()).Return(client.ContainerInspectResult{}, nil)
|
|
backend := &fakeBackend{apiClient: apiClient, listed: []container.Summary{summary("web", "web1")}}
|
|
|
|
got, err := New(backend).Ps(t.Context(), "proj", api.PsOptions{All: all})
|
|
|
|
assert.NilError(t, err)
|
|
assert.Equal(t, len(got), 1)
|
|
assert.Equal(t, got[0].Service, "web")
|
|
assert.Equal(t, backend.all, all)
|
|
if all {
|
|
assert.Equal(t, backend.oneOff, engine.OneOffInclude)
|
|
} else {
|
|
assert.Equal(t, backend.oneOff, engine.OneOffExclude)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestPsKeepsTheRequestedServices(t *testing.T) {
|
|
ctrl := gomock.NewController(t)
|
|
apiClient := mocks.NewMockAPIClient(ctrl)
|
|
apiClient.EXPECT().ContainerInspect(gomock.Any(), "db1", gomock.Any()).Return(client.ContainerInspectResult{}, nil)
|
|
backend := &fakeBackend{apiClient: apiClient, listed: []container.Summary{summary("web", "web1"), summary("db", "db1")}}
|
|
|
|
got, err := New(backend).Ps(t.Context(), "proj", api.PsOptions{Services: []string{"db"}})
|
|
|
|
assert.NilError(t, err)
|
|
assert.Equal(t, len(got), 1)
|
|
assert.Equal(t, got[0].ID, "db1")
|
|
}
|
|
|
|
func TestPortsAsksForTheReplica(t *testing.T) {
|
|
backend := &fakeBackend{listed: []container.Summary{summary("web", "web2")}}
|
|
|
|
_, err := New(backend).Ports(t.Context(), "proj", "web", 0, api.PortOptions{Index: 2})
|
|
|
|
assert.NilError(t, err)
|
|
assert.DeepEqual(t, backend.services, []string{"web"})
|
|
assert.Equal(t, backend.index, 2)
|
|
assert.Equal(t, backend.oneOff, engine.OneOffInclude)
|
|
assert.Equal(t, backend.all, false)
|
|
}
|