|
| 1 | +"""S3 适配器非网络分支测试(fakes client)。""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from types import SimpleNamespace |
| 6 | + |
| 7 | +import pytest |
| 8 | + |
| 9 | +from app.core.exceptions import CustomException |
| 10 | +from app.modules.task.storage.core.s3_adapter import S3StorageAdapter |
| 11 | + |
| 12 | + |
| 13 | +class FakeClient: |
| 14 | + def __init__(self): |
| 15 | + self.calls = [] |
| 16 | + self.objects = { |
| 17 | + "Contents": [ |
| 18 | + {"Key": "dir/a.txt", "Size": 1}, |
| 19 | + {"Key": "dir/", "Size": 0}, |
| 20 | + ], |
| 21 | + "CommonPrefixes": [{"Prefix": "dir/sub/"}], |
| 22 | + "IsTruncated": False, |
| 23 | + } |
| 24 | + |
| 25 | + def head_bucket(self, **kw): |
| 26 | + self.calls.append(("head_bucket", kw)) |
| 27 | + |
| 28 | + def head_object(self, **kw): |
| 29 | + self.calls.append(("head_object", kw)) |
| 30 | + |
| 31 | + def put_object(self, **kw): |
| 32 | + self.calls.append(("put_object", kw)) |
| 33 | + |
| 34 | + def delete_object(self, **kw): |
| 35 | + self.calls.append(("delete_object", kw)) |
| 36 | + |
| 37 | + def list_objects_v2(self, **kw): |
| 38 | + self.calls.append(("list_objects_v2", kw)) |
| 39 | + return self.objects |
| 40 | + |
| 41 | + def list_buckets(self): |
| 42 | + return {"Buckets": [{"Name": "a"}, {"Name": ""}]} |
| 43 | + |
| 44 | + def create_bucket(self, **kw): |
| 45 | + self.calls.append(("create_bucket", kw)) |
| 46 | + |
| 47 | + def generate_presigned_url(self, *args, **kwargs): |
| 48 | + return "https://url" |
| 49 | + |
| 50 | + def copy_object(self, **kw): |
| 51 | + self.calls.append(("copy_object", kw)) |
| 52 | + |
| 53 | + def close(self): |
| 54 | + self.calls.append(("close", {})) |
| 55 | + |
| 56 | + |
| 57 | +def _a(config=None): |
| 58 | + a = S3StorageAdapter.__new__(S3StorageAdapter) |
| 59 | + a.config = SimpleNamespace(bucket="b", region="eu-west-1", **(config or {})) |
| 60 | + a.bucket_name = "b" |
| 61 | + a.client = FakeClient() |
| 62 | + return a |
| 63 | + |
| 64 | + |
| 65 | +def test_s3_connection_and_bucket_operations() -> None: |
| 66 | + a = _a() |
| 67 | + assert a._sync_test_connection() is True |
| 68 | + assert a._sync_exists("x") is True |
| 69 | + assert a._sync_list_buckets() == ["a"] |
| 70 | + a._sync_create_bucket("new") |
| 71 | + assert a.client.calls[-1][1]["CreateBucketConfiguration"] == {"LocationConstraint": "eu-west-1"} |
| 72 | + a._sync_mkdir("d") |
| 73 | + a._sync_rmdir("d") |
| 74 | + a._sync_delete("x") |
| 75 | + assert a._sync_get_url("x", 10) == "https://url" |
| 76 | + a._sync_close() |
| 77 | + |
| 78 | + |
| 79 | +def test_s3_listing_and_page() -> None: |
| 80 | + a = _a() |
| 81 | + items = a._sync_list("dir/") |
| 82 | + assert {x.name for x in items} == {"a.txt", "sub"} |
| 83 | + page = a._sync_list_page("dir/", 10, "cur") |
| 84 | + assert page.items and page.has_next is False |
| 85 | + assert "ContinuationToken" not in page.__dict__ |
| 86 | + |
| 87 | + |
| 88 | +def test_s3_copy_file_and_rename_file() -> None: |
| 89 | + a = _a() |
| 90 | + a._is_dir = lambda key: False # type: ignore[method-assign] |
| 91 | + a._sync_copy("a", "b") |
| 92 | + a._sync_rename("a", "b") |
| 93 | + assert any(c[0] == "copy_object" for c in a.client.calls) |
| 94 | + |
| 95 | + |
| 96 | +def test_s3_directory_copy_uses_base() -> None: |
| 97 | + a = _a() |
| 98 | + a._is_dir = lambda key: True # type: ignore[method-assign] |
| 99 | + called = [] |
| 100 | + a._sync_copy_dir = lambda src, dst: called.append((src, dst)) # type: ignore[method-assign] |
| 101 | + a._sync_copy("a/", "b/") |
| 102 | + assert called == [("a/", "b/")] |
| 103 | + |
| 104 | + |
| 105 | +def test_s3_validation_errors() -> None: |
| 106 | + from app.modules.task.storage.core.base import StorageAdapterConfig, StorageProtocol |
| 107 | + with pytest.raises(CustomException): |
| 108 | + S3StorageAdapter(StorageAdapterConfig(protocol=StorageProtocol.S3, port=0, bucket="b", username="", password="x")) |
| 109 | + with pytest.raises(CustomException): |
| 110 | + S3StorageAdapter(StorageAdapterConfig(protocol=StorageProtocol.S3, port=0, bucket="", username="a", password="x")) |
0 commit comments