compose-go/TESTING.md
Nicolas De Loof 94b0a5aabc template: fuzz Substitute and ExtractVariables
Add native Go fuzz targets for the interpolation parsers, locking that no
template can make them panic or behave non-deterministically, and that
escaping every $ turns any text into a template that interpolates to itself.

Add a `make fuzz` target running every fuzz target of the repository for a
configurable duration, and document fuzzing in TESTING.md. The seeds run as
plain tests in `go test ./...`.

Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
2026-10-23 16:00:08 +02:00

5.3 KiB

Testing compose-go: the contract

compose-go is the reference implementation of the Compose Specification. Its tests are the executable counterpart of the spec: the document humans read to know what behavior is locked, and the surface coding agents work against. A test must state what clause of the spec it locks and express its expectation in the most legible language available — ideally compose YAML itself, not Go internals.

Official patterns

Two patterns have proven themselves in this codebase; new tests should use one of them.

Attribute conformance tests — loader/tests/

One file per compose attribute, named after it (pull_policy_test.go, extra_hosts_test.go). Each file:

  • opens with a comment linking the spec section it locks (https://github.com/compose-spec/compose-spec/blob/main/05-services.md#<attribute>) and quoting the normative sentence being exercised;
  • loads inline YAML via the load(t, yaml) helper and asserts on the result;
  • re-runs its expectations after roundTrip(t, p) (YAML and JSON marshal/reload), so the attribute is locked end to end.

The completeness test (loader/tests/completeness_test.go) walks the JSON schema and fails when a service attribute, a top-level element (networks, volumes, secrets...) or a property of the main definitions (network, volume, healthcheck, job...) has no declared test file, or when the declared file does not use it as a YAML key: adding a property to the schema forces either a conformance test or an explicit, reviewed entry, with its reason, in the matching known-gaps list. It does not descend into deeper structures (deploy, build, the entries of ports or volumes...), and it guards against a stale declaration rather than proving that a test exercises the attribute.

YAML-in / YAML-out — override/, and loadsAs in loader/tests/

When the behavior under test is a document transformation (merge, override, canonicalization of short syntaxes), express the expectation as literal YAML, not as Go struct literals:

  • override/: every test is one assertMergeYaml(t, base, override, want) call — three YAML blocks, nothing else.
  • loader/tests/: loadsAs(t, input, canonical) asserts that the input loads into the canonical model expressed as YAML (the project is loaded, marshaled back, and compared as YAML trees). Prefer it over field-by-field assertions whenever the interesting behavior is how a syntax is parsed and canonicalized; keep typed assertions for behaviors the canonical form does not surface.

The point of both: the expected value reads as compose documentation, is diffable in review, and requires no knowledge of types.Project internals.

Fuzzing — Fuzz* in *_fuzz_test.go

The parsers read untrusted text (Compose files, .env files, interpolated values, byte sizes, ports, volumes...). Each one has a native Go fuzz target (testing.F) next to its package, locking properties that must hold for any input: never panic, stay deterministic, and round-trip (parse, print, parse) wherever the syntax has a canonical form. Properties that only hold for well-formed input do not belong there.

  • The seeds (f.Add, and testdata/fuzz/<FuzzName>/ when a minimized input is worth keeping) run as plain tests in go test ./...: they must be small, deterministic and pass on every OS. A crash the fuzzer finds becomes a readable regression test in the package's usual style first; commit its testdata/fuzz entry only if it adds something the test does not.
  • Like every test, a fuzz target opens with a comment stating the invariant it locks.
  • Fuzzing itself is not part of CI. make fuzz runs every target for FUZZTIME (default 10s) each; use make fuzz FUZZTIME=2m for a deeper run, or one target at a time:
go test -run '^$' -fuzz '^FuzzSubstitute$' -fuzztime 2m ./template

Rules

  • State the intent. Every test (or file, for single-attribute files) carries a comment saying what invariant it locks — phrased as an obligation, with a link to the spec section, and to the issue for regression tests. A reviewer must never have to reverse-engineer why a value is expected.
  • Tables name their cases and run them. A table with a name/doc field must execute each row inside t.Run(name, ...); a case label that does not appear in failure output is dead weight.
  • Error assertions check the config path and the message. Use assert.ErrorContains with the offending config path (services.foo.ports) plus the human-readable fragment. Do not assert full exact error strings unless the message itself is the contract (validation/ does this deliberately).
  • Know what the helpers skip. load / loadYAML set SkipNormalization and SkipConsistencyCheck: they test the parse/merge/interpolate stages only. A behavior involving normalization or consistency checks needs an explicit Load call with those stages enabled — do not assume the helper covered them.
  • A new check or helper is generic and named after what it observes, and lives next to the existing ones (loader/tests/helpers_test.go, override/merge_test.go) where the whole vocabulary can be reviewed as one file.

Running

make test              # everything
go test ./loader/...   # one package
go test ./loader/tests/ -run TestExtraHosts -v