DevOpsInterviewPrep logo
← 📈 Observability & Reliability
Foundational

Prometheus rule tests: prove pending, firing and recovery transitions

Use promtool fixtures to verify an alert duration, labels and recovery. Separate expression correctness from production scraping, rule loading and notification delivery.

TL;DR: promtool test rules evaluates rules against authored sample timelines. Test before and after the for boundary and after recovery, with exact alert labels. A passing rule test does not prove production loaded the rule or Alertmanager delivered a notification.

A true expression may still be pending

An alert with for: 2m does not fire the moment its expression becomes true. The condition must remain active for the required duration under rule evaluation. A test checking only a sample value at the end can miss premature firing, incorrect labels or a failure to resolve.

Create alerts.yml with this teaching rule:

groups:
  - name: fixture
    interval: 1m
    rules:
      - alert: FixtureScrapeFailed
        expr: up{job="fixture"} == 0
        for: 2m
        labels:
          severity: page

Then create test.yml alongside it:

rule_files: [alerts.yml]
evaluation_interval: 1m
tests:
  - interval: 1m
    input_series:
      - series: 'up{job="fixture",instance="a"}'
        values: '0 0 0 1 1'
    alert_rule_test:
      - eval_time: 1m
        alertname: FixtureScrapeFailed
        exp_alerts: []
      - eval_time: 2m
        alertname: FixtureScrapeFailed
        exp_alerts:
          - exp_labels:
              job: fixture
              instance: a
              severity: page
      - eval_time: 3m
        alertname: FixtureScrapeFailed
        exp_alerts: []

Run promtool check rules alerts.yml, then promtool test rules test.yml using the Prometheus version under review. The first validates rule syntax; the second evaluates the timeline. Prometheus documents the fixture format in unit testing rules.

rendering diagram…

Make a meaningful negative case

Change for: 2m to for: 0m in a disposable copy. The expectation at one minute must fail because the alert now fires too early. Restore the duration, then alter the expected instance label: the test must reject a wrongly identified alert. A test that accepts any alert regardless of labels may route a page to the wrong owner in production.

CheckWhat it provesWhat remains outside it
Syntax checkRule file parsesExpression has desired behavior
Timeline testExpected evaluation transitionsReal targets provide these samples
Label assertionAlert identity and routing metadataAlertmanager routing config is correct
Delivery rehearsalConfigured receiver gets a test alertEvery future production event will arrive

Add healthy, missing-data and transient-failure fixtures according to the intended policy. up == 0 does not fire when the target series is absent entirely; missing-series design needs a different expectation contract. Use counter resets and realistic label combinations for rate or aggregation rules rather than testing only steadily increasing ideal input.

Verify the deployed path separately

After rule tests pass, verify configuration loading and rule health in the intended Prometheus instance, then inspect alert state and routing through Alertmanager. A unit-test success cannot detect a production file excluded from rule_files, a failed reload or a silenced receiver. Keep those observations in the release record with the tested rule revision.

Self-check: the fixture fires at two minutes, but production receives no page. Should the rule's threshold be loosened first? No. Locate the first missing transition: loaded rule, firing alert, Alertmanager receipt, route, silence/inhibition or receiver delivery.

Practice the green rule test without a page.

RELATED CONCEPTS
PRACTICE THIS IN REAL QUESTIONS