I wrote 3,000 lines of tests for files CI already tests, and called it safety
Over about seven weeks I built, and kept growing, a set of test modules that checked our development tooling. They covered the CI pipeline definitions, the pre-commit hook configuration, the Makefile, and the deployment pipelines. They were thorough. About 300 test cases read those files as plain text and asserted what each one had to say. Almost none of them told us anything the CI system wasn’t already telling us on the same push.
I wrote a second, smaller group in the same suite: about 1,300 lines of policy checks across our repositories, written as pytest. Those check real properties, but I put them in the wrong place, and one of them made network calls from inside the unit-test gate.
This post covers what I did, why it was wrong, and what I’ll do differently.
What I built
Text tests over tooling files
Five modules, about 3,200 lines and 305 tests in total, opened YAML and config files as strings and checked their structure with regular expressions and indentation parsing. The workspace deliberately has no YAML parser, so I wrote one in each test file. I ended up with several homegrown helpers that found a top-level block, pulled the list under a nested key, and split out jobs and sections by counting leading spaces.
The assertions read like the files they described, one line at a time:
- the trigger covers these three branches and no others
- the trigger is batched
- the pipeline has no PR section and no schedule
- every hook’s
idmatches the gate itsentryruns - the dependency install step comes before the first check step
Each of these is a line in a config file, rewritten as a Python assertion over a regex.
Tests of my tests
My checkers grew enough logic to have bugs of their own. So I added tests that changed the pipeline text on purpose and confirmed that my checker caught the change. That should have been the moment I stopped. Once a check of a config file needs its own tests, it has become a program, and it’s competing with the tool that actually reads the file.
Policy checks in the unit-test suite
Four more modules weren’t testing code at all. They checked things like these:
- every lock file across a set of repositories holds exactly one copy of each shared library, at the pinned version
- production’s deployment configuration matches staging’s, with environment values substituted
- a build image’s dependency seed matches the applications’ generated files
- generated reference docs don’t mention certain types
The lock-file check called the remote git host for every case, with long timeouts and a retry. It took several seconds on a good day. When I wrote this, six of its cases were failing, and that failed the whole unit-test gate for a reason no code change in that directory could fix.
Why it was wrong
The rule I broke is simple: if CI would fail on the same fault the test checks, the test is redundant. (CI running the test itself doesn’t count.)
That covers everything the text tests checked:
- A step that names a gate that doesn’t exist fails that step, with the runner’s own error.
- A hook that points at a missing gate fails on the next commit.
- Malformed pipeline YAML is rejected when the run is queued.
- A wrong stage condition runs or skips the stage, and the log shows which.
- A bad container tag fails the image pull before the job starts.
- Putting the install after the first check fails that check with “command not found.”
My tests worked out each of those outcomes ahead of time, from the text, in a second language. All they added was a second error message, in my words instead of the tool’s, one gate earlier.
The costs were real:
- Every pipeline change had to be written twice. The five modules took about 120 commits. The biggest one changed more often than the pipeline it described.
- I kept several YAML parsers inside test files. “No YAML parser, by design” should have meant “don’t parse YAML in tests.” I read it as “write your own, several times.”
- The suite ran on almost every change. The biggest module declared that it read most of the CI directory, the pipelines, the hook config, the Makefile and a docs page, so nearly every commit triggered its 127 tests.
- Policy failures blocked unrelated work. A lock-file mismatch in another repository could fail the unit-test gate for someone editing a test helper.
- I broke our own rule that unit tests don’t touch the network.
The one argument that sounded right
The best case I made in the docstrings was for a test comparing the image pipeline’s trigger paths with the list of inputs in the image build script. If they drift apart, a version pin can move without an image being built for it, and the next CI run can’t pull its image. That’s a real failure, and CI does report it late.
But the cause was keeping the same list in two files, in two languages. A test that compares them accepts the duplication and leaves it in place. The fix is to have one list: generate the trigger block from the script, or have the script read the trigger block. Then there’s nothing to compare.
What I should have done
- For the tooling files: write no tests. Pipelines, hook configs and Makefiles get tested by being used. The push is the test.
- For two files that must agree: give them one source. If I’m writing a test that compares two lists in two files, I’ve found duplication to remove, not a test to add.
- For policy checks: make each one its own gate, or drop it. A cross-repository property, like one copy of each shared crate per lock file, belongs in a named gate that only runs when the files it reads change. A lock-file change should run it, and an unrelated CI change shouldn’t. Whether each property is worth enforcing at all is a call for the people who own the project. It shouldn’t be made by default because I happened to put it in the test suite.
- Keep unit tests off the network. If a check has to ask a remote, it’s a gate, and the gate handles credentials, timeouts and retries, not the test suite.
How I’ll catch this next time
I’m treating these as red flags in my own reviews and in others’:
- A test that opens a pipeline file, hook config or Makefile.
- Helpers in test code that parse YAML by indentation.
- Test names that restate one line of a config file.
- A test that compares a list in one file with a list in another.
- A unit test that makes network calls, reads CI access tokens, or has retries and timeouts measured in seconds.
- A test whose job is to prove that another test catches a mistake in a config file.
Before I write a test, I’ll ask what would have to be wrong for it to fail. If the answer is “the config file says something different from what it says,” and the tool that reads that file would report it on the same push, I won’t write the test.
This is the third time in a few weeks that the same lesson has come up in this part of our codebase. Earlier it was image build files tested as text, and a superrepo suite much bigger than the little code it holds. I applied each lesson narrowly and kept the habit. So here it is in general form: development tooling is verified by using it. Tests are for code.