Skip to content

Commit 71db3d3

Browse files
committed
add more tests
1 parent 82d31e4 commit 71db3d3

1 file changed

Lines changed: 68 additions & 4 deletions

File tree

libs/giskard-core/tests/test_telemetry.py

Lines changed: 68 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,10 @@ def test_process_env_wins_over_dotenv(env_value, _clean_opt_out_env, monkeypatch
106106
assert telemetry_mod._should_disable() is False
107107

108108

109-
def test_late_opt_out_stops_sender_and_is_one_way(monkeypatch, _clean_opt_out_env):
109+
@pytest.mark.parametrize("channel", ["process-env", "dotenv"])
110+
def test_late_opt_out_stops_sender_and_is_one_way(
111+
channel, monkeypatch, _clean_opt_out_env
112+
):
110113
client = telemetry_mod.telemetry
111114
paused: list[bool] = []
112115

@@ -122,7 +125,12 @@ def pause(self) -> None:
122125
monkeypatch.setattr(
123126
telemetry_mod.atexit, "unregister", lambda fn: unregistered.append(fn)
124127
)
125-
monkeypatch.setenv("GISKARD_TELEMETRY_DISABLED", "1")
128+
if channel == "process-env":
129+
monkeypatch.setenv("GISKARD_TELEMETRY_DISABLED", "1")
130+
else:
131+
(_clean_opt_out_env / ".env").write_text(
132+
"GISKARD_TELEMETRY_DISABLED=1\n", encoding="utf-8"
133+
)
126134

127135
telemetry_mod._apply_env_opt_out()
128136

@@ -132,8 +140,11 @@ def pause(self) -> None:
132140
assert paused == [True]
133141
assert unregistered == [client.join]
134142

135-
# One-way: unsetting the flag does not re-enable sending.
136-
monkeypatch.delenv("GISKARD_TELEMETRY_DISABLED")
143+
# One-way: removing the flag does not re-enable sending.
144+
if channel == "process-env":
145+
monkeypatch.delenv("GISKARD_TELEMETRY_DISABLED")
146+
else:
147+
(_clean_opt_out_env / ".env").unlink()
137148
telemetry_mod._apply_env_opt_out()
138149
assert client.disabled is True
139150
assert client.send is False
@@ -270,6 +281,59 @@ def test_opt_out_before_import_makes_no_http(tmp_path, env_extra, dotenv_text):
270281
assert all(alive is False for alive in payload["consumer_alive"])
271282

272283

284+
_ENABLED_PROBE = r"""
285+
import json
286+
import time
287+
288+
calls = []
289+
290+
291+
def record(self, method, url, *args, **kwargs):
292+
calls.append(str(url))
293+
raise RuntimeError("network blocked")
294+
295+
296+
import requests
297+
298+
requests.Session.request = record
299+
300+
from giskard.core.telemetry.telemetry import (
301+
telemetry,
302+
telemetry_capture,
303+
telemetry_run_context,
304+
)
305+
306+
for consumer in telemetry.consumers:
307+
consumer.flush_interval = 0.2 # shorten the 5s batching window
308+
309+
with telemetry_run_context():
310+
telemetry_capture("enabled_event")
311+
312+
deadline = time.monotonic() + 10
313+
while not calls and time.monotonic() < deadline:
314+
time.sleep(0.05)
315+
print(
316+
json.dumps(
317+
{
318+
"send": bool(telemetry.send),
319+
"disabled": bool(telemetry.disabled),
320+
"urls": calls,
321+
}
322+
)
323+
)
324+
"""
325+
326+
327+
def test_enabled_telemetry_still_sends(tmp_path):
328+
"""Guard the opposite direction: with no opt-out flag an upload to the
329+
PostHog host must be attempted."""
330+
payload = _run_probe(_ENABLED_PROBE, tmp_path, {})
331+
assert payload["send"] is True
332+
assert payload["disabled"] is False
333+
assert any("eu.i.posthog.com" in url for url in payload["urls"])
334+
assert payload["id_file_exists"] is True
335+
336+
273337
_LATE_OPT_OUT_PROBE = r"""
274338
import json
275339
import time

0 commit comments

Comments
 (0)