From 40dae069f80b03461606f49ef9eb05886a05c1b6 Mon Sep 17 00:00:00 2001 From: Andreas Motl Date: Sun, 14 Jan 2024 02:14:48 +0100 Subject: [PATCH] Tests: Integration tests to verify query expressions end-to-end --- tests/test_expressions.py | 191 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 191 insertions(+) create mode 100644 tests/test_expressions.py diff --git a/tests/test_expressions.py b/tests/test_expressions.py new file mode 100644 index 0000000..37664ef --- /dev/null +++ b/tests/test_expressions.py @@ -0,0 +1,191 @@ +""" +## About +Integration tests to verify query expressions end-to-end. + +## Resources +- https://pypi.org/project/prometheus-api-client/ +- https://prometheus.io/docs/prometheus/latest/querying/examples/ +- https://github.com/crate/cratedb-prometheus-adapter/blob/0.5.0/server.go#L124-L160 +""" + + +def test_expression_plain(prometheus_client): + """ + Verify a plain metrics query, without any label constraints. + """ + result = prometheus_client.custom_query(query=""" + prometheus_http_requests_total{ + } + """) + assert len(result) > 15 + + +def test_expression_eq_success(prometheus_client): + """ + Verify a metrics query, with a label constraint using an equality match. + """ + result = prometheus_client.custom_query(query=""" + prometheus_http_requests_total{ + job = "prometheus" + } + """) + assert len(result) > 15 + + +def test_expression_eq_empty(prometheus_client): + """ + Verify a metrics query, with a label constraint using an equality match. + """ + result = prometheus_client.custom_query(query=""" + prometheus_http_requests_total{ + job = "foobar" + } + """) + assert len(result) == 0 + + +def test_expression_neq_success(prometheus_client): + """ + Verify a metrics query, with a label constraint using an inequality match. + """ + result = prometheus_client.custom_query(query=""" + prometheus_http_requests_total{ + job != "foobar" + } + """) + assert len(result) > 15 + + +def test_expression_neq_empty(prometheus_client): + """ + Verify a metrics query, with a label constraint using an inequality match. + """ + result = prometheus_client.custom_query(query=""" + prometheus_http_requests_total{ + job != "prometheus" + } + """) + assert len(result) == 0 + + +def test_expression_re_success_basic(prometheus_client): + """ + Verify a metrics query, with a label constraint using a regular expression match. + """ + result = prometheus_client.custom_query(query=""" + prometheus_http_requests_total{ + job =~ "prom.+" + } + """) + assert len(result) > 15 + + +def test_expression_re_success_dots(prometheus_client): + """ + Verify a metrics query, with a label constraint using a regular expression match. + """ + result = prometheus_client.custom_query(query=""" + prometheus_http_requests_total{ + code =~ "2.." + } + """) + assert len(result) > 15 + + +def test_expression_re_success_or(prometheus_client): + """ + Verify a metrics query, with a label constraint using a regular expression match. + """ + result = prometheus_client.custom_query(query=""" + prometheus_http_requests_total{ + code =~ "2..|3.." + } + """) + assert len(result) > 15 + + +def test_expression_re_empty(prometheus_client): + """ + Verify a metrics query, with a label constraint using a regular expression match. + """ + result = prometheus_client.custom_query(query=""" + prometheus_http_requests_total{ + job =~ "foobar.+" + } + """) + assert len(result) == 0 + + +def test_expression_nre_success_basic(prometheus_client): + """ + Verify a metrics query, with a label constraint using a negated regular expression match. + """ + result = prometheus_client.custom_query(query=""" + prometheus_http_requests_total{ + job !~ "foobar.+" + } + """) + assert len(result) > 15 + + +def test_expression_nre_success_dots(prometheus_client): + """ + Verify a metrics query, with a label constraint using a negated regular expression match. + """ + result = prometheus_client.custom_query(query=""" + prometheus_http_requests_total{ + code !~ "5.." + } + """) + assert len(result) > 15 + + +def test_expression_nre_empty(prometheus_client): + """ + Verify a metrics query, with a label constraint using a negated regular expression match. + """ + result = prometheus_client.custom_query(query=""" + prometheus_http_requests_total{ + job !~ "prom.+" + } + """) + assert len(result) == 0 + + +def test_expression_all(prometheus_client): + """ + Verify a metrics query, with label constraints using all types of label matchers. + """ + result = prometheus_client.custom_query(query=""" + prometheus_http_requests_total{ + code = "200", + handler != "foobar", + job =~ "prom.+", + instance !~ "foobar", + } + """) + assert len(result) > 15 + + +def test_range_vector_selector_success(prometheus_client): + """ + Verify range vector literals. + + https://prometheus.io/docs/prometheus/latest/querying/basics/#range-vector-selectors + """ + result = prometheus_client.custom_query(query=""" + prometheus_http_requests_total{}[1s] + """) + assert len(result) > 15 + + +def test_range_vector_selector_empty(prometheus_client): + """ + Verify range vector literals. With a minimal low value, it will produce zero results. + + https://prometheus.io/docs/prometheus/latest/querying/basics/#range-vector-selectors + """ + result = prometheus_client.custom_query(query=""" + prometheus_http_requests_total{}[1ms] + """) + assert len(result) == 0