Skip to content

Commit 9874fff

Browse files
committed
kube: rename query.py to test_query.py so pytest collects its tests
Also remove dead json_decode_error ResultType. Pydantic v2 validate_json() always raises ValidationError, never JSONDecodeError, so this path has been unreachable since the Pydantic v2 upgrade in December 2023 or possibly earlier. Change-Id: If91cc44413e882d7c5e683d5db09939c7fc7ffbe
1 parent 7248b54 commit 9874fff

5 files changed

Lines changed: 23 additions & 34 deletions

File tree

packages/cmk-plugins/cmk/plugins/kube/agent_based/openshift_queries.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ def discover(section: OpenShiftEndpoint) -> DiscoveryResult:
3535

3636
SEVERITY = {
3737
ResultType.request_exception: State.CRIT,
38-
ResultType.json_decode_error: State.CRIT,
3938
ResultType.validation_error: State.CRIT,
4039
ResultType.response_error: State.CRIT,
4140
ResultType.response_empty_result: State.CRIT,
@@ -45,12 +44,11 @@ def discover(section: OpenShiftEndpoint) -> DiscoveryResult:
4544

4645
RANK_ORDER = {
4746
ResultType.request_exception: 0,
48-
ResultType.json_decode_error: 1,
49-
ResultType.validation_error: 2,
50-
ResultType.response_error: 3,
51-
ResultType.response_empty_result: 4,
52-
ResultType.response_invalid_data: 5,
53-
ResultType.success: 6,
47+
ResultType.validation_error: 1,
48+
ResultType.response_error: 2,
49+
ResultType.response_empty_result: 3,
50+
ResultType.response_invalid_data: 4,
51+
ResultType.success: 5,
5452
}
5553

5654

packages/cmk-plugins/cmk/plugins/kube/prometheus_api.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import datetime
88
import enum
99
from collections.abc import Mapping, Sequence
10-
from json import JSONDecodeError
1110
from typing import Annotated, Literal, NewType
1211

1312
from pydantic import BaseModel, ConfigDict, Field, TypeAdapter, ValidationError
@@ -112,8 +111,8 @@ class ResponseError(ParseModel):
112111

113112
def parse_raw_response(
114113
response: bytes | str,
115-
) -> Response | ValidationError | JSONDecodeError:
114+
) -> Response | ValidationError:
116115
try:
117116
return RESPONSE_ADAPTER.validate_json(response)
118-
except (ValidationError, JSONDecodeError) as e:
117+
except ValidationError as e:
119118
return e

packages/cmk-plugins/cmk/plugins/kube/query.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
import argparse
88
import enum
9-
import json
109
import logging
1110
import os
1211
from collections.abc import Iterable, Iterator, Mapping, MutableMapping
@@ -22,9 +21,7 @@
2221

2322
TCPTimeout = NewType("TCPTimeout", tuple[int, int])
2423

25-
HTTPResult = (
26-
Response | ValidationError | json.JSONDecodeError | requests.exceptions.RequestException
27-
)
24+
HTTPResult = Response | ValidationError | requests.exceptions.RequestException
2825

2926

3027
class PrometheusEndpoints(enum.StrEnum):

packages/cmk-plugins/cmk/plugins/kube/schemata/section.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
"""
1212

1313
import enum
14-
import json
1514
from collections.abc import Mapping, Sequence
1615
from typing import assert_never, Literal, NewType
1716

@@ -622,7 +621,6 @@ class HardResourceRequirement(Section, api.HardResourceRequirement):
622621

623622
class ResultType(enum.Enum):
624623
request_exception = "request_exception"
625-
json_decode_error = "json_decode_error"
626624
validation_error = "validation_error"
627625
response_error = "response_error"
628626
response_invalid_data = "reponse_invalid_data"
@@ -648,8 +646,6 @@ def _from_result(cls, result: query.HTTPResult) -> tuple[ResultType, str | None]
648646
match result:
649647
case requests.exceptions.RequestException():
650648
return ResultType.request_exception, type(result).__name__
651-
case json.JSONDecodeError():
652-
return ResultType.json_decode_error, None
653649
case ValidationError():
654650
return ResultType.validation_error, None
655651
case prometheus_api.ResponseError():
@@ -666,8 +662,6 @@ def summary(self) -> str:
666662
match self.type_:
667663
case ResultType.request_exception:
668664
return f"Request Exception: {self.details}"
669-
case ResultType.json_decode_error:
670-
return "Invalid response: did not receive JSON"
671665
case ResultType.validation_error:
672666
return "Invalid response: did not match Prometheus HTTP API"
673667
case ResultType.response_error:

packages/cmk-plugins/tests/cmk/plugins/kube/utils_kubernetes/query.py renamed to packages/cmk-plugins/tests/cmk/plugins/kube/utils_kubernetes/test_query.py

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@
5353
),
5454
)
5555
)
56-
JSONERROR = prometheus_api.parse_raw_response("{")
5756
VALIDATIONERROR = prometheus_api.parse_raw_response("{}")
5857
EMPTY_DATA = ResponseSuccessFactory.build(data=VectorFactory.build(result=[]))
5958
SUCCESS = ResponseSuccessFactory.build(data=VectorFactory.build(result=SampleFactory.batch(size=3)))
@@ -62,45 +61,47 @@
6261
@pytest.mark.parametrize(
6362
"result, expected_details, expected_type",
6463
[
65-
(
64+
pytest.param(
6665
SSLERROR,
6766
"SSLError",
6867
section.ResultType.request_exception,
68+
id="ssl error",
6969
),
70-
(
70+
pytest.param(
7171
PROXYERROR,
7272
"ProxyError",
7373
section.ResultType.request_exception,
74+
id="proxy error",
7475
),
75-
(
76+
pytest.param(
7677
CONNECTIONERROR,
7778
"ConnectionError",
7879
section.ResultType.request_exception,
80+
id="connection error",
7981
),
80-
(
81-
JSONERROR,
82-
None,
83-
section.ResultType.json_decode_error,
84-
),
85-
(
82+
pytest.param(
8683
VALIDATIONERROR,
8784
None,
8885
section.ResultType.validation_error,
86+
id="validation error",
8987
),
90-
(
88+
pytest.param(
9189
EMPTY_DATA,
9290
None,
9391
section.ResultType.response_empty_result,
92+
id="empty data",
9493
),
95-
(
94+
pytest.param(
9695
SUCCESS,
9796
None,
9897
section.ResultType.success,
98+
id="success",
9999
),
100-
(
100+
pytest.param(
101101
RESPONSEERROR,
102102
f"{RESPONSEERROR.error_type}: {RESPONSEERROR.error}",
103103
section.ResultType.response_error,
104+
id="response error",
104105
),
105106
],
106107
)
@@ -117,7 +118,7 @@ def test_prometheus__from_result(
117118

118119
@pytest.mark.parametrize(
119120
"result",
120-
[SSLERROR, PROXYERROR, CONNECTIONERROR, JSONERROR, VALIDATIONERROR, EMPTY_DATA, SUCCESS],
121+
[SSLERROR, PROXYERROR, CONNECTIONERROR, VALIDATIONERROR, EMPTY_DATA, SUCCESS],
121122
)
122123
def test_prometheus_result_from_response(result: query.HTTPResult) -> None:
123124
# Assemble

0 commit comments

Comments
 (0)