|
2 | 2 |
|
3 | 3 | import pytest
|
4 | 4 |
|
5 |
| -from mcp_grafana.grafana_types import LabelMatcher, Selector |
| 5 | +from mcp_grafana.client import GrafanaError |
| 6 | +from mcp_grafana.grafana_types import DSQueryResponse, LabelMatcher, Selector |
6 | 7 | from mcp_grafana.tools.prometheus import (
|
7 | 8 | query_prometheus,
|
8 | 9 | get_prometheus_metric_metadata,
|
|
16 | 17 | pytestmark = mark_integration
|
17 | 18 |
|
18 | 19 |
|
19 |
| -@pytest.mark.parametrize("step_seconds", [15, 60, 300]) |
20 |
| -async def test_query_prometheus_range(step_seconds): |
21 |
| - end = datetime.now() |
22 |
| - start = end - timedelta(minutes=10) |
| 20 | +class TestPrometheusQueries: |
| 21 | + @pytest.mark.parametrize("step_seconds", [15, 60, 300]) |
| 22 | + async def test_query_prometheus_range(self, step_seconds): |
| 23 | + end = datetime.now() |
| 24 | + start = end - timedelta(minutes=10) |
23 | 25 |
|
24 |
| - results = await query_prometheus( |
25 |
| - "robustperception", |
26 |
| - start_rfc3339=start.isoformat(), |
27 |
| - end_rfc3339=end.isoformat(), |
28 |
| - step_seconds=step_seconds, |
29 |
| - expr="node_load1", |
30 |
| - query_type="range", |
31 |
| - ) |
32 |
| - query_result = results.results["A"] |
33 |
| - assert query_result.status == 200 |
34 |
| - assert ( |
35 |
| - query_result.frames[0].data.values[0][0] - start.timestamp() * 1000 |
36 |
| - < step_seconds * 1000 |
37 |
| - ) |
38 |
| - assert ( |
39 |
| - len(query_result.frames[0].data.values[0]) |
40 |
| - == (end - start).total_seconds() // step_seconds + 1 |
41 |
| - ) |
| 26 | + results = await query_prometheus( |
| 27 | + "robustperception", |
| 28 | + start_rfc3339=start.isoformat(), |
| 29 | + end_rfc3339=end.isoformat(), |
| 30 | + step_seconds=step_seconds, |
| 31 | + expr="node_load1", |
| 32 | + query_type="range", |
| 33 | + ) |
| 34 | + query_result = results.results["A"] |
| 35 | + assert query_result.status == 200 |
| 36 | + assert ( |
| 37 | + query_result.frames[0].data.values[0][0] - start.timestamp() * 1000 |
| 38 | + < step_seconds * 1000 |
| 39 | + ) |
| 40 | + assert ( |
| 41 | + len(query_result.frames[0].data.values[0]) |
| 42 | + == (end - start).total_seconds() // step_seconds + 1 |
| 43 | + ) |
| 44 | + |
| 45 | + async def test_query_prometheus_instant(self): |
| 46 | + timestamp = datetime.now() |
| 47 | + results = await query_prometheus( |
| 48 | + "robustperception", |
| 49 | + start_rfc3339=timestamp.isoformat(), |
| 50 | + expr="up", |
| 51 | + query_type="instant", |
| 52 | + ) |
| 53 | + query_result = results.results["A"] |
| 54 | + |
| 55 | + # Verify the response |
| 56 | + assert query_result.status == 200 |
| 57 | + assert len(query_result.frames) > 0 |
| 58 | + # Instant queries should return a single data point |
| 59 | + assert len(query_result.frames[0].data.values[0]) == 1 |
| 60 | + # Verify we have both time and value columns |
| 61 | + assert len(query_result.frames[0].data.values) == 2 |
| 62 | + # Verify the timestamp is close to our request time (within 60 seconds, |
| 63 | + # we don't know what the scrape interval is). |
| 64 | + assert ( |
| 65 | + abs(query_result.frames[0].data.values[0][0] - timestamp.timestamp() * 1000) |
| 66 | + < 60000 |
| 67 | + ) |
| 68 | + |
| 69 | + async def test_query_prometheus_range_missing_params(self): |
| 70 | + start = datetime.now() |
| 71 | + with pytest.raises( |
| 72 | + ValueError, match="end_rfc3339 and step_seconds must be provided" |
| 73 | + ): |
| 74 | + await query_prometheus( |
| 75 | + "robustperception", |
| 76 | + start_rfc3339=start.isoformat(), |
| 77 | + expr="node_load1", |
| 78 | + query_type="range", |
| 79 | + ) |
| 80 | + |
| 81 | + async def test_query_prometheus_invalid_query(self): |
| 82 | + end = datetime.now() |
| 83 | + start = end - timedelta(minutes=10) |
| 84 | + |
| 85 | + try: |
| 86 | + results = await query_prometheus( |
| 87 | + "robustperception", |
| 88 | + start_rfc3339=start.isoformat(), |
| 89 | + end_rfc3339=end.isoformat(), |
| 90 | + step_seconds=60, |
| 91 | + expr="invalid_metric{", # Invalid PromQL syntax |
| 92 | + query_type="range", |
| 93 | + ) |
| 94 | + except Exception as e: |
| 95 | + assert isinstance(e, GrafanaError) |
| 96 | + results = DSQueryResponse.model_validate_json(str(e)) |
| 97 | + query_result = results.results["A"] |
| 98 | + assert query_result.status == 400 |
| 99 | + assert query_result.error is not None |
| 100 | + assert "parse error" in query_result.error |
| 101 | + |
| 102 | + async def test_query_prometheus_empty_result(self): |
| 103 | + end = datetime.now() |
| 104 | + start = end - timedelta(minutes=10) |
| 105 | + |
| 106 | + results = await query_prometheus( |
| 107 | + "robustperception", |
| 108 | + start_rfc3339=start.isoformat(), |
| 109 | + end_rfc3339=end.isoformat(), |
| 110 | + step_seconds=60, |
| 111 | + expr='metric_that_does_not_exist{label="value"}', |
| 112 | + query_type="range", |
| 113 | + ) |
| 114 | + query_result = results.results["A"] |
| 115 | + assert query_result.status == 200 |
| 116 | + assert len(query_result.frames) == 1 |
| 117 | + assert len(query_result.frames[0].data.values) == 0 |
42 | 118 |
|
43 | 119 |
|
44 | 120 | async def test_get_prometheus_metric_metadata():
|
|
0 commit comments