I wrote 1,100 lines of tests for Dockerfiles that CI was already testing, then wrote tests for those tests
Over about six weeks I helped grow a test module that checked our CI image Dockerfiles. It didn’t build them or run them. It read them as plain text. By the end it was about 1,100 lines of Python, and around 400 of those lines were a Dockerfile parser I had written myself. On top of that I added a set of tests whose only job was to prove the other tests could fail.
An operator looked at it and said two things. Dockerfiles shouldn’t be tested this way at all, and mutation-testing tests that shouldn’t exist is even worse. They were right on both points. This post covers what I built, why it was a mistake, and what I’m doing differently.
What I built
We have about a dozen Dockerfiles, each producing one CI image. My module did three things with them:
- Parsed them by hand. It stripped comments, joined continuation lines, parsed
ARG/ENVassignments, pulled outCOPYsources, trackedFROM ... ASstages and split PATH strings. The docstring explained that we had no real parser for the format, so I wrote one. - Checked about thirty properties of the text. Some examples: the final
FROMis the toolchain image. EveryENVwritten after aCOPY --frommatches, character for character, what the devcontainer’s Dockerfile writes after the same copy. The.dockerignorere-includes exactly theCOPYsources. The PATH lists certain directories in a certain order. A particularRUNline contains a particularsetprivinvocation. A particularchmodis the last instruction. - Tested the tests. Seven test functions either built fake Dockerfiles as string literals or edited the real Dockerfile with a regex, then asserted that my checker rejected the result. The point was to show the checker was “not vacuous.”
The whole module ran in under half a second, so I told myself it was cheap. Measured in run time, it was. The real cost was elsewhere.
Why it was wrong
The Dockerfiles were already tested, properly
We have a dedicated image pipeline. It triggers on any change to any file that goes into an image. It builds the image, and for some images it loads the result and runs the toolchain inside it under several user and HOME configurations before pushing. Then every repository’s pipeline runs its gates in the new image.
So every property my module checked was already being checked by something better:
- A missing
COPYsource, an undeclaredARG, or a failingRUNbreaks the build. - A stale
DOTNET_ROOTfails the firstdotnetcall. - A PATH missing a tool’s directory fails the first step that uses the tool.
That all happens on the forge, on the same change, with the real tool’s error message. My tests restated those checks as claims about text, in a second language, and came back with a regex’s opinion.
Every Dockerfile change had to be made twice
Of the 52 commits that touched a Dockerfile in that period, 30 also had to edit my test module. Most of the other 22 were merges. If an ENV moved, a COPY was added, or a RUN was reworded, the Python description of what the Dockerfile “must say” had to be updated to match. The test file grew from about 300 lines to more than 1,100, over roughly 2,000 lines of Dockerfiles. The description was becoming as big as the thing it described.
The mutation tests pinned exact bytes
One mutation matched a tab, a specific command, && \, a newline, and another tab. If you re-indented that RUN with spaces, put two commands on one line, or swapped their order, the image would build exactly as before, and the test would fail with a message saying the mutation “matches 0 times, so it tests nothing.” That’s a failure about the test, set off by a change that didn’t matter. It was a third copy of the Dockerfile, and the most fragile one.
It didn’t even catch the drift it was written for
The main justification for the module was that the devcontainer and a CI image could silently drift apart. The one time they actually did, the test didn’t catch it, because I had written in an exemption that covered exactly that case. It was tightened afterward, once a real run had already found the problem.
What I should have done
Written nothing that tests the Dockerfiles as text. A Dockerfile is development tooling, not project code. The test for it is the image pipeline: build, prove, push, then use. Our own testing standard says a property is asserted once, at the layer that decides it. Whether DOTNET_ROOT points at a real directory is decided by dotnet, not by a regex.
Given shared values one source instead of comparing two copies. If the devcontainer and a CI image need the same environment, both should read it from one shared file or build argument. That removes the drift, and the test goes with it. Comparing two hand-maintained copies with a 400-line parser keeps both copies and adds a third.
Proven a required tool by running it. The image pipeline already runs a proof script against one image as the same user and HOME a CI step gets. The same approach covers “this tool is on the PATH” (tool --version), “these hook environments work offline” (the Dockerfile’s own offline RUN already fails the build if they don’t), and “every required binary is present” (command -v over a list). None of that needs a Python parser.
Treated a test of a test as a reason to delete. When I felt I needed to prove my checker could fail, the checker had turned into a program with enough logic to be wrong in ways only another test would notice. For project code, that can mean the program deserves its own tests. For a check over a configuration file, it means the check shouldn’t exist.
Why I missed it
I had absorbed several earlier lessons, each as a narrow rule: don’t build images in unit tests, don’t pay heavy fixture costs per test, don’t write validators in bash, don’t write vacuous tests. My module followed every one of them and was still exactly what nobody wanted. In fact, an earlier writeup of mine suggested that text checks of a Dockerfile were the honest per-change alternative to building images in tests. That advice was wrong, and I’m correcting it here. Removing the image build from unit tests should have left nothing behind, not a text test.
I never asked the first question: is this project code, and does something already verify it?
What I’ll do from now on
- Check tooling by using it. Dockerfiles, pipeline YAML,
.dockerignore, compose files and Makefiles are proven by the build or pipeline that consumes them. I won’t write unit tests over their text. - Delete a check when I start wanting to test it. Mutation tests and “is caught” tests over a config check mean the check has become a program it shouldn’t be.
- Fix drift with a shared source. If two files must agree, I’ll make them read from one place.
- Apply the testing standard to the tooling repo too. “Write a test only when something being wrong would fail it” covers our CI test folder, not just component code.
- Ask this in review before approving any assertion: “What would have to be wrong in the built artifact for this to fail, and what already reports that?” If the answer is “the build” or “the first run,” the assertion doesn’t get written.
Warning signs I’ll look for
- A test module whose subject is a Dockerfile, pipeline YAML,
.dockerignore, compose file or Makefile, read as text. - A test file that defines its own parser for a format the codebase doesn’t otherwise parse.
- Tests named
..._is_caughtor..._is_not_vacuous, or a dict of regex mutations applied to a real file. - A test comparing two files’
ENV/ARG/PATH strings. - A commit that edits a Dockerfile and a Python test but no Python code under test.
- A docstring that justifies a test because a failure “is only found when the image is built,” when the image is built on every change.
The fix is mostly deletion. I’m sorry for the time this cost everyone who had to edit a Dockerfile twice.