Repository navigation
test(admin): cover evict_workspace and admin_call (#46) #125
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,185 @@ | ||
| # Copyright 2017-present, The Visdom Authors | ||
| """Asking the visdom instances to close the sockets they hold for a workspace.""" | ||
| import io | ||
| import json | ||
| import urllib.error | ||
|
|
||
| import pytest | ||
|
|
||
| from app.admin import activity | ||
| from app.config import settings | ||
| from app.internal_signing import signature | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def instances(monkeypatch): | ||
| def _set(answers): | ||
| addresses = list(answers) | ||
| asked = [] | ||
|
|
||
| def fake(address, path, body, timeout): | ||
| assert path == activity.EVICT_PATH | ||
| asked.append((address, json.loads(body))) | ||
| return answers[address] | ||
|
|
||
| monkeypatch.setattr(activity, "instance_addresses", lambda: addresses) | ||
| monkeypatch.setattr(activity, "admin_call", fake) | ||
| return asked | ||
| return _set | ||
|
|
||
|
|
||
| def test_every_instance_is_asked_and_the_closed_sockets_are_added_up(instances): | ||
| """Sockets outlive a reassignment, so the instance holding them may not be | ||
| the one the proxy hashes the workspace onto now.""" | ||
| asked = instances({ | ||
| "visdom-1:8097": {"closed": 3}, | ||
| "visdom-2:8097": {"closed": 0}, | ||
| "visdom-3:8097": {"closed": 2}, | ||
| }) | ||
|
|
||
| assert activity.evict_workspace("team-a") == 5 | ||
| assert sorted(address for address, _ in asked) == ["visdom-1:8097", "visdom-2:8097", "visdom-3:8097"] | ||
| assert all(payload["workspace_slug"] == "team-a" for _, payload in asked) | ||
|
|
||
|
|
||
| def test_an_instance_that_cannot_be_reached_counts_as_nothing(instances): | ||
| """Best effort: a silent instance costs promptness, not the other answers.""" | ||
| instances({"visdom-1:8097": None, "visdom-2:8097": {"closed": 4}}) | ||
|
|
||
| assert activity.evict_workspace("team-a") == 4 | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("answer", [{}, {"closed": "n/a"}, {"closed": None}, {"closed": [1]}]) | ||
| def test_an_answer_without_a_usable_count_counts_as_nothing(instances, answer): | ||
| instances({"visdom-1:8097": answer, "visdom-2:8097": {"closed": 1}}) | ||
|
|
||
| assert activity.evict_workspace("team-a") == 1 | ||
|
|
||
|
|
||
| def test_the_reason_is_passed_on_when_there_is_one(instances): | ||
| asked = instances({"visdom-1:8097": {"closed": 1}}) | ||
|
|
||
| activity.evict_workspace("team-a", reason="this workspace was suspended") | ||
|
|
||
| assert asked[0][1] == {"workspace_slug": "team-a", "reason": "this workspace was suspended"} | ||
|
|
||
|
|
||
| def test_no_reason_is_sent_when_none_is_given(instances): | ||
| asked = instances({"visdom-1:8097": {"closed": 1}}) | ||
|
|
||
| activity.evict_workspace("team-a") | ||
|
|
||
| assert asked[0][1] == {"workspace_slug": "team-a"} | ||
|
|
||
|
|
||
| def test_with_no_instances_there_is_nothing_to_ask(monkeypatch): | ||
| monkeypatch.setattr(activity, "instance_addresses", lambda: []) | ||
| monkeypatch.setattr(activity, "admin_call", lambda *a: pytest.fail("nothing should be asked")) | ||
|
|
||
| assert activity.evict_workspace("team-a") == 0 | ||
|
|
||
|
|
||
| def test_an_empty_slug_asks_nobody(monkeypatch): | ||
| monkeypatch.setattr(activity, "instance_addresses", lambda: ["visdom-1:8097"]) | ||
| monkeypatch.setattr(activity, "admin_call", lambda *a: pytest.fail("nothing should be asked")) | ||
|
|
||
| assert activity.evict_workspace("") == 0 | ||
|
|
||
|
|
||
| def test_the_timeout_defaults_to_the_activity_timeout(monkeypatch): | ||
| timeouts = [] | ||
| monkeypatch.setattr(activity, "instance_addresses", lambda: ["visdom-1:8097"]) | ||
| monkeypatch.setattr(activity, "admin_call", lambda a, p, b, timeout: timeouts.append(timeout) or {"closed": 0}) | ||
| monkeypatch.setattr(settings, "VISDOM_ACTIVITY_TIMEOUT", 2.5) | ||
|
|
||
| activity.evict_workspace("team-a") | ||
| activity.evict_workspace("team-a", timeout=0.3) | ||
|
|
||
| assert timeouts == [2.5, 0.3] | ||
|
|
||
|
|
||
| # admin_call is what evict_workspace sends each request through. | ||
|
|
||
|
|
||
| class _Answer(io.BytesIO): | ||
| def __enter__(self): | ||
| return self | ||
|
|
||
| def __exit__(self, *exc): | ||
| self.close() | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def admin_key(monkeypatch): | ||
| monkeypatch.setattr(settings, "VISDOM_INTERNAL_ADMIN_KEY", "admin-key") | ||
|
|
||
|
|
||
| def _answering(monkeypatch, body=None, error=None): | ||
| sent = [] | ||
|
|
||
| def fake_urlopen(request, timeout, context): | ||
| request.timeout = timeout | ||
| sent.append(request) | ||
| if error is not None: | ||
| raise error | ||
| return _Answer(body) | ||
|
|
||
| monkeypatch.setattr(activity.urllib.request, "urlopen", fake_urlopen) | ||
| return sent | ||
|
|
||
|
|
||
| def test_admin_call_posts_a_signed_request_to_the_internal_port(monkeypatch, admin_key): | ||
| sent = _answering(monkeypatch, b'{"closed": 2}') | ||
| body = json.dumps({"workspace_slug": "team-a"}).encode() | ||
|
|
||
| answer = activity.admin_call("visdom-1:8097", activity.EVICT_PATH, body, timeout=1.0) | ||
|
|
||
| assert answer == {"closed": 2} | ||
| request = sent[0] | ||
| assert request.get_method() == "POST" | ||
| assert request.full_url == f"https://visdom-1:{settings.VISDOM_INTERNAL_PORT}{activity.EVICT_PATH}" | ||
| assert request.data == body | ||
| assert request.get_header("Content-type") == "application/json" | ||
| timestamp = int(request.get_header("X-visdom-timestamp")) | ||
| expected = signature("admin-key", "POST", activity.EVICT_PATH, timestamp, body) | ||
| assert request.get_header("X-visdom-signature") == expected | ||
| assert 0 < request.timeout <= 1.0 | ||
|
|
||
|
|
||
| def test_admin_call_does_not_call_without_the_admin_key(monkeypatch): | ||
| monkeypatch.setattr(settings, "VISDOM_INTERNAL_ADMIN_KEY", "") | ||
| sent = _answering(monkeypatch, b'{"closed": 2}') | ||
|
|
||
| assert activity.admin_call("visdom-1:8097", activity.EVICT_PATH, b"{}", timeout=1.0) is None | ||
| assert sent == [] | ||
|
|
||
|
|
||
| def test_admin_call_does_not_call_when_the_internal_setup_is_incomplete(monkeypatch, admin_key): | ||
| monkeypatch.setattr(settings, "VISDOM_INTERNAL_READ_KEY", "") | ||
| sent = _answering(monkeypatch, b'{"closed": 2}') | ||
|
|
||
| assert activity.admin_call("visdom-1:8097", activity.EVICT_PATH, b"{}", timeout=1.0) is None | ||
| assert sent == [] | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "error", | ||
| [urllib.error.URLError("refused"), ConnectionResetError("reset"), TimeoutError("slow")], | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 Medium · Truncated replies abort eviction When an instance sends a chunked response that ends mid-chunk, Catch Prompt for AI agentsIn `gateway/tests/test_evict_workspace.py` at line 165:
**Truncated replies abort eviction**
When an instance sends a chunked response that ends mid-chunk, `admin_call` does not catch `http.client.IncompleteRead` raised while reading a malformed or truncated chunked response, so the exception escapes `pool.map` and aborts `evict_workspace` instead of treating that instance as unreachable; synchronous callers see a failed request.
Catch `http.client.HTTPException` in `admin_call` and add a truncated-response case to this test. |
||
| ) | ||
| def test_admin_call_gives_none_when_the_instance_cannot_be_reached(monkeypatch, admin_key, error): | ||
| _answering(monkeypatch, error=error) | ||
|
|
||
| assert activity.admin_call("visdom-1:8097", activity.EVICT_PATH, b"{}", timeout=1.0) is None | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("body", [b"not json", b"[1, 2]", b'"closed"']) | ||
| def test_admin_call_gives_none_for_an_answer_in_the_wrong_shape(monkeypatch, admin_key, body): | ||
| _answering(monkeypatch, body) | ||
|
|
||
| assert activity.admin_call("visdom-1:8097", activity.EVICT_PATH, b"{}", timeout=1.0) is None | ||
|
|
||
|
|
||
| def test_admin_call_reads_an_empty_answer_as_an_empty_one(monkeypatch, admin_key): | ||
| _answering(monkeypatch, b"") | ||
|
|
||
| assert activity.admin_call("visdom-1:8097", activity.EVICT_PATH, b"{}", timeout=1.0) == {} | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟡 Medium · Invalid counts corrupt eviction totals
When an instance returns a fractional, negative, or infinite
closedvalue,_closedtruncates fractional values, accepts negative values, and raises an uncaughtOverflowErrorfor infinity;evict_workspacetherefore returns an incorrect total or aborts instead of counting the unusable answer as zero.Make
_closedreturn zero for non-finite, fractional, or negative counts beforeevict_workspacesums the answers.Also at
gateway/tests/test_evict_workspace.py:53.Prompt for AI agents