Skip to content

Commit 182e62c

Browse files
authored
Skip kube_discovery setup outside the env-start phase
env test/env stop re-run the dd_environment fixture body to reach kind_run's teardown, but KUBECONFIG isn't guaranteed to resolve to a live cluster at that point. setup_discovery_agent()/save_kube_discovery_state() were running unconditionally, so env stop tried to kubectl apply against a stale/empty kubeconfig and errored (harmlessly, since kind_run's own teardown still ran, but noisily). Gate both on set_up_env(), matching how KindUp/ComposeFileUp/ PortForwardUp already behave. Verified via a real start/test/stop cycle against keda's kind cluster. Environment: Datadog workspace Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Co-authored-by: Vincent Whitchurch <vincent.whitchurch@datadoghq.com>
1 parent 8de8a47 commit 182e62c

2 files changed

Lines changed: 38 additions & 3 deletions

File tree

datadog_checks_dev/datadog_checks/dev/kube_discovery.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import pytest
2424
import yaml
2525

26-
from ._env import e2e_testing, format_config, get_state, replay_check_run, save_state
26+
from ._env import e2e_testing, format_config, get_state, replay_check_run, save_state, set_up_env
2727
from .docker import CONTAINER_STABILITY_LOG_PATTERNS, _assert_no_log_patterns
2828
from .subprocess import run_command
2929
from .utils import find_check_root
@@ -48,7 +48,14 @@
4848

4949

5050
def save_kube_discovery_state(kubeconfig_path: str | os.PathLike[str], *, namespace: str = DISCOVERY_NAMESPACE) -> None:
51-
"""Persist the kubeconfig path and agent namespace from the setup process to the test process."""
51+
"""Persist the kubeconfig path and agent namespace from the setup process to the test process.
52+
53+
A no-op outside the actual environment setup pass (``env test``/``env stop`` re-run the fixture
54+
body to reach ``kind_run``'s teardown, but must not re-derive or overwrite this state).
55+
"""
56+
if not set_up_env():
57+
return
58+
5259
save_state('kube_discovery', {'kubeconfig_path': os.fspath(kubeconfig_path), 'namespace': namespace})
5360

5461

@@ -62,8 +69,14 @@ def setup_discovery_agent(
6269
"""Deploy a sleeping, RBAC-scoped Agent pod that ``run_discovery_check_kubernetes`` can exec into.
6370
6471
Call this inside the integration's existing ``kind_run(...)`` block, while ``KUBECONFIG`` still
65-
points at the freshly created cluster.
72+
points at the freshly created cluster. A no-op outside the actual environment setup pass, matching
73+
how ``KindUp``/``ComposeFileUp``/``PortForwardUp`` behave: ``env test``/``env stop`` re-run the
74+
fixture body to reach ``kind_run``'s teardown, but ``KUBECONFIG`` isn't guaranteed to resolve to a
75+
live cluster at that point.
6676
"""
77+
if not set_up_env():
78+
return
79+
6780
check_root = os.fspath(check_root or find_check_root(depth=1))
6881
check_name = os.path.basename(check_root)
6982

datadog_checks_dev/tests/test_kube_discovery.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,29 @@ def test_get_kube_discovery_state_missing_raises(monkeypatch):
7171
_get_kube_discovery_state()
7272

7373

74+
def test_save_kube_discovery_state_is_noop_when_setup_disabled(monkeypatch):
75+
monkeypatch.delenv('DDEV_E2E_ENV_kube_discovery', raising=False)
76+
monkeypatch.setenv('DDEV_E2E_UP', 'false')
77+
78+
save_kube_discovery_state('/tmp/kubeconfig')
79+
80+
from datadog_checks.dev.kube_discovery import _get_kube_discovery_state
81+
82+
with pytest.raises(AssertionError, match='No kube_discovery state found'):
83+
_get_kube_discovery_state()
84+
85+
7486
class TestSetupDiscoveryAgent:
87+
def test_is_noop_when_setup_disabled(self, monkeypatch):
88+
monkeypatch.setenv('DDEV_E2E_UP', 'false')
89+
90+
with mock.patch('datadog_checks.dev.kube_discovery.run_command') as run_command:
91+
# env test/env stop re-run this fixture body to reach kind_run's teardown, but must not
92+
# re-apply manifests against a KUBECONFIG that isn't guaranteed to point at a live cluster.
93+
setup_discovery_agent('/tmp/kubeconfig')
94+
95+
run_command.assert_not_called()
96+
7597
def test_applies_manifests_and_installs_package(self, tmp_path):
7698
check_root = tmp_path / 'test_check'
7799
_write_auto_conf(check_root)

0 commit comments

Comments
 (0)