Skip to content

Commit cc5c4ef

Browse files
ndeloofglours
authored andcommitted
test(e2e): migrate start/stop legacy tests to scenarios
TestStartAlreadyRunning, TestStopAlreadyStopped, TestStartStopMultipleServices and TestStartStopMultipleFiles now state their intent and observe container state (running/exited, not recreated) instead of matching progress lines. The shared fixtures/start-stop had no other user and is removed. Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
1 parent 3b2f42d commit cc5c4ef

6 files changed

Lines changed: 72 additions & 75 deletions

File tree

‎pkg/e2e/start_stop_test.go‎

Lines changed: 57 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,7 @@
1818

1919
package e2e
2020

21-
import (
22-
"fmt"
23-
"strings"
24-
"testing"
25-
26-
"gotest.tools/v3/assert"
27-
"gotest.tools/v3/icmd"
28-
)
21+
import "testing"
2922

3023
func TestStartStop(t *testing.T) {
3124
s := NewScenario(t, "stop must halt the project's containers in place, start must bring the same ones back")
@@ -117,66 +110,49 @@ func TestStartStopWithOneOffs(t *testing.T) {
117110
}
118111

119112
func TestStartAlreadyRunning(t *testing.T) {
120-
cli := NewParallelCLI(t, WithEnv(
121-
"COMPOSE_PROJECT_NAME=e2e-start-stop-svc-already-running",
122-
"COMPOSE_FILE=./fixtures/start-stop/compose.yaml"))
123-
t.Cleanup(func() {
124-
cli.RunDockerComposeCmd(t, "down", "--remove-orphans", "-v", "-t", "0")
125-
})
126-
127-
cli.RunDockerComposeCmd(t, "up", "-d", "--wait")
128-
129-
res := cli.RunDockerComposeCmd(t, "start", "simple")
130-
assert.Equal(t, res.Stdout(), "", "No output should have been written to stdout")
113+
NewScenario(t, "start on an already running service must be a silent no-op").
114+
Step("up starts the service and waits for it",
115+
ComposeCmd("up", "-d", "--wait"),
116+
ServiceState("simple", "running")).
117+
Step("start leaves the running container alone and writes nothing to stdout",
118+
ComposeCmd("start", "simple"),
119+
StdoutEmpty(),
120+
ServiceState("simple", "running"),
121+
NotRecreated("simple"))
131122
}
132123

133124
func TestStopAlreadyStopped(t *testing.T) {
134-
cli := NewParallelCLI(t, WithEnv(
135-
"COMPOSE_PROJECT_NAME=e2e-start-stop-svc-already-stopped",
136-
"COMPOSE_FILE=./fixtures/start-stop/compose.yaml"))
137-
t.Cleanup(func() {
138-
cli.RunDockerComposeCmd(t, "down", "--remove-orphans", "-v", "-t", "0")
139-
})
140-
141-
cli.RunDockerComposeCmd(t, "up", "-d", "--wait")
142-
143-
// stop the container
144-
cli.RunDockerComposeCmd(t, "stop", "simple")
145-
146-
// attempt to stop it again
147-
res := cli.RunDockerComposeCmdNoCheck(t, "stop", "simple")
148-
// TODO: for consistency, this should NOT write any output because the
149-
// container is already stopped
150-
res.Assert(t, icmd.Expected{
151-
ExitCode: 0,
152-
Err: "Container e2e-start-stop-svc-already-stopped-simple-1 Stopped",
153-
})
125+
s := NewScenario(t, "stop on an already stopped service must succeed and leave it stopped")
126+
s.Step("up starts the services and waits for them",
127+
ComposeCmd("up", "-d", "--wait"),
128+
ServiceState("simple", "running")).
129+
Step("stop halts the service",
130+
ComposeCmd("stop", "simple"),
131+
ServiceState("simple", "exited")).
132+
Step("stopping it again succeeds and leaves it stopped",
133+
ComposeCmd("stop", "simple"),
134+
ServiceState("simple", "exited"),
135+
NotRecreated("simple"),
136+
// TODO: for consistency, this should NOT write any output because the
137+
// container is already stopped
138+
StderrContains("Container "+s.Project()+"-simple-1 Stopped"))
154139
}
155140

156141
func TestStartStopMultipleServices(t *testing.T) {
157-
cli := NewParallelCLI(t, WithEnv(
158-
"COMPOSE_PROJECT_NAME=e2e-start-stop-svc-multiple",
159-
"COMPOSE_FILE=./fixtures/start-stop/compose.yaml"))
160-
t.Cleanup(func() {
161-
cli.RunDockerComposeCmd(t, "down", "--remove-orphans", "-v", "-t", "0")
162-
})
163-
164-
cli.RunDockerComposeCmd(t, "up", "-d", "--wait")
165-
166-
res := cli.RunDockerComposeCmd(t, "stop", "simple", "another")
167-
services := []string{"simple", "another"}
168-
for _, svc := range services {
169-
stopMsg := fmt.Sprintf("Container e2e-start-stop-svc-multiple-%s-1 Stopped", svc)
170-
assert.Assert(t, strings.Contains(res.Stderr(), stopMsg),
171-
fmt.Sprintf("Missing stop message for %s\n%s", svc, res.Combined()))
172-
}
173-
174-
res = cli.RunDockerComposeCmd(t, "start", "simple", "another")
175-
for _, svc := range services {
176-
startMsg := fmt.Sprintf("Container e2e-start-stop-svc-multiple-%s-1 Started", svc)
177-
assert.Assert(t, strings.Contains(res.Stderr(), startMsg),
178-
fmt.Sprintf("Missing start message for %s\n%s", svc, res.Combined()))
179-
}
142+
NewScenario(t, "stop and start must act on every service named on the command line").
143+
Step("up starts the services and waits for them",
144+
ComposeCmd("up", "-d", "--wait"),
145+
ServiceState("simple", "running"),
146+
ServiceState("another", "running")).
147+
Step("stop halts both named services",
148+
ComposeCmd("stop", "simple", "another"),
149+
ServiceState("simple", "exited"),
150+
ServiceState("another", "exited")).
151+
Step("start brings both named services back, reusing their containers",
152+
ComposeCmd("start", "simple", "another"),
153+
ServiceState("simple", "running"),
154+
ServiceState("another", "running"),
155+
NotRecreated("simple", "another"))
180156
}
181157

182158
func TestStartSingleServiceAndDependency(t *testing.T) {
@@ -198,17 +174,23 @@ func TestStartSingleServiceAndDependency(t *testing.T) {
198174
}
199175

200176
func TestStartStopMultipleFiles(t *testing.T) {
201-
cli := NewParallelCLI(t, WithEnv("COMPOSE_PROJECT_NAME=e2e-start-stop-svc-multiple-files"))
202-
t.Cleanup(func() {
203-
cli.RunDockerComposeCmd(t, "-p", "e2e-start-stop-svc-multiple-files", "down", "--remove-orphans")
204-
})
205-
206-
cli.RunDockerComposeCmd(t, "-f", "./fixtures/start-stop/compose.yaml", "up", "-d")
207-
cli.RunDockerComposeCmd(t, "-f", "./fixtures/start-stop/other.yaml", "up", "-d")
208-
209-
res := cli.RunDockerComposeCmd(t, "-f", "./fixtures/start-stop/compose.yaml", "stop")
210-
assert.Assert(t, strings.Contains(res.Combined(), "Container e2e-start-stop-svc-multiple-files-simple-1 Stopped"), res.Combined())
211-
assert.Assert(t, strings.Contains(res.Combined(), "Container e2e-start-stop-svc-multiple-files-another-1 Stopped"), res.Combined())
212-
assert.Assert(t, !strings.Contains(res.Combined(), "Container e2e-start-stop-svc-multiple-files-a-different-one-1 Stopped"), res.Combined())
213-
assert.Assert(t, !strings.Contains(res.Combined(), "Container e2e-start-stop-svc-multiple-files-and-another-one-1 Stopped"), res.Combined())
177+
s := NewScenario(t, "stop must only act on the services of the compose file it is given, in a project spanning several files")
178+
s.Step("up starts the services of the first file",
179+
ComposeCmd("up", "-d"),
180+
ServiceState("simple", "running"),
181+
ServiceState("another", "running"))
182+
s.FromFile("other.yaml").
183+
Step("up of a second file adds its services to the same project",
184+
ComposeCmd("up", "-d"),
185+
ServiceState("simple", "running"),
186+
ServiceState("another", "running"),
187+
ServiceState("a-different-one", "running"),
188+
ServiceState("and-another-one", "running"))
189+
s.FromFile("compose.yaml").
190+
Step("stop with the first file halts its services only",
191+
ComposeCmd("stop"),
192+
ServiceState("simple", "exited"),
193+
ServiceState("another", "exited"),
194+
ServiceState("a-different-one", "running"),
195+
ServiceState("and-another-one", "running"))
214196
}
File renamed without changes.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
services:
2+
simple:
3+
image: nginx:alpine
4+
another:
5+
image: nginx:alpine
File renamed without changes.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
services:
2+
simple:
3+
image: nginx:alpine
4+
another:
5+
image: nginx:alpine
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
services:
2+
simple:
3+
image: nginx:alpine
4+
another:
5+
image: nginx:alpine

0 commit comments

Comments
 (0)