/
tests.py
267 lines (217 loc) · 9.94 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
"""
For simplicity and to avoid requiring a paid-for account on some cloud
storage system testing is conducted against two local storage backends. Since
the QueuedStorage backend is truly agnostic about the local and remote
storage systems, this should work as transparently as using one (or even two!)
remote storage systems.
"""
import os
import shutil
import tempfile
from os import path
from datetime import datetime
from django.core.files.base import File
from django.core.files.storage import FileSystemStorage, Storage
from django.test import TestCase
from queued_storage.backends import QueuedStorage
from queued_storage.conf import settings
from . import models
class StorageTests(TestCase):
def setUp(self):
self.old_celery_always_eager = getattr(
settings, 'CELERY_ALWAYS_EAGER', False)
settings.CELERY_ALWAYS_EAGER = True
self.local_dir = tempfile.mkdtemp()
self.remote_dir = tempfile.mkdtemp()
tmp_dir = tempfile.mkdtemp()
self.test_file_name = 'queued_storage.txt'
self.test_file_path = path.join(tmp_dir, self.test_file_name)
self.test_file = file(self.test_file_path, 'a')
self.addCleanup(shutil.rmtree, self.local_dir)
self.addCleanup(shutil.rmtree, self.remote_dir)
self.addCleanup(shutil.rmtree, tmp_dir)
def tearDown(self):
settings.CELERY_ALWAYS_EAGER = self.old_celery_always_eager
models.retried = False
def test_storage_init(self):
"""
Make sure that creating a QueuedStorage object works
"""
storage = QueuedStorage(
'django.core.files.storage.FileSystemStorage',
'django.core.files.storage.FileSystemStorage')
self.assertIsInstance(storage, QueuedStorage)
self.assertEqual(FileSystemStorage, storage.local.__class__)
self.assertEqual(FileSystemStorage, storage.remote.__class__)
def test_storage_cache_key(self):
storage = QueuedStorage(
'django.core.files.storage.FileSystemStorage',
'django.core.files.storage.FileSystemStorage',
cache_prefix='test_cache_key')
self.assertEqual(storage.cache_prefix, 'test_cache_key')
def test_storage_methods(self):
"""
Make sure that QueuedStorage implements all the methods
"""
storage = QueuedStorage(
'django.core.files.storage.FileSystemStorage',
'django.core.files.storage.FileSystemStorage')
file_storage = Storage()
for attr in dir(file_storage):
method = getattr(file_storage, attr)
if not callable(method):
continue
method = getattr(storage, attr, False)
self.assertTrue(callable(method),
"QueuedStorage has no method '%s'" % attr)
def test_storage_simple_save(self):
"""
Make sure that saving to remote locations actually works
"""
storage = QueuedStorage(
local='django.core.files.storage.FileSystemStorage',
remote='django.core.files.storage.FileSystemStorage',
local_options=dict(location=self.local_dir),
remote_options=dict(location=self.remote_dir),
task='queued_storage.tests.tasks.test_task')
field = models.TestModel._meta.get_field('file')
field.storage = storage
obj = models.TestModel(file=File(self.test_file))
obj.save()
self.assertTrue(path.isfile(path.join(self.local_dir, obj.file.name)))
self.assertTrue(path.isfile(path.join(self.remote_dir, obj.file.name)))
def test_storage_celery_save(self):
"""
Make sure it actually works when using Celery as a task queue
"""
storage = QueuedStorage(
local='django.core.files.storage.FileSystemStorage',
remote='django.core.files.storage.FileSystemStorage',
local_options=dict(location=self.local_dir),
remote_options=dict(location=self.remote_dir))
field = models.TestModel._meta.get_field('file')
field.storage = storage
obj = models.TestModel(file=File(self.test_file))
obj.save()
self.assertTrue(obj.file.storage.result.get())
self.assertTrue(path.isfile(path.join(self.local_dir, obj.file.name)))
self.assertTrue(
path.isfile(path.join(self.remote_dir, obj.file.name)),
"Remote file is not available.")
self.assertFalse(storage.using_local(obj.file.name))
self.assertTrue(storage.using_remote(obj.file.name))
self.assertEqual(self.test_file_name,
storage.get_valid_name(self.test_file_name))
self.assertEqual(self.test_file_name,
storage.get_available_name(self.test_file_name))
subdir_path = os.path.join('test', self.test_file_name)
self.assertTrue(storage.exists(subdir_path))
self.assertEqual(storage.path(self.test_file_name),
path.join(self.local_dir, self.test_file_name))
self.assertEqual(storage.listdir('test')[1], [self.test_file_name])
self.assertEqual(storage.size(subdir_path),
os.stat(self.test_file_path).st_size)
self.assertEqual(storage.url(self.test_file_name), self.test_file_name)
self.assertIsInstance(storage.accessed_time(subdir_path), datetime)
self.assertIsInstance(storage.created_time(subdir_path), datetime)
self.assertIsInstance(storage.modified_time(subdir_path), datetime)
subdir_name = 'queued_storage_2.txt'
testfile = storage.open(subdir_name, 'w')
try:
testfile.write('test')
finally:
testfile.close()
self.assertTrue(storage.exists(subdir_name))
storage.delete(subdir_name)
self.assertFalse(storage.exists(subdir_name))
def test_transfer_and_delete(self):
"""
Make sure the TransferAndDelete task does what it says
"""
storage = QueuedStorage(
local='django.core.files.storage.FileSystemStorage',
remote='django.core.files.storage.FileSystemStorage',
local_options=dict(location=self.local_dir),
remote_options=dict(location=self.remote_dir),
task='queued_storage.tasks.TransferAndDelete')
field = models.TestModel._meta.get_field('file')
field.storage = storage
obj = models.TestModel(file=File(self.test_file))
obj.save()
obj.file.storage.result.get()
self.assertFalse(
path.isfile(path.join(self.local_dir, obj.file.name)),
"Local file is still available")
self.assertTrue(
path.isfile(path.join(self.remote_dir, obj.file.name)),
"Remote file is not available.")
def test_transfer_returns_boolean(self):
"""
Make sure an exception is thrown when the transfer task does not return
a boolean. We don't want to confuse Celery.
"""
storage = QueuedStorage(
local='django.core.files.storage.FileSystemStorage',
remote='django.core.files.storage.FileSystemStorage',
local_options=dict(location=self.local_dir),
remote_options=dict(location=self.remote_dir),
task='queued_storage.tests.tasks.NoneReturningTask')
field = models.TestModel._meta.get_field('file')
field.storage = storage
obj = models.TestModel(file=File(self.test_file))
obj.save()
self.assertRaises(ValueError,
obj.file.storage.result.get, propagate=True)
def test_transfer_retried(self):
"""
Make sure the transfer task is retried correctly.
"""
storage = QueuedStorage(
local='django.core.files.storage.FileSystemStorage',
remote='django.core.files.storage.FileSystemStorage',
local_options=dict(location=self.local_dir),
remote_options=dict(location=self.remote_dir),
task='queued_storage.tests.tasks.RetryingTask')
field = models.TestModel._meta.get_field('file')
field.storage = storage
self.assertFalse(models.retried)
obj = models.TestModel(file=File(self.test_file))
obj.save()
self.assertFalse(obj.file.storage.result.get())
self.assertTrue(models.retried)
def test_delayed_storage(self):
storage = QueuedStorage(
local='django.core.files.storage.FileSystemStorage',
remote='django.core.files.storage.FileSystemStorage',
local_options=dict(location=self.local_dir),
remote_options=dict(location=self.remote_dir),
delayed=True)
field = models.TestModel._meta.get_field('file')
field.storage = storage
obj = models.TestModel(file=File(self.test_file))
obj.save()
self.assertIsNone(getattr(obj.file.storage, 'result', None))
self.assertFalse(
path.isfile(path.join(self.remote_dir, obj.file.name)),
"Remote file should not be transferred automatically.")
result = obj.file.storage.transfer(obj.file.name)
result.get()
self.assertTrue(
path.isfile(path.join(self.remote_dir, obj.file.name)),
"Remote file is not available.")
def test_remote_file_field(self):
storage = QueuedStorage(
local='django.core.files.storage.FileSystemStorage',
remote='django.core.files.storage.FileSystemStorage',
local_options=dict(location=self.local_dir),
remote_options=dict(location=self.remote_dir),
delayed=True)
field = models.TestModel._meta.get_field('remote')
field.storage = storage
obj = models.TestModel(remote=File(self.test_file))
obj.save()
self.assertIsNone(getattr(obj.file.storage, 'result', None))
result = obj.remote.transfer()
self.assertTrue(result)
self.assertTrue(path.isfile(path.join(self.remote_dir,
obj.remote.name)))