Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix promtool alert tests for no alert validation #8462

Closed
wants to merge 1 commit into from
Closed

Fix promtool alert tests for no alert validation #8462

wants to merge 1 commit into from

Conversation

rbauduin
Copy link

@rbauduin rbauduin commented Feb 9, 2021

The documentation of promtool rules test states "If you want to test
if an alerting rule should not be firing, then .. leave 'exp_alerts' empty."
This was not working though, as the code always based its checks on the
expected alerts in the test definition and didn't look at the alerts
that effectively fired. With an empty list for the expected alert, no
validation occured!
With this change, the code now looks at the alerts that fired and checks
this is corresponding to the expected alerts found in the test
definition.

The documentation of `promtool rules test` states "If you want to test
if an alerting rule should not be firing, then .. leave 'exp_alerts' empty."
This was not working though, as the code always based its checks on the
expected alerts in the test definition and didn't look at the alerts
that effectively fired. With an empty list for the expected alert, no
validation occured!
With this change, the code now looks at the alerts that fired and checks
this is corresponding to the expected alerts found in the test
definition.

Signed-off-by: Raphael Bauduin <raphael.bauduin@tessares.net>
@beorn7
Copy link
Member

beorn7 commented Feb 15, 2021

@simonpasquier and @dgl, do you have an opinion on this?

@dgl
Copy link
Member

dgl commented Feb 15, 2021

The existing behaviour is correct, there's no requirement that a unit test tests all the alerts in a rule file, so the tests have to name each alert that is expected to be tested by name.

This change would make it impossible to split testing of some alerts out to a different file. (It also just doesn't handle two different alert names firing -- see how it discards the alertname: https://github.com/prometheus/prometheus/pull/8462/files#diff-7effa22958bf71b1531230dc8acc6322aa3a54b332dd85b80211e590b9ae93dfR282)

@dgl
Copy link
Member

dgl commented Feb 15, 2021

I added some more tests in #8490 which maybe make it clearer, if I apply this change on top of those tests I get:

$ ../promtool test rules unittest.yml
Unit Testing:  unittest.yml
  FAILED:
    alertname:InstanceDown, time:0s,
        exp:"[]",
        got:"[Labels:{alertname=\"AlwaysFiring\"} Annotations:{}]"

i.e. it shows how this change wouldn't handle different alertnames.

@rbauduin
Copy link
Author

Thanks for having taken a look and the explanation. The problem I encountered, as posted on the forum, is that I didn't specify the alertname when no alert was expected. Would you be interested in a patch to report an error when the alertname is not set?

@dgl
Copy link
Member

dgl commented Feb 16, 2021

@rbauduin yes, definitely.

@beorn7
Copy link
Member

beorn7 commented Feb 17, 2021

Closing this as the issue is now addressed in #8504.

@beorn7 beorn7 closed this Feb 17, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants