-
Notifications
You must be signed in to change notification settings - Fork 259
/
test_qiniu.py
362 lines (304 loc) · 11.9 KB
/
test_qiniu.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
# -*- coding: utf-8 -*-
# flake8: noqa
import os
import string
import random
import tempfile
import functools
import requests
import unittest
import pytest
from freezegun import freeze_time
from qiniu import Auth, set_default, etag, PersistentFop, build_op, op_save, Zone, QiniuMacAuth
from qiniu import BucketManager, build_batch_copy, build_batch_rename, build_batch_move, build_batch_stat, \
build_batch_delete, DomainManager
from qiniu import urlsafe_base64_encode, urlsafe_base64_decode, canonical_mime_header_key, entry, decode_entry
from qiniu.compat import is_py2, is_py3, b, json
import qiniu.config
if is_py2:
import sys
import StringIO
import urllib
from imp import reload
reload(sys)
sys.setdefaultencoding('utf-8')
StringIO = StringIO.StringIO
urlopen = urllib.urlopen
elif is_py3:
import io
import urllib
StringIO = io.StringIO
urlopen = urllib.request.urlopen
access_key = os.getenv('QINIU_ACCESS_KEY')
secret_key = os.getenv('QINIU_SECRET_KEY')
bucket_name = os.getenv('QINIU_TEST_BUCKET')
hostscache_dir = None
def rand_string(length):
lib = string.ascii_uppercase
return ''.join([random.choice(lib) for i in range(0, length)])
def create_temp_file(size):
t = tempfile.mktemp()
f = open(t, 'wb')
f.seek(size - 1)
f.write(b('0'))
f.close()
return t
def remove_temp_file(file):
try:
os.remove(file)
except OSError:
pass
class BucketTestCase(unittest.TestCase):
q = Auth(access_key, secret_key)
bucket = BucketManager(q)
def test_list(self):
ret, eof, info = self.bucket.list(bucket_name, limit=4)
assert eof is False
assert len(ret.get('items')) == 4
ret, eof, info = self.bucket.list(bucket_name, limit=1000)
assert info.status_code == 200, info
def test_buckets(self):
ret, info = self.bucket.buckets()
print(info)
assert bucket_name in ret
def test_prefetch(self):
ret, info = self.bucket.prefetch(bucket_name, 'python-sdk.html', hostscache_dir=hostscache_dir)
print(info)
assert ret['key'] == 'python-sdk.html'
def test_fetch(self):
ret, info = self.bucket.fetch('https://developer.qiniu.com/kodo/sdk/python', bucket_name,
'fetch.html', hostscache_dir=hostscache_dir)
print(info)
assert ret['key'] == 'fetch.html'
assert 'hash' in ret
def test_fetch_without_key(self):
ret, info = self.bucket.fetch('https://developer.qiniu.com/kodo/sdk/python', bucket_name,
hostscache_dir=hostscache_dir)
print(info)
assert ret['key'] == ret['hash']
assert 'hash' in ret
def test_stat(self):
ret, info = self.bucket.stat(bucket_name, 'python-sdk.html')
print(info)
assert 'hash' in ret
def test_delete(self):
ret, info = self.bucket.delete(bucket_name, 'del')
print(info)
assert ret is None
assert info.status_code == 612
def test_rename(self):
key = 'renameto' + rand_string(8)
self.bucket.copy(bucket_name, 'copyfrom', bucket_name, key)
key2 = key + 'move'
ret, info = self.bucket.rename(bucket_name, key, key2)
print(info)
assert ret == {}
ret, info = self.bucket.delete(bucket_name, key2)
print(info)
assert ret == {}
def test_copy(self):
key = 'copyto' + rand_string(8)
ret, info = self.bucket.copy(bucket_name, 'copyfrom', bucket_name, key)
print(info)
assert ret == {}
ret, info = self.bucket.delete(bucket_name, key)
print(info)
assert ret == {}
def test_change_mime(self):
ret, info = self.bucket.change_mime(bucket_name, 'python-sdk.html', 'text/html')
print(info)
assert ret == {}
def test_change_type(self):
target_key = 'copyto' + rand_string(8)
self.bucket.copy(bucket_name, 'copyfrom', bucket_name, target_key)
ret, info = self.bucket.change_type(bucket_name, target_key, 1)
print(info)
assert ret == {}
ret, info = self.bucket.stat(bucket_name, target_key)
print(info)
assert 'type' in ret
self.bucket.delete(bucket_name, target_key)
def test_copy_force(self):
ret, info = self.bucket.copy(bucket_name, 'copyfrom', bucket_name, 'copyfrom', force='true')
print(info)
assert info.status_code == 200
def test_batch_copy(self):
key = 'copyto' + rand_string(8)
ops = build_batch_copy(bucket_name, {'copyfrom': key}, bucket_name)
ret, info = self.bucket.batch(ops)
print(info)
assert ret[0]['code'] == 200
ops = build_batch_delete(bucket_name, [key])
ret, info = self.bucket.batch(ops)
print(info)
assert ret[0]['code'] == 200
def test_batch_copy_force(self):
ops = build_batch_copy(bucket_name, {'copyfrom': 'copyfrom'}, bucket_name, force='true')
ret, info = self.bucket.batch(ops)
print(info)
assert ret[0]['code'] == 200
def test_batch_move(self):
key = 'moveto' + rand_string(8)
self.bucket.copy(bucket_name, 'copyfrom', bucket_name, key)
key2 = key + 'move'
ops = build_batch_move(bucket_name, {key: key2}, bucket_name)
ret, info = self.bucket.batch(ops)
print(info)
assert ret[0]['code'] == 200
ret, info = self.bucket.delete(bucket_name, key2)
print(info)
assert ret == {}
def test_batch_move_force(self):
ret, info = self.bucket.copy(bucket_name, 'copyfrom', bucket_name, 'copyfrom', force='true')
print(info)
assert info.status_code == 200
ops = build_batch_move(bucket_name, {'copyfrom': 'copyfrom'}, bucket_name, force='true')
ret, info = self.bucket.batch(ops)
print(info)
assert ret[0]['code'] == 200
def test_batch_rename(self):
key = 'rename' + rand_string(8)
self.bucket.copy(bucket_name, 'copyfrom', bucket_name, key)
key2 = key + 'rename'
ops = build_batch_move(bucket_name, {key: key2}, bucket_name)
ret, info = self.bucket.batch(ops)
print(info)
assert ret[0]['code'] == 200
ret, info = self.bucket.delete(bucket_name, key2)
print(info)
assert ret == {}
def test_batch_rename_force(self):
ret, info = self.bucket.rename(bucket_name, 'copyfrom', 'copyfrom', force='true')
print(info)
assert info.status_code == 200
ops = build_batch_rename(bucket_name, {'copyfrom': 'copyfrom'}, force='true')
ret, info = self.bucket.batch(ops)
print(info)
assert ret[0]['code'] == 200
def test_batch_stat(self):
ops = build_batch_stat(bucket_name, ['python-sdk.html'])
ret, info = self.bucket.batch(ops)
print(info)
assert ret[0]['code'] == 200
def test_delete_after_days(self):
days = '5'
ret, info = self.bucket.delete_after_days(bucket_name, 'invaild.html', days)
assert info.status_code == 612
key = 'copyto' + rand_string(8)
ret, info = self.bucket.copy(bucket_name, 'copyfrom', bucket_name, key)
ret, info = self.bucket.delete_after_days(bucket_name, key, days)
assert info.status_code == 200
def test_set_object_lifecycle(self):
key = 'test_set_object_lifecycle' + rand_string(8)
ret, info = self.bucket.copy(bucket_name, 'copyfrom', bucket_name, key)
assert info.status_code == 200
ret, info = self.bucket.set_object_lifecycle(
bucket=bucket_name,
key=key,
to_line_after_days=10,
to_archive_ir_after_days=15,
to_archive_after_days=20,
to_deep_archive_after_days=30,
delete_after_days=40
)
assert info.status_code == 200
def test_set_object_lifecycle_with_cond(self):
key = 'test_set_object_lifecycle_cond' + rand_string(8)
ret, info = self.bucket.copy(bucket_name, 'copyfrom', bucket_name, key)
assert info.status_code == 200
ret, info = self.bucket.stat(bucket_name, key)
assert info.status_code == 200
key_hash = ret['hash']
ret, info = self.bucket.set_object_lifecycle(
bucket=bucket_name,
key=key,
to_line_after_days=10,
to_archive_ir_after_days=15,
to_archive_after_days=20,
to_deep_archive_after_days=30,
delete_after_days=40,
cond={
'hash': key_hash
}
)
assert info.status_code == 200
def test_list_domains(self):
ret, info = self.bucket.list_domains(bucket_name)
print(info)
assert info.status_code == 200
assert isinstance(ret, list)
@freeze_time("1970-01-01")
def test_invalid_x_qiniu_date(self):
ret, info = self.bucket.stat(bucket_name, 'python-sdk.html')
assert ret is None
assert info.status_code == 403
@freeze_time("1970-01-01")
def test_invalid_x_qiniu_date_with_disable_date_sign(self):
q = Auth(access_key, secret_key, disable_qiniu_timestamp_signature=True)
bucket = BucketManager(q)
ret, info = bucket.stat(bucket_name, 'python-sdk.html')
assert 'hash' in ret
@freeze_time("1970-01-01")
def test_invalid_x_qiniu_date_env(self):
os.environ['DISABLE_QINIU_TIMESTAMP_SIGNATURE'] = 'True'
ret, info = self.bucket.stat(bucket_name, 'python-sdk.html')
if hasattr(os, 'unsetenv'):
os.unsetenv('DISABLE_QINIU_TIMESTAMP_SIGNATURE')
else:
# fix unsetenv not exists in earlier python on windows
os.environ['DISABLE_QINIU_TIMESTAMP_SIGNATURE'] = ''
assert 'hash' in ret
@freeze_time("1970-01-01")
def test_invalid_x_qiniu_date_env_be_ignored(self):
os.environ['DISABLE_QINIU_TIMESTAMP_SIGNATURE'] = 'True'
q = Auth(access_key, secret_key, disable_qiniu_timestamp_signature=False)
bucket = BucketManager(q)
ret, info = bucket.stat(bucket_name, 'python-sdk.html')
if hasattr(os, 'unsetenv'):
os.unsetenv('DISABLE_QINIU_TIMESTAMP_SIGNATURE')
else:
# fix unsetenv not exists in earlier python on windows
os.environ['DISABLE_QINIU_TIMESTAMP_SIGNATURE'] = ''
assert ret is None
assert info.status_code == 403
class DownloadTestCase(unittest.TestCase):
q = Auth(access_key, secret_key)
def test_private_url(self):
private_bucket_domain = 'private-sdk.peterpy.cn'
private_key = 'gogopher.jpg'
base_url = 'http://%s/%s' % (private_bucket_domain, private_key)
private_url = self.q.private_download_url(base_url, expires=3600)
print(private_url)
r = requests.get(private_url)
assert r.status_code == 200
class EtagTestCase(unittest.TestCase):
def test_zero_size(self):
open("x", 'a').close()
hash = etag("x")
assert hash == 'Fto5o-5ea0sNMlW_75VgGJCv2AcJ'
remove_temp_file("x")
def test_small_size(self):
localfile = create_temp_file(1024 * 1024)
hash = etag(localfile)
assert hash == 'FnlAdmDasGTQOIgrU1QIZaGDv_1D'
remove_temp_file(localfile)
def test_large_size(self):
localfile = create_temp_file(4 * 1024 * 1024 + 1)
hash = etag(localfile)
assert hash == 'ljF323utglY3GI6AvLgawSJ4_dgk'
remove_temp_file(localfile)
class CdnTestCase(unittest.TestCase):
q = Auth(access_key, secret_key)
domain_manager = DomainManager(q)
def test_get_domain(self):
ret, info = self.domain_manager.get_domain('pythonsdk.qiniu.io')
print(info)
assert info.status_code == 200
class ReadWithoutSeek(object):
def __init__(self, str):
self.str = str
pass
def read(self):
print(self.str)
if __name__ == '__main__':
unittest.main()