def test_default_bagging_date(self): info = {'Contact-Email': '*****@*****.**'} bagit.make_bag(self.tmpdir, bag_info=info) bag_info_txt = slurp_text_file(j(self.tmpdir, 'bag-info.txt')) 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)
def test_make_bag_with_empty_directory(self): logger.info(self.getTestHeader(sys._getframe().f_code.co_name)) tmpdir = tempfile.mkdtemp() try: bagit.make_bag(tmpdir) finally: shutil.rmtree(tmpdir)
def test_make_bag_with_data_dir_present(self): logger.info(self.getTestHeader(sys._getframe().f_code.co_name)) os.mkdir(j(self.tmpdir, 'data')) 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_make_bag(self): info = { 'Bagging-Date': '1970-01-01', 'Contact-Email': '*****@*****.**', 'Bag-Software-Agent': 'bagit.py v1.5.4 <https://github.com/LibraryOfCongress/bagit-python>' } bagit.make_bag(self.tmpdir, bag_info=info, checksums=['md5']) # 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 = slurp_text_file(j(self.tmpdir, 'bagit.txt')) self.assertTrue('BagIt-Version: 0.97', bagit_txt) self.assertTrue('Tag-File-Character-Encoding: UTF-8', bagit_txt) # check manifest self.assertTrue(os.path.isfile(j(self.tmpdir, 'manifest-md5.txt'))) manifest_txt = slurp_text_file(j(self.tmpdir, 'manifest-md5.txt')).splitlines() self.assertIn('8e2af7a0143c7b8f4de0b3fc90f27354 data/README', manifest_txt) self.assertIn( '9a2b89e9940fea6ac3a0cc71b0a933a0 data/loc/2478433644_2839c5e8b8_o_d.jpg', manifest_txt) self.assertIn( '6172e980c2767c12135e3b9d246af5a3 data/loc/3314493806_6f1db86d66_o_d.jpg', manifest_txt) self.assertIn( '38a84cd1c41de793a0bccff6f3ec8ad0 data/si/2584174182_ffd5c24905_b_d.jpg', manifest_txt) self.assertIn( '5580eaa31ad1549739de12df819e9af8 data/si/4011399822_65987a4806_b_d.jpg', manifest_txt) # check bag-info.txt self.assertTrue(os.path.isfile(j(self.tmpdir, 'bag-info.txt'))) bag_info_txt = slurp_text_file(j(self.tmpdir, 'bag-info.txt')) bag_info_txt = bag_info_txt.splitlines() self.assertIn('Contact-Email: [email protected]', bag_info_txt) self.assertIn('Bagging-Date: 1970-01-01', bag_info_txt) self.assertIn('Payload-Oxum: 991765.5', bag_info_txt) self.assertIn( 'Bag-Software-Agent: bagit.py v1.5.4 <https://github.com/LibraryOfCongress/bagit-python>', bag_info_txt) # check tagmanifest-md5.txt self.assertTrue(os.path.isfile(j(self.tmpdir, 'tagmanifest-md5.txt'))) tagmanifest_txt = slurp_text_file(j( self.tmpdir, 'tagmanifest-md5.txt')).splitlines() self.assertIn('9e5ad981e0d29adc278f6a294b8c2aca bagit.txt', tagmanifest_txt) self.assertIn('a0ce6631a2a6d1a88e6d38453ccc72a5 manifest-md5.txt', tagmanifest_txt) self.assertIn('0a6ffcffe67e9a34e44220f7ebcb4baa bag-info.txt', tagmanifest_txt)
def test_make_bag_with_bogus_directory(self): bogus_directory = os.path.realpath('this-directory-does-not-exist') with self.assertRaises(RuntimeError) as error_catcher: bagit.make_bag(bogus_directory) self.assertEqual('Bag directory %s does not exist' % bogus_directory, str(error_catcher.exception))
def test_make_bag_with_empty_directory_tree(self): tmpdir = tempfile.mkdtemp() path = j(tmpdir, "test1", "test2") try: os.makedirs(path) bagit.make_bag(tmpdir) finally: shutil.rmtree(tmpdir)
def test_default_bagging_date(self): logger.info(self.getTestHeader(sys._getframe().f_code.co_name)) info = {'Contact-Email': '*****@*****.**'} bagit.make_bag(self.tmpdir, bag_info=info) bag_info_txt = slurp_text_file(j(self.tmpdir, 'bag-info.txt')) 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)
def test_make_bag_with_unreadable_file(self): os.chmod(j(self.tmpdir, 'loc', '2478433644_2839c5e8b8_o_d.jpg'), 0) with self.assertRaises(bagit.BagError) as error_catcher: bagit.make_bag(self.tmpdir, checksums=['sha256']) self.assertEqual( 'Read permissions are required to calculate file fixities', str(error_catcher.exception))
def test_garbage_in_bagit_txt(self): bagit.make_bag(self.tmpdir) bagfile = """BagIt-Version: 0.97 Tag-File-Character-Encoding: UTF-8 ================================== """ with open(j(self.tmpdir, "bagit.txt"), "w") as bf: bf.write(bagfile) self.assertRaises(bagit.BagValidationError, bagit.BDBag, self.tmpdir)
def test_make_bag_with_empty_directory_tree(self): logger.info(self.getTestHeader(sys._getframe().f_code.co_name)) tmpdir = tempfile.mkdtemp() path = j(tmpdir, "test1", "test2") try: os.makedirs(path) bagit.make_bag(tmpdir) finally: shutil.rmtree(tmpdir)
def test_make_bag_with_unreadable_source(self): os.chmod(self.tmpdir, 0) with self.assertRaises(bagit.BagError) as error_catcher: bagit.make_bag(self.tmpdir, checksums=['sha256']) self.assertEqual( 'Missing permissions to move all files and directories', str(error_catcher.exception))
def test_make_bag_with_bogus_directory(self): logger.info(self.getTestHeader(sys._getframe().f_code.co_name)) bogus_directory = os.path.realpath('this-directory-does-not-exist') with self.assertRaises(RuntimeError) as error_catcher: bagit.make_bag(bogus_directory) self.assertEqual('Bag directory %s does not exist' % bogus_directory, str(error_catcher.exception))
def test_garbage_in_bagit_txt(self): logger.info(self.getTestHeader(sys._getframe().f_code.co_name)) bagit.make_bag(self.tmpdir) bagfile = """BagIt-Version: 0.97 Tag-File-Character-Encoding: UTF-8 ================================== """ with open(j(self.tmpdir, "bagit.txt"), "w") as bf: bf.write(bagfile) self.assertRaises(bagit.BagValidationError, bagit.BDBag, self.tmpdir)
def test_make_bag_with_unreadable_subdirectory(self): # We'll set this write-only to exercise the second permission check in make_bag: os.chmod(j(self.tmpdir, 'loc'), 0o200) with self.assertRaises(bagit.BagError) as error_catcher: bagit.make_bag(self.tmpdir, checksums=['sha256']) self.assertEqual( 'Read permissions are required to calculate file fixities', str(error_catcher.exception))
def test_make_bag_with_unreadable_source(self): logger.info(self.getTestHeader(sys._getframe().f_code.co_name)) os.chmod(self.tmpdir, 0) with self.assertRaises(bagit.BagError) as error_catcher: bagit.make_bag(self.tmpdir, checksums=['sha256']) self.assertEqual( 'Missing permissions to move all files and directories', str(error_catcher.exception))
def test_open_bag_with_missing_bagit_txt(self): bagit.make_bag(self.tmpdir) os.unlink(j(self.tmpdir, 'bagit.txt')) with self.assertRaises(bagit.BagError) as error_catcher: bagit.BDBag(self.tmpdir) self.assertEqual( 'Expected bagit.txt does not exist: %s' % j(self.tmpdir, "bagit.txt"), str(error_catcher.exception))
def test_open_bag_with_unsupported_version(self): bagit.make_bag(self.tmpdir) with open(j(self.tmpdir, 'bagit.txt'), 'w') as f: f.write('BagIt-Version: 2.0\nTag-File-Character-Encoding: UTF-8\n') with self.assertRaises(bagit.BagError) as error_catcher: bagit.BDBag(self.tmpdir) self.assertEqual('Unsupported bag version: 2.0', str(error_catcher.exception))
def test_open_bag_with_missing_bagit_txt(self): logger.info(self.getTestHeader(sys._getframe().f_code.co_name)) bagit.make_bag(self.tmpdir) os.unlink(j(self.tmpdir, 'bagit.txt')) with self.assertRaises(bagit.BagError) as error_catcher: bagit.BDBag(self.tmpdir) self.assertEqual( 'Expected bagit.txt does not exist: %s' % j(self.tmpdir, "bagit.txt"), str(error_catcher.exception))
def test_unicode_bag_info(self): info = { 'Test-BMP': u'This element contains a \N{LATIN SMALL LETTER U WITH DIAERESIS}', 'Test-SMP': u'This element contains a \N{LINEAR B SYMBOL B049}', } bagit.make_bag(self.tmpdir, bag_info=info, checksums=['md5']) bag_info_txt = slurp_text_file(j(self.tmpdir, 'bag-info.txt')) for v in info.values(): self.assertIn(v, bag_info_txt)
def test_open_bag_with_malformed_bagit_txt(self): bagit.make_bag(self.tmpdir) with open(j(self.tmpdir, 'bagit.txt'), 'w') as f: os.ftruncate(f.fileno(), 0) with self.assertRaises(bagit.BagError) as error_catcher: bagit.BDBag(self.tmpdir) self.assertEqual( 'Missing required tag in bagit.txt: BagIt-Version, Tag-File-Character-Encoding', str(error_catcher.exception))
def test_make_bag_with_unwritable_source(self): path_suffixes = ('', 'loc') for path_suffix in reversed(path_suffixes): os.chmod(j(self.tmpdir, path_suffix), 0o500) with self.assertRaises(bagit.BagError) as error_catcher: bagit.make_bag(self.tmpdir, checksums=['sha256']) self.assertEqual( 'Missing permissions to move all files and directories', str(error_catcher.exception))
def test_validate_missing_directory(self): bagit.make_bag(self.tmpdir) tmp_data_dir = os.path.join(self.tmpdir, 'data') shutil.rmtree(tmp_data_dir) bag = bagit.BDBag(self.tmpdir) with self.assertRaises(bagit.BagValidationError) as error_catcher: bag.validate() self.assertEqual( 'Expected data directory %s does not exist' % tmp_data_dir, str(error_catcher.exception))
def test_open_bag_with_unknown_encoding(self): logger.info(self.getTestHeader(sys._getframe().f_code.co_name)) bagit.make_bag(self.tmpdir) with open(j(self.tmpdir, 'bagit.txt'), 'w') as f: f.write( 'BagIt-Version: 0.97\nTag-File-Character-Encoding: WTF-8\n') with self.assertRaises(bagit.BagError) as error_catcher: bagit.BDBag(self.tmpdir) self.assertEqual('Unsupported encoding: WTF-8', str(error_catcher.exception))
def test_validate_missing_directory(self): logger.info(self.getTestHeader(sys._getframe().f_code.co_name)) bagit.make_bag(self.tmpdir) tmp_data_dir = os.path.join(self.tmpdir, 'data') shutil.rmtree(tmp_data_dir) bag = bagit.BDBag(self.tmpdir) with self.assertRaises(bagit.BagValidationError) as error_catcher: bag.validate() self.assertEqual( 'Expected data directory %s does not exist' % tmp_data_dir, str(error_catcher.exception))
def test_payload_permissions(self): perms = os.stat(self.tmpdir).st_mode # our tmpdir should not be writeable by group self.assertEqual(perms & stat.S_IWOTH, 0) # but if we make it writeable by the group then resulting # payload directory should have the same permissions new_perms = perms | stat.S_IWOTH self.assertTrue(perms != new_perms) os.chmod(self.tmpdir, new_perms) bagit.make_bag(self.tmpdir) payload_dir = j(self.tmpdir, 'data') self.assertEqual(os.stat(payload_dir).st_mode, new_perms)
def test_open_bag_with_invalid_versions(self): bagit.make_bag(self.tmpdir) for v in ('a.b', '2.', '0.1.2', '1.2.3'): with open(j(self.tmpdir, 'bagit.txt'), 'w') as f: f.write( 'BagIt-Version: %s\nTag-File-Character-Encoding: UTF-8\n' % v) with self.assertRaises(bagit.BagError) as error_catcher: bagit.BDBag(self.tmpdir) self.assertEqual( 'Bag version numbers must be MAJOR.MINOR numbers, not %s' % v, str(error_catcher.exception))
def test_make_bag_md5_sha256_manifest(self): bag = bagit.make_bag(self.tmpdir, checksums=['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(self.validate(bag, fast=True))
def test_validate_optional_tagfile_in_directory(self): bag = bagit.make_bag(self.tmpdir, checksums=['md5']) tagdir = tempfile.mkdtemp(dir=self.tmpdir) if not os.path.exists(j(tagdir, "tagfolder")): os.makedirs(j(tagdir, "tagfolder")) with open(j(tagdir, "tagfolder", "tagfile"), "w") as tagfile: tagfile.write("test") relpath = j(tagdir, "tagfolder", "tagfile").replace(self.tmpdir + os.sep, "") relpath.replace("\\", "/") with open(j(self.tmpdir, "tagmanifest-md5.txt"), "w") as tagman: # Incorrect checksum. tagman.write("8e2af7a0143c7b8f4de0b3fc90f27354 " + relpath + "\n") bag = bagit.BDBag(self.tmpdir) self.assertRaises(bagit.BagValidationError, self.validate, bag) hasher = hashlib.new("md5") with open(j(tagdir, "tagfolder", "tagfile"), "r") as tf: contents = tf.read().encode('utf-8') hasher.update(contents) with open(j(self.tmpdir, "tagmanifest-md5.txt"), "w") as tagman: tagman.write(hasher.hexdigest() + " " + relpath + "\n") bag = bagit.BDBag(self.tmpdir) self.assertTrue(self.validate(bag)) # Missing tagfile. os.remove(j(tagdir, "tagfolder", "tagfile")) bag = bagit.BDBag(self.tmpdir) self.assertRaises(bagit.BagValidationError, self.validate, bag)
def test_is_valid(self): bag = bagit.make_bag(self.tmpdir) bag = bagit.BDBag(self.tmpdir) self.assertTrue(bag.is_valid()) with open(j(self.tmpdir, "data", "extra_file"), "w") as ef: ef.write("bar") self.assertFalse(bag.is_valid())
def test_validate_unreadable_file(self): bag = bagit.make_bag(self.tmpdir, checksums=["md5"]) os.chmod(j(self.tmpdir, "data/loc/2478433644_2839c5e8b8_o_d.jpg"), 0) self.assertRaises(bagit.BagValidationError, self.validate, bag, fast=False)