How to mock query functions in AlertManager unit tests

107 views
Skip to first unread message

Emmanuel Alan Ferreyra Montes

unread,
Jan 7, 2022, 10:59:53 AM1/7/22
to Prometheus Users
I'm trying to run some tests over an alert I've added but I haven't been able to "mock" of defining the value of the function "time". Do you know how to do it?

I'm currently doing this:

- interval: 5m
input_series:
- series: 'probe_ssl_earliest_cert_expiry{instance="instance1", job="job-1"}'
values: '17000000 35000000 5000000 10000000'
- series: 'time'
values: '100000000 20000000 25000000 30000000'
alert_rule_test:
- alertname: CertificateExpiry
eval_time: 0m
exp_alerts:
- exp_labels:
severity: warning
instance: instance1
exp_annotations:
summary: "Certificate in instance1 is due to expiry in less than 30 days"
description: "Certificate in instance1 is due to expiry in less than 30 days."


and the alert is defined as follow:

- alert: CertificateExpiry
expr: probe_ssl_earliest_cert_expiry - time() < 86400 * 30
labels:
severity: warning
annotations:
summary: "Certificate in {{ $labels.instance }} is due to expiry in less than 30 days"
description: "Certificate in {{ $labels.instance }} is due to expiry in less than 30 days."


Do you know how can I define the values of time in the same way I do it with the series?

Thanks in advance,

Emmanuel

sayf eddine Hammemi

unread,
Jan 7, 2022, 11:29:35 AM1/7/22
to Emmanuel Alan Ferreyra Montes, Prometheus Users
Imo it is easier to generate your test cases dynamically, you can in that case choose the values based on the current time

--
You received this message because you are subscribed to the Google Groups "Prometheus Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to prometheus-use...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/prometheus-users/3d9e32d3-fa3a-4415-8ad7-4c5db5d1e7b8n%40googlegroups.com.

Emmanuel Alan Ferreyra Montes

unread,
Jan 7, 2022, 11:36:15 AM1/7/22
to Prometheus Users
At some point, I will need to define that time function (rather here or in the recording rule) so it won't be enough to relay on the current time.

Emmanuel Alan Ferreyra Montes

unread,
Jan 7, 2022, 11:38:57 AM1/7/22
to Prometheus Users
Ohh, you mean, relaying on the current time of the test. Let me check if I can do it

Emmanuel Alan Ferreyra Montes

unread,
Jan 7, 2022, 11:53:37 AM1/7/22
to Prometheus Users
Wonderful. It worked: 

- interval: 5m
input_series:
- series: 'probe_ssl_earliest_cert_expiry{instance="instance1", job="job-1"}'
values: ''2600000 2400000 2200000 2000000'

- series: 'time'
values: '100000000 20000000 25000000 30000000'
alert_rule_test:
- alertname: CertificateExpiry
eval_time: 0m
exp_alerts:
- exp_labels:
severity: warning
instance: instance1
exp_annotations:
summary: "Certificate in instance1 is due to expiry in less than 30 days"
description: "Certificate in instance1 is due to expiry in less than 30 days."


Thank you sayf!
Reply all
Reply to author
Forward
0 new messages