3
from pathlib import Path
7
from google.auth.exceptions import GoogleAuthError
8
from google.cloud import storage
9
from google.cloud.exceptions import NotFound
11
from autogpt.file_storage.gcs import GCSFileStorage, GCSFileStorageConfiguration
15
except GoogleAuthError:
16
pytest.skip("Google Cloud Authentication not configured", allow_module_level=True)
19
@pytest.fixture(scope="module")
20
def gcs_bucket_name() -> str:
21
return f"test-bucket-{str(uuid.uuid4())[:8]}"
24
@pytest.fixture(scope="module")
25
def gcs_root() -> Path:
26
return Path("/workspaces/AutoGPT-some-unique-task-id")
29
@pytest.fixture(scope="module")
30
def gcs_storage_uninitialized(gcs_bucket_name: str, gcs_root: Path) -> GCSFileStorage:
31
os.environ["STORAGE_BUCKET"] = gcs_bucket_name
32
storage_config = GCSFileStorageConfiguration.from_env()
33
storage_config.root = gcs_root
34
storage = GCSFileStorage(storage_config)
36
del os.environ["STORAGE_BUCKET"]
39
def test_initialize(gcs_bucket_name: str, gcs_storage_uninitialized: GCSFileStorage):
40
gcs = gcs_storage_uninitialized._gcs
43
with pytest.raises(NotFound):
44
gcs.get_bucket(gcs_bucket_name)
46
gcs_storage_uninitialized.initialize()
49
bucket = gcs.get_bucket(gcs_bucket_name)
52
bucket.delete(force=True)
55
@pytest.fixture(scope="module")
56
def gcs_storage(gcs_storage_uninitialized: GCSFileStorage) -> GCSFileStorage:
57
(gcs_storage := gcs_storage_uninitialized).initialize()
61
gcs_storage._bucket.delete(force=True)
64
def test_workspace_bucket_name(
65
gcs_storage: GCSFileStorage,
68
assert gcs_storage._bucket.name == gcs_bucket_name
71
NESTED_DIR = "existing/test/dir"
72
TEST_FILES: list[tuple[str | Path, str]] = [
73
("existing_test_file_1", "test content 1"),
74
("existing_test_file_2.txt", "test content 2"),
75
(Path("existing_test_file_3"), "test content 3"),
76
(Path(f"{NESTED_DIR}/test_file_4"), "test content 4"),
80
@pytest_asyncio.fixture
81
async def gcs_storage_with_files(gcs_storage: GCSFileStorage) -> GCSFileStorage:
82
for file_name, file_content in TEST_FILES:
83
gcs_storage._bucket.blob(
84
str(gcs_storage.get_path(file_name))
85
).upload_from_string(file_content)
90
async def test_read_file(gcs_storage_with_files: GCSFileStorage):
91
for file_name, file_content in TEST_FILES:
92
content = gcs_storage_with_files.read_file(file_name)
93
assert content == file_content
95
with pytest.raises(NotFound):
96
gcs_storage_with_files.read_file("non_existent_file")
99
def test_list_files(gcs_storage_with_files: GCSFileStorage):
102
files := gcs_storage_with_files.list_files()
103
) == gcs_storage_with_files.list_files()
104
assert len(files) > 0
105
assert set(files) == set(Path(file_name) for file_name, _ in TEST_FILES)
109
nested_files := gcs_storage_with_files.list_files(NESTED_DIR)
110
) == gcs_storage_with_files.list_files(NESTED_DIR)
111
assert len(nested_files) > 0
112
assert set(nested_files) == set(
113
p.relative_to(NESTED_DIR)
114
for file_name, _ in TEST_FILES
115
if (p := Path(file_name)).is_relative_to(NESTED_DIR)
119
def test_list_folders(gcs_storage_with_files: GCSFileStorage):
121
folders = gcs_storage_with_files.list_folders(recursive=True)
122
assert len(folders) > 0
123
assert set(folders) == {
125
Path("existing/test"),
126
Path("existing/test/dir"),
129
folders = gcs_storage_with_files.list_folders(recursive=False)
130
assert len(folders) > 0
131
assert set(folders) == {Path("existing")}
135
async def test_write_read_file(gcs_storage: GCSFileStorage):
136
await gcs_storage.write_file("test_file", "test_content")
137
assert gcs_storage.read_file("test_file") == "test_content"
141
async def test_overwrite_file(gcs_storage_with_files: GCSFileStorage):
142
for file_name, _ in TEST_FILES:
143
await gcs_storage_with_files.write_file(file_name, "new content")
144
assert gcs_storage_with_files.read_file(file_name) == "new content"
147
def test_delete_file(gcs_storage_with_files: GCSFileStorage):
148
for file_to_delete, _ in TEST_FILES:
149
gcs_storage_with_files.delete_file(file_to_delete)
150
assert not gcs_storage_with_files.exists(file_to_delete)
153
def test_exists(gcs_storage_with_files: GCSFileStorage):
154
for file_name, _ in TEST_FILES:
155
assert gcs_storage_with_files.exists(file_name)
157
assert not gcs_storage_with_files.exists("non_existent_file")
160
def test_rename_file(gcs_storage_with_files: GCSFileStorage):
161
for file_name, _ in TEST_FILES:
162
new_name = str(file_name) + "_renamed"
163
gcs_storage_with_files.rename(file_name, new_name)
164
assert gcs_storage_with_files.exists(new_name)
165
assert not gcs_storage_with_files.exists(file_name)
168
def test_rename_dir(gcs_storage_with_files: GCSFileStorage):
169
gcs_storage_with_files.rename(NESTED_DIR, "existing/test/dir_renamed")
170
assert gcs_storage_with_files.exists("existing/test/dir_renamed")
171
assert not gcs_storage_with_files.exists(NESTED_DIR)
174
def test_clone(gcs_storage_with_files: GCSFileStorage, gcs_root: Path):
175
cloned = gcs_storage_with_files.clone_with_subroot("existing/test")
176
assert cloned.root == gcs_root / Path("existing/test")
177
assert cloned._bucket.name == gcs_storage_with_files._bucket.name
178
assert cloned.exists("dir")
179
assert cloned.exists("dir/test_file_4")