-
Notifications
You must be signed in to change notification settings - Fork 526
[fix] Resolve broken snapshot access in Daytona #4464
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
Open
junaway
wants to merge
2
commits into
main
Choose a base branch
from
fix/broken-daytona-snapshots
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+169
−2
Open
Changes from all commits
Commits
Show all changes
2 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
112 changes: 112 additions & 0 deletions
112
sdks/python/oss/tests/pytest/unit/test_daytona_resolve_snapshot.py
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,112 @@ | ||
| from unittest.mock import MagicMock | ||
|
|
||
| import httpx | ||
| import pytest | ||
|
|
||
| from agenta.sdk.engines.running.runners.daytona import DaytonaRunner | ||
|
|
||
|
|
||
| def _make_response(items): | ||
| response = MagicMock() | ||
| response.raise_for_status = MagicMock() | ||
| response.json.return_value = {"items": items} | ||
| return response | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def runner(monkeypatch): | ||
| monkeypatch.setenv("DAYTONA_API_KEY", "test-key") | ||
| monkeypatch.delenv("DAYTONA_API_URL", raising=False) | ||
| r = DaytonaRunner() | ||
| # Singleton bleed-through: ensure a clean cache between tests. | ||
| r._snapshot_id_cache.cache.clear() | ||
| return r | ||
|
|
||
|
|
||
| def test_resolve_snapshot_id_returns_matching_active_snapshot(runner, monkeypatch): | ||
| calls = [] | ||
|
|
||
| def fake_get(url, params=None, headers=None, timeout=None): | ||
| calls.append((url, params, headers)) | ||
| return _make_response( | ||
| [ | ||
| {"id": "id-1", "name": "other", "state": "active", "regionIds": ["eu"]}, | ||
| {"id": "id-2", "name": "snap", "state": "active", "regionIds": ["eu"]}, | ||
| {"id": "id-3", "name": "snap", "state": "active", "regionIds": ["us"]}, | ||
| ] | ||
| ) | ||
|
|
||
| monkeypatch.setattr(httpx, "get", fake_get) | ||
|
|
||
| assert runner._resolve_snapshot_id("snap", "eu") == "id-2" | ||
| assert len(calls) == 1 | ||
| assert calls[0][1] == {"limit": 25} | ||
| assert calls[0][2] == {"Authorization": "Bearer test-key"} | ||
|
|
||
|
|
||
| def test_resolve_snapshot_id_uses_cache_on_second_call(runner, monkeypatch): | ||
| call_count = {"n": 0} | ||
|
|
||
| def fake_get(url, params=None, headers=None, timeout=None): | ||
| call_count["n"] += 1 | ||
| return _make_response( | ||
| [{"id": "id-1", "name": "snap", "state": "active", "regionIds": ["eu"]}] | ||
| ) | ||
|
|
||
| monkeypatch.setattr(httpx, "get", fake_get) | ||
|
|
||
| assert runner._resolve_snapshot_id("snap", "eu") == "id-1" | ||
| assert runner._resolve_snapshot_id("snap", "eu") == "id-1" | ||
| assert call_count["n"] == 1 | ||
|
|
||
|
|
||
| def test_resolve_snapshot_id_caches_per_target(runner, monkeypatch): | ||
| responses_by_target = { | ||
| "eu": [{"id": "eu-id", "name": "snap", "state": "active", "regionIds": ["eu"]}], | ||
| "us": [{"id": "us-id", "name": "snap", "state": "active", "regionIds": ["us"]}], | ||
| } | ||
| seen_targets = [] | ||
|
|
||
| def fake_get(url, params=None, headers=None, timeout=None): | ||
| # The endpoint isn't target-scoped; we filter client-side. | ||
| # Return the union so per-target filtering is exercised. | ||
| seen_targets.append(params) | ||
| return _make_response(responses_by_target["eu"] + responses_by_target["us"]) | ||
|
|
||
| monkeypatch.setattr(httpx, "get", fake_get) | ||
|
|
||
| assert runner._resolve_snapshot_id("snap", "eu") == "eu-id" | ||
| assert runner._resolve_snapshot_id("snap", "us") == "us-id" | ||
| # Two distinct cache keys → two HTTP calls. | ||
| assert len(seen_targets) == 2 | ||
|
|
||
|
|
||
| def test_resolve_snapshot_id_skips_inactive_snapshots(runner, monkeypatch): | ||
| def fake_get(url, params=None, headers=None, timeout=None): | ||
| return _make_response( | ||
| [ | ||
| { | ||
| "id": "id-1", | ||
| "name": "snap", | ||
| "state": "building", | ||
| "regionIds": ["eu"], | ||
| }, | ||
| {"id": "id-2", "name": "snap", "state": "active", "regionIds": ["eu"]}, | ||
| ] | ||
| ) | ||
|
|
||
| monkeypatch.setattr(httpx, "get", fake_get) | ||
|
|
||
| assert runner._resolve_snapshot_id("snap", "eu") == "id-2" | ||
|
|
||
|
|
||
| def test_resolve_snapshot_id_raises_when_no_match(runner, monkeypatch): | ||
| def fake_get(url, params=None, headers=None, timeout=None): | ||
| return _make_response( | ||
| [{"id": "id-1", "name": "other", "state": "active", "regionIds": ["eu"]}] | ||
| ) | ||
|
|
||
| monkeypatch.setattr(httpx, "get", fake_get) | ||
|
|
||
| with pytest.raises(RuntimeError, match="No active Daytona snapshot named 'snap'"): | ||
| runner._resolve_snapshot_id("snap", "eu") |
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.