Skip to content

Commit 0b1c867

Browse files
laurazardndeloof
authored andcommitted
Add tests for filtering containers not created by Compose
Signed-off-by: Laura Brehm <[email protected]>
1 parent 82ef998 commit 0b1c867

File tree

7 files changed

+80
-12
lines changed

7 files changed

+80
-12
lines changed

e2e/cucumber-features/ps.feature

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
Feature: PS
2+
3+
Background:
4+
Given a compose file
5+
"""
6+
services:
7+
build:
8+
image: test:latest
9+
build:
10+
context: ./
11+
pull:
12+
image: alpine
13+
command: top
14+
"""
15+
And a dockerfile
16+
"""
17+
FROM golang:1.19-alpine
18+
"""
19+
20+
Scenario: external container from compose image exists
21+
When I run "compose build"
22+
Then the exit code is 0
23+
And I run "docker run --name external-test test:latest ls"
24+
Then the exit code is 0
25+
And I run "compose ps -a"
26+
Then the output does not contain "external-test"
27+

e2e/cucumber-features/up.feature

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,7 @@ Background:
1111

1212
Scenario: --pull always
1313
When I run "compose up --pull=always -d"
14-
Then the output contains "simple Pulled"
14+
And the output contains "simple Pulled"
15+
Then I run "compose up --pull=always -d"
16+
And the output contains "simple Pulled"
1517

e2e/cucumber_test.go

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"context"
2121
"fmt"
2222
"os"
23+
"path/filepath"
2324
"regexp"
2425
"strings"
2526
"testing"
@@ -78,16 +79,20 @@ func setup(s *godog.ScenarioContext) {
7879
})
7980

8081
s.Step(`^a compose file$`, th.setComposeFile)
82+
s.Step(`^a dockerfile$`, th.setDockerfile)
8183
s.Step(`^I run "compose (.*)"$`, th.runComposeCommand)
84+
s.Step(`^I run "docker (.*)"$`, th.runDockerCommand)
8285
s.Step(`service "(.*)" is "(.*)"$`, th.serviceIsStatus)
83-
s.Step(`output contains "(.*)"$`, th.outputContains)
86+
s.Step(`output contains "(.*)"$`, th.outputContains(true))
87+
s.Step(`output does not contain "(.*)"$`, th.outputContains(false))
8488
s.Step(`exit code is (\d+)$`, th.exitCodeIs)
8589
}
8690

8791
type testHelper struct {
8892
T *testing.T
8993
ProjectName string
9094
ComposeFile string
95+
TestDir string
9196
CommandOutput string
9297
CommandExitCode int
9398
CLI *e2e.CLI
@@ -104,16 +109,21 @@ func (th *testHelper) serviceIsStatus(service, status string) error {
104109
return nil
105110
}
106111

107-
func (th *testHelper) outputContains(substring string) error {
108-
if !strings.Contains(th.CommandOutput, substring) {
109-
return fmt.Errorf("Missing output substring: %s\noutput: %s", substring, th.CommandOutput)
112+
func (th *testHelper) outputContains(expected bool) func(string) error {
113+
return func(substring string) error {
114+
contains := strings.Contains(th.CommandOutput, substring)
115+
if contains && !expected {
116+
return fmt.Errorf("Unexpected substring in output: %s\noutput: %s", substring, th.CommandOutput)
117+
} else if !contains && expected {
118+
return fmt.Errorf("Missing substring in output: %s\noutput: %s", substring, th.CommandOutput)
119+
}
120+
return nil
110121
}
111-
return nil
112122
}
113123

114124
func (th *testHelper) exitCodeIs(exitCode int) error {
115125
if exitCode != th.CommandExitCode {
116-
return fmt.Errorf("Wrong exit code: %d expected: %d", th.CommandExitCode, exitCode)
126+
return fmt.Errorf("Wrong exit code: %d expected: %d || command output: %s", th.CommandExitCode, exitCode, th.CommandOutput)
117127
}
118128
return nil
119129
}
@@ -127,6 +137,21 @@ func (th *testHelper) runComposeCommand(command string) error {
127137

128138
cmd := th.CLI.NewDockerComposeCmd(th.T, commandArgs...)
129139
cmd.Stdin = strings.NewReader(th.ComposeFile)
140+
cmd.Dir = th.TestDir
141+
res := icmd.RunCmd(cmd)
142+
th.CommandOutput = res.Combined()
143+
th.CommandExitCode = res.ExitCode
144+
return nil
145+
}
146+
147+
func (th *testHelper) runDockerCommand(command string) error {
148+
commandArgs, err := shellwords.Parse(command)
149+
if err != nil {
150+
return err
151+
}
152+
153+
cmd := th.CLI.NewDockerCmd(th.T, commandArgs...)
154+
cmd.Dir = th.TestDir
130155
res := icmd.RunCmd(cmd)
131156
th.CommandOutput = res.Combined()
132157
th.CommandExitCode = res.ExitCode
@@ -137,3 +162,14 @@ func (th *testHelper) setComposeFile(composeString string) error {
137162
th.ComposeFile = composeString
138163
return nil
139164
}
165+
166+
func (th *testHelper) setDockerfile(dockerfileString string) error {
167+
tempDir := th.T.TempDir()
168+
th.TestDir = tempDir
169+
170+
err := os.WriteFile(filepath.Join(tempDir, "Dockerfile"), []byte(dockerfileString), 0o644)
171+
if err != nil {
172+
return err
173+
}
174+
return nil
175+
}

pkg/compose/convergence_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ func TestServiceLinks(t *testing.T) {
6868
projectFilter(testProject),
6969
serviceFilter("db"),
7070
oneOffFilter(false),
71+
hasConfigHashLabel(),
7172
),
7273
All: true,
7374
}
@@ -193,6 +194,7 @@ func TestServiceLinks(t *testing.T) {
193194
projectFilter(testProject),
194195
serviceFilter("web"),
195196
oneOffFilter(false),
197+
hasConfigHashLabel(),
196198
),
197199
All: true,
198200
}

pkg/compose/kill_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func TestKillAll(t *testing.T) {
5050

5151
ctx := context.Background()
5252
api.EXPECT().ContainerList(ctx, moby.ContainerListOptions{
53-
Filters: filters.NewArgs(projectFilter(name)),
53+
Filters: filters.NewArgs(projectFilter(name), hasConfigHashLabel()),
5454
}).Return(
5555
[]moby.Container{testContainer("service1", "123", false), testContainer("service1", "456", false), testContainer("service2", "789", false)}, nil)
5656
api.EXPECT().VolumeList(gomock.Any(), filters.NewArgs(projectFilter(strings.ToLower(testProject)))).
@@ -81,7 +81,7 @@ func TestKillSignal(t *testing.T) {
8181

8282
name := strings.ToLower(testProject)
8383
listOptions := moby.ContainerListOptions{
84-
Filters: filters.NewArgs(projectFilter(name), serviceFilter(serviceName)),
84+
Filters: filters.NewArgs(projectFilter(name), serviceFilter(serviceName), hasConfigHashLabel()),
8585
}
8686

8787
ctx := context.Background()
@@ -133,6 +133,7 @@ func anyCancellableContext() gomock.Matcher {
133133
func projectFilterListOpt(withOneOff bool) moby.ContainerListOptions {
134134
filter := filters.NewArgs(
135135
projectFilter(strings.ToLower(testProject)),
136+
hasConfigHashLabel(),
136137
)
137138
if !withOneOff {
138139
filter.Add("label", fmt.Sprintf("%s=False", compose.OneoffLabel))

pkg/compose/logs_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func TestComposeService_Logs_Demux(t *testing.T) {
5252
ctx := context.Background()
5353
api.EXPECT().ContainerList(ctx, moby.ContainerListOptions{
5454
All: true,
55-
Filters: filters.NewArgs(oneOffFilter(false), projectFilter(name)),
55+
Filters: filters.NewArgs(oneOffFilter(false), projectFilter(name), hasConfigHashLabel()),
5656
}).Return(
5757
[]moby.Container{
5858
testContainer("service", "c", false),
@@ -125,7 +125,7 @@ func TestComposeService_Logs_ServiceFiltering(t *testing.T) {
125125
ctx := context.Background()
126126
api.EXPECT().ContainerList(ctx, moby.ContainerListOptions{
127127
All: true,
128-
Filters: filters.NewArgs(oneOffFilter(false), projectFilter(name)),
128+
Filters: filters.NewArgs(oneOffFilter(false), projectFilter(name), hasConfigHashLabel()),
129129
}).Return(
130130
[]moby.Container{
131131
testContainer("serviceA", "c1", false),

pkg/compose/ps_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func TestPs(t *testing.T) {
4242
cli.EXPECT().Client().Return(api).AnyTimes()
4343

4444
ctx := context.Background()
45-
args := filters.NewArgs(projectFilter(strings.ToLower(testProject)))
45+
args := filters.NewArgs(projectFilter(strings.ToLower(testProject)), hasConfigHashLabel())
4646
args.Add("label", "com.docker.compose.oneoff=False")
4747
listOpts := moby.ContainerListOptions{Filters: args, All: false}
4848
c1, inspect1 := containerDetails("service1", "123", "running", "healthy", 0)

0 commit comments

Comments
 (0)