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

Tests: Integration tests to verify query expressions end-to-end #115

Merged
merged 1 commit into from
Jan 17, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
191 changes: 191 additions & 0 deletions tests/test_expressions.py
Original file line number Diff line number Diff line change
@@ -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]
hlcianfagna marked this conversation as resolved.
Show resolved Hide resolved
""")
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
Loading