Write a contract¶
Goal: an aval.yaml that lists every input of your test, so a result is
reused only when none of them changed.
flowchart LR
c([aval.yaml]) --> s[scope] --> fp[fingerprint] --> d{reuse?}
c --> k[cacheability] --> d
The example¶
A Go test against Redis from Docker Compose,
examples/go-compose/aval.yaml:
apiVersion: aval.trianalab.io/v1
kind: Test
metadata:
name: counter-integration
runner:
command: ["sh", "-c", "docker compose up -d --wait && REDIS_ADDR=localhost:16379 go test -tags=integration -count=1 ./...; rc=$?; docker compose down; exit $rc"]
scope:
source: ["**/*.go", "compose.yaml"]
dependencies: [go.mod]
environment:
toolchain: {go: "1.25"}
cacheability: controlled-environment
aval init writes a first one (Quickstart).
For a project that will hold many tests, aval init --set starts from two
files instead: aval.yaml as a kind: TestSet with your test and a
sources fragment for the globs, and .github/aval/policy.yaml with one
placeholder keyless identity:
The identity's subjectRegexp names <OWNER>/<REPO>, so it trusts no signer
until you replace it with your CI's
(Trust policy). aval explain --all checks the set.
Step by step¶
- Name the test.
metadata.namesays what is checked. It does not change when inputs do. - Say how to run it.
runner.commandis an argv list. No shell unless you call one. - List the inputs.
scope.sourcefor files the test reads,scope.dependenciesfor manifests and lockfiles. Globs are relative to the contract;**is any depth. A glob that matches nothing means no fingerprint: the test always runs (excludeglobs may match nothing). - Pick a cacheability class. How safe reuse is. Tags and ranges block
reuse for
hermeticandcontrolled-environment: pin them.
| Class | Use it when | Reused |
|---|---|---|
hermetic |
the test reads only the declared files | yes |
controlled-environment |
it uses services you pin, such as a Compose image by digest | yes |
external-dependencies |
it calls something you do not control | only if the policy allows it |
non-deterministic |
the same inputs can give a different result | never |
Check the scope¶
It prints one line per input: kind, sha256, x if executable, path. Not
listed means not hashed.
Restore coverage and reports on reuse¶
A reused result writes no coverage file. List the files later steps read
under outputs, e.g. outputs: [coverage.xml, junit.xml]:
aval run --pushandaval pushpublish them, signed.aval pushreads them from disk: push right afteraval run.aval runwrites them back after a result verifies.aval verifywrites nothing.
Outputs are published
Anyone who can read the registry can read them. On reuse Aval writes any
file a trusted signer published under the globs, except the contract and
its inputs. List no secrets and keep the globs narrow: ** can overwrite
files later steps read. Caps: outputs.
Find undeclared inputs¶
A file the test reads but scope misses is a false hit waiting to happen.
On Linux, compare what the test opens with aval explain:
strace -f -e trace=openat -o trace.txt pytest tests/unit
grep -v ENOENT trace.txt | grep -o '"[^"]*"' | tr -d '"' | sed "s|^$PWD/||" \
| grep -v '^/' | sort -u > read.txt
aval explain | awk '$1 == "source" || $1 == "dependency"' \
| cut -d' ' -f4- | sort -u > scope.txt
comm -23 read.txt scope.txt # prints tests/conftest.py
Read the list, do not paste it: some lines are caches. On macOS use
sudo fs_usage -w -f pathname <process>, on Windows Process Monitor.
Anywhere: copy only the scope to an empty directory and run the test there.
Read next¶
- Shards and matrices
- Write a trust policy
- The test contract: every field and rule