-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
test_qmk_storage.py
141 lines (110 loc) · 3.79 KB
/
test_qmk_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
import datetime
from io import BytesIO
from tempfile import NamedTemporaryFile
import qmk_storage
def test_put_and_get():
"""Make sure we can store a string and retrieve it.
"""
test_key = 'qmk_compiler_test_unique_key_name'
# Make sure our test key doesn't exist
try:
qmk_storage.get(test_key)
raise RuntimeError('%s exists on S3 when it should not!' % test_key)
except Exception as e:
if e.__class__.__name__ != 'NoSuchKey':
raise
# Write it to S3
qmk_storage.put(test_key, 'hello')
# Make sure we can retrieve it
saved_file = qmk_storage.get(test_key)
qmk_storage.delete(test_key)
assert saved_file == 'hello'
def test_delete():
"""Create and then delete an object from s3, make sure we can't fetch it afterward."""
test_key = 'qmk_compiler_test_unique_key_name'
# Make sure our test key doesn't exist
try:
qmk_storage.get(test_key)
raise RuntimeError('%s exists on S3 when it should not!' % test_key)
except Exception as e:
if e.__class__.__name__ != 'NoSuchKey':
raise
# Store a test key we can delete
qmk_storage.put(test_key, 'hello')
assert qmk_storage.get(test_key) == 'hello'
qmk_storage.delete(test_key)
# Make sure it actually deleted
try:
qmk_storage.get(test_key)
raise RuntimeError('%s exists on S3 when it should not!' % test_key)
except Exception as e:
if e.__class__.__name__ != 'NoSuchKey':
raise
def test_list_objects():
"""Make sure we can list objects on S3.
"""
x = 0
for obj in qmk_storage.list_objects():
assert 'Key' in obj
assert type(obj.get('LastModified')) == datetime.datetime
if x > 5:
break
x += 1
# FIXME: Add a test for pagination here.
def test_save_fd():
"""Make sure we can stream file-like objects to S3.
"""
test_key = 'qmk_compiler_test_unique_key_name'
# Make sure our test key doesn't exist
try:
qmk_storage.get(test_key)
raise RuntimeError('%s exists on S3 when it should not!' % test_key)
except Exception as e:
if e.__class__.__name__ != 'NoSuchKey':
raise
# Save our file in S3
with BytesIO(b'hello') as fd:
qmk_storage.save_fd(fd, test_key)
# Make sure we get it back
saved_file = qmk_storage.get(test_key)
qmk_storage.delete(test_key)
assert saved_file == 'hello'
def test_save_file():
"""Make sure we can store a file and retrieve it.
"""
test_key = 'qmk_compiler_test_unique_key_name'
# Make sure our test key doesn't exist
try:
qmk_storage.get(test_key)
raise RuntimeError('%s exists on S3 when it should not!' % test_key)
except Exception as e:
if e.__class__.__name__ != 'NoSuchKey':
raise
# Write it to S3
with NamedTemporaryFile(mode='w', encoding='utf-8') as tempfile:
tempfile.write('hello')
tempfile.flush()
qmk_storage.save_file(tempfile.name, test_key)
# Make sure we can retrieve it
saved_file = qmk_storage.get(test_key)
qmk_storage.delete(test_key)
assert saved_file == 'hello'
def test_get_fd():
"""Make sure we can get a file with a file-like interface
"""
test_key = 'qmk_compiler_test_unique_key_name'
# Make sure our test key doesn't exist
try:
qmk_storage.get(test_key)
raise RuntimeError('%s exists on S3 when it should not!' % test_key)
except Exception as e:
if e.__class__.__name__ != 'NoSuchKey':
raise
# Create it on S3
qmk_storage.put(test_key, 'hello')
# Make sure we can retrieve it
fd = qmk_storage.get_fd(test_key)
saved_file = fd.read()
fd.close()
qmk_storage.delete(test_key)
assert saved_file == b'hello'