-
-
Notifications
You must be signed in to change notification settings - Fork 232
Use paginated Vast.ai v1 instances endpoint #3938
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
7Z0nE
wants to merge
1
commit into
dstackai:master
Choose a base branch
from
7Z0nE:vastai-v1-instances-pagination
base: master
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.
+159
−17
Open
Changes from all commits
Commits
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
108 changes: 108 additions & 0 deletions
108
src/tests/_internal/core/backends/vastai/test_api_client.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,108 @@ | ||
| import json | ||
| from urllib.parse import parse_qs, urlparse | ||
|
|
||
| import pytest | ||
|
|
||
| from dstack._internal.core.backends.vastai.api_client import VastAIAPIClient | ||
|
|
||
|
|
||
| class _FakeResponse: | ||
| def __init__(self, payload, status_code=200): | ||
| self._payload = payload | ||
| self.status_code = status_code | ||
| self.text = json.dumps(payload) | ||
|
|
||
| def json(self): | ||
| return self._payload | ||
|
|
||
| def raise_for_status(self): | ||
| if self.status_code >= 400: | ||
| raise AssertionError(f"unexpected status {self.status_code}") | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def client(): | ||
| return VastAIAPIClient(api_key="test-key") | ||
|
|
||
|
|
||
| def _parse_call(call): | ||
| """Return (path, query_dict) for a recorded get() call.""" | ||
| url = call.args[0] if call.args else call.kwargs["url"] | ||
| parsed = urlparse(url) | ||
| query = {k: v[0] for k, v in parse_qs(parsed.query).items()} | ||
| if "params" in call.kwargs and call.kwargs["params"]: | ||
| for k, v in call.kwargs["params"].items(): | ||
| query[k] = str(v) | ||
| return parsed.path, query | ||
|
|
||
|
|
||
| def test_get_instances_uses_v1_endpoint_and_paginates(client, monkeypatch): | ||
| pages = [ | ||
| {"instances": [{"id": 1}, {"id": 2}], "next_token": "tok-1"}, | ||
| {"instances": [{"id": 3}], "next_token": None}, | ||
| ] | ||
| calls = [] | ||
|
|
||
| def fake_get(url, params=None): | ||
| calls.append((url, dict(params or {}))) | ||
| return _FakeResponse(pages[len(calls) - 1]) | ||
|
|
||
| monkeypatch.setattr(client.s, "get", fake_get) | ||
|
|
||
| instances = client.get_instances(cache_ttl=0) | ||
|
|
||
| assert [i["id"] for i in instances] == [1, 2, 3] | ||
| assert len(calls) == 2 | ||
| # First call hits v1 and includes select_filters={} and a limit. | ||
| first_url, first_params = calls[0] | ||
| assert "/api/v1/instances/" in first_url | ||
| assert first_params["select_filters"] == "{}" | ||
| assert first_params["limit"] == 25 | ||
| assert "after_token" not in first_params | ||
| # Second call carries the next_token from the prior response. | ||
| _, second_params = calls[1] | ||
| assert second_params["after_token"] == "tok-1" | ||
|
|
||
|
|
||
| def test_get_instance_uses_v1_select_filters(client, monkeypatch): | ||
| calls = [] | ||
|
|
||
| def fake_get(url, params=None): | ||
| calls.append((url, params)) | ||
| return _FakeResponse( | ||
| {"instances": [{"id": 42, "actual_status": "running"}], "next_token": None} | ||
| ) | ||
|
|
||
| monkeypatch.setattr(client.s, "get", fake_get) | ||
|
|
||
| instance = client.get_instance(42) | ||
|
|
||
| assert instance == {"id": 42, "actual_status": "running"} | ||
| assert len(calls) == 1 | ||
| url, params = calls[0] | ||
| assert "/api/v1/instances/" in url | ||
| assert json.loads(params["select_filters"]) == {"id": {"eq": 42}} | ||
| assert params["limit"] == 1 | ||
|
|
||
|
|
||
| def test_get_instance_returns_none_when_missing(client, monkeypatch): | ||
| monkeypatch.setattr( | ||
| client.s, | ||
| "get", | ||
| lambda url, params=None: _FakeResponse({"instances": [], "next_token": None}), | ||
| ) | ||
| assert client.get_instance(99) is None | ||
|
|
||
|
|
||
| def test_destroy_instance_still_uses_v0(client, monkeypatch): | ||
| calls = [] | ||
|
|
||
| def fake_delete(url): | ||
| calls.append(url) | ||
| return _FakeResponse({"success": True}) | ||
|
|
||
| monkeypatch.setattr(client.s, "delete", fake_delete) | ||
| monkeypatch.setattr(client, "_invalidate_cache", lambda: None) | ||
|
|
||
| assert client.destroy_instance(7) is True | ||
| assert "/api/v0/instances/7/" in calls[0] |
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.
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.
Let's drop this new test file completely – the tests don't seem very useful.