-
Notifications
You must be signed in to change notification settings - Fork 1
DEV-14971: Python SDK storage-service compatibility unit tests #459
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
sihrc
wants to merge
12
commits into
master
Choose a base branch
from
dev-14971-python-sdk-storage-compat
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.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
197a6d8
Support post-release tag matching in version pattern
jacobmanderson e330040
Add changelog entry for post-release version tag matching
jacobmanderson af597d3
DEV-14971: Add unit tests validating Python SDK compat with storage-s…
sihrc e7b97ad
Merge remote-tracking branch 'origin/master' into dev-14971-python-sd…
sihrc 00d3720
test: include unit-test runtime deps in dev group
sihrc 516ba6f
test: make release pattern test py310-compatible
sihrc 34bdc7f
test(DEV-14971): add redirect and signed-url static model upload comp…
sihrc a86cbd6
chore(DEV-14971): retrigger code_checks after clean tox verification
sihrc 92bd191
Fix ruff formatting for CI code_checks
sihrc 5bb0cf9
Stabilize storage compat tests in clean CI envs
sihrc 746b773
ci: retrigger publish_python_sdk code_checks for DEV-14971
sihrc abca0c4
ci: retrigger publish_python_sdk code checks
sihrc 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
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,35 @@ | ||
| import re | ||
| from pathlib import Path | ||
|
|
||
| try: # pragma: no cover - exercised indirectly on Python <3.11 tox envs | ||
| import tomllib | ||
| except ModuleNotFoundError: # pragma: no cover | ||
| import tomli as tomllib | ||
|
|
||
|
|
||
| def _load_release_pattern() -> str: | ||
| pyproject_path = Path(__file__).resolve().parents[2] / "pyproject.toml" | ||
| with pyproject_path.open("rb") as f: | ||
| pyproject = tomllib.load(f) | ||
| return pyproject["tool"]["uv-dynamic-versioning"]["pattern"] | ||
|
|
||
|
|
||
| def test_release_pattern_matches_standard_tag() -> None: | ||
| pattern = re.compile(_load_release_pattern()) | ||
| match = pattern.match("7.9.0") | ||
| assert match is not None | ||
| assert match.groupdict() == {"base": "7.9.0", "stage": None, "revision": None} | ||
|
|
||
|
|
||
| def test_release_pattern_matches_hyphen_prerelease_tag() -> None: | ||
| pattern = re.compile(_load_release_pattern()) | ||
| match = pattern.match("7.9.0-rc1") | ||
| assert match is not None | ||
| assert match.groupdict() == {"base": "7.9.0", "stage": "rc", "revision": "1"} | ||
|
|
||
|
|
||
| def test_release_pattern_matches_post_release_tag() -> None: | ||
| pattern = re.compile(_load_release_pattern()) | ||
| match = pattern.match("7.9.0.post1") | ||
| assert match is not None | ||
| assert match.groupdict() == {"base": "7.9.0", "stage": "post", "revision": "1"} |
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,293 @@ | ||
| """ | ||
| Storage-service compatibility unit tests. | ||
|
|
||
| Validates that Python SDK storage query classes remain compatible with the | ||
| response shapes produced by storage-service (the Rainbow replacement). These | ||
| tests mock at the HTTP level and require no running service. | ||
|
|
||
| Covered flows: | ||
| * UploadDocument: POST /storage/files/store, LegacyUploadResponseItem shape | ||
| * CreateStorageURLs: indico-file:///storage<path> URI construction | ||
| * RetrieveStorageObject: indico-file:// prefix stripping and GET path | ||
| """ | ||
|
|
||
| import io | ||
| import json | ||
| import threading | ||
| from http.server import BaseHTTPRequestHandler, HTTPServer | ||
| from pathlib import Path | ||
|
|
||
| import pytest | ||
|
|
||
| from indico.client import IndicoClient | ||
| from indico.client.request import HTTPMethod | ||
| from indico.config import IndicoConfig | ||
| from indico.errors import IndicoRequestError | ||
| from indico.queries.model_import import _UploadSMExport | ||
| from indico.queries.storage import ( | ||
| CreateStorageURLs, | ||
| RetrieveStorageObject, | ||
| UploadDocument, | ||
| ) | ||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # Response shape produced by storage-service /files/store endpoint | ||
| # (mirrors LegacyUploadResponseItem from storage_service/routes/blob_routes.py) | ||
| # --------------------------------------------------------------------------- | ||
| STORAGE_SERVICE_UPLOAD_RESPONSE = [ | ||
| { | ||
| "path": "/uploads/42/abc-uuid", | ||
| "name": "document.pdf", | ||
| "size": 12345, | ||
| "upload_type": "user", | ||
| } | ||
| ] | ||
|
|
||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # Fixtures | ||
| # --------------------------------------------------------------------------- | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def cfg(): | ||
| return IndicoConfig(protocol="mock", host="mock", api_token="test-token") | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def mock_request(requests_mock, cfg): | ||
| """Register a URL on requests_mock using the test config base URL.""" | ||
|
|
||
| def _register(method, path, **kwargs): | ||
| url = f"{cfg.protocol}://{cfg.host}{path}" | ||
| getattr(requests_mock, method)( | ||
| url, **kwargs, headers={"Content-Type": "application/json"} | ||
| ) | ||
|
|
||
| return _register | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def client(mock_request, cfg): | ||
| mock_request("post", "/auth/users/refresh_token", json={"auth_token": "tok"}) | ||
| return IndicoClient(config=cfg) | ||
|
|
||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # UploadDocument — request shape and response parsing | ||
| # --------------------------------------------------------------------------- | ||
|
|
||
|
|
||
| def test_upload_document_posts_to_storage_files_store(mock_request, client): | ||
| """UploadDocument sends POST to /storage/files/store.""" | ||
| captured = [] | ||
|
|
||
| def capture(request, context): | ||
| captured.append(request.path) | ||
| context.status_code = 200 | ||
| context.headers["Content-Type"] = "application/json" | ||
| import json as _json | ||
|
|
||
| return _json.dumps(STORAGE_SERVICE_UPLOAD_RESPONSE) | ||
|
|
||
| mock_request("post", "/storage/files/store", text=capture) | ||
| client.call(UploadDocument(streams={"test.pdf": io.BytesIO(b"data")})) | ||
| assert captured == ["/storage/files/store"] | ||
|
|
||
|
|
||
| def test_upload_document_processes_path_name_upload_type(mock_request, client): | ||
| """UploadDocument.process_response reads path/name/upload_type from storage-service.""" | ||
| mock_request("post", "/storage/files/store", json=STORAGE_SERVICE_UPLOAD_RESPONSE) | ||
| result = client.call(UploadDocument(streams={"test.pdf": io.BytesIO(b"data")})) | ||
|
|
||
| assert len(result) == 1 | ||
| assert result[0]["filename"] == "document.pdf" | ||
| meta = json.loads(result[0]["filemeta"]) | ||
| assert meta["path"] == "/uploads/42/abc-uuid" | ||
| assert meta["name"] == "document.pdf" | ||
| assert meta["uploadType"] == "user" | ||
|
|
||
|
|
||
| def test_upload_document_handles_multiple_files(mock_request, client): | ||
| """Multiple files in one upload are each parsed correctly.""" | ||
| multi_response = [ | ||
| { | ||
| "path": "/uploads/42/uuid-1", | ||
| "name": "a.pdf", | ||
| "size": 100, | ||
| "upload_type": "user", | ||
| }, | ||
| { | ||
| "path": "/uploads/42/uuid-2", | ||
| "name": "b.pdf", | ||
| "size": 200, | ||
| "upload_type": "user", | ||
| }, | ||
| ] | ||
| mock_request("post", "/storage/files/store", json=multi_response) | ||
| result = client.call( | ||
| UploadDocument( | ||
| streams={ | ||
| "a.pdf": io.BytesIO(b"aaa"), | ||
| "b.pdf": io.BytesIO(b"bbb"), | ||
| } | ||
| ) | ||
| ) | ||
| assert len(result) == 2 | ||
| assert result[0]["filename"] == "a.pdf" | ||
| assert result[1]["filename"] == "b.pdf" | ||
|
|
||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # CreateStorageURLs — indico-file URI construction | ||
| # --------------------------------------------------------------------------- | ||
|
|
||
|
|
||
| def test_create_storage_urls_builds_indico_file_uris(mock_request, client): | ||
| """CreateStorageURLs returns indico-file:///storage<path> from storage-service response.""" | ||
| mock_request("post", "/storage/files/store", json=STORAGE_SERVICE_UPLOAD_RESPONSE) | ||
| result = client.call(CreateStorageURLs(streams={"test.pdf": io.BytesIO(b"data")})) | ||
| assert result == ["indico-file:///storage/uploads/42/abc-uuid"] | ||
|
|
||
|
|
||
| def test_create_storage_urls_round_trips_through_retrieve(): | ||
| """A URI from CreateStorageURLs can be fed directly into RetrieveStorageObject.""" | ||
| uri = "indico-file:///storage/uploads/42/abc-uuid" | ||
| req = RetrieveStorageObject(uri) | ||
| assert req.path == "/storage/uploads/42/abc-uuid" | ||
| assert req.method == HTTPMethod.GET | ||
|
|
||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # RetrieveStorageObject — path construction | ||
| # --------------------------------------------------------------------------- | ||
|
|
||
|
|
||
| def test_retrieve_storage_object_strips_indico_file_scheme(): | ||
| """indico-file:// prefix is stripped; remaining path becomes the GET path.""" | ||
| req = RetrieveStorageObject("indico-file:///storage/submissions/1/2/result.json") | ||
| assert req.path == "/storage/submissions/1/2/result.json" | ||
| assert req.method == HTTPMethod.GET | ||
|
|
||
|
|
||
| def test_retrieve_storage_object_accepts_dict_with_url_key(): | ||
| """Accepts a dict with 'url' key (as returned by GraphQL result objects).""" | ||
| req = RetrieveStorageObject({"url": "indico-file:///storage/extractions/99.json"}) | ||
| assert req.path == "/storage/extractions/99.json" | ||
|
|
||
|
|
||
| def test_retrieve_storage_object_fetches_content(mock_request, client): | ||
| """GET /storage/<path> is issued and the response body is returned.""" | ||
| payload = {"status": "complete", "results": [{"text": "hello"}]} | ||
| mock_request("get", "/storage/submissions/1/2/result.json", json=payload) | ||
| result = client.call( | ||
| RetrieveStorageObject("indico-file:///storage/submissions/1/2/result.json") | ||
| ) | ||
| assert result == payload | ||
|
|
||
|
|
||
| def test_retrieve_storage_object_follows_redirects(): | ||
| """Storage GET requests follow redirects in redirect-mode deployments.""" | ||
| payload = {"status": "complete", "results": [{"text": "redirected"}]} | ||
| refresh_path = "/auth/users/refresh_token" | ||
| source_path = "/storage/submissions/1/2/result.json" | ||
| redirected_path = "/storage/signed/submissions/1/2/result.json" | ||
|
|
||
| class Handler(BaseHTTPRequestHandler): | ||
| def do_POST(self): # noqa: N802 | ||
| if self.path != refresh_path: | ||
| self.send_response(404) | ||
| self.end_headers() | ||
| return | ||
| body = json.dumps({"auth_token": "tok"}).encode("utf-8") | ||
| self.send_response(200) | ||
| self.send_header("Content-Type", "application/json") | ||
| self.send_header("Content-Length", str(len(body))) | ||
| self.end_headers() | ||
| self.wfile.write(body) | ||
|
|
||
| def do_GET(self): # noqa: N802 | ||
| if self.path == source_path: | ||
| self.send_response(302) | ||
| self.send_header( | ||
| "Location", | ||
| f"http://{self.server.server_address[0]}:{self.server.server_address[1]}{redirected_path}", | ||
| ) | ||
| self.end_headers() | ||
| return | ||
| if self.path == redirected_path: | ||
| body = json.dumps(payload).encode("utf-8") | ||
| self.send_response(200) | ||
| self.send_header("Content-Type", "application/json") | ||
| self.send_header("Content-Length", str(len(body))) | ||
| self.end_headers() | ||
| self.wfile.write(body) | ||
| return | ||
| self.send_response(404) | ||
| self.end_headers() | ||
|
|
||
| def log_message(self, format, *args): # noqa: A003 | ||
| return | ||
|
|
||
| with HTTPServer(("127.0.0.1", 0), Handler) as server: | ||
| thread = threading.Thread(target=server.serve_forever, daemon=True) | ||
| thread.start() | ||
| try: | ||
| host = f"{server.server_address[0]}:{server.server_address[1]}" | ||
| client = IndicoClient( | ||
| config=IndicoConfig(protocol="http", host=host, api_token="test-token") | ||
| ) | ||
| result = client.call( | ||
| RetrieveStorageObject( | ||
| "indico-file:///storage/submissions/1/2/result.json" | ||
| ) | ||
| ) | ||
| assert result == payload | ||
| finally: | ||
| server.shutdown() | ||
| thread.join(timeout=5) | ||
|
|
||
|
|
||
| def test_upload_static_model_export_puts_zip_to_signed_url(tmp_path, requests_mock): | ||
| """Static model export upload uses the signed URL with zip content-type.""" | ||
| export_path = tmp_path / "model.zip" | ||
| export_bytes = b"zip-bytes" | ||
| export_path.write_bytes(export_bytes) | ||
| signed_url = "https://signed.example/upload" | ||
| storage_uri = "indico-file:///storage/exports/model.zip" | ||
|
|
||
| requests_mock.put(signed_url, status_code=200, text="") | ||
|
|
||
| request = _UploadSMExport(str(export_path)) | ||
| result = request.process_response( | ||
| {"data": {"exportUpload": {"signedUrl": signed_url, "storageUri": storage_uri}}} | ||
| ) | ||
|
|
||
| assert result == storage_uri | ||
| assert len(requests_mock.request_history) == 1 | ||
| put_call = requests_mock.request_history[0] | ||
| assert put_call.method == "PUT" | ||
| assert put_call.headers["Content-Type"] == "application/zip" | ||
| assert put_call.body == export_bytes | ||
|
|
||
|
|
||
| def test_upload_static_model_export_raises_on_put_failure(tmp_path, requests_mock): | ||
| """A failing signed-url PUT raises IndicoRequestError.""" | ||
| export_path = tmp_path / "model.zip" | ||
| export_path.write_bytes(b"zip-bytes") | ||
| signed_url = "https://signed.example/upload" | ||
|
|
||
| requests_mock.put(signed_url, status_code=403, text="forbidden") | ||
|
|
||
| request = _UploadSMExport(str(Path(export_path))) | ||
| with pytest.raises(IndicoRequestError): | ||
| request.process_response( | ||
| { | ||
| "data": { | ||
| "exportUpload": { | ||
| "signedUrl": signed_url, | ||
| "storageUri": "indico-file:///storage/exports/model.zip", | ||
| } | ||
| } | ||
| } | ||
| ) | ||
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
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.