knowledge-platform/tests/unit/test_bos_object_storage.py

179 lines
5.9 KiB
Python

"""Offline contract tests for the Baidu BOS object-storage adapter."""
from io import BytesIO
from types import SimpleNamespace
from typing import BinaryIO, cast
import pytest
from ekp_object_storage import (
BosConfigurationError,
BosObjectStorage,
InvalidStorageKeyError,
ObjectAlreadyExistsError,
ObjectNotFoundError,
ObjectStorageError,
)
from ekp_object_storage.bos import _normalize_endpoint
class FakeBosError(Exception):
"""SDK-like failure carrying only the HTTP status used by the adapter."""
def __init__(self, message: str, status_code: int) -> None:
super().__init__(message)
self.status_code = status_code
class FakeBosClient:
"""Capture official SDK-shaped calls without network or credentials."""
def __init__(self) -> None:
self.objects: dict[tuple[str, str], bytes] = {}
self.put_calls: list[tuple[str, str, int, dict[bytes, bytes]]] = []
self.delete_calls: list[tuple[str, str]] = []
self.failure: Exception | None = None
def put_object(
self,
bucket_name: str,
key: str,
data: object,
content_length: int,
*,
cond_read_write: dict[bytes, bytes],
) -> object:
self._raise_failure()
self.put_calls.append((bucket_name, key, content_length, cond_read_write))
self.objects[(bucket_name, key)] = cast(BinaryIO, data).read()
return object()
def get_object(self, bucket_name: str, key: str) -> object:
self._raise_failure()
try:
payload = self.objects[(bucket_name, key)]
except KeyError as exc:
raise FakeBosError("missing", 404) from exc
return SimpleNamespace(data=BytesIO(payload))
def get_object_meta_data(self, bucket_name: str, key: str) -> object:
self._raise_failure()
if (bucket_name, key) not in self.objects:
raise FakeBosError("missing", 404)
return object()
def delete_object(self, bucket_name: str, key: str) -> object:
self._raise_failure()
self.delete_calls.append((bucket_name, key))
if self.objects.pop((bucket_name, key), None) is None:
raise FakeBosError("missing", 404)
return object()
def _raise_failure(self) -> None:
if self.failure is not None:
raise self.failure
class FailingSource(BytesIO):
"""Simulate an input failure before a remote PutObject begins."""
def read(self, size: int | None = -1) -> bytes:
raise OSError("simulated source failure")
def test_bos_put_uses_prefix_length_and_atomic_no_overwrite() -> None:
client = FakeBosClient()
storage = BosObjectStorage(client, "ekp-test-bucket", "tenant/demo")
stored = storage.put("documents/source.txt", BytesIO(b"source bytes"))
assert stored.key == "documents/source.txt"
assert stored.size_bytes == 12
assert client.put_calls == [
(
"ekp-test-bucket",
"tenant/demo/documents/source.txt",
12,
{b"If-None-Match": b"*"},
)
]
assert client.objects[("ekp-test-bucket", "tenant/demo/documents/source.txt")] == (
b"source bytes"
)
def test_bos_put_maps_precondition_failure_to_already_exists() -> None:
client = FakeBosClient()
client.failure = FakeBosError("precondition failed", 412)
storage = BosObjectStorage(client, "ekp-test-bucket")
with pytest.raises(ObjectAlreadyExistsError):
storage.put("documents/source.txt", BytesIO(b"replacement"))
def test_bos_stages_source_before_remote_upload() -> None:
client = FakeBosClient()
storage = BosObjectStorage(client, "ekp-test-bucket")
with pytest.raises(OSError, match="simulated source failure"):
storage.put("documents/source.txt", FailingSource())
assert client.put_calls == []
def test_bos_open_exists_and_delete_use_prefixed_key() -> None:
client = FakeBosClient()
client.objects[("ekp-test-bucket", "tenant/demo/documents/source.txt")] = b"source"
storage = BosObjectStorage(client, "ekp-test-bucket", "tenant/demo")
assert storage.exists("documents/source.txt")
with storage.open("documents/source.txt") as source:
assert source.read() == b"source"
storage.delete("documents/source.txt")
assert client.delete_calls == [("ekp-test-bucket", "tenant/demo/documents/source.txt")]
assert not storage.exists("documents/source.txt")
with pytest.raises(ObjectNotFoundError):
storage.open("documents/source.txt")
@pytest.mark.parametrize(
"key",
["", "/absolute.txt", "../escape.txt", "nested/../escape.txt", "bad\\key.txt"],
)
def test_bos_rejects_unsafe_logical_keys(key: str) -> None:
storage = BosObjectStorage(FakeBosClient(), "ekp-test-bucket")
with pytest.raises(InvalidStorageKeyError):
storage.exists(key)
def test_bos_sanitizes_sdk_failures() -> None:
client = FakeBosClient()
client.failure = FakeBosError("secret-value at private-endpoint", 500)
storage = BosObjectStorage(client, "ekp-test-bucket")
with pytest.raises(ObjectStorageError, match="metadata check failed") as caught:
storage.exists("documents/source.txt")
rendered = str(caught.value)
assert "secret-value" not in rendered
assert "private-endpoint" not in rendered
def test_bos_normalizes_legacy_host_only_endpoint_to_https() -> None:
assert _normalize_endpoint("bj.bcebos.com") == "https://bj.bcebos.com"
assert _normalize_endpoint("https://bj.bcebos.com/") == "https://bj.bcebos.com"
with pytest.raises(BosConfigurationError, match="endpoint"):
_normalize_endpoint("bj.bcebos.com/private-path")
@pytest.mark.parametrize(
("bucket", "prefix"),
[("", ""), ("ekp-test-bucket", "bad/../prefix")],
)
def test_bos_rejects_incomplete_or_unsafe_target(bucket: str, prefix: str) -> None:
with pytest.raises((BosConfigurationError, InvalidStorageKeyError)):
BosObjectStorage(FakeBosClient(), bucket, prefix)