-
Notifications
You must be signed in to change notification settings - Fork 4
telemetry: fail closed when remote config can't be fetched or parsed #72
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| """Tests for the telemetry remote-config download behaviour.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from unittest.mock import MagicMock, patch | ||
|
|
||
| import pytest | ||
| import requests | ||
|
|
||
| from tabpfn_common_utils.telemetry.core import config as config_module | ||
|
|
||
|
|
||
| @pytest.fixture(autouse=True) | ||
| def _clear_ttl_cache(): | ||
| """Clear the lru_cache on download_config between tests. | ||
|
|
||
| download_config is wrapped by @ttl_cache (which itself uses functools.lru_cache). | ||
| functools.wraps preserves the underlying cached callable as __wrapped__, so we | ||
| can clear it directly to ensure each test sees a fresh fetch. | ||
| """ | ||
| config_module.download_config.__wrapped__.cache_clear() # type: ignore[attr-defined] | ||
| yield | ||
| config_module.download_config.__wrapped__.cache_clear() # type: ignore[attr-defined] | ||
|
|
||
|
|
||
| def _mock_response(status_code: int, json_payload=None, raise_on_json: bool = False): | ||
| resp = MagicMock(spec=requests.Response) | ||
| resp.status_code = status_code | ||
| if raise_on_json: | ||
| resp.json.side_effect = ValueError("not json") | ||
| else: | ||
| resp.json.return_value = json_payload | ||
| return resp | ||
|
|
||
|
|
||
| def test_download_config_returns_payload_on_200(): | ||
| payload = {"enabled": True, "sampling_rate": 0.5} | ||
| with patch.object(config_module.requests, "get", return_value=_mock_response(200, payload)): | ||
| assert config_module.download_config() == payload | ||
|
|
||
|
|
||
| def test_download_config_fails_closed_on_network_exception(): | ||
| """If requests.get raises (timeout, DNS, firewall, etc.), default to disabled.""" | ||
| with patch.object(config_module.requests, "get", side_effect=requests.exceptions.ConnectionError("boom")): | ||
| assert config_module.download_config() == {"enabled": False} | ||
|
|
||
|
|
||
| def test_download_config_fails_closed_on_timeout(): | ||
| with patch.object(config_module.requests, "get", side_effect=requests.exceptions.Timeout("slow")): | ||
| assert config_module.download_config() == {"enabled": False} | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("status", [403, 404, 500, 503]) | ||
| def test_download_config_fails_closed_on_non_200(status: int): | ||
| with patch.object(config_module.requests, "get", return_value=_mock_response(status, {"enabled": True})): | ||
| assert config_module.download_config() == {"enabled": False} | ||
|
|
||
|
|
||
| def test_download_config_fails_closed_on_invalid_json(): | ||
| """If the body isn't parseable JSON, default to disabled.""" | ||
| with patch.object( | ||
| config_module.requests, "get", return_value=_mock_response(200, raise_on_json=True) | ||
| ): | ||
| assert config_module.download_config() == {"enabled": False} | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "payload", | ||
| [ | ||
| [1, 2, 3], # list | ||
| "enabled", # string | ||
| 42, # int | ||
| None, # null | ||
| {"foo": "bar"}, # dict missing "enabled" | ||
| {}, # empty dict | ||
| ], | ||
| ) | ||
| def test_download_config_fails_closed_on_malformed_shape(payload): | ||
| """If the JSON parses but isn't a dict with an 'enabled' key, default to disabled.""" | ||
| with patch.object( | ||
| config_module.requests, "get", return_value=_mock_response(200, payload) | ||
| ): | ||
| assert config_module.download_config() == {"enabled": False} | ||
|
|
||
|
|
||
| def test_download_config_uses_env_override(monkeypatch): | ||
| """Respects TABPFN_TELEMETRY_CONFIG_URL when set.""" | ||
| monkeypatch.setenv("TABPFN_TELEMETRY_CONFIG_URL", "https://example.test/cfg.json") | ||
| with patch.object( | ||
| config_module.requests, "get", return_value=_mock_response(200, {"enabled": True}) | ||
| ) as mock_get: | ||
| config_module.download_config() | ||
| called_url = mock_get.call_args.args[0] if mock_get.call_args.args else mock_get.call_args.kwargs.get("url") | ||
| assert called_url == "https://example.test/cfg.json" |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.