-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathblobstore_common_tests.py
More file actions
360 lines (300 loc) · 11.9 KB
/
Copy pathblobstore_common_tests.py
File metadata and controls
360 lines (300 loc) · 11.9 KB
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
from datetime import datetime
import io
import requests
from cloud_blobstore import BlobMetadataField, BlobNotFoundError, BlobStore, BlobPagingError
from tests import infra
class BlobStoreTests:
"""
Common blobstore tests. We want to avoid repeating ourselves, so if we
built the abstractions correctly, common operations can all be tested here.
"""
def test_get_metadata(self):
"""
Ensure that the ``get_metadata`` methods return sane data.
"""
handle = self.handle # type: BlobStore
metadata = handle.get_user_metadata(
self.test_fixtures_bucket,
"test_good_source_data/0")
self.assertIn('test_metadata', metadata)
self.assertEqual(metadata['test_metadata'], "12345")
with self.assertRaises(BlobNotFoundError):
handle.get_user_metadata(
self.test_fixtures_bucket,
"test_good_source_data_DOES_NOT_EXIST")
def test_get_content_type(self):
"""
Ensure that the ``get_content_type`` methods return sane data.
"""
handle = self.handle # type: BlobStore
content_type = handle.get_content_type(
self.test_fixtures_bucket,
"test_good_source_data/0")
self.assertEqual(content_type, "text/plain")
content_type = handle.get_content_type(
self.test_fixtures_bucket,
"test_good_source_data/1")
self.assertEqual(content_type, "binary/octet-stream")
with self.assertRaises(BlobNotFoundError):
handle.get_content_type(
self.test_fixtures_bucket,
"test_good_source_data_DOES_NOT_EXIST")
def test_get_last_modified_date(self):
last_modified = self.handle.get_last_modified_date(
self.test_fixtures_bucket,
"test_good_source_data/0")
self.assertTrue(isinstance(last_modified, datetime))
with self.assertRaises(BlobNotFoundError):
self.handle.get_last_modified_date(
self.test_fixtures_bucket,
"test_good_source_data_DOES_NOT_EXIST")
def testList(self):
"""
Ensure that the ```list``` method returns sane data.
"""
items = list(self.handle.list(
self.test_fixtures_bucket,
"test_good_source_data/0",
))
self.assertIn("test_good_source_data/0", items)
for item in items:
if item == "test_good_source_data/0":
break
else:
self.fail("did not find the requisite key")
# fetch a bunch of items all at once.
items = list(self.handle.list(
self.test_fixtures_bucket,
"testList/prefix",
))
self.assertEqual(len(items), 10)
# this should fetch both testList/delimiter and testList/delimiter/test
items = list(self.handle.list(
self.test_fixtures_bucket,
"testList/delimiter",
))
self.assertEqual(len(items), 2)
# this should fetch only testList/delimiter
items = list(self.handle.list(
self.test_fixtures_bucket,
"testList/delimiter",
delimiter="/"
))
self.assertEqual(len(items), 1)
def testListV2(self):
"""
Ensure that the ```list_v2``` method returns sane data.
"""
keys = list((key for key, item in self.handle.list_v2(
self.test_fixtures_bucket,
"test_good_source_data/0",
)))
self.assertIn("test_good_source_data/0", keys)
# fetch a bunch of items all at once.
items = list(self.handle.list_v2(
self.test_fixtures_bucket,
"testList/prefix",
))
self.assertEqual(len(items), 10)
for ix, (key, metadata) in enumerate(items):
self.assertTrue(f"prefix.00{ix}" in key)
self.assertTrue(all(field in metadata for field in BlobMetadataField))
self.assertTrue(all(key in BlobMetadataField for key in metadata))
# fetch a bunch of items all at once with small page size
items = list(self.handle.list_v2(
self.test_fixtures_bucket,
"testList/prefix",
k_page_max=3
))
self.assertEqual(len(items), 10)
# this should fetch both testList/delimiter and testList/delimiter/test
items = list(self.handle.list_v2(
self.test_fixtures_bucket,
"testList/delimiter",
))
self.assertEqual(len(items), 2)
# this should fetch only testList/delimiter
items = list(self.handle.list_v2(
self.test_fixtures_bucket,
"testList/delimiter",
delimiter="/"
))
self.assertEqual(len(items), 1)
def testListV2Continuation(self):
self._testListV2Continuation(2, 3)
self._testListV2Continuation(2, 4)
def _testListV2Continuation(self, page_size, break_size):
blobiter = self.handle.list_v2(
self.test_fixtures_bucket,
"testList/prefix",
k_page_max=page_size,
)
items1 = list()
items2 = list()
for ix, (key, item) in enumerate(blobiter):
items1.append(
key
)
if ix >= break_size - 1:
break
blobiter = self.handle.list_v2(
self.test_fixtures_bucket,
"testList/prefix",
token=blobiter.token,
start_after_key=blobiter.start_after_key,
k_page_max=page_size,
)
for blob in blobiter:
items2.append(blob)
self.assertEquals(blobiter.start_after_key, blob[0])
self.assertEqual(len(items1) + len(items2), 10)
# starting from an unfound start_after_key should raise an error
with self.assertRaises(BlobPagingError):
[item for item in
self.handle.list_v2(
self.test_fixtures_bucket,
start_after_key="nonsensicalnonsene"
)]
def testTokenAvailability(self):
blobiter = self.handle.list_v2(
self.test_fixtures_bucket,
"testList/prefix",
k_page_max=1,
)
stop_length = len([x for x in blobiter])
blobiter = self.handle.list_v2(
self.test_fixtures_bucket,
"testList/prefix",
k_page_max=1,
)
for idx, _ in enumerate(blobiter):
if idx >= stop_length - 1:
self.assertTrue(blobiter.token)
def testGetPresignedUrl(self):
presigned_url = self.handle.generate_presigned_GET_url(
self.test_fixtures_bucket,
"test_good_source_data/0",
)
resp = requests.get(presigned_url)
self.assertEqual(resp.status_code, requests.codes.ok)
def testUploadFileHandle(self):
with self.subTest("without optional parameters"):
fobj = io.BytesIO(b"abcabcabc")
dst_blob_name = infra.generate_test_key()
self.handle.upload_file_handle(
self.test_bucket,
dst_blob_name,
fobj
)
# should be able to get metadata for the file.
self.assertFalse(self.handle.get_user_metadata(self.test_bucket, dst_blob_name))
with self.subTest("with optional parameters"):
fobj = io.BytesIO(b"abcabcabc")
dst_blob_name = infra.generate_test_key()
content_type = "test/content-type"
metadata = {"stuff": "things"}
self.handle.upload_file_handle(
self.test_bucket,
dst_blob_name,
fobj,
content_type=content_type,
metadata=metadata,
)
# should be able to get metadata for the file.
self.assertEqual(self.handle.get_user_metadata(self.test_bucket, dst_blob_name), metadata)
self.assertEqual(self.handle.get_content_type(self.test_bucket, dst_blob_name), content_type)
def testGet(self):
data = self.handle.get(
self.test_fixtures_bucket,
"test_good_source_data/0",
)
self.assertEqual(len(data), 11358)
with self.assertRaises(BlobNotFoundError):
self.handle.get(
self.test_fixtures_bucket,
"test_good_source_data_DOES_NOT_EXIST",
)
def testGetSize(self):
sz = self.handle.get_size(self.test_fixtures_bucket, "test_good_source_data/0")
self.assertEqual(sz, 11358)
def testCopy(self):
dst_blob_name = infra.generate_test_key()
self.handle.copy(
self.test_fixtures_bucket,
"test_good_source_data/0",
self.test_bucket,
dst_blob_name,
)
# should be able to get metadata for the file.
self.handle.get_user_metadata(
self.test_bucket, dst_blob_name)
def testCopyTokenMatching(self):
cloud_checksum = self.handle.get_cloud_checksum(self.test_fixtures_bucket, "test_good_source_data/0")
copy_token = self.handle.get_copy_token(self.test_fixtures_bucket, "test_good_source_data/0", cloud_checksum)
dst_blob_name = infra.generate_test_key()
self.handle.copy(
self.test_fixtures_bucket,
"test_good_source_data/0",
self.test_bucket,
dst_blob_name,
copy_token,
)
# should be able to get metadata for the file.
self.handle.get_user_metadata(
self.test_bucket, dst_blob_name)
def testCopyTokenNotMatching(self):
intermediate_blob_name = infra.generate_test_key()
self.handle.copy(
self.test_fixtures_bucket,
"test_good_source_data/0",
self.test_bucket,
intermediate_blob_name,
)
cloud_checksum = self.handle.get_cloud_checksum(self.test_bucket, intermediate_blob_name)
copy_token = self.handle.get_copy_token(self.test_bucket, intermediate_blob_name, cloud_checksum)
self.handle.copy(
self.test_fixtures_bucket,
"test_good_source_data/1",
self.test_bucket,
intermediate_blob_name,
)
dst_blob_name = infra.generate_test_key()
try:
self.handle.copy(
self.test_bucket,
intermediate_blob_name,
self.test_bucket,
dst_blob_name,
copy_token,
)
except BlobNotFoundError:
return
# either the file should be copied from the _previous_ contents, or it should not be present.
try:
dst_cloud_checksum = self.handle.get_cloud_checksum(self.test_bucket, dst_blob_name)
self.assertEqual(dst_cloud_checksum, cloud_checksum)
except BlobNotFoundError:
pass
def testDelete(self):
fobj = io.BytesIO(b"abcabcabc")
dst_blob_name = infra.generate_test_key()
self.handle.upload_file_handle(
self.test_bucket,
dst_blob_name,
fobj
)
# should be able to get metadata for the file.
self.handle.get_user_metadata(
self.test_bucket, dst_blob_name)
self.handle.delete(self.test_bucket, dst_blob_name)
with self.assertRaises(BlobNotFoundError):
self.handle.get_user_metadata(
self.test_bucket, dst_blob_name)
def test_check_bucket_exists(self):
"""
Ensure that the ``check_bucket_exists`` method returns true for FIXTURE AND TEST buckets.
"""
handle = self.handle # type: BlobStore
self.assertEqual(handle.check_bucket_exists(self.test_fixtures_bucket), True)
self.assertEqual(handle.check_bucket_exists(self.test_bucket), True)
self.assertEqual(handle.check_bucket_exists('e47114c9-bb96-480f-b6f5-c3e07aae399f'), False)