From 1f5a77e67cd36122bd7c0d39711615e94c8ce01b Mon Sep 17 00:00:00 2001 From: Nicolas De Loof Date: Tue, 8 Dec 2020 12:24:34 +0100 Subject: [PATCH] move test to command-specific test file Signed-off-by: Nicolas De Loof --- local/compose/create_test.go | 40 ++++++++++++++++++++++ local/compose/ls_test.go | 65 ++++++++++++++++++++++++++++++++++++ local/compose/ps.go | 26 --------------- 3 files changed, 105 insertions(+), 26 deletions(-) diff --git a/local/compose/create_test.go b/local/compose/create_test.go index cfbe186c0..2ae4254e6 100644 --- a/local/compose/create_test.go +++ b/local/compose/create_test.go @@ -1 +1,41 @@ +/* + 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 compose + +import ( + "os" + "path/filepath" + "testing" + + composetypes "github.com/compose-spec/compose-go/types" + mountTypes "github.com/docker/docker/api/types/mount" + "gotest.tools/v3/assert" +) + +func TestBuildBindMount(t *testing.T) { + volume := composetypes.ServiceVolumeConfig{ + Type: composetypes.VolumeTypeBind, + Source: "e2e/volume-test", + Target: "/data", + } + mount, err := buildMount(volume) + assert.NilError(t, err) + assert.Assert(t, filepath.IsAbs(mount.Source)) + _, err = os.Stat(mount.Source) + assert.NilError(t, err) + assert.Equal(t, mount.Type, mountTypes.TypeBind) +} diff --git a/local/compose/ls_test.go b/local/compose/ls_test.go index cfbe186c0..9d9ea9e95 100644 --- a/local/compose/ls_test.go +++ b/local/compose/ls_test.go @@ -1 +1,66 @@ +/* + 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 compose + +import ( + "testing" + + "github.com/docker/compose-cli/api/compose" + + moby "github.com/docker/docker/api/types" + "gotest.tools/v3/assert" +) + +func TestContainersToStacks(t *testing.T) { + containers := []moby.Container{ + { + ID: "service1", + State: "running", + Labels: map[string]string{projectLabel: "project1"}, + }, + { + ID: "service2", + State: "running", + Labels: map[string]string{projectLabel: "project1"}, + }, + { + ID: "service3", + State: "running", + Labels: map[string]string{projectLabel: "project2"}, + }, + } + stacks, err := containersToStacks(containers) + assert.NilError(t, err) + assert.DeepEqual(t, stacks, []compose.Stack{ + { + ID: "project1", + Name: "project1", + Status: "running(2)", + }, + { + ID: "project2", + Name: "project2", + Status: "running(1)", + }, + }) +} + +func TestStacksMixedStatus(t *testing.T) { + assert.Equal(t, combinedStatus([]string{"running"}), "running(1)") + assert.Equal(t, combinedStatus([]string{"running", "running", "running"}), "running(3)") + assert.Equal(t, combinedStatus([]string{"running", "exited", "running"}), "exited(1), running(2)") +} diff --git a/local/compose/ps.go b/local/compose/ps.go index dafacda23..7c6ca1b5e 100644 --- a/local/compose/ps.go +++ b/local/compose/ps.go @@ -21,8 +21,6 @@ import ( "fmt" "sort" - convert "github.com/docker/compose-cli/local/moby" - "github.com/docker/compose-cli/api/compose" moby "github.com/docker/docker/api/types" "github.com/docker/docker/api/types/filters" @@ -66,30 +64,6 @@ func (s *composeService) Ps(ctx context.Context, projectName string) ([]compose. return summary, nil } -func containersToServiceStatus(containers []moby.Container) ([]compose.ServiceStatus, error) { - containersByLabel, keys, err := groupContainerByLabel(containers, serviceLabel) - if err != nil { - return nil, err - } - var services []compose.ServiceStatus - for _, service := range keys { - containers := containersByLabel[service] - runnningContainers := []moby.Container{} - for _, container := range containers { - if container.State == convert.ContainerRunning { - runnningContainers = append(runnningContainers, container) - } - } - services = append(services, compose.ServiceStatus{ - ID: service, - Name: service, - Desired: len(containers), - Replicas: len(runnningContainers), - }) - } - return services, nil -} - func groupContainerByLabel(containers []moby.Container, labelName string) (map[string][]moby.Container, []string, error) { containersByLabel := map[string][]moby.Container{} keys := []string{}