test(e2e): lifecycle scenarios — cascade, profiles, healthcheck, recreate

Vocabulary grows with Recreated (the mirror of NotRecreated),
ServiceHealthy (healthcheck status observed from inspect) and a Within
action modifier bounding blocking commands like up --wait, replacing
the hand-rolled goroutine+timer of the legacy TestStartInterval.

- cascade: abort-on-container-exit/failure scenarios inline the model;
  exit-code propagation (111) is locked with ExitCode. The one-off
  variant stays legacy — it drives a long-running up in the background.
- profiles: the four scenarios observe which services materialize
  instead of grepping ps output; the dotenv variant declares its .env
  inline, making the activation mechanism visible.
- TestRecreateWithNoDeps now asserts both sides: the service is
  replaced (Recreated) and the healthy dependency is left alone.
- Fixtures start_interval/, profiles/compose.yaml+test-profile.env and
  recreate-no-deps.yaml are removed; cascade/ stays for the legacy
  one-off test.

Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
This commit is contained in:
Nicolas De Loof 2026-08-17 08:24:09 +02:00 • committed by Nicolas De loof
parent 6bc1b36516
commit fe17e31be0
17 changed files with 272 additions and 272 deletions

View file

@ -29,17 +29,21 @@ import (
)
func TestCascadeStop(t *testing.T) {
c := NewCLI(t)
const projectName = "compose-e2e-cascade-stop"
t.Cleanup(func() {
c.RunDockerComposeCmd(t, "--project-name", projectName, "down")
})
NewScenario(t, "up --abort-on-container-exit must stop the project on the first exit, and exit 0 without --exit-code-from").
Step("up aborts once a container exits, reporting which one",
ComposeCmd("up", "--abort-on-container-exit").Within(60*time.Second),
OutputContains("exit-1 exited with code 0"),
ServiceState("running", "exited"))
}
res := c.RunDockerComposeCmd(t, "-f", "./fixtures/cascade/compose.yaml", "--project-name", projectName,
"up", "--abort-on-container-exit")
assert.Assert(t, strings.Contains(res.Combined(), "exit-1 exited with code 0"), res.Combined())
// no --exit-code-from, so this is not an error
assert.Equal(t, res.ExitCode, 0)
func TestCascadeFail(t *testing.T) {
NewScenario(t, "up --abort-on-container-failure must propagate the failing container's exit code").
Step("up keeps going on clean exits and aborts on the failure, with its exit code",
ComposeCmd("up", "--abort-on-container-failure").MayFail().Within(60*time.Second),
ExitCode(111),
OutputContains("exit-1 exited with code 0"),
OutputContains("fail-1 exited with code 111"),
ServiceState("running", "exited"))
}
func TestCascadeIgnoresOneOffContainer(t *testing.T) {
@ -67,18 +71,3 @@ func TestCascadeIgnoresOneOffContainer(t *testing.T) {
assert.Assert(t, !strings.Contains(res.Combined(), "Aborting on container exit"), res.Combined())
RequireServiceState(t, c, "running", "running")
}
func TestCascadeFail(t *testing.T) {
c := NewCLI(t)
const projectName = "compose-e2e-cascade-fail"
t.Cleanup(func() {
c.RunDockerComposeCmd(t, "--project-name", projectName, "down")
})
res := c.RunDockerComposeCmdNoCheck(t, "-f", "./fixtures/cascade/compose.yaml", "--project-name", projectName,
"up", "--abort-on-container-failure")
assert.Assert(t, strings.Contains(res.Combined(), "exit-1 exited with code 0"), res.Combined())
assert.Assert(t, strings.Contains(res.Combined(), "fail-1 exited with code 111"), res.Combined())
// failing exit code should be propagated
assert.Equal(t, res.ExitCode, 111)
}

View file

@ -209,6 +209,28 @@ func NotRecreated(services ...string) Check {
}
}
// Recreated expects the services' containers to have been replaced by the
// step: none of the containers that existed before survived it.
func Recreated(services ...string) Check {
return Check{
name: fmt.Sprintf("services %s recreated", strings.Join(services, ", ")),
fn: func(ctx *CheckContext) error {
for _, service := range services {
before, after := containerIDs(ctx.prev.service(service)), containerIDs(ctx.curr.service(service))
if len(before) == 0 {
return fmt.Errorf("service %q had no container before the step", service)
}
for _, id := range after {
if slices.Contains(before, id) {
return fmt.Errorf("service %q kept container %s", service, id[:12])
}
}
}
return nil
},
}
}
func containerIDs(containers []containerState) []string {
ids := make([]string, 0, len(containers))
for _, c := range containers {
@ -218,6 +240,31 @@ func containerIDs(containers []containerState) []string {
return ids
}
// ServiceHealthy expects every container of the service to report a healthy
// state from its healthcheck.
func ServiceHealthy(service string) Check {
return Check{
name: fmt.Sprintf("service %q is healthy", service),
fn: func(ctx *CheckContext) error {
containers := ctx.curr.service(service)
if len(containers) == 0 {
return fmt.Errorf("service has no container")
}
for _, c := range containers {
res := icmd.RunCmd(ctx.scenario.cli.NewDockerCmd(ctx.scenario.t,
"inspect", "--format", "{{.State.Health.Status}}", c.ID))
if res.ExitCode != 0 {
return fmt.Errorf("inspect failed: %s", res.Combined())
}
if status := strings.TrimSpace(res.Stdout()); status != "healthy" {
return fmt.Errorf("container %s is %s", c.Name, status)
}
}
return nil
},
}
}
// OneOffState expects every one-off (run) container of the service to be in
// the given state.
func OneOffState(service, state string) Check {

View file

@ -1,17 +0,0 @@
version: '3.8'
services:
my-service:
image: alpine
command: tail -f /dev/null
init: true
depends_on:
nginx: {condition: service_healthy}
nginx:
image: nginx:alpine
stop_signal: SIGTERM
healthcheck:
test: "echo | nc -w 5 localhost:80"
interval: 2s
timeout: 1s
retries: 10

View file

@ -1,8 +0,0 @@
services:
regular-service:
image: nginx:alpine
profiled-service:
image: nginx:alpine
profiles:
- test-profile

View file

@ -17,39 +17,18 @@
package e2e
import (
"strings"
"testing"
"time"
"gotest.tools/v3/assert"
"gotest.tools/v3/icmd"
)
func TestStartInterval(t *testing.T) {
c := NewParallelCLI(t)
const projectName = "e2e-start-interval"
t.Cleanup(func() {
c.cleanupWithDown(t, projectName)
})
res := c.RunDockerComposeCmdNoCheck(t, "-f", "fixtures/start_interval/compose.yaml", "--project-name", projectName, "up", "--wait", "-d", "error")
res.Assert(t, icmd.Expected{ExitCode: 1, Err: "healthcheck.start_interval requires healthcheck.start_period to be set"})
timeout := time.After(30 * time.Second)
done := make(chan bool)
go func() {
//nolint:nolintlint,testifylint // helper asserts inside goroutine; acceptable in this e2e test
res := c.RunDockerComposeCmd(t, "-f", "fixtures/start_interval/compose.yaml", "--project-name", projectName, "up", "--wait", "-d", "test")
out := res.Combined()
assert.Assert(t, strings.Contains(out, "Healthy"), out)
done <- true
}()
select {
case <-timeout:
t.Fatal("test did not finish in time")
case <-done:
break
}
NewScenario(t, "start_interval must require start_period, and must accelerate the initial health probes").
Step("up --wait on a start_interval without start_period is rejected",
ComposeCmd("up", "--wait", "-d", "error").MayFail(),
ExitCode(1),
OutputContains("healthcheck.start_interval requires healthcheck.start_period to be set")).
Step("up --wait turns healthy well before the regular 30s interval",
ComposeCmd("up", "--wait", "-d", "test").Within(30*time.Second),
ServiceState("test", "running"),
ServiceHealthy("test"))
}

View file

@ -17,191 +17,86 @@
package e2e
import (
"strings"
"testing"
"gotest.tools/v3/assert"
"gotest.tools/v3/icmd"
)
const (
profiledService = "profiled-service"
regularService = "regular-service"
)
func TestExplicitProfileUsage(t *testing.T) {
c := NewParallelCLI(t)
const projectName = "compose-e2e-explicit-profiles"
const profileName = "test-profile"
t.Run("compose up with profile", func(t *testing.T) {
res := c.RunDockerComposeCmd(t, "-f", "./fixtures/profiles/compose.yaml",
"-p", projectName, "--profile", profileName, "up", "-d")
res.Assert(t, icmd.Expected{ExitCode: 0})
res = c.RunDockerComposeCmd(t, "-p", projectName, "ps")
res.Assert(t, icmd.Expected{Out: regularService})
res.Assert(t, icmd.Expected{Out: profiledService})
})
t.Run("compose stop with profile", func(t *testing.T) {
res := c.RunDockerComposeCmd(t, "-f", "./fixtures/profiles/compose.yaml",
"-p", projectName, "--profile", profileName, "stop")
res.Assert(t, icmd.Expected{ExitCode: 0})
res = c.RunDockerComposeCmd(t, "-p", projectName, "ps", "--status", "running")
assert.Assert(t, !strings.Contains(res.Combined(), regularService))
assert.Assert(t, !strings.Contains(res.Combined(), profiledService))
})
t.Run("compose start with profile", func(t *testing.T) {
res := c.RunDockerComposeCmd(t, "-f", "./fixtures/profiles/compose.yaml",
"-p", projectName, "--profile", profileName, "start")
res.Assert(t, icmd.Expected{ExitCode: 0})
res = c.RunDockerComposeCmd(t, "-p", projectName, "ps", "--status", "running")
res.Assert(t, icmd.Expected{Out: regularService})
res.Assert(t, icmd.Expected{Out: profiledService})
})
t.Run("compose restart with profile", func(t *testing.T) {
res := c.RunDockerComposeCmd(t, "-f", "./fixtures/profiles/compose.yaml",
"-p", projectName, "--profile", profileName, "restart")
res.Assert(t, icmd.Expected{ExitCode: 0})
res = c.RunDockerComposeCmd(t, "-p", projectName, "ps", "--status", "running")
res.Assert(t, icmd.Expected{Out: regularService})
res.Assert(t, icmd.Expected{Out: profiledService})
})
t.Run("down", func(t *testing.T) {
_ = c.RunDockerComposeCmd(t, "--project-name", projectName, "down")
})
t.Run("check containers after down", func(t *testing.T) {
res := c.RunDockerCmd(t, "ps")
assert.Assert(t, !strings.Contains(res.Combined(), projectName), res.Combined())
})
NewScenario(t, "with --profile, lifecycle commands must act on profiled and regular services alike").
Step("up with the profile starts both services",
ComposeCmd("--profile", "test-profile", "up", "-d"),
ServiceState("regular-service", "running"),
ServiceState("profiled-service", "running")).
Step("stop with the profile halts both services",
ComposeCmd("--profile", "test-profile", "stop"),
ServiceState("regular-service", "exited"),
ServiceState("profiled-service", "exited")).
Step("start with the profile resumes both services",
ComposeCmd("--profile", "test-profile", "start"),
ServiceState("regular-service", "running"),
ServiceState("profiled-service", "running"),
NotRecreated("regular-service", "profiled-service")).
Step("restart with the profile restarts both services in place",
ComposeCmd("--profile", "test-profile", "restart"),
ServiceState("regular-service", "running"),
ServiceState("profiled-service", "running"),
NotRecreated("regular-service", "profiled-service")).
Step("down removes every container",
ComposeCmd("--profile", "test-profile", "down"),
ServiceNotCreated("regular-service"),
ServiceNotCreated("profiled-service"))
}
func TestNoProfileUsage(t *testing.T) {
c := NewParallelCLI(t)
const projectName = "compose-e2e-no-profiles"
t.Run("compose up without profile", func(t *testing.T) {
res := c.RunDockerComposeCmd(t, "-f", "./fixtures/profiles/compose.yaml",
"-p", projectName, "up", "-d")
res.Assert(t, icmd.Expected{ExitCode: 0})
res = c.RunDockerComposeCmd(t, "-p", projectName, "ps")
res.Assert(t, icmd.Expected{Out: regularService})
assert.Assert(t, !strings.Contains(res.Combined(), profiledService))
})
t.Run("compose stop without profile", func(t *testing.T) {
res := c.RunDockerComposeCmd(t, "-f", "./fixtures/profiles/compose.yaml",
"-p", projectName, "stop")
res.Assert(t, icmd.Expected{ExitCode: 0})
res = c.RunDockerComposeCmd(t, "-p", projectName, "ps", "--status", "running")
assert.Assert(t, !strings.Contains(res.Combined(), regularService))
assert.Assert(t, !strings.Contains(res.Combined(), profiledService))
})
t.Run("compose start without profile", func(t *testing.T) {
res := c.RunDockerComposeCmd(t, "-f", "./fixtures/profiles/compose.yaml",
"-p", projectName, "start")
res.Assert(t, icmd.Expected{ExitCode: 0})
res = c.RunDockerComposeCmd(t, "-p", projectName, "ps", "--status", "running")
res.Assert(t, icmd.Expected{Out: regularService})
assert.Assert(t, !strings.Contains(res.Combined(), profiledService))
})
t.Run("compose restart without profile", func(t *testing.T) {
res := c.RunDockerComposeCmd(t, "-f", "./fixtures/profiles/compose.yaml",
"-p", projectName, "restart")
res.Assert(t, icmd.Expected{ExitCode: 0})
res = c.RunDockerComposeCmd(t, "-p", projectName, "ps", "--status", "running")
res.Assert(t, icmd.Expected{Out: regularService})
assert.Assert(t, !strings.Contains(res.Combined(), profiledService))
})
t.Run("down", func(t *testing.T) {
_ = c.RunDockerComposeCmd(t, "--project-name", projectName, "down")
})
t.Run("check containers after down", func(t *testing.T) {
res := c.RunDockerCmd(t, "ps")
assert.Assert(t, !strings.Contains(res.Combined(), projectName), res.Combined())
})
NewScenario(t, "without a profile, lifecycle commands must never materialize the profiled service").
Step("up starts only the regular service",
ComposeCmd("up", "-d"),
ServiceState("regular-service", "running"),
ServiceNotCreated("profiled-service")).
Step("stop halts the regular service",
ComposeCmd("stop"),
ServiceState("regular-service", "exited"),
ServiceNotCreated("profiled-service")).
Step("start resumes the regular service only",
ComposeCmd("start"),
ServiceState("regular-service", "running"),
ServiceNotCreated("profiled-service"),
NotRecreated("regular-service")).
Step("restart touches the regular service only",
ComposeCmd("restart"),
ServiceState("regular-service", "running"),
ServiceNotCreated("profiled-service"),
NotRecreated("regular-service")).
Step("down removes every container",
ComposeCmd("down"),
ServiceNotCreated("regular-service"))
}
func TestActiveProfileViaTargetedService(t *testing.T) {
c := NewParallelCLI(t)
const projectName = "compose-e2e-via-target-service-profiles"
const profileName = "test-profile"
t.Run("compose up with service name", func(t *testing.T) {
res := c.RunDockerComposeCmd(t, "-f", "./fixtures/profiles/compose.yaml",
"-p", projectName, "up", profiledService, "-d")
res.Assert(t, icmd.Expected{ExitCode: 0})
res = c.RunDockerComposeCmd(t, "-p", projectName, "ps")
assert.Assert(t, !strings.Contains(res.Combined(), regularService))
res.Assert(t, icmd.Expected{Out: profiledService})
res = c.RunDockerComposeCmd(t, "-p", projectName, "--profile", profileName, "ps")
assert.Assert(t, !strings.Contains(res.Combined(), regularService))
res.Assert(t, icmd.Expected{Out: profiledService})
})
t.Run("compose stop with service name", func(t *testing.T) {
res := c.RunDockerComposeCmd(t, "-f", "./fixtures/profiles/compose.yaml",
"-p", projectName, "stop", profiledService)
res.Assert(t, icmd.Expected{ExitCode: 0})
res = c.RunDockerComposeCmd(t, "-p", projectName, "ps", "--status", "running")
assert.Assert(t, !strings.Contains(res.Combined(), regularService))
assert.Assert(t, !strings.Contains(res.Combined(), profiledService))
})
t.Run("compose start with service name", func(t *testing.T) {
res := c.RunDockerComposeCmd(t, "-f", "./fixtures/profiles/compose.yaml",
"-p", projectName, "start", profiledService)
res.Assert(t, icmd.Expected{ExitCode: 0})
res = c.RunDockerComposeCmd(t, "-p", projectName, "ps", "--status", "running")
assert.Assert(t, !strings.Contains(res.Combined(), regularService))
res.Assert(t, icmd.Expected{Out: profiledService})
})
t.Run("compose restart with service name", func(t *testing.T) {
res := c.RunDockerComposeCmd(t, "-f", "./fixtures/profiles/compose.yaml",
"-p", projectName, "restart")
res.Assert(t, icmd.Expected{ExitCode: 0})
res = c.RunDockerComposeCmd(t, "-p", projectName, "ps", "--status", "running")
assert.Assert(t, !strings.Contains(res.Combined(), regularService))
res.Assert(t, icmd.Expected{Out: profiledService})
})
t.Run("down", func(t *testing.T) {
_ = c.RunDockerComposeCmd(t, "--project-name", projectName, "down")
})
t.Run("check containers after down", func(t *testing.T) {
res := c.RunDockerCmd(t, "ps")
assert.Assert(t, !strings.Contains(res.Combined(), projectName), res.Combined())
})
NewScenario(t, "targeting a profiled service must activate its profile implicitly, and only for it").
Step("up on the profiled service starts it without the regular one",
ComposeCmd("up", "-d", "profiled-service"),
ServiceState("profiled-service", "running"),
ServiceNotCreated("regular-service")).
Step("stop on the profiled service halts it",
ComposeCmd("stop", "profiled-service"),
ServiceState("profiled-service", "exited"),
ServiceNotCreated("regular-service")).
Step("start on the profiled service resumes it",
ComposeCmd("start", "profiled-service"),
ServiceState("profiled-service", "running"),
ServiceNotCreated("regular-service"),
NotRecreated("profiled-service")).
Step("restart keeps acting on the existing containers only",
ComposeCmd("restart"),
ServiceState("profiled-service", "running"),
ServiceNotCreated("regular-service"),
NotRecreated("profiled-service"))
}
func TestDotEnvProfileUsage(t *testing.T) {
c := NewParallelCLI(t)
const projectName = "compose-e2e-dotenv-profiles"
const profileName = "test-profile"
t.Cleanup(func() {
_ = c.RunDockerComposeCmd(t, "--project-name", projectName, "down")
})
t.Run("compose up with profile", func(t *testing.T) {
res := c.RunDockerComposeCmd(t, "-f", "./fixtures/profiles/compose.yaml",
"--env-file", "./fixtures/profiles/test-profile.env",
"-p", projectName, "--profile", profileName, "up", "-d")
res.Assert(t, icmd.Expected{ExitCode: 0})
res = c.RunDockerComposeCmd(t, "-p", projectName, "ps")
res.Assert(t, icmd.Expected{Out: regularService})
res.Assert(t, icmd.Expected{Out: profiledService})
})
NewScenario(t, "COMPOSE_PROFILES from the project's .env must activate the profile").
Step("up starts both services, the profile coming from .env",
ComposeCmd("up", "-d"),
ServiceState("regular-service", "running"),
ServiceState("profiled-service", "running"))
}

View file

@ -18,22 +18,18 @@ package e2e
import (
"testing"
"gotest.tools/v3/icmd"
"time"
)
func TestRecreateWithNoDeps(t *testing.T) {
c := NewParallelCLI(t, WithEnv(
"COMPOSE_PROJECT_NAME=recreate-no-deps",
))
res := c.RunDockerComposeCmdNoCheck(t, "-f", "fixtures/dependencies/recreate-no-deps.yaml", "up", "-d")
res.Assert(t, icmd.Success)
res = c.RunDockerComposeCmdNoCheck(t, "-f", "fixtures/dependencies/recreate-no-deps.yaml", "up", "-d", "--force-recreate", "--no-deps", "my-service")
res.Assert(t, icmd.Success)
RequireServiceState(t, c, "my-service", "running")
c.RunDockerComposeCmd(t, "down")
NewScenario(t, "up --force-recreate --no-deps must replace the service without touching its healthy dependency").
Step("up starts the service once its dependency is healthy",
ComposeCmd("up", "-d", "--wait").Within(60*time.Second),
ServiceState("my-service", "running"),
ServiceHealthy("dep")).
Step("force-recreate with --no-deps replaces only the service",
ComposeCmd("up", "-d", "--force-recreate", "--no-deps", "my-service"),
ServiceState("my-service", "running"),
Recreated("my-service"),
NotRecreated("dep"))
}

View file

@ -312,6 +312,7 @@ func (s *Scenario) command(action Action) icmd.Cmd {
}
cmd.Env = append(cmd.Env, s.env...)
cmd.Env = append(cmd.Env, action.env...)
cmd.Timeout = action.timeout
return cmd
}
@ -501,6 +502,7 @@ type Action struct {
args []string
env []string
mayFail bool
timeout time.Duration
}
// ComposeCmd runs `docker compose <args>` against the scenario's compose
@ -527,6 +529,15 @@ func (a Action) MayFail() Action {
return a
}
// Within bounds the command's execution time, for blocking commands whose
// termination is itself the expectation (e.g. `up --wait` on a service that
// must become healthy). Exceeding the timeout kills the command and fails the
// step.
func (a Action) Within(timeout time.Duration) Action {
a.timeout = timeout
return a
}
// ---------- Requirements ----------
// Requirement checks an environment prerequisite; it returns a non-empty

View file

@ -0,0 +1,12 @@
services:
regular-service:
image: alpine
init: true
command: sleep infinity
profiled-service:
image: alpine
init: true
command: sleep infinity
profiles:
- test-profile

View file

@ -0,0 +1,19 @@
services:
running:
image: alpine
command: sleep infinity
init: true
exit:
image: alpine
command: /bin/true
depends_on:
running:
condition: service_started
fail:
image: alpine
command: sh -c "return 111"
depends_on:
exit:
condition: service_completed_successfully

View file

@ -0,0 +1,19 @@
services:
running:
image: alpine
command: sleep infinity
init: true
exit:
image: alpine
command: /bin/true
depends_on:
running:
condition: service_started
fail:
image: alpine
command: sh -c "return 111"
depends_on:
exit:
condition: service_completed_successfully

View file

@ -1 +1,2 @@
COMPOSE_PROFILES=test-profile

View file

@ -0,0 +1,12 @@
services:
regular-service:
image: alpine
init: true
command: sleep infinity
profiled-service:
image: alpine
init: true
command: sleep infinity
profiles:
- test-profile

View file

@ -0,0 +1,12 @@
services:
regular-service:
image: alpine
init: true
command: sleep infinity
profiled-service:
image: alpine
init: true
command: sleep infinity
profiles:
- test-profile

View file

@ -0,0 +1,12 @@
services:
regular-service:
image: alpine
init: true
command: sleep infinity
profiled-service:
image: alpine
init: true
command: sleep infinity
profiles:
- test-profile

View file

@ -0,0 +1,17 @@
services:
my-service:
image: alpine
command: sleep infinity
init: true
depends_on:
dep: {condition: service_healthy}
dep:
image: alpine
command: sleep infinity
init: true
healthcheck:
test: "/bin/true"
interval: 2s
timeout: 1s
retries: 10

View file

@ -1,6 +1,8 @@
services:
test:
image: "nginx"
image: alpine
init: true
command: sleep infinity
healthcheck:
interval: 30s
start_period: 10s
@ -8,8 +10,10 @@ services:
test: "/bin/true"
error:
image: "nginx"
image: alpine
init: true
command: sleep infinity
healthcheck:
interval: 30s
start_interval: 1s
test: "/bin/true"
test: "/bin/true"