forked from All-Hands-AI/OpenHands
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_storage.py
269 lines (221 loc) Β· 8.83 KB
/
test_storage.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
from __future__ import annotations
import os
import shutil
from abc import ABC
from dataclasses import dataclass, field
from io import BytesIO, StringIO
from typing import Dict, List, Optional
from unittest import TestCase
from unittest.mock import patch
import botocore.exceptions
from google.api_core.exceptions import NotFound
from openhands.storage.files import FileStore
from openhands.storage.google_cloud import GoogleCloudFileStore
from openhands.storage.local import LocalFileStore
from openhands.storage.memory import InMemoryFileStore
from openhands.storage.s3 import S3FileStore
class _StorageTest(ABC):
store: FileStore
def get_store(self) -> FileStore:
try:
self.store.delete('')
except Exception:
pass
return self.store
def test_basic_fileops(self):
filename = 'test.txt'
store = self.get_store()
store.write(filename, 'Hello, world!')
self.assertEqual(store.read(filename), 'Hello, world!')
self.assertEqual(store.list(''), [filename])
store.delete(filename)
with self.assertRaises(FileNotFoundError):
store.read(filename)
def test_complex_path_fileops(self):
filenames = ['foo.bar.baz', './foo/bar/baz', 'foo/bar/baz', '/foo/bar/baz']
store = self.get_store()
for filename in filenames:
store.write(filename, 'Hello, world!')
self.assertEqual(store.read(filename), 'Hello, world!')
store.delete(filename)
with self.assertRaises(FileNotFoundError):
store.read(filename)
def test_list(self):
store = self.get_store()
store.write('foo.txt', 'Hello, world!')
store.write('bar.txt', 'Hello, world!')
store.write('baz.txt', 'Hello, world!')
file_names = store.list('')
file_names.sort()
self.assertEqual(file_names, ['bar.txt', 'baz.txt', 'foo.txt'])
store.delete('foo.txt')
store.delete('bar.txt')
store.delete('baz.txt')
def test_deep_list(self):
store = self.get_store()
store.write('foo/bar/baz.txt', 'Hello, world!')
store.write('foo/bar/qux.txt', 'Hello, world!')
store.write('foo/bar/quux.txt', 'Hello, world!')
self.assertEqual(store.list(''), ['foo/'])
self.assertEqual(store.list('foo'), ['foo/bar/'])
file_names = store.list('foo/bar')
file_names.sort()
self.assertEqual(
file_names, ['foo/bar/baz.txt', 'foo/bar/quux.txt', 'foo/bar/qux.txt']
)
store.delete('foo/bar/baz.txt')
store.delete('foo/bar/qux.txt')
store.delete('foo/bar/quux.txt')
def test_directory_deletion(self):
store = self.get_store()
# Create a directory structure
store.write('foo/bar/baz.txt', 'Hello, world!')
store.write('foo/bar/qux.txt', 'Hello, world!')
store.write('foo/other.txt', 'Hello, world!')
store.write('foo/bar/subdir/file.txt', 'Hello, world!')
# Verify initial structure
self.assertEqual(store.list(''), ['foo/'])
self.assertEqual(sorted(store.list('foo')), ['foo/bar/', 'foo/other.txt'])
self.assertEqual(
sorted(store.list('foo/bar')),
['foo/bar/baz.txt', 'foo/bar/qux.txt', 'foo/bar/subdir/'],
)
# Delete a directory
store.delete('foo/bar')
# Verify directory and its contents are gone, but other files remain
self.assertEqual(store.list(''), ['foo/'])
self.assertEqual(store.list('foo'), ['foo/other.txt'])
# Delete root directory
store.delete('foo')
# Verify everything is gone
self.assertEqual(store.list(''), [])
class TestLocalFileStore(TestCase, _StorageTest):
def setUp(self):
os.makedirs('./_test_files_tmp', exist_ok=True)
self.store = LocalFileStore('./_test_files_tmp')
def tearDown(self):
shutil.rmtree('./_test_files_tmp')
class TestInMemoryFileStore(TestCase, _StorageTest):
def setUp(self):
self.store = InMemoryFileStore()
class TestGoogleCloudFileStore(TestCase, _StorageTest):
def setUp(self):
with patch('google.cloud.storage.Client', _MockGoogleCloudClient):
self.store = GoogleCloudFileStore('dear-liza')
class TestS3FileStore(TestCase, _StorageTest):
def setUp(self):
with patch('boto3.client', lambda service, **kwargs: _MockS3Client()):
self.store = S3FileStore('dear-liza')
# I would have liked to use cloud-storage-mocker here but the python versions were incompatible :(
# If we write tests for the S3 storage class I would definitely recommend we use moto.
class _MockGoogleCloudClient:
def bucket(self, name: str):
assert name == 'dear-liza'
return _MockGoogleCloudBucket()
@dataclass
class _MockGoogleCloudBucket:
blobs_by_path: Dict[str, _MockGoogleCloudBlob] = field(default_factory=dict)
def blob(self, path: Optional[str] = None) -> _MockGoogleCloudBlob:
return self.blobs_by_path.get(path) or _MockGoogleCloudBlob(self, path)
def list_blobs(self, prefix: Optional[str] = None) -> List[_MockGoogleCloudBlob]:
blobs = list(self.blobs_by_path.values())
if prefix and prefix != '/':
blobs = [blob for blob in blobs if blob.name.startswith(prefix)]
return blobs
@dataclass
class _MockGoogleCloudBlob:
bucket: _MockGoogleCloudBucket
name: str
content: Optional[str | bytes] = None
def open(self, op: str):
if op == 'r':
if self.content is None:
raise FileNotFoundError()
return StringIO(self.content)
if op == 'w':
return _MockGoogleCloudBlobWriter(self)
def delete(self):
if self.name not in self.bucket.blobs_by_path:
raise NotFound('Blob not found')
del self.bucket.blobs_by_path[self.name]
@dataclass
class _MockGoogleCloudBlobWriter:
blob: _MockGoogleCloudBlob
content: str | bytes = None
def __enter__(self):
return self
def write(self, __b):
assert (
self.content is None
) # We don't support buffered writes in this mock for now, as it is not needed
self.content = __b
def __exit__(self, exc_type, exc_val, exc_tb):
blob = self.blob
blob.content = self.content
blob.bucket.blobs_by_path[blob.name] = blob
class _MockS3Client:
def __init__(self):
self.objects_by_bucket: Dict[str, Dict[str, _MockS3Object]] = {}
def put_object(self, Bucket: str, Key: str, Body: str | bytes) -> None:
if Bucket not in self.objects_by_bucket:
self.objects_by_bucket[Bucket] = {}
self.objects_by_bucket[Bucket][Key] = _MockS3Object(Key, Body)
def get_object(self, Bucket: str, Key: str) -> Dict:
if Bucket not in self.objects_by_bucket:
raise botocore.exceptions.ClientError(
{
'Error': {
'Code': 'NoSuchBucket',
'Message': f"The bucket '{Bucket}' does not exist",
}
},
'GetObject',
)
if Key not in self.objects_by_bucket[Bucket]:
raise botocore.exceptions.ClientError(
{
'Error': {
'Code': 'NoSuchKey',
'Message': f"The specified key '{Key}' does not exist",
}
},
'GetObject',
)
content = self.objects_by_bucket[Bucket][Key].content
if isinstance(content, bytes):
return {'Body': BytesIO(content)}
return {'Body': StringIO(content)}
def list_objects_v2(self, Bucket: str, Prefix: str = '') -> Dict:
if Bucket not in self.objects_by_bucket:
raise botocore.exceptions.ClientError(
{
'Error': {
'Code': 'NoSuchBucket',
'Message': f"The bucket '{Bucket}' does not exist",
}
},
'ListObjectsV2',
)
objects = self.objects_by_bucket[Bucket]
contents = [
{'Key': key}
for key in objects.keys()
if not Prefix or key.startswith(Prefix)
]
return {'Contents': contents} if contents else {}
def delete_object(self, Bucket: str, Key: str) -> None:
if Bucket not in self.objects_by_bucket:
raise botocore.exceptions.ClientError(
{
'Error': {
'Code': 'NoSuchBucket',
'Message': f"The bucket '{Bucket}' does not exist",
}
},
'DeleteObject',
)
self.objects_by_bucket[Bucket].pop(Key, None)
@dataclass
class _MockS3Object:
key: str
content: str | bytes