Skip to content

Commit a42f52a

Browse files
vitkyrkaclaude
andcommitted
velero: extract shared replay_collector_blobs helper
The "parse each collector JSON blob, replay it, raise on bad JSON" loop was still duplicated between plugin/pytest.py's dd_agent_check and kube_discovery.py's replay_collector_output, right down to the identical error string — the neighboring flags_to_argv/find_collector_blobs extraction stopped one step short of this one. Extract it into replay_collector_blobs() in _env.py, tighten the bare raise Exception into raise ValueError(...) from e so the original JSONDecodeError traceback survives, and add a regression test that feeds a malformed JSON blob through run_discovery_check_kubernetes. Environment: Datadog workspace Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
1 parent 6b1d7fc commit a42f52a

4 files changed

Lines changed: 24 additions & 14 deletions

File tree

datadog_checks_dev/datadog_checks/dev/_env.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,16 @@ def find_collector_blobs(output: str) -> list[str]:
132132
return re.findall(JSON_COLLECTOR_PATTERN, output, re.DOTALL)
133133

134134

135+
def replay_collector_blobs(matches: list[str], stub_aggregator: Any, stub_agent: Any) -> None:
136+
"""Parse and replay each collector JSON blob found by ``find_collector_blobs``."""
137+
for raw_json in matches:
138+
try:
139+
collector = json.loads(raw_json)
140+
except Exception as e:
141+
raise ValueError(f'Error loading json: {e}\nCollector Json Output:\n{raw_json}') from e
142+
replay_check_run(collector, stub_aggregator, stub_agent)
143+
144+
135145
def replay_check_run(agent_collector, stub_aggregator, stub_agent):
136146
errors = []
137147
for collector in agent_collector:

datadog_checks_dev/datadog_checks/dev/kube_discovery.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
flags_to_argv,
3535
format_config,
3636
get_state,
37-
replay_check_run,
37+
replay_collector_blobs,
3838
save_state,
3939
set_up_env,
4040
)
@@ -235,12 +235,7 @@ def get_kube_discovery_state() -> dict[str, Any]:
235235
def replay_collector_output(output: str, aggregator: Any, datadog_agent: Any) -> int:
236236
"""Replay collector JSON blobs from ``agent check --json`` output."""
237237
matches = find_collector_blobs(output)
238-
for raw_json in matches:
239-
try:
240-
collector = json.loads(raw_json)
241-
except Exception as e:
242-
raise Exception(f'Error loading json: {e}\nCollector Json Output:\n{raw_json}')
243-
replay_check_run(collector, aggregator, datadog_agent)
238+
replay_collector_blobs(matches, aggregator, datadog_agent)
244239

245240
return len(matches)
246241

datadog_checks_dev/datadog_checks/dev/plugin/pytest.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
format_config,
2626
get_env_vars,
2727
get_state,
28-
replay_check_run,
28+
replay_collector_blobs,
2929
save_state,
3030
serialize_data,
3131
set_up_env,
@@ -219,12 +219,7 @@ def run_check(config=None, **kwargs):
219219
message_parts.append(result.stdout + result.stderr)
220220
raise ValueError('{}\nCould not find valid check output'.format('\n'.join(message_parts)))
221221

222-
for raw_json in matches:
223-
try:
224-
collector = json.loads(raw_json)
225-
except Exception as e:
226-
raise Exception("Error loading json: {}\nCollector Json Output:\n{}".format(e, raw_json))
227-
replay_check_run(collector, aggregator, datadog_agent)
222+
replay_collector_blobs(matches, aggregator, datadog_agent)
228223

229224
return aggregator
230225

datadog_checks_dev/tests/test_kube_discovery.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,16 @@ def test_no_json_output_raises(self, e2e_mode):
264264
with pytest.raises(ValueError, match='Could not find valid check output'):
265265
run_discovery_check_kubernetes(mock.Mock(), mock.Mock())
266266

267+
def test_malformed_json_output_raises(self, e2e_mode):
268+
save_kube_discovery_state('/tmp/kubeconfig')
269+
270+
stdout = 'preamble\n[ not valid json\n]\n'
271+
272+
with mock.patch('datadog_checks.dev.kube_discovery.run_command', return_value=result(stdout=stdout)):
273+
with mock.patch('datadog_checks.dev.kube_discovery.find_check_root', return_value='/root/test_check'):
274+
with pytest.raises(ValueError, match='Error loading json'):
275+
run_discovery_check_kubernetes(mock.Mock(), mock.Mock())
276+
267277

268278
class TestAssertAllDiscoveryCandidatesStableKubernetes:
269279
def test_skips_outside_e2e(self, monkeypatch):

0 commit comments

Comments
 (0)