Unit Testing Rules with promtool
Alert rules are the code that wakes people up. An alert that never fires (a typo in a label, a wrong ratio) is worse than none, because everyone believes they're covered. promtool test rules runs rules against synthetic data on a virtual clock, in milliseconds, with no server. Put it in CI next to promtool check rules.
The test file
rule_files: # the rules under test (globs allowed)
- alerts.yml
evaluation_interval: 1m # how often rules are evaluated in the virtual clock
group_eval_order: [] # optional: order of groups
tests:
- name: high error rate fires after 5m # optional, shown on failure
interval: 1m # spacing of input samples
external_labels: {cluster: test}
input_series:
- series: '<metric>{<labels>}'
values: '<expanding notation>'
alert_rule_test: # which alerts are FIRING at eval_time, with labels and annotations
- eval_time: 10m
alertname: X
exp_alerts: [...]
promql_expr_test: # what any PromQL expression returns at eval_time
- expr: ...
eval_time: 10m
exp_samples: [...]The expanding notation
| Values string | Produces (one per interval) |
|---|---|
'1 2 3' |
1, 2, 3 |
'0+10x5' |
0, 10, 20, 30, 40, 50 (start, then 5 more steps) |
'100-5x3' |
100, 95, 90, 85 |
'7x4' |
7, 7, 7, 7, 7 (shorthand for 7+0x4) |
'1 _ 3' |
1, missing, 3 (_ = no sample: a missed scrape) |
'_x5' |
five missing samples |
'1 stale' |
1, then a staleness marker (the series ends) |
'0+10x5 100+10x5' |
segments concatenate; restarting below the last value models a counter reset |
'0+10x5' has six values: the start plus five increments. Off-by-one mistakes here are the most common cause of confusing failures.
Reading a failure
This test has two bugs: the expected value forgets that rate() is per second, and an alert is expected without its instance label (the alert keeps every label of its series):
cat > alerts.yml <<'YML'
groups:
- name: linkly
rules:
- alert: LinklyInstanceDown
expr: up{job="linkly"} == 0
for: 2m
labels: {severity: page}
- record: instance:http_requests:rate1m
expr: sum by (instance) (rate(http_requests_total[1m]))
YML
cat > alerts.test.yml <<'YML'
rule_files: [alerts.yml]
tests:
- name: instance down and request rate
interval: 30s
input_series:
- series: 'up{job="linkly", instance="api-1"}'
values: '1 1 0 0 0 0 0 0' # down from 1m on
- series: 'http_requests_total{instance="api-1"}'
values: '0+300x8' # +300 every 30 s
alert_rule_test:
- eval_time: 3m
alertname: LinklyInstanceDown
exp_alerts:
- exp_labels: {severity: page, job: linkly}
promql_expr_test:
- expr: instance:http_requests:rate1m
eval_time: 3m
exp_samples:
- labels: 'instance:http_requests:rate1m{instance="api-1"}'
value: 600
YML
promtool test rules alerts.test.yml
echo "exit code: $?" FAILED:
name: instance down and request rate,
alertname: LinklyInstanceDown, time: 3m,
exp:[
0:
Labels:{alertname="LinklyInstanceDown", job="linkly", severity="page"}
Annotations:{}
],
got:[
0:
Labels:{alertname="LinklyInstanceDown", instance="api-1", job="linkly", severity="page"}
Annotations:{}
]
expr: "instance:http_requests:rate1m", time: 3m,
exp: {__name__="instance:http_requests:rate1m", instance="api-1"} 6E+02
got: {__name__="instance:http_requests:rate1m", instance="api-1"} 1E+01
exit code: 1
How to read it:
- For alerts:
expis what you wrote,gotwhat the rules produced. The alert did fire, but with aninstance="api-1"label you didn't list. The fix isexp_labels: {severity: page, job: linkly, instance: api-1}(alertnameis added automatically). - For expressions: values print in scientific notation (
1E+01= 10).rate()gives 10 per second, not 600 per minute. - The exit code is 1, so CI fails.
Fixed:
cat > alerts.yml <<'YML'
groups:
- name: linkly
rules:
- alert: LinklyInstanceDown
expr: up{job="linkly"} == 0
for: 2m
labels: {severity: page}
- record: instance:http_requests:rate1m
expr: sum by (instance) (rate(http_requests_total[1m]))
YML
cat > alerts.test.yml <<'YML'
rule_files: [alerts.yml]
tests:
- name: instance down and request rate
interval: 30s
input_series:
- series: 'up{job="linkly", instance="api-1"}'
values: '1 1 0 0 0 0 0 0'
- series: 'http_requests_total{instance="api-1"}'
values: '0+300x8'
alert_rule_test:
- eval_time: 2m30s # down since 1m: still pending (for: 2m)
alertname: LinklyInstanceDown
exp_alerts: []
- eval_time: 3m
alertname: LinklyInstanceDown
exp_alerts:
- exp_labels: {severity: page, job: linkly, instance: api-1}
promql_expr_test:
- expr: instance:http_requests:rate1m
eval_time: 3m
exp_samples:
- labels: 'instance:http_requests:rate1m{instance="api-1"}'
value: 10
YML
promtool test rules --junit=report.xml alerts.test.yml
grep -o '<testsuite [^>]*>' report.xml | sed 's/ time="[^"]*"//; s/ timestamp="[^"]*"//' SUCCESS
<testsuite name="alerts.test.yml" tests="1" failures="0" errors="0" skipped="0">
--junit writes a JUnit XML report that CI systems (GitLab, Jenkins, GitHub Actions with a reporter) display per test.
What to test
Each alert deserves at least:
- It fires in the scenario it exists for, with the right labels (routing depends on them) and annotations (the human reads them).
- It doesn't fire just below the threshold, and not before
forhas elapsed. - Edge cases: missing data (
_values,stale), a counter reset, a second instance that is healthy (does the alert name the right one?).
And run the tests where the rules live:
# .github/workflows/rules.yml (sample)
name: rules
on: [pull_request]
jobs:
promtool:
runs-on: ubuntu-24.04
env:
PROM_VERSION: 3.15.1
steps:
- uses: actions/checkout@v7
- name: Install promtool
run: |
curl -fsSL "https://github.com/prometheus/prometheus/releases/download/v${PROM_VERSION}/prometheus-${PROM_VERSION}.linux-amd64.tar.gz" |
tar -xz --strip-components=1 "prometheus-${PROM_VERSION}.linux-amd64/promtool"
- run: ./promtool check rules rules/*.yml
- run: ./promtool test rules --junit=promtool.xml tests/*.ymlFor the Prometheus Operator, rules live in PrometheusRule objects; extract spec with yq '.spec' rule.yaml > rules.yml in CI and test that file.
Test yourself
How many values does '5+5x3' produce?
- 3
- 4: 5, 10, 15, 20
- 5
- 15
A firing alert in a test has more labels than exp_labels lists. What happens?
- The test passes
- The test fails: expected and actual label sets must match exactly (alertname is implied)
- Extra labels are ignored
- promtool warns only
Which alert_rule_test checks pending alerts?
- alert_rule_test does
- None: it checks firing alerts; query the ALERTS series with promql_expr_test for pending ones
- pending_alert_test
- --debug