Testing checks locally
Testing checks locally
This document covers how to test a check while you write it, before you open a merge request.
You can also watch this November 2026 walkthrough by Support Engineer Warren Wolcott, which covers most of the same content.
To review a check in a merge request with the generated execution image, see Reviewing Checks.
Exit codes
A check command uses inverted exit codes:
0: the problem was found. The check shows up in the failed checks report.1: no problem. The check passes.- Anything higher: the command failed. Detective reports it as a script error, so a check must never exit with
2or higher.
Test every case that matters for your check, including the healthy ones, such as a missing file or a node that doesn’t run the service.
1. Validate the check file
Run the same checks that the pipeline runs:
# YAML style
scripts/lint-yaml.sh checks/<directory>/<file>.yml
# Run all tests (run `uv sync` once first)
uv run pytest
# Check schema (run `uv sync` once first)
uv run pytest test/test_check_validation.py -k <file>.yml
# For grep_file checks: the sample line in test/resources/checks/grep_checks_params.py
uv run pytest test/test_grep_checks.py -k <file>.yml
2. See the command the shell gets
The command that runs isn’t always what the check file shows:
- For
grep_filechecks, Detective builds the command from thefile,regex, andgrep_flagsfields. - YAML can change a
shellcommand. For example, an unquoted!at the start of a value is a YAML tag, so YAML removes it. - Detective runs every Omnibus command with
bash, and addsset -o pipefail;to commands that contain a|.
From the root of the repository, print the command exactly as Detective builds it:
uv run python - checks/<directory>/<file>.yml > cmd.sh <<'EOF'
import sys
from pathlib import Path
from lib.converter.resources.helper import read_check_file
from lib.converter.resources.task_handler import Omnibus
command, args = Omnibus.build_command_and_args(read_check_file(Path(sys.argv[1])).command.omnibus)
print(command["ansible.builtin.shell"])
EOF
cat cmd.sh
3. Run the command against test cases
You don’t need a GitLab instance to test most commands. Create the files the command reads,
then run the command in containers for the operating systems GitLab supports. Tools such as grep, zgrep, and sed
behave differently between distributions.
For example, to test a check that reads /etc/gitlab/gitlab.rb:
-
Create one folder per test case, each with a
gitlab.rbfile:mkdir -p cases/affected cases/healthy echo "registry['storage'] = { 's3' => { 'v4auth' => false } }" > cases/affected/gitlab.rb echo "registry['storage'] = { 's3' => { 'v4auth' => true } }" > cases/healthy/gitlab.rb -
Create
run.sh, which copies each case into place, runs the command, and compares the exit code with the expected one:for c in affected:0 healthy:1 missing:1; do name=${c%%:*}; expected=${c##*:} rm -rf /etc/gitlab; mkdir -p /etc/gitlab [ -d /cases/$name ] && cp /cases/$name/gitlab.rb /etc/gitlab/ bash /cmd.sh > /tmp/out 2>&1; rc=$? [ -s /tmp/out ] && rc="$rc (printed output)" [ "$rc" = "$expected" ] && echo "$name: ok" || echo "$name: WRONG, got $rc, expected $expected" doneThe
missingcase has no folder, sogitlab.rbdoesn’t exist. If the file can be missing on the nodes the check runs on, the command must still exit1in this case. If the file always exists there, for examplegitlab.rbon Rails nodes, you can leave this case out.run.shusesbashto run the command, like Detective does. -
Run it on each operating system:
for image in almalinux:8 ubuntu:22.04 debian:12; do docker run --rm -v "$PWD/cases:/cases:ro" -v "$PWD/run.sh:/run.sh:ro" -v "$PWD/cmd.sh:/cmd.sh:ro" \ "docker.io/library/$image" sh /run.sh done # The Amazon Linux 2 image doesn't include gzip, which zgrep needs docker run --rm -v "$PWD/cases:/cases:ro" -v "$PWD/run.sh:/run.sh:ro" -v "$PWD/cmd.sh:/cmd.sh:ro" \ docker.io/library/amazonlinux:2 sh -c 'yum install -q -y gzip >/dev/null 2>&1; sh /run.sh'You can use
podmaninstead ofdocker.
For log checks, base your test lines on real log lines. You can find examples in the
GitLab log system documentation, in the issue that describes the
problem, or on a test instance. Include rotated and compressed logs, such as api_json.log.1 and api_json.log.2.gz.
4. Common mistakes
| Mistake | What happens | What to do instead |
|---|---|---|
Relying on grep -s for a file that might not exist |
-s only hides the error message. grep still exits 2, which is a script error. |
Check that the file exists first, in a shell command: test -f <file> && grep -sq <pattern> <file> |
Using grep_file for a file that might not exist |
A missing file, a glob that matches nothing, or a missing part of a brace pattern (for example @* in {current,@*} when there are no rotated files) makes the command exit 2, even if another file matches. |
Only use grep_file when the file always exists on the nodes the check runs on. Otherwise, use a shell command with a test -f guard. |
Searching several files with zgrep on Amazon Linux 2 |
The older zgrep on Amazon Linux 2 (gzip 1.5) exits 1 unless every file matches, so a match in one rotated log is missed. Because Detective adds set -o pipefail, piping its output to grep -q . doesn’t help either. GitLab supports Amazon Linux 2 up to 19.2. |
For checks that run on those versions, use a shell command that ignores zgrep’s exit code: { zgrep -El <pattern> <files> 2>/dev/null || true; } | grep -q . |
Starting a command with an unquoted ! |
YAML reads ! as a tag and removes it, so the logic is inverted. |
Quote the value, or rewrite the command so it doesn’t start with !. |
| Printing matching lines | The output isn’t needed. If the check errors, Detective saves the output in the failed checks report, and it might include configuration values. | Use -q with grep and zgrep, or the grep_flags of a grep_file check. |
| Searching every log ever written | Some logs are never rotated or removed, for example /var/log/gitlab/reconfigure/. An old match keeps the check failing after the problem is fixed. |
Check whether the log is rotated, and limit the search to recent files if it isn’t. |
5. Test on a real GitLab instance
To test against real configuration and logs, run GitLab in a container. GitLab needs at least 4 GB of memory.
If you use Podman, give its machine enough memory, for example podman machine set --memory 8192.
-
Start the version you want to test. For example:
docker run -d --name detective-test --hostname localhost -p 8929:80 \ -e GITLAB_OMNIBUS_CONFIG="external_url 'http://localhost:8929'" \ docker.io/gitlab/gitlab-ce:19.0.11-ce.0 -
Make the change your check looks for, such as a
gitlab.rbsetting followed bygitlab-ctl reconfigure. -
Copy the command into the container and run it:
docker cp cmd.sh detective-test:/tmp/cmd.sh docker exec detective-test sh /tmp/cmd.sh; echo "exit code: $?"
When the merge request pipeline has built the execution image, test the check with Detective itself, as described in Reviewing Checks.