Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

check service names based on project, not running containers #10111

Merged
merged 1 commit into from Dec 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 10 additions & 10 deletions cmd/compose/ps.go
Expand Up @@ -96,6 +96,16 @@ func runPs(ctx context.Context, streams api.Streams, backend api.Service, servic
if err != nil {
return err
}

if project != nil && len(services) > 0 {
names := project.ServiceNames()
for _, service := range services {
if !utils.StringContains(names, service) {
return fmt.Errorf("no such service: %s", service)
}
}
}

containers, err := backend.Ps(ctx, name, api.PsOptions{
Project: project,
All: opts.All,
Expand All @@ -105,16 +115,6 @@ func runPs(ctx context.Context, streams api.Streams, backend api.Service, servic
return err
}

SERVICES:
for _, s := range services {
for _, c := range containers {
if c.Service == s {
continue SERVICES
}
}
return fmt.Errorf("no such service: %s", s)
}

if len(opts.Status) != 0 {
containers = filterByStatus(containers, opts.Status)
}
Expand Down
11 changes: 11 additions & 0 deletions pkg/e2e/ps_test.go
Expand Up @@ -23,6 +23,7 @@ import (

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gotest.tools/v3/icmd"

"github.com/docker/compose/v2/pkg/api"
)
Expand Down Expand Up @@ -108,4 +109,14 @@ func TestPs(t *testing.T) {
assert.Equal(t, 4, len(lines))
})

t.Run("ps unknown", func(t *testing.T) {
res := c.RunDockerComposeCmd(t, "--project-name", projectName, "stop")
assert.NoError(t, res.Error)

res = c.RunDockerComposeCmd(t, "-f", "./fixtures/ps-test/compose.yaml", "--project-name", projectName, "ps", "nginx")
res.Assert(t, icmd.Success)

res = c.RunDockerComposeCmdNoCheck(t, "-f", "./fixtures/ps-test/compose.yaml", "--project-name", projectName, "ps", "unknown")
res.Assert(t, icmd.Expected{ExitCode: 1, Err: "no such service: unknown"})
})
}