Skip to content

Commit 155a97a

Browse files
vitkyrkaclaude
andcommitted
velero: extract monkeypatch boilerplate into fixtures in kube_discovery tests
monkeypatch.delenv('DDEV_E2E_ENV_kube_discovery', ...) was repeated as the first line of 10 test bodies, and monkeypatch.setenv('...PARENT_PYTHON', ...) in 7 more — boilerplate that's easy to get out of sync with a future test that forgets it. Pull both into an autouse clear_kube_discovery_state fixture and an opt-in e2e_mode fixture. Environment: Datadog workspace Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
1 parent a036c1f commit 155a97a

1 file changed

Lines changed: 17 additions & 26 deletions

File tree

datadog_checks_dev/tests/test_kube_discovery.py

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -52,27 +52,32 @@ def write_auto_conf(check_root):
5252
f.write('ad_identifiers:\n - test\ndiscovery: {}\ninit_config:\ninstances: []\n')
5353

5454

55-
def test_save_and_get_kube_discovery_state(monkeypatch):
55+
@pytest.fixture(autouse=True)
56+
def clear_kube_discovery_state(monkeypatch):
5657
monkeypatch.delenv('DDEV_E2E_ENV_kube_discovery', raising=False)
5758

59+
60+
@pytest.fixture
61+
def e2e_mode(monkeypatch):
62+
monkeypatch.setenv('DDEV_E2E_PARENT_PYTHON', '/usr/bin/python3')
63+
64+
65+
def test_save_and_get_kube_discovery_state():
5866
save_kube_discovery_state('/tmp/kubeconfig')
5967

6068
from datadog_checks.dev.kube_discovery import get_kube_discovery_state
6169

6270
assert get_kube_discovery_state() == {'kubeconfig_path': '/tmp/kubeconfig', 'namespace': DISCOVERY_NAMESPACE}
6371

6472

65-
def test_get_kube_discovery_state_missing_raises(monkeypatch):
66-
monkeypatch.delenv('DDEV_E2E_ENV_kube_discovery', raising=False)
67-
73+
def test_get_kube_discovery_state_missing_raises():
6874
from datadog_checks.dev.kube_discovery import get_kube_discovery_state
6975

7076
with pytest.raises(AssertionError, match='No kube_discovery state found'):
7177
get_kube_discovery_state()
7278

7379

7480
def test_save_kube_discovery_state_is_noop_when_setup_disabled(monkeypatch):
75-
monkeypatch.delenv('DDEV_E2E_ENV_kube_discovery', raising=False)
7681
monkeypatch.setenv('DDEV_E2E_UP', 'false')
7782

7883
save_kube_discovery_state('/tmp/kubeconfig')
@@ -214,16 +219,11 @@ def test_skips_outside_e2e(self, monkeypatch):
214219
with pytest.raises(pytest.skip.Exception, match='Not running E2E tests'):
215220
run_discovery_check_kubernetes(mock.Mock(), mock.Mock())
216221

217-
def test_missing_state_raises(self, monkeypatch):
218-
monkeypatch.delenv('DDEV_E2E_ENV_kube_discovery', raising=False)
219-
monkeypatch.setenv('DDEV_E2E_PARENT_PYTHON', '/usr/bin/python3')
220-
222+
def test_missing_state_raises(self, e2e_mode):
221223
with pytest.raises(AssertionError, match='No kube_discovery state found'):
222224
run_discovery_check_kubernetes(mock.Mock(), mock.Mock())
223225

224-
def test_replays_collector_output(self, monkeypatch):
225-
monkeypatch.delenv('DDEV_E2E_ENV_kube_discovery', raising=False)
226-
monkeypatch.setenv('DDEV_E2E_PARENT_PYTHON', '/usr/bin/python3')
226+
def test_replays_collector_output(self, e2e_mode):
227227
save_kube_discovery_state('/tmp/kubeconfig', namespace='dd-agent-discovery')
228228

229229
collector = {
@@ -255,9 +255,7 @@ def fake_run_command(command, **kwargs):
255255
'--json',
256256
]
257257

258-
def test_no_json_output_raises(self, monkeypatch):
259-
monkeypatch.delenv('DDEV_E2E_ENV_kube_discovery', raising=False)
260-
monkeypatch.setenv('DDEV_E2E_PARENT_PYTHON', '/usr/bin/python3')
258+
def test_no_json_output_raises(self, e2e_mode):
261259
save_kube_discovery_state('/tmp/kubeconfig')
262260

263261
with mock.patch('datadog_checks.dev.kube_discovery.run_command', return_value=result(stdout='no json here')):
@@ -275,16 +273,13 @@ def test_skips_outside_e2e(self, monkeypatch):
275273
mock.Mock(), mock.Mock(), mock.Mock(), namespace='keda', pod_name='workload'
276274
)
277275

278-
def test_requires_pod_name_or_selector(self, monkeypatch):
279-
monkeypatch.delenv('DDEV_E2E_ENV_kube_discovery', raising=False)
276+
def test_requires_pod_name_or_selector(self):
280277
save_kube_discovery_state('/tmp/kubeconfig')
281278

282279
with pytest.raises(TypeError, match='pod_name or pod_selector'):
283280
assert_all_discovery_candidates_stable_kubernetes(mock.Mock(), mock.Mock(), mock.Mock(), namespace='keda')
284281

285-
def test_probes_generated_candidates_and_detects_restart(self, monkeypatch):
286-
monkeypatch.delenv('DDEV_E2E_ENV_kube_discovery', raising=False)
287-
monkeypatch.setenv('DDEV_E2E_PARENT_PYTHON', '/usr/bin/python3')
282+
def test_probes_generated_candidates_and_detects_restart(self, e2e_mode):
288283
save_kube_discovery_state('/tmp/kubeconfig')
289284

290285
class DiscoveryCheck:
@@ -319,9 +314,7 @@ def fake_run_command(command, **kwargs):
319314
assert DiscoveryCheck.service.host == '10.0.0.5'
320315
assert [port.number for port in DiscoveryCheck.service.ports] == [8080]
321316

322-
def test_no_candidates_raises(self, monkeypatch):
323-
monkeypatch.delenv('DDEV_E2E_ENV_kube_discovery', raising=False)
324-
monkeypatch.setenv('DDEV_E2E_PARENT_PYTHON', '/usr/bin/python3')
317+
def test_no_candidates_raises(self, e2e_mode):
325318
save_kube_discovery_state('/tmp/kubeconfig')
326319

327320
class DiscoveryCheck:
@@ -342,9 +335,7 @@ def fake_run_command(command, **kwargs):
342335
DiscoveryCheck, mock.Mock(), mock.Mock(), namespace='keda', pod_name='workload'
343336
)
344337

345-
def test_resolves_pod_via_selector(self, monkeypatch):
346-
monkeypatch.delenv('DDEV_E2E_ENV_kube_discovery', raising=False)
347-
monkeypatch.setenv('DDEV_E2E_PARENT_PYTHON', '/usr/bin/python3')
338+
def test_resolves_pod_via_selector(self, e2e_mode):
348339
save_kube_discovery_state('/tmp/kubeconfig')
349340

350341
class DiscoveryCheck:

0 commit comments

Comments
 (0)