-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathtests.py
394 lines (344 loc) · 11 KB
/
tests.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
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
from miniopy_async import Minio
from miniopy_async.commonconfig import (
ComposeSource,
CopySource,
ENABLED,
Filter,
Tags,
SnowballObject
)
from miniopy_async.lifecycleconfig import Expiration, LifecycleConfig, Rule as lcRule
from miniopy_async.versioningconfig import VersioningConfig
from miniopy_async.sseconfig import Rule as sseRule, SSEConfig
from miniopy_async.datatypes import PostPolicy
from miniopy_async.deleteobjects import DeleteObject
from miniopy_async.select import (
CSVInputSerialization,
CSVOutputSerialization,
SelectRequest,
)
import asyncio
import os
import json
from datetime import datetime, timedelta
import traceback
import aiohttp
from io import BytesIO
client = Minio(
"play.min.io",
access_key="Q3AM3UQ867SPQQA43P2F",
secret_key="zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG",
secure=True, # http for False, https for True
)
bucket_name = "miniopy-async"
test_file_name = ["testfile-1", "testfile-2", "testfile-3"]
error_func_list = []
async def create_bucket():
try:
if not await client.bucket_exists(bucket_name):
await client.make_bucket(bucket_name)
print("Pass")
except:
traceback.print_exc()
error_func_list.append("create_bucket")
async def upload_snowball_objects():
try:
test_content0 = BytesIO(b"minio" * 1024 * 1024) # 5MB
test_content1 = BytesIO(b"hello" * 1024 * 1024) # 5MB
test_content2 = BytesIO(b"world" * 1024 * 1024) # 5MB
await client.upload_snowball_objects(
bucket_name,
[
SnowballObject(
test_file_name[0],
data=test_content0,
length=test_content1.getbuffer().nbytes
),
SnowballObject(
test_file_name[1],
data=test_content1,
length=test_content1.getbuffer().nbytes
),
SnowballObject(
test_file_name[2],
data=test_content2,
length=test_content2.getbuffer().nbytes,
mod_time=datetime.now()
),
],
staging_filename="testfiles.tar"
)
os.remove("testfiles.tar")
print("Pass")
except:
traceback.print_exc()
error_func_list.append("upload_snowball_objects")
async def compose_object():
try:
sources = [
ComposeSource(bucket_name, file_name) for file_name in test_file_name
]
await client.compose_object(bucket_name, "composed-object", sources)
print("Pass")
except:
traceback.print_exc()
error_func_list.append("compose_object")
async def copy_object():
try:
await client.copy_object(
bucket_name,
"copied-object",
CopySource(bucket_name, test_file_name[0]),
)
print("Pass")
except:
traceback.print_exc()
error_func_list.append("copy_object")
async def set_bucket_versioning():
try:
await client.set_bucket_versioning(bucket_name, VersioningConfig(ENABLED))
print("Pass")
except:
traceback.print_exc()
error_func_list.append("set_bucket_versioning")
async def set_bucket_encryption():
try:
await client.set_bucket_encryption(
bucket_name,
SSEConfig(sseRule.new_sse_s3_rule()),
)
print("Pass")
except:
traceback.print_exc()
error_func_list.append("set_bucket_encryption")
async def set_bucket_lifecycle():
try:
lifecycle_config = LifecycleConfig(
[
lcRule(
ENABLED,
rule_filter=Filter(prefix="logs/"),
rule_id="rule2",
expiration=Expiration(days=365),
),
],
)
await client.set_bucket_lifecycle(bucket_name, lifecycle_config)
print("Pass")
except:
traceback.print_exc()
error_func_list.append("set_bucket_lifecycle")
async def set_bucket_policy():
try:
bucket_policy = {
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {"AWS": "*"},
"Action": ["s3:GetBucketLocation", "s3:ListBucket"],
"Resource": "arn:aws:s3:::%s" % bucket_name,
},
{
"Effect": "Allow",
"Principal": {"AWS": "*"},
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::%s/*" % bucket_name,
},
],
}
await client.set_bucket_policy(bucket_name, json.dumps(bucket_policy))
print("Pass")
except:
traceback.print_exc()
error_func_list.append("set_bucket_policy")
async def set_bucket_tags():
try:
bucket_tags = Tags.new_bucket_tags()
bucket_tags["Project"] = "Project One"
bucket_tags["User"] = "jsmith"
await client.set_bucket_tags(bucket_name, bucket_tags)
print("Pass")
except:
traceback.print_exc()
error_func_list.append("set_bucket_tags")
async def set_object_tags():
try:
object_tags = Tags.new_object_tags()
object_tags["Project"] = "Project One"
object_tags["User"] = "jsmith"
await client.set_object_tags(bucket_name, test_file_name[0], object_tags)
print("Pass")
except:
traceback.print_exc()
error_func_list.append("set_object_tags")
async def stat_object():
try:
await client.stat_object(bucket_name, test_file_name[0])
print("Pass")
except:
traceback.print_exc()
error_func_list.append("stat_object")
async def presigned_get_object():
try:
await client.presigned_get_object(bucket_name, test_file_name[0])
print("Pass")
except:
traceback.print_exc()
error_func_list.append("presigned_get_object")
async def presigned_put_object():
try:
await client.presigned_put_object(bucket_name, test_file_name[0])
print("Pass")
except:
traceback.print_exc()
error_func_list.append("presigned_put_object")
async def presigned_post_policy():
try:
bucket_post_policy = PostPolicy(
bucket_name,
datetime.utcnow() + timedelta(days=10),
)
bucket_post_policy.add_starts_with_condition("key", "my/object/prefix/")
bucket_post_policy.add_content_length_range_condition(
1 * 1024 * 1024, 10 * 1024 * 1024
)
await client.presigned_post_policy(bucket_post_policy)
print("Pass")
except:
traceback.print_exc()
error_func_list.append("presigned_post_policy")
async def list_buckets():
try:
await client.list_buckets()
print("Pass")
except:
traceback.print_exc()
error_func_list.append("list_buckets")
async def list_objects():
try:
await client.list_objects(bucket_name)
print("Pass")
except:
traceback.print_exc()
error_func_list.append("list_objects")
async def get_object():
try:
async with aiohttp.ClientSession() as session:
response = await client.get_object(bucket_name, test_file_name[0], session)
await response.content.read()
print("Pass")
except:
traceback.print_exc()
error_func_list.append("get_object")
async def fget_object():
try:
await client.fget_object(bucket_name, test_file_name[0], "testfile")
os.remove("testfile")
print("Pass")
except:
traceback.print_exc()
error_func_list.append("fget_object")
async def select_object_content():
try:
test_content = b"1" * 1024 * 512
file_name = "testfile-3"
with open(file_name, "wb") as file:
file.write(test_content)
await client.fput_object(bucket_name, file_name, file_name)
os.remove(file_name)
result = await client.select_object_content(
bucket_name,
file_name,
SelectRequest(
"select * from s3object",
CSVInputSerialization(),
CSVOutputSerialization(),
request_progress=True,
),
)
async for data in result.stream():
data.decode()
print("Pass")
except:
traceback.print_exc()
error_func_list.append("select_object_content")
async def remove_object():
try:
await client.remove_object(bucket_name, test_file_name[0])
print("Pass")
except:
traceback.print_exc()
error_func_list.append("remove_object")
async def remove_objects():
try:
objs = await client.list_objects(bucket_name, include_version=True)
await client.remove_objects(
bucket_name,
[DeleteObject(obj.object_name, obj.version_id) for obj in objs],
)
print("Pass")
except:
traceback.print_exc()
error_func_list.append("remove_objects")
async def remove_bucket():
try:
await client.remove_bucket(bucket_name)
print("Pass")
except:
traceback.print_exc()
error_func_list.append("remove_bucket")
async def main():
print("Testing create bucket...")
await create_bucket()
print("Testing upload snowball objects...")
await upload_snowball_objects()
print("Testing compose object..")
await compose_object()
print("Testing copy object...")
await copy_object()
print("Testing set bucket versioning...")
await set_bucket_versioning()
print("Testing set bucket encryption...")
await set_bucket_encryption()
print("Testing set bucket lifecycle...")
await set_bucket_lifecycle()
print("Testing set bucket policy...")
await set_bucket_policy()
print("Testing set bucket tags...")
await set_bucket_tags()
print("Testing set object tags...")
await set_object_tags()
print("Testing stat object...")
await stat_object()
print("Testing presigned get object...")
await presigned_get_object()
print("Testing presigned put object...")
await presigned_put_object()
print("Testing presigned post policy...")
await presigned_post_policy()
print("Testing list bucket...")
await list_buckets()
print("Testing list objects...")
await list_objects()
print("Testing get object...")
await get_object()
print("Testing fget object...")
await fget_object()
print("Testing select object content...")
await select_object_content()
print("Testing remove object...")
await remove_object()
print("Testing remove objects...")
await remove_objects()
print("Testing remove bucket...")
await remove_bucket()
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
loop.close()
if len(error_func_list) == 0:
print("\nAll Pass")
else:
print("\nError Functions:")
for error_func in error_func_list:
print(error_func)