forked from LibraryOfCongress/bagit-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
361 lines (304 loc) · 16.3 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
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
import os
import shutil
import logging
import datetime
import tempfile
import unittest
import codecs
import hashlib
from os.path import join as j
import bagit
# don't let < ERROR clutter up test output
logging.basicConfig(level=logging.ERROR)
class TestBag(unittest.TestCase):
def setUp(self):
self.tmpdir = tempfile.mkdtemp()
if os.path.isdir(self.tmpdir):
shutil.rmtree(self.tmpdir)
shutil.copytree('test-data', self.tmpdir)
def tearDown(self):
if os.path.isdir(self.tmpdir):
shutil.rmtree(self.tmpdir)
def test_make_bag(self):
info = {'Bagging-Date': '1970-01-01', 'Contact-Email': '[email protected]'}
bag = bagit.make_bag(self.tmpdir, bag_info=info)
# data dir should've been created
self.assertTrue(os.path.isdir(j(self.tmpdir, 'data')))
# check bagit.txt
self.assertTrue(os.path.isfile(j(self.tmpdir, 'bagit.txt')))
bagit_txt = open(j(self.tmpdir, 'bagit.txt')).read()
self.assertTrue('BagIt-Version: 0.97' in bagit_txt)
self.assertTrue('Tag-File-Character-Encoding: UTF-8' in bagit_txt)
# check manifest
self.assertTrue(os.path.isfile(j(self.tmpdir, 'manifest-md5.txt')))
manifest_txt = open(j(self.tmpdir, 'manifest-md5.txt')).read()
self.assertTrue('8e2af7a0143c7b8f4de0b3fc90f27354 data/README' in manifest_txt)
self.assertTrue('9a2b89e9940fea6ac3a0cc71b0a933a0 data/loc/2478433644_2839c5e8b8_o_d.jpg' in manifest_txt)
self.assertTrue('6172e980c2767c12135e3b9d246af5a3 data/loc/3314493806_6f1db86d66_o_d.jpg' in manifest_txt)
self.assertTrue('38a84cd1c41de793a0bccff6f3ec8ad0 data/si/2584174182_ffd5c24905_b_d.jpg' in manifest_txt)
self.assertTrue('5580eaa31ad1549739de12df819e9af8 data/si/4011399822_65987a4806_b_d.jpg' in manifest_txt)
# check bag-info.txt
self.assertTrue(os.path.isfile(j(self.tmpdir, 'bag-info.txt')))
bag_info_txt = open(j(self.tmpdir, 'bag-info.txt')).read()
self.assertTrue('Contact-Email: [email protected]' in bag_info_txt)
self.assertTrue('Bagging-Date: 1970-01-01' in bag_info_txt)
self.assertTrue('Payload-Oxum: 991765.5' in bag_info_txt)
self.assertTrue('Bag-Software-Agent: bagit.py <http://github.com/libraryofcongress/bagit-python>' in bag_info_txt)
# check tagmanifest-md5.txt
self.assertTrue(os.path.isfile(j(self.tmpdir, 'tagmanifest-md5.txt')))
tagmanifest_txt = open(j(self.tmpdir, 'tagmanifest-md5.txt')).read()
self.assertTrue('9e5ad981e0d29adc278f6a294b8c2aca bagit.txt' in tagmanifest_txt)
self.assertTrue('a0ce6631a2a6d1a88e6d38453ccc72a5 manifest-md5.txt' in tagmanifest_txt)
self.assertTrue('6a5090e27cb29d5dda8a0142fbbdf37e bag-info.txt' in tagmanifest_txt)
def test_make_bag_sha1_manifest(self):
bag = bagit.make_bag(self.tmpdir, checksum=['sha1'])
# check manifest
self.assertTrue(os.path.isfile(j(self.tmpdir, 'manifest-sha1.txt')))
manifest_txt = open(j(self.tmpdir, 'manifest-sha1.txt')).read()
self.assertTrue('ace19416e605cfb12ab11df4898ca7fd9979ee43 data/README' in manifest_txt)
self.assertTrue('4c0a3da57374e8db379145f18601b159f3cad44b data/loc/2478433644_2839c5e8b8_o_d.jpg' in manifest_txt)
self.assertTrue('62095aeddae2f3207cb77c85937e13c51641ef71 data/loc/3314493806_6f1db86d66_o_d.jpg' in manifest_txt)
self.assertTrue('e592194b3733e25166a631e1ec55bac08066cbc1 data/si/2584174182_ffd5c24905_b_d.jpg' in manifest_txt)
self.assertTrue('db49ef009f85a5d0701829f38d29f8cf9c5df2ea data/si/4011399822_65987a4806_b_d.jpg' in manifest_txt)
def test_make_bag_sha256_manifest(self):
bag = bagit.make_bag(self.tmpdir, checksum=['sha256'])
# check manifest
self.assertTrue(os.path.isfile(j(self.tmpdir, 'manifest-sha256.txt')))
manifest_txt = open(j(self.tmpdir, 'manifest-sha256.txt')).read()
self.assertTrue('b6df8058fa818acfd91759edffa27e473f2308d5a6fca1e07a79189b95879953 data/loc/2478433644_2839c5e8b8_o_d.jpg' in manifest_txt)
self.assertTrue('1af90c21e72bb0575ae63877b3c69cfb88284f6e8c7820f2c48dc40a08569da5 data/loc/3314493806_6f1db86d66_o_d.jpg' in manifest_txt)
self.assertTrue('f065a4ae2bc5d47c6d046c3cba5c8cdfd66b07c96ff3604164e2c31328e41c1a data/si/2584174182_ffd5c24905_b_d.jpg' in manifest_txt)
self.assertTrue('45d257c93e59ec35187c6a34c8e62e72c3e9cfbb548984d6f6e8deb84bac41f4 data/si/4011399822_65987a4806_b_d.jpg' in manifest_txt)
def test_make_bag_sha1_sha256_manifest(self):
bag = bagit.make_bag(self.tmpdir, checksum=['sha1', 'sha256'])
# check that relevant manifests are created
self.assertTrue(os.path.isfile(j(self.tmpdir, 'manifest-sha1.txt')))
self.assertTrue(os.path.isfile(j(self.tmpdir, 'manifest-sha256.txt')))
# check valid with two manifests
self.assertTrue(bag.validate(fast=True))
def test_make_bag_md5_sha256_manifest(self):
bag = bagit.make_bag(self.tmpdir, checksum=['md5', 'sha256'])
# check that relevant manifests are created
self.assertTrue(os.path.isfile(j(self.tmpdir, 'manifest-md5.txt')))
self.assertTrue(os.path.isfile(j(self.tmpdir, 'manifest-sha256.txt')))
# check valid with two manifests
self.assertTrue(bag.validate(fast=True))
def test_make_bag_md5_sha1_sha256_manifest(self):
bag = bagit.make_bag(self.tmpdir, checksum=['md5', 'sha1', 'sha256'])
# check that relevant manifests are created
self.assertTrue(os.path.isfile(j(self.tmpdir, 'manifest-md5.txt')))
self.assertTrue(os.path.isfile(j(self.tmpdir, 'manifest-sha1.txt')))
self.assertTrue(os.path.isfile(j(self.tmpdir, 'manifest-sha256.txt')))
# check valid with three manifests
self.assertTrue(bag.validate(fast=True))
def test_make_bag_with_data_dir_present(self):
os.mkdir(j(self.tmpdir, 'data'))
bag = bagit.make_bag(self.tmpdir)
# data dir should now contain another data dir
self.assertTrue(os.path.isdir(j(self.tmpdir, 'data', 'data')))
def test_bag_class(self):
info = {'Contact-Email': '[email protected]'}
bag = bagit.make_bag(self.tmpdir, bag_info=info)
self.assertTrue(isinstance(bag, bagit.Bag))
self.assertEqual(set(bag.payload_files()), set([
'data/README',
'data/si/2584174182_ffd5c24905_b_d.jpg',
'data/si/4011399822_65987a4806_b_d.jpg',
'data/loc/2478433644_2839c5e8b8_o_d.jpg',
'data/loc/3314493806_6f1db86d66_o_d.jpg']))
self.assertEqual(list(bag.manifest_files()), ['%s/manifest-md5.txt' %
self.tmpdir])
def test_has_oxum(self):
bag = bagit.make_bag(self.tmpdir)
self.assertTrue(bag.has_oxum())
def test_bag_constructor(self):
bag = bagit.make_bag(self.tmpdir)
bag = bagit.Bag(self.tmpdir)
self.assertEqual(type(bag), bagit.Bag)
self.assertEqual(len(list(bag.payload_files())), 5)
def test_validate_flipped_bit(self):
bag = bagit.make_bag(self.tmpdir)
readme = os.path.join(self.tmpdir, "data", "README")
txt = open(readme).read()
txt = 'A' + txt[1:]
open(readme, "w").write(txt)
bag = bagit.Bag(self.tmpdir)
self.assertRaises(bagit.BagValidationError, bag.validate)
# fast doesn't catch the flipped bit, since oxsum is the same
self.assertTrue(bag.validate(fast=True))
def test_validate_fast(self):
bag = bagit.make_bag(self.tmpdir)
self.assertEqual(bag.validate(fast=True), True)
os.remove(os.path.join(self.tmpdir, "data", "loc",
"2478433644_2839c5e8b8_o_d.jpg"))
self.assertRaises(bagit.BagValidationError, bag.validate, fast=True)
def test_validate_fast_without_oxum(self):
bag = bagit.make_bag(self.tmpdir)
os.remove(os.path.join(self.tmpdir, "bag-info.txt"))
bag = bagit.Bag(self.tmpdir)
self.assertRaises(bagit.BagValidationError, bag.validate, fast=True)
def test_validate_slow_without_oxum_extra_file(self):
bag = bagit.make_bag(self.tmpdir)
os.remove(os.path.join(self.tmpdir, "bag-info.txt"))
open(os.path.join(self.tmpdir, "data", "extra_file"), "w").write("foo")
bag = bagit.Bag(self.tmpdir)
self.assertRaises(bagit.BagValidationError, bag.validate, fast=False)
def test_validation_error_details(self):
bag = bagit.make_bag(self.tmpdir)
readme = os.path.join(self.tmpdir, "data", "README")
txt = open(readme).read()
txt = 'A' + txt[1:]
open(readme, "w").write(txt)
extra_file = os.path.join(self.tmpdir, "data", "extra")
open(extra_file, "w").write('foo')
# remove the bag-info.txt which contains the oxum to force a full
# check of the manifest
os.remove(os.path.join(self.tmpdir, "bag-info.txt"))
bag = bagit.Bag(self.tmpdir)
got_exception = False
try:
bag.validate()
except bagit.BagValidationError, e:
got_exception = True
self.assertEqual(str(e), "invalid bag: bag-info.txt exists in manifest but not found on filesystem ; data/extra exists on filesystem but is not in manifest ; data/README checksum validation failed (alg=md5 expected=8e2af7a0143c7b8f4de0b3fc90f27354 found=fd41543285d17e7c29cd953f5cf5b955)")
self.assertEqual(len(e.details), 3)
error = e.details[0]
self.assertEqual(str(error), "bag-info.txt exists in manifest but not found on filesystem")
self.assertTrue(isinstance(error, bagit.FileMissing))
self.assertEqual(error.path, "bag-info.txt")
error = e.details[1]
self.assertEqual(str(error), "data/extra exists on filesystem but is not in manifest")
self.assertTrue(isinstance(error, bagit.UnexpectedFile))
self.assertEqual(error.path, "data/extra")
error = e.details[2]
self.assertEqual(str(error), "data/README checksum validation failed (alg=md5 expected=8e2af7a0143c7b8f4de0b3fc90f27354 found=fd41543285d17e7c29cd953f5cf5b955)")
self.assertTrue(isinstance(error, bagit.ChecksumMismatch))
self.assertEqual(error.algorithm, 'md5')
self.assertEqual(error.path, 'data/README')
self.assertEqual(error.expected, '8e2af7a0143c7b8f4de0b3fc90f27354')
self.assertEqual(error.found, 'fd41543285d17e7c29cd953f5cf5b955')
if not got_exception:
self.fail("didn't get BagValidationError")
def test_is_valid(self):
bag = bagit.make_bag(self.tmpdir)
bag = bagit.Bag(self.tmpdir)
self.assertTrue(bag.is_valid())
open(os.path.join(self.tmpdir, "data", "extra_file"), "w").write("bar")
self.assertFalse(bag.is_valid())
def test_bom_in_bagit_txt(self):
bag = bagit.make_bag(self.tmpdir)
bagfile = codecs.BOM_UTF8
bagfile += open(os.path.join(self.tmpdir, "bagit.txt"), "rb").read()
bf = open(os.path.join(self.tmpdir, "bagit.txt"), "wb")
bf.write(bagfile)
bf.close()
bag = bagit.Bag(self.tmpdir)
self.assertRaises(bagit.BagValidationError, bag.validate)
def test_missing_file(self):
bag = bagit.make_bag(self.tmpdir)
os.remove(j(self.tmpdir, 'data', 'loc', '3314493806_6f1db86d66_o_d.jpg'))
self.assertRaises(bagit.BagValidationError, bag.validate)
def test_handle_directory_end_slash_gracefully(self):
bag = bagit.make_bag(self.tmpdir + '/')
self.assertTrue(bag.validate())
bag2 = bagit.Bag(self.tmpdir + '/')
self.assertTrue(bag2.validate())
def test_allow_extraneous_files_in_base(self):
bag = bagit.make_bag(self.tmpdir)
self.assertTrue(bag.validate())
f = os.path.join(self.tmpdir, "IGNOREFILE")
open(f, 'w')
self.assertTrue(bag.validate())
def test_allow_extraneous_dirs_in_base(self):
bag = bagit.make_bag(self.tmpdir)
self.assertTrue(bag.validate())
d = os.path.join(self.tmpdir, "IGNOREDIR")
os.mkdir(d)
self.assertTrue(bag.validate())
def test_missing_tagfile_raises_error(self):
bag = bagit.make_bag(self.tmpdir)
self.assertTrue(bag.validate())
os.remove(os.path.join(self.tmpdir, "bagit.txt"))
self.assertRaises(bagit.BagValidationError, bag.validate)
def test_missing_manifest_raises_error(self):
bag = bagit.make_bag(self.tmpdir)
self.assertTrue(bag.validate())
os.remove(os.path.join(self.tmpdir, "manifest-md5.txt"))
self.assertRaises(bagit.BagValidationError, bag.validate)
def test_make_bag_multiprocessing(self):
bag = bagit.make_bag(self.tmpdir, processes=2)
self.assertTrue(os.path.isdir(j(self.tmpdir, 'data')))
def test_mixed_case_checksums(self):
bag = bagit.make_bag(self.tmpdir)
hashstr = {}
#Extract entries only for the payload and ignore
# entries from the tagmanifest file
for key in bag.entries.iterkeys():
if key.startswith('data' + os.sep):
hashstr = bag.entries[key]
hashstr = hashstr.itervalues().next()
manifest = open(os.path.join(self.tmpdir, "manifest-md5.txt"),
"r").read()
manifest = manifest.replace(hashstr, hashstr.upper())
open(os.path.join(self.tmpdir, "manifest-md5.txt"),
"w").write(manifest)
#Since manifest-md5.txt file is updated, re-calculate its
# md5 checksum and update it in the tagmanifest-md5.txt file
hasher = hashlib.new('md5')
hasher.update(open(os.path.join(self.tmpdir, "manifest-md5.txt"),
"r").read())
tagmanifest = open(os.path.join(self.tmpdir, "tagmanifest-md5.txt"),
"r").read()
tagmanifest = tagmanifest.replace(
bag.entries['manifest-md5.txt']['md5'], hasher.hexdigest())
open(os.path.join(self.tmpdir, "tagmanifest-md5.txt"),
"w").write(tagmanifest)
bag = bagit.Bag(self.tmpdir)
self.assertTrue(bag.validate())
def test_multiple_oxum_values(self):
bag = bagit.make_bag(self.tmpdir)
baginfo = open(os.path.join(self.tmpdir, "bag-info.txt"), "a")
baginfo.write('Payload-Oxum: 7.7\n')
baginfo.close()
bag = bagit.Bag(self.tmpdir)
self.assertTrue(bag.validate(fast=True))
def test_multiple_meta_values(self):
baginfo = {"Multival-Meta": [7, 4, 8, 6, 8]}
bag = bagit.make_bag(self.tmpdir, baginfo)
meta = bag.info.get("Multival-Meta")
self.assertEqual(type(meta), list)
self.assertEqual(len(meta), len(baginfo["Multival-Meta"]))
def test_validate_optional_tagfile(self):
bag = bagit.make_bag(self.tmpdir)
tagdir = tempfile.mkdtemp(dir=self.tmpdir)
tagfile = open(os.path.join(tagdir, "tagfile"), "w")
tagfile.write("test")
tagfile.close()
relpath = os.path.join(tagdir, "tagfile").replace(self.tmpdir + os.sep, "")
relpath.replace("\\", "/")
tagman = open(os.path.join(self.tmpdir, "tagmanifest-md5.txt"), "w")
# Incorrect checksum.
tagman.write("8e2af7a0143c7b8f4de0b3fc90f27354 " + relpath + "\n")
tagman.close()
bag = bagit.Bag(self.tmpdir)
self.assertRaises(bagit.BagValidationError, bag.validate)
hasher = hashlib.new("md5")
hasher.update(open(os.path.join(tagdir, "tagfile"), "rb").read())
tagman = open(os.path.join(self.tmpdir, "tagmanifest-md5.txt"), "w")
tagman.write(hasher.hexdigest() + " " + relpath + "\n")
tagman.close()
bag = bagit.Bag(self.tmpdir)
self.assertTrue(bag.validate())
# Missing tagfile.
os.remove(os.path.join(tagdir, "tagfile"))
bag = bagit.Bag(self.tmpdir)
self.assertRaises(bagit.BagValidationError, bag.validate)
def test_default_bagging_date(self):
info = {'Contact-Email': '[email protected]'}
bag = bagit.make_bag(self.tmpdir, bag_info=info)
bag_info_txt = open(j(self.tmpdir, 'bag-info.txt')).read()
self.assertTrue('Contact-Email: [email protected]' in bag_info_txt)
today = datetime.date.strftime(datetime.date.today(), "%Y-%m-%d")
self.assertTrue('Bagging-Date: %s' % today in bag_info_txt)
if __name__ == '__main__':
unittest.main()