-
-
Notifications
You must be signed in to change notification settings - Fork 90
Expand file tree
/
Copy pathtest_metric_query.py
More file actions
47 lines (42 loc) · 1.95 KB
/
test_metric_query.py
File metadata and controls
47 lines (42 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
"""Test module for class PrometheusConnect."""
import unittest
from prometheus_api_client.metric_query import query_to_str
class TestMetricQuery(unittest.TestCase):
"""Test module for metric query."""
def test_query_to_str_with_wrong_label_query(self): # noqa D102
# wrong op ('~=' instead of '=~')
with self.assertRaises(ValueError, msg=f"unknown label operator: '~='"):
_ = query_to_str(
metric_name="up",
label_query={"some_label": ("~=", "some-value-.*")}
)
# inverted label value and op
with self.assertRaises(ValueError, msg=f"unknown label operator: 'some-value-.*'"):
_ = query_to_str(
metric_name="up",
label_query={"some_label": ("some-value-.*", "=~")}
)
# Wrong number of label query arguments
with self.assertRaises(ValueError, msg=f"wrong number of elements in label query with operator: 3 instead of 2"):
_ = query_to_str(
metric_name="up",
label_query={"some_label": ("=~", "some-value-.*", "whatever")}
)
def test_query_to_str_with_correct_label_query(self): # noqa D102
correct_label_queries = [
{ "some_label": "some-value"}, # exact match
{ "some_label": ("=", "some-value")}, # exact match, explicit op
{ "some_label": ("!=", "some-value")}, # negative match
{ "some_label": ("=~", "some-value-.*")}, # regex match
{ "some_label": ("!~", "some-value-.*")}, # negative regex match
]
for label_query in correct_label_queries:
try:
_ = query_to_str(
metric_name="up",
label_query=label_query
)
except Exception as e:
self.fail(f"query_to_str('up') with label_config raised an unexpected exception: {e}")
if __name__ == "__main__":
unittest.main()