def setUp(self): self.tree = FakeTree() self.tree._add_file( ('dbpath', '2015', '04-03T10:46'), FakeFile(testdata.dbfiledata('backup-1'))) self.db = FakeDatabase(self.tree, ('dbpath',)) self.bk = backupinfo.BackupInfo(self.db, '2015-04-03T10:46')
def test_open_backup_with_wrong_name(self): tree = FakeTree() tree._add_file( ('path', 'to', 'db', '2015', '04-03T10:45'), testdata.dbfiledata('backup-1')) self.assertRaisesRegex( datafile.InvalidDataError, 'non-matching start time.*10:46.*10:45', datafile.open_backup_by_name, tree, ('path', 'to', 'db'), '2015-04-03T10:45')
def test_open_content_when_already_opened(self): tree = FakeTree() tree._add_file( ('path', 'to', 'db', 'content'), testdata.dbfiledata('content-1')) content = datafile.get_unopened_content(tree, ('path', 'to', 'db')) content.open_and_lock_readonly() self.assertRaisesRegex( AssertionError, 'already open', content.open_and_lock_readonly)
def test_access_content_without_opening_it(self): tree = FakeTree() tree._add_file( ('path', 'to', 'db', 'content'), testdata.dbfiledata('content-1')) content = datafile.get_unopened_content(tree, ('path', 'to', 'db')) self.assertRaisesRegex(AssertionError, 'is not open', next, content) self.assertRaisesRegex( AssertionError, 'is not open', content.append_item, datafile.ItemSetting(b'key', b'value'))
def test_main_with_non_matching_checksum(self): dbdata = testdata.dbfiledata('main-1') self.assertEqual(4096, len(dbdata)) dbdata = dbdata[:-3] + b'xxx' tree = FakeTree() tree._add_file( ('path', 'to', 'db', 'main'), dbdata) self.assertRaisesRegex( datafile.InvalidDataError, 'hecksum mismatch', datafile.open_main, tree, ('path', 'to', 'db')) self.assertCountEqual((), tree._files_modified)
def test_read_simple_backup(self): expect = StandardItemData() expect.load_backup_1() tree = FakeTree() tree._add_file( ('path', 'to', 'db', '2015', '04-03T10:46'), testdata.dbfiledata('backup-1')) backup = datafile.open_backup_by_name( tree, ('path', 'to', 'db'), '2015-04-03T10:46') self.assertItemSequence(expect.items, backup) self.assertRaises(StopIteration, next, backup) backup.close() self.assertCountEqual((), tree._files_modified)
def test_read_typical_content_db(self): tree = FakeTree() tree._add_file( ('path', 'to', 'db', 'content'), testdata.dbfiledata('content-1')) expect = StandardItemData() expect.load_content_1() content = datafile.open_content(tree, ('path', 'to', 'db')) self.assertItemSequence(expect.items, content) self.assertRaises(StopIteration, next, content) self.assertRaises(StopIteration, next, content) self.assertRaises(StopIteration, next, content) content.close() self.assertCountEqual((), tree._files_modified)
def test_access_content_after_closing_it(self): tree = FakeTree() tree._add_file( ('path', 'to', 'db', 'content'), testdata.dbfiledata('content-1')) content = datafile.get_unopened_content(tree, ('path', 'to', 'db')) content.open_and_lock_readonly() item = next(content) self.assertEqual('magic', item.kind) self.assertEqual(b'ebakup content data', item.value) content.close() self.assertRaisesRegex(AssertionError, 'is not open', next, content) self.assertRaisesRegex( AssertionError, 'is not open', content.append_item, datafile.ItemSetting(b'key', b'value'))
def test_read_typical_main(self): expect = ( {'kind': 'magic', 'value': b'ebakup database v1'}, {'kind': 'setting', 'key': b'edb-blocksize', 'value': b'4096'}, {'kind': 'setting', 'key': b'edb-blocksum', 'value': b'sha256'}, {'kind': 'setting', 'key': b'checksum', 'value': b'sha256'} ) tree = FakeTree() tree._add_file( ('path', 'to', 'db', 'main'), testdata.dbfiledata('main-1')) main = datafile.open_main(tree, ('path', 'to', 'db')) self.assertItemSequence(expect, main) self.assertRaises(StopIteration, next, main) self.assertRaises(StopIteration, next, main) self.assertRaises(StopIteration, next, main) main.close() self.assertCountEqual((), tree._files_modified)
def test_read_main_with_non_default_block_sum(self): expect = ( {'kind': 'magic', 'value': b'ebakup database v1'}, {'kind': 'setting', 'key': b'edb-blocksize', 'value': b'4096'}, {'kind': 'setting', 'key': b'edb-blocksum', 'value': b'md5'}, {'kind': 'setting', 'key': b'checksum', 'value': b'sha256'} ) tree = FakeTree() data = testdata.dbfiledata('main-1')[:4064].replace( b'blocksum:sha256', b'blocksum:md5') + b'\x00' * 19 data += hashlib.md5(data).digest() tree._add_file( ('path', 'to', 'db', 'main'), data) main = datafile.open_main(tree, ('path', 'to', 'db')) self.assertItemSequence(expect, main) self.assertRaises(StopIteration, next, main) main.close() self.assertCountEqual((), tree._files_modified)
def test_raw_create_main_with_non_default_block_sum(self): tree = FakeTree() tree._add_directory(('path', 'to')) main = datafile.DataFile(tree, ('path', 'to', 'db', 'main')) main.create_and_lock() main.append_item(datafile.ItemMagic(b'ebakup database v1')) main.append_item(datafile.ItemSetting(b'edb-blocksize', b'4096')) main.append_item(datafile.ItemSetting(b'edb-blocksum', b'md5')) main.append_item(datafile.ItemSetting(b'checksum', b'sha256')) main.close() self.assertCountEqual( (('path', 'to', 'db'), ('path', 'to', 'db', 'main')), tree._files_modified) data = testdata.dbfiledata('main-1')[:4064].replace( b'blocksum:sha256', b'blocksum:md5') + b'\x00' * 19 self.assertEqual( data + hashlib.md5(data).digest(), tree._files[('path', 'to', 'db', 'main')].content)
def test_create_typical_main(self): tree = FakeTree() tree._add_directory(('path', 'to')) main = datafile.create_main_in_replacement_mode( tree, ('path', 'to', 'db')) main.append_item(datafile.ItemSetting(b'checksum', b'sha256')) self.assertCountEqual( (('path', 'to', 'db', 'main.new'), ('path', 'to', 'db')), tree._files_modified) main.commit_and_close() self.assertCountEqual( (('path', 'to', 'db'), ('path', 'to', 'db', 'main'), ('path', 'to', 'db', 'main.new')), tree._files_modified) self.assertNotIn(('path', 'to', 'db', 'main.new'), tree._files) self.assertEqual( testdata.dbfiledata('main-1'), tree._files[('path', 'to', 'db', 'main')].content)
def test_get_unopened_content(self): expect = StandardItemData() expect.load_content_1() tree = FakeTree() tree._add_file( ('path', 'to', 'db', 'content'), testdata.dbfiledata('content-1')) content = datafile.get_unopened_content(tree, ('path', 'to', 'db')) content.open_and_lock_readonly() self.assertEqual(6, len(expect.items)) # Don't read to the end before closing, to test that # re-opening the file really starts at the beginning. self.assertItemSequence(expect.items[:4], content) content.close() content.open_and_lock_readonly() self.assertItemSequence(expect.items, content) self.assertRaises(StopIteration, next, content) self.assertRaises(StopIteration, next, content) self.assertRaises(StopIteration, next, content) content.close() self.assertCountEqual((), tree._files_modified)
def test_read_and_write_content_db(self): expect = StandardItemData() expect.load_content_1() expect.append_item( { 'kind':'content', 'cid':b'this is a new file', 'checksum':b'this is a new file', 'first':1409428462 }) expect.append_item( { 'kind':'content', 'cid':b'this is another one', 'checksum':b'this is another one', 'first':1402611839 } ) tree = FakeTree() tree._add_file( ('path', 'to', 'db', 'content'), testdata.dbfiledata('content-1')) content = datafile.open_content( tree, ('path', 'to', 'db'), writable=True) content.append_item( datafile.ItemContent( b'this is a new file', b'this is a new file', 1409428462)) content.append_item( datafile.ItemContent( b'this is another one', b'this is another one', 1402611839)) self.assertItemSequence(expect.items, content) self.assertRaises(StopIteration, next, content) content.close() self.assertCountEqual( (('path', 'to', 'db', 'content'),), tree._files_modified) tree._files_modified = [] content = datafile.open_content(tree, ('path', 'to', 'db')) self.assertItemSequence(expect.items, content) self.assertRaises(StopIteration, next, content) content.close() self.assertCountEqual((), tree._files_modified)
def setUp(self): self.tree = FakeTree() self.tree._add_file( ('db', 'content'), testdata.dbfiledata('content-1')) self.db = FakeDatabase(self.tree, ('db',)) self.contentfile = contentdb.ContentInfoFile(self.db)