Files
SteamBase/tests/test_upload_snapshot.py
T
strsv ffe9e76a5b
CI / test (push) Successful in 40s
feat: export and upload library snapshots
Add snapshot.zip export with manifest/catalog/library, deterministic checksum, --no-notes/--include-dlc, plus an upload client with idempotency, token config and import polling.
2026-11-07 07:09:29 +03:00

89 lines
3.8 KiB
Python

"""Tests for the snapshot upload client."""
import io
import json
import os
import sys
import tempfile
import unittest
import urllib.error
from unittest import mock
ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, os.path.join(ROOT, "tools"))
import upload_snapshot
class UploadSnapshotTests(unittest.TestCase):
def setUp(self):
self.tmp = tempfile.TemporaryDirectory()
self.addCleanup(self.tmp.cleanup)
self.path = os.path.join(self.tmp.name, "snapshot.zip")
with open(self.path, "wb") as handle:
handle.write(b"zip-bytes")
def test_resolve_config_precedence(self):
path = os.path.join(self.tmp.name, "server.json")
with open(path, "w", encoding="utf-8") as handle:
json.dump({"url": "file", "token": "file", "handle": "file"}, handle)
with mock.patch.dict(os.environ, {
"STEAMBASE_SERVER_URL": "env",
"STEAMBASE_UPLOAD_TOKEN": "env",
"STEAMBASE_SERVER_HANDLE": "env"}, clear=True):
config = upload_snapshot.resolve_config(url="cli", path=path)
self.assertEqual(config["url"], "cli")
self.assertEqual(config["token"], "env")
self.assertEqual(config["handle"], "env")
def test_upload_headers(self):
with mock.patch.object(upload_snapshot.exporter, "snapshot_checksum",
return_value="abc"), \
mock.patch.object(upload_snapshot, "_request",
return_value={"snapshot": {"id": "s1", "status": "pending"}}) as request:
snapshot = upload_snapshot.upload("https://server/", "token", self.path,
idempotency_key="key-1")
self.assertEqual(snapshot["id"], "s1")
args = request.call_args.args
self.assertEqual(args[0], "https://server/api/v1/snapshots")
self.assertEqual(args[1], "token")
self.assertEqual(args[2], b"zip-bytes")
self.assertEqual(args[3]["X-Snapshot-Checksum"], "sha256=abc")
self.assertEqual(args[3]["Idempotency-Key"], "key-1")
self.assertEqual(args[3]["Content-Type"], "application/zip")
def test_missing_credentials(self):
with self.assertRaises(upload_snapshot.UploadError):
upload_snapshot.upload("", "token", self.path)
with self.assertRaises(upload_snapshot.UploadError):
upload_snapshot.upload("https://server", "", self.path)
def test_wait(self):
with mock.patch.object(upload_snapshot, "status",
side_effect=[{"status": "running"}, {"status": "done"}]):
result = upload_snapshot.wait("https://server", "token", "s1",
interval=0.001, timeout=1)
self.assertEqual(result["status"], "done")
def test_status_error_is_mapped(self):
error = urllib.error.HTTPError(
"https://server", 401, "Unauthorized", {},
io.BytesIO(b'{"error": {"code": "invalid_credentials", "message": "bad"}}'))
with mock.patch("urllib.request.urlopen", side_effect=error):
with self.assertRaises(upload_snapshot.UploadError) as context:
upload_snapshot._request("https://server", "token")
self.assertEqual(context.exception.status, 401)
self.assertIn("токен", str(context.exception))
def test_config_file(self):
path = os.path.join(self.tmp.name, "server.json")
with open(path, "w", encoding="utf-8") as handle:
json.dump({"url": "https://s", "token": "t", "handle": "h"}, handle)
with mock.patch.dict(os.environ, {}, clear=True):
config = upload_snapshot.resolve_config(path=path)
self.assertEqual(config, {"url": "https://s", "token": "t", "handle": "h"})
if __name__ == "__main__":
unittest.main()