compose/pkg/e2e/scale_test.go
Nicolas De Loof 7af58e3626 test(e2e): gate the e2e suite behind the build tag the docs already promise
AGENTS.md documents 'Test unit: go test ./pkg/...' and 'E2E tests:
go test -tags e2e ./pkg/e2e/'. Both statements were false: no file in
pkg/e2e carried an e2e build constraint, so the -tags flag was a no-op
and the 'unit' command actually launched the full e2e suite — requiring
a Docker daemon and the locally built binary, creating and destroying
containers, for up to 20 minutes. CI only avoided this by grepping e2e
out of the package list (Dockerfile).

Every *_test.go in pkg/e2e now carries //go:build e2e (merged with the
existing !windows constraints), and the Makefile e2e targets pass the
tag. The documented commands become true:
- go test ./pkg/...          -> pkg/e2e reports [no test files]
- go test -tags e2e ./pkg/e2e -> runs the suite

AGENTS.md now also states the daemon/binary prerequisites explicitly.

Part of #14074 (A: the code misdescribes its own structure).

Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
2026-08-27 11:02:07 +02:00

108 lines
4.1 KiB
Go

//go:build e2e
/*
Copyright 2020 Docker Compose CLI authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package e2e
import (
"testing"
)
func TestScaleBasicCases(t *testing.T) {
NewScenario(t, "scale must add and remove replicas to match the requested count, per service").
Step("up honors deploy.replicas, including zero",
ComposeCmd("up", "-d"),
ServiceScale("back", 1),
ServiceScale("db", 1),
ServiceScale("front", 2),
ServiceNotCreated("dbadmin")).
Step("scale up a zero-replica service",
ComposeCmd("scale", "dbadmin=2"),
ServiceScale("dbadmin", 2)).
Step("scale two services at once",
ComposeCmd("scale", "front=3", "back=2"),
ServiceScale("front", 3),
ServiceScale("back", 2)).
Step("scale down keeps the surviving replica untouched",
ComposeCmd("scale", "dbadmin=1"),
ServiceScale("dbadmin", 1)).
Step("scale to zero removes every replica",
ComposeCmd("scale", "dbadmin=0"),
ServiceNotCreated("dbadmin")).
Step("scale down two services at once",
ComposeCmd("scale", "front=2", "back=1"),
ReplicaNumbers("front", 1, 2),
ServiceScale("back", 1))
}
func TestScaleWithDepsCases(t *testing.T) {
NewScenario(t, "scale --no-deps must leave dependencies alone, while a plain scale reconciles them to the model").
Step("up with an explicit dependency scale",
ComposeCmd("up", "-d", "--scale", "db=2"),
ServiceScale("db", 2)).
Step("scale --no-deps does not touch the dependency's replicas",
ComposeCmd("scale", "--no-deps", "back=2"),
ServiceScale("back", 2),
ServiceScale("db", 2)).
Step("scale without --no-deps resets the dependency to the model's count",
ComposeCmd("scale", "back=2"),
ServiceScale("back", 2),
ServiceScale("db", 1))
}
func TestScaleUpAndDownPreserveContainerNumber(t *testing.T) {
NewScenario(t, "scaling down then up must reuse replica numbers instead of drifting").
Step("up creates replicas #1 and #2",
ComposeCmd("up", "-d", "--scale", "db=2", "db"),
ReplicaNumbers("db", 1, 2)).
Step("scale down removes replica #2",
ComposeCmd("up", "-d", "--scale", "db=1", "db"),
ReplicaNumbers("db", 1)).
Step("scale up restores replica #2",
ComposeCmd("up", "-d", "--scale", "db=2", "db"),
ReplicaNumbers("db", 1, 2))
}
func TestScaleDownRemovesObsolete(t *testing.T) {
NewScenario(t, "scale down must keep the up-to-date replicas, not the lowest numbers").
Step("up creates replica #1",
ComposeCmd("up", "-d", "db"),
ReplicaNumbers("db", 1)).
Step("scale up with a changed config adds replica #2",
ComposeCmd("up", "-d", "--scale", "db=2", "db").WithEnv("MAYBE=value"),
ReplicaNumbers("db", 1, 2)).
Step("scale down under the same config keeps replica #1",
ComposeCmd("up", "-d", "--scale", "db=1", "db").WithEnv("MAYBE=value"),
ReplicaNumbers("db", 1))
}
func TestScaleDownNoRecreate(t *testing.T) {
s := NewScenario(t, "up --no-recreate must scale up with fresh replicas and scale down by dropping the stale ones")
s.Defer(DockerCmd("image", "rm", "-f", s.Project()+"-test")).
Step("build and start two replicas of the first image",
ComposeCmd("build", "--build-arg", "FOO=test")).
Step("up starts replicas #1 and #2",
ComposeCmd("up", "-d", "--scale", "test=2"),
ReplicaNumbers("test", 1, 2)).
Step("rebuild changes the image the service should run",
ComposeCmd("build", "--build-arg", "FOO=updated")).
Step("up --no-recreate adds fresh replicas without touching stale ones",
ComposeCmd("up", "-d", "--scale", "test=4", "--no-recreate"),
ReplicaNumbers("test", 1, 2, 3, 4)).
Step("scale down drops the stale replicas and keeps the up-to-date ones",
ComposeCmd("up", "-d", "--scale", "test=2"),
ReplicaNumbers("test", 3, 4))
}