-
Notifications
You must be signed in to change notification settings - Fork 2
/
test.py
329 lines (272 loc) · 10.7 KB
/
test.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
#! /usr/bin/python -t
import os
import sys
import tempfile
import shutil
import time
import unittest
sys.path.insert(0, '.')
import cashe
_tmptest = True
def _cleanup(tdir):
if _tmptest:
shutil.rmtree(tdir)
# Data to sha256 checksums ...
d2s = {
'a' :'ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb',
'a'*2 :'961b6dd3ede3cb8ecbaacbd68de040cd78eb2ed5889130cceb4c49268ea4d506',
'a'*3 :'9834876dcfb05cb167a5c24953eba58c4ac89b1adf57f28f2f9d09af107ee8f0',
'a'*4 :'61be55a8e2f6b4e172338bddf184d6dbee29c98853e0a0485ecee7f27b9af0b4',
'a'*5 :'ed968e840d10d2d313a870bc131a4e2c311d7ad09bdf32b3418147221f51a6e2',
'a'*6 :'ed02457b5c41d964dbd2f2a609d63fe1bb7528dbe55e1abf5b52c249cd735797',
'a'*7 :'e46240714b5db3a23eee60479a623efba4d633d27fe4f03c904b9e219a7fbe60',
'a'*8 :'1f3ce40415a2081fa3eee75fc39fff8e56c22270d1a978a7249b592dcebd20b4',
'a'*9 :'f2aca93b80cae681221f0445fa4e2cae8a1f9f8fa1e1741d9639caad222f537d',
'b' :'3e23e8160039594a33894f6564e1b1348bbd7a0088d42c4acb73eeaed59c009d',
'b'*2 :'3b64db95cb55c763391c707108489ae18b4112d783300de38e033b4c98c3deaf',
'b'*3 :'3e744b9dc39389baf0c5a0660589b8402f3dbb49b89b3e75f2c9355852a3c677',
'b'*4 :'81cc5b17018674b401b42f35ba07bb79e211239c23bffe658da1577e3e646877',
'b'*5 :'5e846c64f2db12266e6b658a8e5b5b42cc225419b3ee1fca88acbb181ddfdb52',
'b'*6 :'4625fd63b0e96fc0d656ae7381605e48d4a0f63a319fc743adf22688613883c7',
'b'*7 :'ea415a61bd19915084366a0a2fdaebe070a9c3168877ecdb5e36f4905b5f8aa3',
'b'*8 :'fb398cc690e15ddba43ee811b6c0d3ec190901ad3df377fec9a1f9004b919a06',
}
# End of setup
def fwrite(path, data):
fo = open(path, "w")
fo.write(data)
class Cashe_tests(unittest.TestCase):
def __init__(self, *args, **kwargs):
unittest.TestCase.__init__(self, *args, **kwargs)
if _tmptest:
self.tdir = tempfile.mkdtemp()
else:
self.tdir = "saved-tests-dir"
def stopTest(self):
if _tmptest:
_cleanup(self.tdir)
def assertPathExists(self, path):
if os.path.exists(path):
return
self.fail("Path doesn't exist: " + path)
def assertPathNotExists(self, path):
if not os.path.exists(path):
return
self.fail("Path doesn't exist: " + path)
def assertPathLinks(self, path, links):
self.assertPathExists(path)
sd = os.stat(path)
if links == sd.st_nlink:
return
self.fail("Path %s links don't match, expected=%u found=%u" %
(path, links, sd.st_nlink))
def assertFileEqual(self, path1, path2):
d1 = open(path1).read()
d2 = open(path2).read()
if d1 == d2:
return
self.fail("Paths %s and %s do not match" %
(path1, path2))
# -------------------------------------------------------------------------
# Tests...
# -------------------------------------------------------------------------
def test1_init(self):
x = cashe.CAShe(self.tdir + "/test1")
def test2_save_1(self):
x = cashe.CAShe(self.tdir + "/test2")
data = x.path + "/data2.1"
fwrite(data, "a")
self.assertPathLinks(data, 1)
co = x.get('sha256', d2s['a'])
co.save(data, checksum=False)
print co.filename
self.assertPathLinks(data, 2)
self.assertPathLinks(co.filename, 2)
def test2_save_2(self):
x = cashe.CAShe(self.tdir + "/test2")
data = x.path + "/data2.2"
fwrite(data, "b")
self.assertPathLinks(data, 1)
co = x.get('sha256', d2s['b'])
ret = co.save(data)
print ret, co.filename
self.assertPathLinks(data, 2)
self.assertPathLinks(co.filename, 2)
def test2_save_3(self):
x = cashe.CAShe(self.tdir + "/test2")
datai = x.path + "/data2.3"
datao = x.path + "/data2.3-out"
fwrite(datai, "b")
self.assertPathLinks(datai, 1)
self.assertPathNotExists(datao)
co = x.get('sha256', d2s['a'])
ret = co.save(datai, checksum=False)
self.assertFileEqual(datai, co.filename)
self.assertPathLinks(datai, 2)
self.assertPathLinks(co.filename, 2)
self.assertEqual(1, len(list(x.ls())))
ret = co.load(datao)
self.assertFileEqual(datai, co.filename)
self.assertFileEqual(datao, co.filename)
self.assertPathLinks(datai, 3)
self.assertPathLinks(datao, 3)
self.assertPathLinks(co.filename, 3)
self.assertEqual(1, len(list(x.ls())))
self.assertTrue(co.exists)
ret = co.load(datao, checksum=True)
self.assertPathLinks(datai, 2)
self.assertPathNotExists(co.filename)
self.assertEqual(0, len(list(x.ls())))
self.assertFalse(co.exists)
os.unlink(datao)
co = x.get('sha256', d2s['b'])
self.assertFalse(co.exists)
ret = co.save(datai, checksum=False)
self.assertFileEqual(datai, co.filename)
self.assertPathLinks(datai, 2)
self.assertPathLinks(co.filename, 2)
self.assertTrue(co.exists)
self.assertEqual(1, len(list(x.ls())))
co = x.get('sha256', d2s['b'])
ret = co.load(datao, checksum=True)
self.assertEqual(co.size, 1)
self.assertFileEqual(datai, datao)
self.assertFileEqual(datai, co.filename)
self.assertPathLinks(datai, 3)
self.assertPathLinks(datao, 3)
self.assertPathLinks(co.filename, 3)
self.assertTrue(co.exists)
self.assertEqual(1, len(list(x.ls())))
def test3_save_3(self):
x = cashe.CAShe(self.tdir + "/test3")
x.link = False
datai = x.path + "/data3.3"
datao = x.path + "/data3.3-out"
fwrite(datai, "b")
self.assertPathLinks(datai, 1)
self.assertPathNotExists(datao)
co = x.get('sha256', d2s['a'])
ret = co.save(datai, checksum=False)
self.assertFileEqual(datai, co.filename)
self.assertPathLinks(datai, 1)
self.assertPathLinks(co.filename, 1)
self.assertEqual(1, len(list(x.ls())))
ret = co.load(datao)
self.assertFileEqual(datai, co.filename)
self.assertFileEqual(datao, co.filename)
self.assertPathLinks(datai, 1)
self.assertPathLinks(datao, 1)
self.assertPathLinks(co.filename, 1)
self.assertEqual(1, len(list(x.ls())))
self.assertTrue(co.exists)
ret = co.load(datao, checksum=True)
self.assertPathLinks(datai, 1)
self.assertPathNotExists(co.filename)
self.assertEqual(0, len(list(x.ls())))
self.assertFalse(co.exists)
os.unlink(datao)
co = x.get('sha256', d2s['b'])
self.assertFalse(co.exists)
ret = co.save(datai, checksum=False)
self.assertFileEqual(datai, co.filename)
self.assertPathLinks(datai, 1)
self.assertPathLinks(co.filename, 1)
self.assertTrue(co.exists)
self.assertEqual(1, len(list(x.ls())))
co = x.get('sha256', d2s['b'])
ret = co.load(datao, checksum=True)
self.assertEqual(co.size, 1)
self.assertFileEqual(datai, datao)
self.assertFileEqual(datai, co.filename)
self.assertPathLinks(datai, 1)
self.assertPathLinks(datao, 1)
self.assertPathLinks(co.filename, 1)
self.assertTrue(co.exists)
self.assertEqual(1, len(list(x.ls())))
# print "JDBG: ls:", list(x.ls())
def test2_ls(self):
x = cashe.CAShe(self.tdir + "/test2")
datai = x.path + "/data2.4a"
datao = x.path + "/data2.4a-out"
fwrite(datai, "a")
co = x.get('sha256', d2s['a'])
co.save(datai)
co.load(datao)
self.assertPathLinks(datai, 3)
self.assertPathLinks(datao, 3)
self.assertPathLinks(co.filename, 3)
self.assertEqual(1, len(list(x.ls())))
self.assertTrue(co.exists)
datai = x.path + "/data2.4aa"
datao = x.path + "/data2.4aa-out"
fwrite(datai, "aa")
self.assertPathLinks(datai, 1)
self.assertPathNotExists(datao)
self.assertEqual(co.size, 1)
co = x.get('sha256', d2s['aa'])
ret = co.save(datai)
ret = co.load(datao)
self.assertPathLinks(datai, 3)
self.assertPathLinks(datao, 3)
self.assertPathLinks(co.filename, 3)
self.assertEqual(2, len(list(x.ls())))
self.assertTrue(co.exists)
self.assertEqual(co.size, 2)
self.assertEqual(len(co), co.size)
def test3_1_cleanup(self):
x = cashe.CAShe(self.tdir + "/test3")
def _put(d, tm):
datai = x.path + "/data3.1" + d
fwrite(datai, d)
co = x.get('sha256', d2s[d])
co.save(datai, link=False)
# We need ordering
os.utime(co.filename, (tm, tm))
del co.size
now = time.time() - 100
for num in range(1, 9):
_put("a"*num, now + num)
_put("b"*num, now + num + 0.5)
fwrite(x.path + "/config", "lo = 40 \n hi = 40\n")
self.assertEqual(16, len(list(x.ls())))
self.assertEqual((40, 40, 8 * 60 * 60 * 24, "atime"), x._get_config())
x.cleanup()
self.assertEqual(5, len(list(x.ls())))
fwrite(x.path + "/config", "lo = 4 \n hi = 4\n")
self.assertEqual((4, 4, 8 * 60 * 60 * 24, "atime"), x._get_config())
x.cleanup()
self.assertEqual(0, len(list(x.ls())))
def test3_2_cleanup(self):
x = cashe.CAShe(self.tdir + "/test3")
def _put(d, tm):
datai = x.path + "/data3.1" + d
fwrite(datai, d)
co = x.get('sha256', d2s[d])
co.save(datai, link=False)
# We need ordering
os.utime(co.filename, (tm, tm))
del co.size
now = time.time() - 100
for num in range(1, 9):
_put("a"*num, (now - num) + 0.5)
_put("b"*num, now - num)
fwrite(x.path + "/config", "lo = 40 \n hi = 40\n")
self.assertEqual(16, len(list(x.ls())))
self.assertEqual((40, 40, 8 * 60 * 60 * 24, "atime"), x._get_config())
x.cleanup()
self.assertEqual(11, len(list(x.ls())))
fwrite(x.path + "/config", "lo = 4 \n hi = 4\n")
self.assertEqual((4, 4, 8 * 60 * 60 * 24, "atime"), x._get_config())
x.cleanup()
self.assertEqual(3, len(list(x.ls())))
def test_reg1(self):
x = cashe.CAShe(self.tdir + "/test-reg1")
co = x.get('sha256', d2s['aa'])
self.assertEqual(co.size, 0)
self.assertEqual(len(co), co.size)
self.assertTrue(co)
self.assertFalse(co.exists)
self.assertEqual(0, len(list(x.ls())))
# Main
if __name__ == '__main__':
unittest.main()