Safeguards against weakened tests: test guard, owner approval, weekly mutation check
ci / test-guard (pull_request) Successful in 3s
ci / go (pull_request) Successful in 4m33s
ci / package (pull_request) Successful in 2m56s
ci / install-test (debian:12) (pull_request) Successful in 1m21s
ci / install-test (debian:13) (pull_request) Successful in 1m17s
ci / smb (pull_request) Successful in 1m11s
ci / e2e (pull_request) Successful in 2m59s
ci / test-guard (pull_request) Successful in 3s
ci / go (pull_request) Successful in 4m33s
ci / package (pull_request) Successful in 2m56s
ci / install-test (debian:12) (pull_request) Successful in 1m21s
ci / install-test (debian:13) (pull_request) Successful in 1m17s
ci / smb (pull_request) Successful in 1m11s
ci / e2e (pull_request) Successful in 2m59s
Development is largely automated; when a test fails, the shortest path to a green CI is changing the test. Three safeguards: - Test guard (CI job, every PR): changing or deleting existing test lines, deleting a test file or lowering .coverage-min / .mutation-min fails unless a commit message says "Test changed because: …". Adding tests is free. The script is taken from the target branch, so a PR can't disable it for itself. Checked against 7 cases (new test, changed expectation with/without reason, coverage minimum lowered/raised, deleted test file, code-only change). - Owner approval, optional: with the repo variable TEST_GUARD_APPROVER such a PR also needs that person's approving review of the latest commit (checked through the API: state, user, commit, not dismissed). - Weekly mutation check: gremlins on server, access mapping, path confinement and auth; efficacy may not fall below .mutation-min. gremlins' own --threshold-efficacy doesn't change its exit code (v0.5.0, verified), so the script compares the summary itself. AGENTS.md: what to do when a test fails (spec first, reproduce on the real system, fix the code — or say why the test was wrong). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
987c55a877
commit
d8ffbfd7e2
7 files changed
+200
No files matched your search
@@ -62,6 +62,30 @@ jobs:
|
||||
- name: known vulnerabilities
|
||||
run: go run golang.org/x/vuln/cmd/govulncheck@latest ./...
|
||||
|
||||
test-guard:
|
||||
# A pull request that weakens existing tests needs a reason in a commit
|
||||
# message (and, if the repo variable TEST_GUARD_APPROVER is set, that
|
||||
# person's approval). The guard script comes from the target branch, so
|
||||
# a pull request can't switch it off for itself.
|
||||
if: github.event_name == 'pull_request'
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v7
|
||||
with:
|
||||
fetch-depth: 0
|
||||
- name: existing tests weakened?
|
||||
env:
|
||||
TEST_GUARD_APPROVER: ${{ vars.TEST_GUARD_APPROVER }}
|
||||
API_URL: ${{ github.api_url }}
|
||||
REPO: ${{ github.repository }}
|
||||
PR_NUMBER: ${{ github.event.pull_request.number }}
|
||||
HEAD_SHA: ${{ github.event.pull_request.head.sha }}
|
||||
TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
run: |
|
||||
base="origin/${{ github.base_ref }}"
|
||||
git show "$base:scripts/test-guard.sh" > /tmp/test-guard.sh 2>/dev/null || cp scripts/test-guard.sh /tmp/test-guard.sh
|
||||
sh /tmp/test-guard.sh "$base" "${{ github.event.pull_request.head.sha }}"
|
||||
|
||||
package:
|
||||
# .deb packages for every run (pull requests test them below); pushes to
|
||||
# main also go to the Gitea Debian registry, channel "dev".
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
# Weekly mutation check: do the tests still catch wrong code? The efficacy
|
||||
# per package may not fall below .mutation-min (scripts/mutation-check.sh).
|
||||
name: mutation
|
||||
on:
|
||||
schedule:
|
||||
- cron: '17 3 * * 0'
|
||||
workflow_dispatch:
|
||||
|
||||
jobs:
|
||||
mutation:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v7
|
||||
- uses: actions/setup-go@v6
|
||||
with:
|
||||
go-version-file: go.mod
|
||||
- name: PAM headers (cgo)
|
||||
run: |
|
||||
SUDO=$(command -v sudo >/dev/null && echo sudo || true)
|
||||
$SUDO apt-get update -qq && $SUDO apt-get install -y -qq libpam0g-dev samba-common-bin
|
||||
- name: gremlins
|
||||
run: go install github.com/go-gremlins/gremlins/cmd/gremlins@v0.5.0
|
||||
- name: mutation efficacy (.mutation-min)
|
||||
run: GREMLINS="$(go env GOPATH)/bin/gremlins" scripts/mutation-check.sh
|
||||
@@ -0,0 +1,7 @@
|
||||
# Minimum mutation test efficacy in % (killed of covered mutants, gremlins),
|
||||
# checked weekly by scripts/mutation-check.sh. Raise a number when it went up;
|
||||
# lowering one needs "Test changed because:" in the commit message (test guard).
|
||||
internal/server 75
|
||||
internal/domain/share 85
|
||||
internal/rootfs 83
|
||||
internal/auth 88
|
||||
@@ -53,6 +53,20 @@ go run ./cmd/sharestead -openapi > dist/openapi.json && (cd web && npm run lint:
|
||||
- UI states (greyed out, warnings): add a scenario to `web/e2e/fixtures/scenarios.json` and its
|
||||
expected UI to `web/e2e/ui-states.spec.ts`; `fixtures_test.go` checks the fixture against the API.
|
||||
|
||||
## When a test fails
|
||||
|
||||
1. **Read the requirement** the test refers to (02-requirements, 07, the API description).
|
||||
2. **Reproduce it** — on the real system where possible (CT 151, VM 152, `smbclient`), not only
|
||||
in the fake. A fake that is too friendly or too strict is a bug in the test.
|
||||
3. **Decide from the spec, not from the code:** code wrong → fix the code (the test stays);
|
||||
test wrong → fix the test and say why in the commit message:
|
||||
`Test changed because: <requirement / what was broken in the test, and why the code is right>`.
|
||||
A test that passes only on its retry is a failure too (CI fails on flaky tests).
|
||||
4. CI enforces this: the **test guard** fails a pull request that changes or deletes existing
|
||||
test lines, deletes a test file or lowers `.coverage-min` / `.mutation-min` without that block
|
||||
(adding tests is free). The guard script comes from the target branch, so a PR can't switch it
|
||||
off for itself. Lowering a minimum is weakening a test.
|
||||
|
||||
## UI changes: check them in a real browser first
|
||||
|
||||
Test instance: **CT 151** (`https://192.168.0.157:8443`), test admin `ssttest`. It's yours to
|
||||
|
||||
@@ -140,6 +140,21 @@ like the real system (e.g. `useradd` adds the account, a missing path is `fs.Err
|
||||
| Coverage | **may not fall** (ratchet): minimums in `.coverage-min` for the total and the security-relevant packages, checked by `scripts/coverage-check.sh`; raised when coverage goes up. Target: total ≥ 80 %, parsers/auth/access/path checks ≥ 90 %, API ≥ 80 %, system adapters ≥ 60 % (rest via test instances). Authorization matrix 100 % of endpoints |
|
||||
| Release candidate → release | all of the above + E2E + package tests (incl. **upgrade from the previous release**) + nightly NFS/ZAP/crash tests green on the rc; milestone checklist passed on CT 151; release notes list security and compatibility notes |
|
||||
| Regression | every bug fix comes with a test that fails without the fix |
|
||||
| Flaky | a test that passes only on its retry fails CI (`failOnFlakyTests`) — a failure on every first try was a real bug once |
|
||||
|
||||
### 3.2b Safeguards against weakened tests
|
||||
|
||||
Development is largely done by AI agents; under pressure to get CI green, the shortest path is to
|
||||
change the test. Three safeguards (owner, 2026-11-30):
|
||||
|
||||
| Safeguard | What it does | Owner's choice |
|
||||
|---|---|---|
|
||||
| **Test guard** (`scripts/test-guard.sh`, CI job `test-guard`) | a PR that changes/deletes existing test lines, deletes a test file or lowers `.coverage-min` / `.mutation-min` fails unless a commit message has `Test changed because: …`. Adding tests is free. The script is taken from the target branch | — |
|
||||
| **Owner approval** (same job) | with the repo variable `TEST_GUARD_APPROVER` set (e.g. to the owner's user name), such a PR also needs that person's approving review of its latest commit — an AI agent can't give it. Without the variable the reason suffices | set the variable (Repo → Settings → Actions → Variables), re-run the job after approving |
|
||||
| **Weekly mutation check** (`.gitea/workflows/mutation.yml`, `scripts/mutation-check.sh`) | gremlins on the security-relevant packages; the share of injected faults the tests catch may not fall below `.mutation-min` | — |
|
||||
|
||||
For any of this to bind, `main` needs branch protection with the CI jobs as required checks
|
||||
(including `test-guard`): a PR that deletes a job then can't be merged either.
|
||||
|
||||
### 3.3 Test environments
|
||||
|
||||
|
||||
Executable
+30
@@ -0,0 +1,30 @@
|
||||
#!/bin/sh
|
||||
# Mutation check: do the tests catch wrong code? gremlins changes conditions
|
||||
# and arithmetic in each package of .mutation-min and runs its tests; the
|
||||
# share of changes a test catches (efficacy) may not fall below the minimum.
|
||||
# # on CT 140, in the repo (takes a few minutes)
|
||||
# go install github.com/go-gremlins/gremlins/cmd/gremlins@v0.5.0
|
||||
# scripts/mutation-check.sh
|
||||
set -eu
|
||||
gremlins=${GREMLINS:-gremlins}
|
||||
fail=0
|
||||
for line in $(grep -v '^#' .mutation-min | awk 'NF == 2 { print $1 "=" $2 }'); do
|
||||
pkg=${line%=*} min=${line#*=}
|
||||
echo "== $pkg (minimum $min %)"
|
||||
# gremlins' own --threshold-efficacy doesn't change its exit code (v0.5.0),
|
||||
# so the efficacy is read from its summary and compared here.
|
||||
"$gremlins" unleash --workers 2 --timeout-coefficient 60 "./$pkg" > /tmp/mutation.$$ 2>&1 || true
|
||||
grep -E "LIVED|Killed:|Timed out|efficacy|coverage" /tmp/mutation.$$ || true
|
||||
eff=$(sed -n 's/^Test efficacy: \([0-9.]*\)%.*/\1/p' /tmp/mutation.$$)
|
||||
if [ -z "$eff" ]; then
|
||||
fail=1
|
||||
echo "::error::no mutation result for $pkg"
|
||||
elif awk -v a="$eff" -v b="$min" 'BEGIN { exit !(a < b) }'; then
|
||||
fail=1
|
||||
echo "::error::mutation efficacy of $pkg fell to $eff % (minimum $min %)"
|
||||
elif awk -v a="$eff" -v b="$min" 'BEGIN { exit !(a >= b + 3) }'; then
|
||||
echo "$pkg: $eff % — raise its minimum in .mutation-min (now $min %)"
|
||||
fi
|
||||
done
|
||||
rm -f /tmp/mutation.$$
|
||||
exit $fail
|
||||
Executable
+86
@@ -0,0 +1,86 @@
|
||||
#!/bin/sh
|
||||
# Test guard: a change that weakens existing tests needs a reason.
|
||||
# Adding tests is free. Changing or deleting lines of existing tests,
|
||||
# deleting a test file, or lowering a minimum in .coverage-min /
|
||||
# .mutation-min needs a block in one of the commit messages:
|
||||
#
|
||||
# Test changed because: <which requirement says the old expectation was
|
||||
# wrong — or why the test itself was broken — and why the code is right>
|
||||
#
|
||||
# Usage: scripts/test-guard.sh <base> <head> (CI passes the PR's base)
|
||||
set -eu
|
||||
base=${1:?base}
|
||||
head=${2:-HEAD}
|
||||
mb=$(git merge-base "$base" "$head")
|
||||
|
||||
is_test() {
|
||||
case "$1" in
|
||||
*_test.go | */testdata/* | web/e2e/* | *.test.ts | .coverage-min | .mutation-min) return 0 ;;
|
||||
esac
|
||||
return 1
|
||||
}
|
||||
|
||||
# lowered reports names whose minimum went down or disappeared in a minimum file.
|
||||
lowered() {
|
||||
git show "$mb:$1" 2>/dev/null | grep -v '^#' | while read -r name min; do
|
||||
[ -n "$name" ] || continue
|
||||
new=$(git show "$head:$1" 2>/dev/null | awk -v n="$name" '$1 == n { print $2 }')
|
||||
if [ -z "$new" ] || awk -v a="$new" -v b="$min" 'BEGIN { exit !(a < b) }'; then
|
||||
echo "$1: $name $min → ${new:-removed}"
|
||||
fi
|
||||
done
|
||||
}
|
||||
|
||||
touched=""
|
||||
git diff --name-status "$mb" "$head" | while IFS="$(printf '\t')" read -r status path rest; do
|
||||
[ -n "$rest" ] && path=$rest # renames: the new name
|
||||
is_test "$path" || continue
|
||||
case "$status" in
|
||||
A) continue ;;
|
||||
D) echo "deleted: $path" ;;
|
||||
*)
|
||||
case "$path" in
|
||||
.coverage-min | .mutation-min) lowered "$path" ;;
|
||||
*)
|
||||
n=$(git diff "$mb" "$head" -- "$path" | grep '^-' | grep -v '^---' | grep -cv '^-[[:space:]]*$' || true)
|
||||
[ "$n" -gt 0 ] && echo "changed: $path ($n existing lines changed or removed)"
|
||||
;;
|
||||
esac
|
||||
;;
|
||||
esac
|
||||
done > /tmp/test-guard.$$ || true
|
||||
touched=$(cat /tmp/test-guard.$$)
|
||||
rm -f /tmp/test-guard.$$
|
||||
|
||||
if [ -z "$touched" ]; then
|
||||
echo "test guard: no existing test weakened"
|
||||
exit 0
|
||||
fi
|
||||
echo "test guard: existing tests changed:"
|
||||
echo "$touched" | sed 's/^/ /'
|
||||
reason=$(git log --format=%B "$mb..$head" | awk '/^Test changed because:/ { on = 1 } on { print } /^$/ { on = 0 }')
|
||||
if [ "$(printf '%s' "$reason" | wc -c)" -lt 40 ]; then
|
||||
echo "::error::Existing tests were changed, but no commit message says why. Add a block starting with 'Test changed because:' — which requirement shows the old expectation was wrong (or the test itself was broken), and why the code is right. If the code is wrong, fix the code instead."
|
||||
exit 1
|
||||
fi
|
||||
echo "reason given in the commit message:"
|
||||
echo "$reason" | sed 's/^/ /'
|
||||
|
||||
# Optional: TEST_GUARD_APPROVER (a repo variable) also requires that person's
|
||||
# approving review of the pull request's latest commit — a step an AI agent
|
||||
# can't do for itself. Needs API_URL, REPO, PR_NUMBER, HEAD_SHA, TOKEN.
|
||||
[ -n "${TEST_GUARD_APPROVER:-}" ] || exit 0
|
||||
reviews=$(curl -sf -H "Authorization: token $TOKEN" "$API_URL/repos/$REPO/pulls/$PR_NUMBER/reviews") || {
|
||||
echo "::error::test guard: can't read the reviews of PR $PR_NUMBER"
|
||||
exit 1
|
||||
}
|
||||
if printf '%s' "$reviews" | APPROVER="$TEST_GUARD_APPROVER" SHA="$HEAD_SHA" python3 -c '
|
||||
import json, os, sys
|
||||
ok = any(r.get("state") == "APPROVED" and (r.get("user") or {}).get("login") == os.environ["APPROVER"]
|
||||
and r.get("commit_id") == os.environ["SHA"] and not r.get("dismissed") for r in json.load(sys.stdin))
|
||||
sys.exit(0 if ok else 1)'; then
|
||||
echo "approved by $TEST_GUARD_APPROVER"
|
||||
exit 0
|
||||
fi
|
||||
echo "::error::Existing tests were changed: $TEST_GUARD_APPROVER has to approve this pull request (latest commit), then run this job again."
|
||||
exit 1
|
||||
Reference in new issue
Block a user