Skip to content

Commit 9bd9f17

Browse files
committed
check service names based on project, not running containers
Signed-off-by: Nicolas De Loof <[email protected]>
1 parent 91371fe commit 9bd9f17

File tree

2 files changed

+21
-10
lines changed

2 files changed

+21
-10
lines changed

cmd/compose/ps.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,16 @@ func runPs(ctx context.Context, streams api.Streams, backend api.Service, servic
9696
if err != nil {
9797
return err
9898
}
99+
100+
if project != nil && len(services) > 0 {
101+
names := project.ServiceNames()
102+
for _, service := range services {
103+
if !utils.StringContains(names, service) {
104+
return fmt.Errorf("no such service: %s", service)
105+
}
106+
}
107+
}
108+
99109
containers, err := backend.Ps(ctx, name, api.PsOptions{
100110
Project: project,
101111
All: opts.All,
@@ -105,16 +115,6 @@ func runPs(ctx context.Context, streams api.Streams, backend api.Service, servic
105115
return err
106116
}
107117

108-
SERVICES:
109-
for _, s := range services {
110-
for _, c := range containers {
111-
if c.Service == s {
112-
continue SERVICES
113-
}
114-
}
115-
return fmt.Errorf("no such service: %s", s)
116-
}
117-
118118
if len(opts.Status) != 0 {
119119
containers = filterByStatus(containers, opts.Status)
120120
}

pkg/e2e/ps_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323

2424
"github.com/stretchr/testify/assert"
2525
"github.com/stretchr/testify/require"
26+
"gotest.tools/v3/icmd"
2627

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

112+
t.Run("ps unknown", func(t *testing.T) {
113+
res := c.RunDockerComposeCmd(t, "--project-name", projectName, "stop")
114+
assert.NoError(t, res.Error)
115+
116+
res = c.RunDockerComposeCmd(t, "-f", "./fixtures/ps-test/compose.yaml", "--project-name", projectName, "ps", "nginx")
117+
res.Assert(t, icmd.Success)
118+
119+
res = c.RunDockerComposeCmdNoCheck(t, "-f", "./fixtures/ps-test/compose.yaml", "--project-name", projectName, "ps", "unknown")
120+
res.Assert(t, icmd.Expected{ExitCode: 1, Err: "no such service: unknown"})
121+
})
111122
}

0 commit comments

Comments
 (0)