Merge pull request #2025 from ulyssessouza/tolower-projectname-on-down

Lowercase project name for "compose down"
This commit is contained in:
Lorena Rangel 2021-08-19 11:31:58 +02:00 committed by GitHub
commit 9c615dc22e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 30 additions and 20 deletions

View File

@ -37,7 +37,7 @@ type downOp func() error
func (s *composeService) Down(ctx context.Context, projectName string, options api.DownOptions) error { func (s *composeService) Down(ctx context.Context, projectName string, options api.DownOptions) error {
return progress.Run(ctx, func(ctx context.Context) error { return progress.Run(ctx, func(ctx context.Context) error {
return s.down(ctx, projectName, options) return s.down(ctx, strings.ToLower(projectName), options)
}) })
} }

View File

@ -18,6 +18,7 @@ package compose
import ( import (
"context" "context"
"strings"
"testing" "testing"
compose "github.com/docker/compose-cli/pkg/api" compose "github.com/docker/compose-cli/pkg/api"
@ -47,11 +48,12 @@ func TestDown(t *testing.T) {
api.EXPECT().ContainerRemove(gomock.Any(), "456", moby.ContainerRemoveOptions{Force: true}).Return(nil) api.EXPECT().ContainerRemove(gomock.Any(), "456", moby.ContainerRemoveOptions{Force: true}).Return(nil)
api.EXPECT().ContainerRemove(gomock.Any(), "789", moby.ContainerRemoveOptions{Force: true}).Return(nil) api.EXPECT().ContainerRemove(gomock.Any(), "789", moby.ContainerRemoveOptions{Force: true}).Return(nil)
api.EXPECT().NetworkList(gomock.Any(), moby.NetworkListOptions{Filters: filters.NewArgs(projectFilter(testProject))}).Return([]moby.NetworkResource{{ID: "myProject_default"}}, nil) api.EXPECT().NetworkList(gomock.Any(), moby.NetworkListOptions{Filters: filters.NewArgs(projectFilter(strings.ToLower(testProject)))}).Return([]moby.NetworkResource{{ID: "myProject_default"}},
nil)
api.EXPECT().NetworkRemove(gomock.Any(), "myProject_default").Return(nil) api.EXPECT().NetworkRemove(gomock.Any(), "myProject_default").Return(nil)
err := tested.Down(context.Background(), testProject, compose.DownOptions{}) err := tested.Down(context.Background(), strings.ToLower(testProject), compose.DownOptions{})
assert.NilError(t, err) assert.NilError(t, err)
} }
@ -72,11 +74,12 @@ func TestDownRemoveOrphans(t *testing.T) {
api.EXPECT().ContainerRemove(gomock.Any(), "789", moby.ContainerRemoveOptions{Force: true}).Return(nil) api.EXPECT().ContainerRemove(gomock.Any(), "789", moby.ContainerRemoveOptions{Force: true}).Return(nil)
api.EXPECT().ContainerRemove(gomock.Any(), "321", moby.ContainerRemoveOptions{Force: true}).Return(nil) api.EXPECT().ContainerRemove(gomock.Any(), "321", moby.ContainerRemoveOptions{Force: true}).Return(nil)
api.EXPECT().NetworkList(gomock.Any(), moby.NetworkListOptions{Filters: filters.NewArgs(projectFilter(testProject))}).Return([]moby.NetworkResource{{ID: "myProject_default"}}, nil) api.EXPECT().NetworkList(gomock.Any(), moby.NetworkListOptions{Filters: filters.NewArgs(projectFilter(strings.ToLower(testProject)))}).Return([]moby.NetworkResource{{ID: "myProject_default"}},
nil)
api.EXPECT().NetworkRemove(gomock.Any(), "myProject_default").Return(nil) api.EXPECT().NetworkRemove(gomock.Any(), "myProject_default").Return(nil)
err := tested.Down(context.Background(), testProject, compose.DownOptions{RemoveOrphans: true}) err := tested.Down(context.Background(), strings.ToLower(testProject), compose.DownOptions{RemoveOrphans: true})
assert.NilError(t, err) assert.NilError(t, err)
} }
@ -92,11 +95,11 @@ func TestDownRemoveVolumes(t *testing.T) {
api.EXPECT().ContainerStop(gomock.Any(), "123", nil).Return(nil) api.EXPECT().ContainerStop(gomock.Any(), "123", nil).Return(nil)
api.EXPECT().ContainerRemove(gomock.Any(), "123", moby.ContainerRemoveOptions{Force: true, RemoveVolumes: true}).Return(nil) api.EXPECT().ContainerRemove(gomock.Any(), "123", moby.ContainerRemoveOptions{Force: true, RemoveVolumes: true}).Return(nil)
api.EXPECT().NetworkList(gomock.Any(), moby.NetworkListOptions{Filters: filters.NewArgs(projectFilter(testProject))}).Return(nil, nil) api.EXPECT().NetworkList(gomock.Any(), moby.NetworkListOptions{Filters: filters.NewArgs(projectFilter(strings.ToLower(testProject)))}).Return(nil, nil)
api.EXPECT().VolumeList(gomock.Any(), filters.NewArgs(projectFilter(testProject))).Return(volume.VolumeListOKBody{Volumes: []*moby.Volume{{Name: "myProject_volume"}}}, nil) api.EXPECT().VolumeList(gomock.Any(), filters.NewArgs(projectFilter(strings.ToLower(testProject)))).Return(volume.VolumeListOKBody{Volumes: []*moby.Volume{{Name: "myProject_volume"}}}, nil)
api.EXPECT().VolumeRemove(gomock.Any(), "myProject_volume", true).Return(nil) api.EXPECT().VolumeRemove(gomock.Any(), "myProject_volume", true).Return(nil)
err := tested.Down(context.Background(), testProject, compose.DownOptions{Volumes: true}) err := tested.Down(context.Background(), strings.ToLower(testProject), compose.DownOptions{Volumes: true})
assert.NilError(t, err) assert.NilError(t, err)
} }

View File

@ -19,6 +19,7 @@ package compose
import ( import (
"context" "context"
"path/filepath" "path/filepath"
"strings"
"testing" "testing"
"github.com/compose-spec/compose-go/types" "github.com/compose-spec/compose-go/types"
@ -41,7 +42,7 @@ func TestKillAll(t *testing.T) {
api := mocks.NewMockAPIClient(mockCtrl) api := mocks.NewMockAPIClient(mockCtrl)
tested.apiClient = api tested.apiClient = api
project := types.Project{Name: testProject, Services: []types.ServiceConfig{testService("service1"), testService("service2")}} project := types.Project{Name: strings.ToLower(testProject), Services: []types.ServiceConfig{testService("service1"), testService("service2")}}
ctx := context.Background() ctx := context.Background()
api.EXPECT().ContainerList(ctx, projectFilterListOpt()).Return( api.EXPECT().ContainerList(ctx, projectFilterListOpt()).Return(
@ -60,7 +61,7 @@ func TestKillSignal(t *testing.T) {
api := mocks.NewMockAPIClient(mockCtrl) api := mocks.NewMockAPIClient(mockCtrl)
tested.apiClient = api tested.apiClient = api
project := types.Project{Name: testProject, Services: []types.ServiceConfig{testService("service1")}} project := types.Project{Name: strings.ToLower(testProject), Services: []types.ServiceConfig{testService("service1")}}
ctx := context.Background() ctx := context.Background()
api.EXPECT().ContainerList(ctx, projectFilterListOpt()).Return([]moby.Container{testContainer("service1", "123")}, nil) api.EXPECT().ContainerList(ctx, projectFilterListOpt()).Return([]moby.Container{testContainer("service1", "123")}, nil)
@ -89,7 +90,7 @@ func containerLabels(service string) map[string]string {
compose.ServiceLabel: service, compose.ServiceLabel: service,
compose.ConfigFilesLabel: composefile, compose.ConfigFilesLabel: composefile,
compose.WorkingDirLabel: workingdir, compose.WorkingDirLabel: workingdir,
compose.ProjectLabel: testProject} compose.ProjectLabel: strings.ToLower(testProject)}
} }
func anyCancellableContext() gomock.Matcher { func anyCancellableContext() gomock.Matcher {
@ -100,7 +101,7 @@ func anyCancellableContext() gomock.Matcher {
func projectFilterListOpt() moby.ContainerListOptions { func projectFilterListOpt() moby.ContainerListOptions {
return moby.ContainerListOptions{ return moby.ContainerListOptions{
Filters: filters.NewArgs(projectFilter(testProject)), Filters: filters.NewArgs(projectFilter(strings.ToLower(testProject))),
All: true, All: true,
} }
} }

View File

@ -18,6 +18,7 @@ package compose
import ( import (
"context" "context"
"strings"
"testing" "testing"
"github.com/golang/mock/gomock" "github.com/golang/mock/gomock"
@ -37,7 +38,7 @@ func TestPs(t *testing.T) {
tested.apiClient = api tested.apiClient = api
ctx := context.Background() ctx := context.Background()
args := filters.NewArgs(projectFilter(testProject)) args := filters.NewArgs(projectFilter(strings.ToLower(testProject)))
args.Add("label", "com.docker.compose.oneoff=False") args.Add("label", "com.docker.compose.oneoff=False")
listOpts := moby.ContainerListOptions{Filters: args, All: true} listOpts := moby.ContainerListOptions{Filters: args, All: true}
c1, inspect1 := containerDetails("service1", "123", "running", "healthy", 0) c1, inspect1 := containerDetails("service1", "123", "running", "healthy", 0)
@ -49,12 +50,13 @@ func TestPs(t *testing.T) {
api.EXPECT().ContainerInspect(anyCancellableContext(), "456").Return(inspect2, nil) api.EXPECT().ContainerInspect(anyCancellableContext(), "456").Return(inspect2, nil)
api.EXPECT().ContainerInspect(anyCancellableContext(), "789").Return(inspect3, nil) api.EXPECT().ContainerInspect(anyCancellableContext(), "789").Return(inspect3, nil)
containers, err := tested.Ps(ctx, testProject, compose.PsOptions{}) containers, err := tested.Ps(ctx, strings.ToLower(testProject), compose.PsOptions{})
expected := []compose.ContainerSummary{ expected := []compose.ContainerSummary{
{ID: "123", Name: "123", Project: testProject, Service: "service1", State: "running", Health: "healthy", Publishers: nil}, {ID: "123", Name: "123", Project: strings.ToLower(testProject), Service: "service1", State: "running", Health: "healthy", Publishers: nil},
{ID: "456", Name: "456", Project: testProject, Service: "service1", State: "running", Health: "", Publishers: []compose.PortPublisher{{URL: "localhost:80", TargetPort: 90, PublishedPort: 80}}}, {ID: "456", Name: "456", Project: strings.ToLower(testProject), Service: "service1", State: "running", Health: "", Publishers: []compose.PortPublisher{{URL: "localhost:80", TargetPort: 90,
{ID: "789", Name: "789", Project: testProject, Service: "service2", State: "exited", Health: "", ExitCode: 130, Publishers: nil}, PublishedPort: 80}}},
{ID: "789", Name: "789", Project: strings.ToLower(testProject), Service: "service2", State: "exited", Health: "", ExitCode: 130, Publishers: nil},
} }
assert.NilError(t, err) assert.NilError(t, err)
assert.DeepEqual(t, containers, expected) assert.DeepEqual(t, containers, expected)

View File

@ -18,6 +18,7 @@ package compose
import ( import (
"context" "context"
"strings"
"testing" "testing"
"time" "time"
@ -50,7 +51,7 @@ func TestStopTimeout(t *testing.T) {
api.EXPECT().ContainerStop(gomock.Any(), "789", &timeout).Return(nil) api.EXPECT().ContainerStop(gomock.Any(), "789", &timeout).Return(nil)
err := tested.Stop(ctx, &types.Project{ err := tested.Stop(ctx, &types.Project{
Name: testProject, Name: strings.ToLower(testProject),
Services: []types.ServiceConfig{ Services: []types.ServiceConfig{
{Name: "service1"}, {Name: "service1"},
{Name: "service2"}, {Name: "service2"},

View File

@ -62,7 +62,10 @@ func TestLocalComposeUp(t *testing.T) {
t.Run("top", func(t *testing.T) { t.Run("top", func(t *testing.T) {
res := c.RunDockerCmd("compose", "-p", projectName, "top") res := c.RunDockerCmd("compose", "-p", projectName, "top")
output := res.Stdout() output := res.Stdout()
assert.Assert(t, strings.Contains(output, `UID PID PPID C STIME TTY TIME CMD`), output) head := []string{"UID", "PID", "PPID", "C", "STIME", "TTY", "TIME", "CMD"}
for _, h := range head {
assert.Assert(t, strings.Contains(output, h), output)
}
assert.Assert(t, strings.Contains(output, `java -Xmx8m -Xms8m -jar /app/words.jar`), output) assert.Assert(t, strings.Contains(output, `java -Xmx8m -Xms8m -jar /app/words.jar`), output)
assert.Assert(t, strings.Contains(output, `/dispatcher`), output) assert.Assert(t, strings.Contains(output, `/dispatcher`), output)
}) })
@ -139,7 +142,7 @@ func TestDownComposefileInParentFolder(t *testing.T) {
tmpFolder, err := ioutil.TempDir("fixtures/simple-composefile", "test-tmp") tmpFolder, err := ioutil.TempDir("fixtures/simple-composefile", "test-tmp")
assert.NilError(t, err) assert.NilError(t, err)
defer os.Remove(tmpFolder) //nolint: errcheck defer os.Remove(tmpFolder) // nolint: errcheck
projectName := filepath.Base(tmpFolder) projectName := filepath.Base(tmpFolder)
res := c.RunDockerCmd("compose", "--project-directory", tmpFolder, "up", "-d") res := c.RunDockerCmd("compose", "--project-directory", tmpFolder, "up", "-d")