Testing¶
The kinds of tests¶
flowchart LR
unit[Unit tests<br/>pure functions, fakes] --> golden[Golden files<br/>render output]
golden --> envtest[envtest<br/>real API server + etcd]
envtest --> store[Store tests<br/>real Postgres]
store --> itest[Integration<br/>real builds on the cluster]
itest --> live[Live checks<br/>watchers during migrations]
| Kind | Where | Needs | What it proves |
|---|---|---|---|
| Unit | every package | nothing | parsing, validation, planning, detection, hashing, log parsing… |
| Golden | internal/render (testdata/*.golden.yaml) |
nothing | the exact Kubernetes objects a spec renders to |
| envtest | internal/controller, internal/platform |
a test API server and etcd (downloaded by setup-envtest) |
controllers against a real Kubernetes API: apply, adoption, pruning, hooks, needs, finalizers |
| Store | internal/store, internal/api, internal/platform |
Postgres (TEST_DATABASE_URL) |
SQL, migrations, the run queue, sessions |
| Integration | internal/pipeline (build tag integration) |
the cluster's BuildKit pool | a real Dockerfile build and a real Railpack build, pushed with a digest |
| Windows app | escritorio (a module of its own) |
nothing | the wsl package against a fake wsl.exe, and that the app compiles for Windows (Setting up) |
| Quickstart | hack/ci-inicio-rapido.sh, in GitHub Actions (inicio-rapido.yml) |
a new amd64 machine with systemd and sudo | the installer for one computer, with no container engine and with Docker: rendimiento answers; then the example app (rendimiento-ai/hola) is deployed until its address answers, with a stand-in for GitHub's pages (hack/github-de-prueba.py); how long each part took (notices on the run; more than ten minutes fails), and the uninstaller. It runs when the installer or the test changes, with the program built from that commit, and after each release, with the release |
| Quickstart on a cluster | hack/ci-inicio-rapido-k3d.sh, in GitHub Actions (inicio-rapido-k3d.yml) |
a new amd64 machine with Docker | the quickstart's commands for k3d: a cluster with its registry, the chart of that commit with the image built from it; rendimiento answers at rendimiento.localhost:8080, the example app is deployed until http://hola.localhost:8080 answers and rendimiento shows that address, with the same stand-in for GitHub's pages; how long each part took (more than ten minutes fails), and the cluster is deleted. It runs when the chart, internal/render or the test changes |
| UI | web |
Node | the TypeScript compiles (npm run typecheck) |
| Browser | web/e2e |
Playwright, Postgres, Go | the real UI and API in a browser, in English and Spanish: "New app" with every example of internal/spec/ejemplos, then every page in the menu, with no error in the browser and no 500 from the API (below) |
Running them¶
make test-remote # everything, on a worker (use this)
go test ./internal/spec ./internal/render # light packages, fine anywhere
go test ./internal/render -run Golden -update # rewrite golden files after an intended change
make itest # real builds on the BuildKit pool (minutes)
Remote tests¶
make test-remote (hack/test-remote.sh) runs the steps of make test in a pod on a worker, because compiling and running envtest on main (the control plane) slows the whole cluster:
echo "test-remote: starting $POD…"
kubectl wait -n "$NS" --for=condition=Ready "pod/$POD" --timeout=10m >/dev/null
echo "test-remote: on $(kubectl get pod -n "$NS" "$POD" -o jsonpath='{.spec.nodeName}'); shipping the working tree"
git ls-files -co --exclude-standard -z | tar --null -T - -czf - | kubectl exec -i -n "$NS" "$POD" -c go -- tar xzf - -C /src
go_status=0
kubectl exec -n "$NS" "$POD" -c go -- env CONTROLLER_GEN="$CONTROLLER_GEN" SETUP_ENVTEST="$SETUP_ENVTEST" ENVTEST_K8S="$ENVTEST_K8S" bash -euo pipefail -c "
until (echo > /dev/tcp/127.0.0.1/5432) 2>/dev/null; do sleep 1; done
hack/ci-test.sh
" || go_status=$?
# Bring back what generate produced, so the repo matches what was tested.
kubectl exec -n "$NS" "$POD" -c go -- tar czf - api/v1alpha1/zz_generated.deepcopy.go internal/spec/zz_generated.deepcopy.go deploy/crds | tar xzf - -C .
ui_status=0
kubectl exec -n "$NS" "$POD" -c node -- bash -euo pipefail -c "
cd /src/web
echo '== ui typecheck'
npm ci --prefer-offline --no-audit --no-fund --loglevel=error
npm run typecheck
" || ui_status=$?
- A pod with three containers is created in
rendimiento-builds, avoiding the control plane and anyTEST_EXCLUDE_NODES: go (the toolchain), node (the typecheck) and postgres (a sidecar the store tests use onlocalhost). - A node-local cache volume (
rendimiento-test-cache, local-path) keeps Go modules, the build cache, envtest binaries and npm's cache between runs; the first run fills it (about 20 minutes), later runs reuse it. - The working tree, including uncommitted changes, is streamed in as a tarball.
-
hack/ci-test.shrunscontroller-gen,go vetandgo test -p 1 ./..., then the Windows app's module (escritorio/); thennpm run typecheckruns, their output streamed here:# store, platform and api share one test database, so in one step they run # one at a time; the rest (internal/controller's envtest among them) run # beside them, two packages at a time (with the memory the test step has). # Split into steps, api and platform have a database each, on another node. case "$parte" in api) shared='/internal/api$' rest='^$' ;; plataforma) shared='/internal/platform$' rest='^$' ;; resto) shared='/internal/store$' rest='/internal/(api|platform|store)$' ;; *) shared='/internal/(api|platform|store)$' rest='/internal/(api|platform|store)$' ;; esac out=$(mktemp -d) go test -v -p 1 $(go list ./... | grep -E "$shared") >"$out/shared.log" 2>&1 & a=$! others=$(go list ./... | grep -vE "$rest" | grep -vE "$shared" || true) if [ -n "$parte" ] && [ "$parte" != resto ]; then others=""; fi if [ -n "$others" ]; then go test -v -p 2 $others >"$out/rest.log" 2>&1 & b=$! else : >"$out/rest.log" b="" fi -
Regenerated files are copied back, so the repository matches what was tested.
- The pod is deleted.
-p 1 runs packages one after another: the store, platform and API tests share one database. On every push they do not: CI_PARTE splits the script into three steps, each with its own database (below).
The store, platform and API tests each migrate a database once per package (<db>_plantilla, twice, since migrating must be idempotent) and give each test a copy of it (CREATE DATABASE … TEMPLATE), dropped when the test ends: pgprueba.Base in internal/pgprueba. A new migration costs one more statement, not one more per test, and a test starts with an empty, migrated database whatever earlier tests wrote. hack/ci-test.sh connects with synchronous_commit=off, since a throwaway database need not wait for its disk at each commit. Dropping the schema and migrating again for every test had made the store's tests six to eight minutes long on a busy node, and the API's four.
On every push¶
rendimiento's own repository is an app on rendimiento: its rendimiento.yaml lists the book (a service) and the platform's image (builds: platform). So every push and pull request also runs the same hack/ci-test.sh, as the platform:test step, with a throwaway Postgres (postgres: true) and a kept cache (cache: true); then platform:build builds the image, whose Dockerfile runs npm run typecheck. The result is the check on the commit in GitHub. In CI the script also fails when the generated files (deepcopy, CRDs) were not committed (CHECK_GENERATED=1). A pull request from a fork is never run by rendimiento (its code would run with the installation's token); .github/workflows/forks.yml runs hack/ci-test.sh, the book and the browser tests for it on GitHub's machines, with no secret. make test-remote is still the way to test uncommitted changes. See How rendimiento builds itself.
Browser tests¶
The interfaz-en and interfaz-es tasks of rendimiento.yaml run hack/ci-interfaz.sh (one language each, PROYECTO, at the same time) in the Playwright image on every push, with the task's throwaway Postgres and kept cache (Go is installed there the first time). It builds the UI and hack/interfaz, a server with the real API on that empty database, as on a computer without Kubernetes, and a fake GitHub that serves every example of internal/spec/ejemplos as a repository (and one without a rendimiento.yaml). It signs in a person, and Playwright drives the pages (web/e2e). Every test fails on an error thrown in the browser, an error in the console, or an API answer of 500 or more, even when what it looked for was there. A new kind of app only needs its example: the wizard test walks every one. With a cache it takes about three minutes. The task asks for two cores, and the tests wait up to 30 seconds for what they look for (two minutes for a whole test): on a busy cluster, one core and shorter waits failed tests that were right. They are not retried, since they share one database and a second try would find what the first made.
Writing tests¶
Unit tests: table-driven¶
for _, tc := range []struct{ yaml, want string }{
{"services: [{name: a, lan: {ip: 8.8.8.8}}]", "private IPv4"},
} { … }
Name what is being checked in the failure message, and include the got/want values.
Fakes over mocks¶
Code depends on small interfaces, so tests pass simple fakes: a fakeExec that records execution order, a fakeDNS that keeps records in a map, a fakeGit serving an fstest.MapFS, a fakeSRV resolver, an httptest.Server playing a Helm repository. No mocking framework is needed.
envtest¶
setup(t) / setupAddons(t) start a real API server and etcd, install the CRDs from deploy/crds, start the manager with the controller under test, and return a client. Tests create objects and use eventually(t, what, cond) to wait (up to 20 seconds) for the controller to act.
envtest has no kubelet and no controllers besides yours: pods never run, Deployments never become ready, garbage collection never happens. Tests play those parts when needed: TestAddonHooks marks hook pods Succeeded or Failed by updating their status.
Golden files¶
render_test.go renders a spec and compares the YAML with testdata/*.golden.yaml. An intended change: run with -update, then read the diff before committing; it is the clearest possible review of what users' workloads will look like.
What is not covered yet¶
- the UI beyond "New app" and opening each page (runs, releases, settings);
- end-to-end tests of the whole platform on a disposable cluster (onboard, push, release, rollback);
- load tests of the run queue and many concurrent builds.
These are on the roadmap.