I wrote a ten-minute shell test suite and put it in front of every push

I wrote the test suite for our deployment scripts, and I got it wrong. It wasn’t buggy. Every test passed. The problem was its size and shape, and that’s harder to notice. The suite took more than ten minutes and ran as a commit hook and a push hook. That made a one-line change to the deployments tree, like bumping an image pin, take as long as a coffee break. Eventually pushes stopped working at all.

This post covers what I built, what it broke, and what I’m changing.

What I built

Each deployment script got a test file next to it, and a CI gate runs them all in sequence. By the end there were about four dozen test files. Together they had more lines than the scripts they tested. A full run took a bit over ten minutes, and eight tests accounted for about 70% of that.

Those slow tests all followed the same pattern:

  • Config checks tested against the full rendered output. Each check that rejects a bad configuration got a table of dozens of cases. Every case rendered the entire example configuration, over a hundred documents, changed one line with sed, and ran the whole check on the result. One test had about sixty of these cases and took over two minutes.
  • Cloud-facing scripts tested end to end against stubs. The scripts that talk to our cloud provider, our DNS/CDN provider and the cluster each got 60 to 80+ cases. Every case built a throwaway copy of the tree with stub CLIs and stub curl, then ran the full script. Two of these test files were longer than the scripts they covered.
  • Real waiting. One script polls until things become ready. Its test set the poll interval to one second instead of zero, so every timeout case actually waited several seconds.
  • Slow scripts made the tests slower. The scripts were written as one process per step: a grep per document per lookup, a jq per field. One helper could start hundreds of grep processes for a single lookup, ran several lookups per check, and the test ran the check dozens of times. So each case took one to three seconds, and a table of sixty cases took minutes.

What it broke

  • Pushes failed. The pre-push hook ran so long that the SSH connection to the remote timed out. The hooks passed, then the push died with a broken pipe. The only way to push was --no-verify, which skips every hook, not just the slow one.
  • Commits were slow too. The hook had no stage restriction, so it ran on commit as well as on push.
  • CI paid for it on every run. The pipeline ran the same gate on every push, so every CI run spent ten minutes on shell tests, whatever had changed.
  • The tests didn’t buy much confidence. The big stub-based tables rewrote each script’s branches case by case. A stub can check that a script passes the right arguments to a CLI. It can’t tell you whether the real cloud or DNS API will accept the call. The staging deploy already tests that properly, with real inputs.

What I should have written

One small input per rule. When a check refuses a configuration that breaks a rule, that rule needs one test. The input should be the smallest document that breaks the rule, not the whole rendered output with one line changed. A test like that should take milliseconds.

A smoke case for scripts that only act on a cloud. Deploy, DNS, bootstrap and in-cluster scripts get exercised for real in staging. Against stubs, one or two happy-path cases are enough to check that arguments get passed through correctly. Sixty to eighty cases just restate the script and add maintenance work.

No real time in tests. Any script that polls should get its sleep from a function or variable that the test can set to zero. One of my own tests already did this with a stub sleep. I should have done the same everywhere.

Script performance is part of the review. The check scripts already call python3. Parsing the rendered output once there, or indexing documents by kind and name once, would make each run take milliseconds. A shell loop that starts a process per element per query should be fixed before anyone writes tests around it.

Slow suites don’t go on hooks. Commit and push hooks have to finish in seconds. Longer suites belong in the pipeline. Even there, ten minutes of tests for shell scripts means the tests are the wrong shape.

What I’m changing

  1. Moving the shell-test gate off commit and push. It will run in CI, and only when the deployments tree changes.
  2. Rewriting the config-check tests to use a minimal fixture per rule, and replacing the per-document grep loops in the check scripts with a single parse.
  3. Cutting the stub-based tables for cloud-facing scripts down to a smoke case or two, and relying on the staging deploy for real coverage.
  4. Making every poll interval and sleep in the scripts configurable down to zero, and setting them to zero in tests.
  5. Including measured wall time in reviews of test changes. I’ll run each test I add and write down how long it took. Anything over a few seconds for a unit-style test needs a justification.

A checklist for reviewers (and for me)

  • Does a table test build its per-case fixture from the whole tree or the whole rendered output when the rule only needs one document?
  • Is the test file longer than the script, re-stating its branches against stubs?
  • Does any test call a real sleep or use a non-zero poll interval?
  • Does the script start a process per element inside a loop, especially in code a test calls many times?
  • Is anything on the commit or push path measured in minutes?
  • Did the author run the test and report its wall time?

Lots of tests felt like diligence while I was writing them. But tests cost time on every run, and I never measured that cost. Everyone who pushed to the repo paid for it until the push path broke. From now on I’ll time my tests and keep them small.

Advanced search: filter by model or tag