Skip to content

Commit

Permalink
check service names based on project, not running containers
Browse files Browse the repository at this point in the history
Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
  • Loading branch information
ndeloof committed Dec 21, 2022
1 parent 91371fe commit 9bd9f17
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
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"})
})
}

0 comments on commit 9bd9f17

Please sign in to comment.