Buckets:

download
raw
13.8 kB
"""Tests for the Calkit filesystem implementation."""
import os
import subprocess
import uuid
from unittest.mock import MagicMock, patch
import pytest
from requests.exceptions import Timeout
import calkit
from calkit import fs as ckfs
def test_parse_path():
owner, project, file_path = ckfs._parse_path("ck://owner/project/file.txt")
assert owner == "owner"
assert project == "project"
assert file_path == "file.txt"
owner, project, file_path = ckfs._parse_path("ck://owner/project")
assert owner == "owner"
assert project == "project"
assert file_path == ""
owner, project, file_path = ckfs._parse_path(
"ck://owner/project/data/nested/file.txt"
)
assert owner == "owner"
assert project == "project"
assert file_path == "data/nested/file.txt"
owner, project, file_path = ckfs._parse_path("owner/project/file.txt")
assert owner == "owner"
assert project == "project"
assert file_path == "file.txt"
with pytest.raises(ValueError, match="Invalid path format"):
ckfs._parse_path("ck://owner")
with pytest.raises(ValueError, match="Invalid path format"):
ckfs._parse_path("ck://")
def test_execute_operation_presigned_chunked_progress():
"""Progress callback is called for each acknowledged chunk."""
chunk_size = 5
data = b"A" * 13 # 13 bytes: chunks of 5, 5, 3
total = len(data)
session = MagicMock()
# Simulate GCS resumable upload responses:
# chunk 1 (bytes 0-4): 308 with Range: bytes=0-4
resp1 = MagicMock()
resp1.status_code = 308
resp1.headers = {"Range": "bytes=0-4"}
# chunk 2 (bytes 5-9): 308 with Range: bytes=0-9
resp2 = MagicMock()
resp2.status_code = 308
resp2.headers = {"Range": "bytes=0-9"}
# chunk 3 (bytes 10-12): 200 - final success
resp3 = MagicMock()
resp3.status_code = 200
resp3.headers = {}
# init response returns a session URI
init_resp = MagicMock()
init_resp.raise_for_status = MagicMock()
init_resp.headers = {"Location": "https://example.com/upload-session"}
session.request.side_effect = [init_resp, resp1, resp2, resp3]
fs = ckfs.CalkitFileSystem.__new__(ckfs.CalkitFileSystem)
fs._session = session
operation_info = {
"access": {
"kind": "presigned-chunked",
"init_url": "https://example.com/initiate",
"chunk_size_bytes": chunk_size,
"http_method": "POST",
"chunk_http_method": "PUT",
"headers": {"x-goog-resumable": "start"},
}
}
callback = MagicMock()
callback.relative_update = MagicMock()
fs._execute_operation(operation_info, "put", data=data, callback=callback)
# Verify callback was called with correct byte counts per chunk
calls = [c.args[0] for c in callback.relative_update.call_args_list]
# chunk 1: ack offset 5 - start offset 0 = 5
# chunk 2: ack offset 10 - start offset 5 = 5
# chunk 3: chunk_end 13 - start offset 10 = 3
assert calls == [5, 5, 3], f"Expected [5, 5, 3], got {calls}"
assert sum(calls) == total
def test_execute_operation_presigned_multipart_progress():
"""Progress callback is called for each uploaded part."""
part_size = 5
data = b"B" * 13 # 13 bytes -> parts of 5, 5, 3
total = len(data)
session = MagicMock()
part_resp = MagicMock()
part_resp.raise_for_status = MagicMock()
part_resp.headers = {"ETag": '"abc123"'}
complete_resp = MagicMock()
complete_resp.raise_for_status = MagicMock()
session.put.return_value = part_resp
session.post.return_value = complete_resp
fs = ckfs.CalkitFileSystem.__new__(ckfs.CalkitFileSystem)
fs._session = session
operation_info = {
"access": {
"kind": "presigned-multipart",
"upload_id": "uid-123",
"part_urls": [
"https://s3.example.com/part1",
"https://s3.example.com/part2",
"https://s3.example.com/part3",
],
"complete_url": "https://s3.example.com/complete",
"part_size_bytes": part_size,
}
}
callback = MagicMock()
callback.relative_update = MagicMock()
fs._execute_operation(operation_info, "put", data=data, callback=callback)
calls = [c.args[0] for c in callback.relative_update.call_args_list]
assert calls == [5, 5, 3], f"Expected [5, 5, 3], got {calls}"
assert sum(calls) == total
def test_put_file_uses_callback(tmp_path):
"""put_file reports progress via callback based on actual upload bytes."""
data = b"C" * 20
local_file = tmp_path / "upload.bin"
local_file.write_bytes(data)
session = MagicMock()
put_resp = MagicMock()
put_resp.raise_for_status = MagicMock()
session.request.return_value = put_resp
fs = ckfs.CalkitFileSystem.__new__(ckfs.CalkitFileSystem)
fs._session = session
operation_info = {
"access": {
"kind": "presigned-url",
"url": "https://storage.example.com/upload",
"http_method": "PUT",
}
}
with patch.object(fs, "_get_fs_op_info", return_value=operation_info):
with patch.object(fs, "exists", return_value=False):
callback = MagicMock()
callback.set_size = MagicMock()
callback.relative_update = MagicMock()
fs.put_file(
str(local_file),
"ck://owner/project/upload.bin",
callback=callback,
)
callback.set_size.assert_called_once_with(len(data))
calls = [c.args[0] for c in callback.relative_update.call_args_list]
assert sum(calls) == len(data)
def test_execute_operation_retries_transient_timeout_across_upload_paths():
"""Retries transient timeout for both simple and chunked upload flows."""
import io
with patch("calkit.fs.time.sleep", return_value=None):
# Use case 1: simple presigned-url upload retries and rewinds stream.
payload_simple = b"R" * 8
start_positions = []
session_simple = MagicMock()
def _simple_side_effect(**kwargs):
body = kwargs["data"]
start_positions.append(body.tell())
if len(start_positions) == 1:
raise Timeout("read timeout")
resp = MagicMock()
resp.raise_for_status = MagicMock()
return resp
session_simple.request.side_effect = _simple_side_effect
fs_simple = ckfs.CalkitFileSystem.__new__(ckfs.CalkitFileSystem)
fs_simple._session = session_simple
op_simple = {
"access": {
"kind": "presigned-url",
"url": "https://storage.example.com/upload",
"http_method": "PUT",
}
}
fs_simple._execute_operation(
op_simple,
"put",
file_obj=io.BytesIO(payload_simple),
file_size=len(payload_simple),
)
assert start_positions == [0, 0]
# Use case 2: chunked upload retries a timed-out chunk and continues.
chunk_size = 5
payload_chunked = b"T" * 13
session_chunked = MagicMock()
init_resp = MagicMock()
init_resp.raise_for_status = MagicMock()
init_resp.headers = {"Location": "https://example.com/session"}
resp1 = MagicMock()
resp1.status_code = 308
resp1.headers = {"Range": "bytes=0-4"}
resp2 = MagicMock()
resp2.status_code = 308
resp2.headers = {"Range": "bytes=0-9"}
resp3 = MagicMock()
resp3.status_code = 200
resp3.headers = {}
session_chunked.request.side_effect = [
init_resp,
Timeout("read timeout"),
resp1,
resp2,
resp3,
]
fs_chunked = ckfs.CalkitFileSystem.__new__(ckfs.CalkitFileSystem)
fs_chunked._session = session_chunked
op_chunked = {
"access": {
"kind": "presigned-chunked",
"init_url": "https://example.com/initiate",
"chunk_size_bytes": chunk_size,
"http_method": "POST",
"chunk_http_method": "PUT",
"headers": {"x-goog-resumable": "start"},
}
}
callback = MagicMock()
callback.relative_update = MagicMock()
fs_chunked._execute_operation(
op_chunked,
"put",
file_obj=io.BytesIO(payload_chunked),
file_size=len(payload_chunked),
callback=callback,
)
calls = [c.args[0] for c in callback.relative_update.call_args_list]
assert calls == [5, 5, 3]
def test_execute_operation_retries_transient_5xx():
"""Retries a transient 5xx response from the storage backend."""
import io
with patch("calkit.fs.time.sleep", return_value=None):
# A 503 from the backend is retried; the follow-up 200 is returned.
statuses = [503, 200]
seen = []
def _side_effect(**kwargs):
resp = MagicMock()
resp.status_code = statuses[len(seen)]
seen.append(resp.status_code)
return resp
session = MagicMock()
session.request.side_effect = _side_effect
fs = ckfs.CalkitFileSystem.__new__(ckfs.CalkitFileSystem)
fs._session = session
op = {
"access": {
"kind": "presigned-url",
"url": "https://storage.example.com/upload",
"http_method": "PUT",
}
}
resp = fs._execute_operation(
op,
"put",
file_obj=io.BytesIO(b"hello"),
file_size=5,
)
assert seen == [503, 200]
assert resp.status_code == 200
def _calkit_hub_available(hub: str = "localhost:5173") -> bool:
"""Check if the Calkit hub is available.
Named by URL, and left with CALKIT_ENV=test in place, so credentials
still come from the suite's own isolated config rather than the
developer's real ones.
"""
with patch.dict(os.environ, {"CALKIT_HUB": hub}):
try:
calkit.hub.get_current_user()
return True
except Exception:
return False
@pytest.mark.skipif(
not _calkit_hub_available(), reason="Calkit hub not available"
)
def test_calkitfilesystem():
"""Test basic CalkitFileSystem functionality."""
fs = ckfs.CalkitFileSystem()
assert fs.protocol == "ck"
txt = uuid.uuid4().hex
uri = "ck://calkit/example-basic/test.txt"
with fs.open(uri, "w") as f:
f.write(txt)
with fs.open(uri, "r") as f:
read_txt = f.read()
assert read_txt == txt
info = fs.info(uri)
assert info["size"] == len(txt)
assert info["type"] == "file"
res = fs.ls("ck://calkit/example-basic/")
assert "calkit/example-basic/test.txt" in res
@pytest.mark.skipif(
not _calkit_hub_available("staging.calkit.io"),
reason="Calkit hub not available",
)
def test_calkitfilesystem_staging(monkeypatch):
"""Test CalkitFileSystem with staging environment."""
monkeypatch.setenv("CALKIT_HUB", "staging.calkit.io")
fs = ckfs.CalkitFileSystem()
assert fs.base_url == "https://api.staging.calkit.io"
assert fs.protocol == "ck"
txt = uuid.uuid4().hex
uri = "ck://calkit/example-basic/test_staging.txt"
with fs.open(uri, "w") as f:
f.write(txt)
with fs.open(uri, "r") as f:
read_txt = f.read()
assert read_txt == txt
info = fs.info(uri)
assert info["size"] == len(txt)
assert info["type"] == "file"
res = fs.ls("ck://calkit/example-basic/")
assert "calkit/example-basic/test_staging.txt" in res
@pytest.mark.skipif(
not _calkit_hub_available(), reason="Calkit hub not available"
)
def test_calkitfilesystem_dvc(tmp_dir):
"""Test CalkitFileSystem as a DVC remote."""
subprocess.run(["calkit", "init"])
subprocess.run(
[
"calkit",
"dvc",
"remote",
"add",
"calkit",
"ck://calkit/example-basic/",
],
check=True,
)
subprocess.run(
["calkit", "dvc", "remote", "default", "calkit"], check=True
)
with open("data.txt", "w") as f:
f.write("hello dvc")
subprocess.run(["calkit", "dvc", "add", "data.txt"], check=True)
subprocess.run(["calkit", "dvc", "push"], check=True)
subprocess.run(["calkit", "dvc", "pull"], check=True)
@pytest.mark.skipif(
not _calkit_hub_available("staging.calkit.io"),
reason="Calkit hub not available",
)
def test_calkitfilesystem_dvc_staging(monkeypatch, tmp_dir):
"""Test CalkitFileSystem as a DVC remote against the staging
environment.
"""
monkeypatch.setenv("CALKIT_HUB", "staging.calkit.io")
# Verify the env var reaches a subprocess, since DVC runs the
# filesystem in one
result = subprocess.run(
["python", "-c", "import os; print(os.environ.get('CALKIT_HUB'))"],
capture_output=True,
text=True,
)
assert result.stdout.strip() == "staging.calkit.io"
subprocess.run(["calkit", "init"])
subprocess.run(
[
"calkit",
"dvc",
"remote",
"add",
"calkit",
"ck://calkit/example-basic/",
],
check=True,
)
subprocess.run(
["calkit", "dvc", "remote", "default", "calkit"], check=True
)
with open("data.txt", "w") as f:
f.write("hello dvc")
subprocess.run(["calkit", "dvc", "add", "data.txt"], check=True)
subprocess.run(["calkit", "dvc", "push"], check=True)
subprocess.run(["calkit", "dvc", "pull"], check=True)

Xet Storage Details

Size:
13.8 kB
·
Xet hash:
43a0a403849475e3457b41a355e40179ae762b5b523a195f4f9ac0fd970881e5

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.