Exemplo n.º 1
0
    def test_move_block_to_end(self):
        items = StandardItemData()
        items.load_backup_1()
        tree = FakeTree()
        tree._add_directory(('path', 'to', 'db'))
        starttime = datetime.datetime(2015, 4, 3, 10, 46, 6)

        backup = datafile.create_backup_in_replacement_mode(
            tree, ('path', 'to', 'db'), starttime)
        self.assertEqual('setting', items.items[4]['kind'])
        self.assertEqual('directory', items.items[5]['kind'])
        self.append_item_sequence(items.items[5:], backup)
        self.assertEqual(1, backup.get_last_block_index())
        backup.move_block(1, -1)
        self.assertEqual(2, backup.get_last_block_index())
        backup.insert_item(
            1, -1, datafile.ItemKeyValue(0, b'extra key', b'extra value'))
        backup.insert_item(
            0, -1, datafile.ItemSetting(b'end', b'2015-04-03T10:47:59'))
        backup.commit_and_close()
        backup = datafile.open_backup_by_name(
            tree, ('path', 'to', 'db'), '2015-04-03T10:46')
        self.assertEqual(2, backup.get_last_block_index())
        self.assertItemSequence(items.items[:5], backup)
        item = next(backup)
        self.assertEqual('key-value', item.kind)
        self.assertEqual(b'extra key', item.key)
        self.assertEqual(b'extra value', item.value)
        self.assertItemSequence(items.items[5:], backup)
        backup.close()
Exemplo n.º 2
0
    def test_create_simple_backup(self):
        # This one is not using StandardItemData in order to preserve
        # the particular choices of data and their comments.
        items = (
            {'kind': 'magic', 'value': b'ebakup backup data'},
            {'kind': 'setting', 'key': b'edb-blocksize', 'value': b'4096'},
            {'kind': 'setting', 'key': b'edb-blocksum', 'value': b'sha256'},
            {'kind': 'setting',
             'key': b'start', 'value': b'2015-09-05T21:22:42'},
            {'kind': 'setting',
             'key': b'end', 'value': b'2015-09-05T21:24:06'},
            {'kind': 'directory', 'dirid': 8, 'parent': 0, 'name': b'path',
             'extra_data': 0 },
            {'kind': 'directory', 'dirid': 9, 'parent': 8, 'name': b'to',
             'extra_data': 0 },
            # size here is set small to check that I don't
            # accidentally overwrite the "size" of the data during
            # parsing with the "size" of this file.
            {'kind': 'file', 'parent': 9, 'name': b'file',
             'cid': b'\x92!G\xa0\xbfQ\x8bQL\xb5\xc1\x1e\x1a\x10\xbf\xeb;y\x00'
                    b'\xe3/~\xd7\x1b\xf4C\x04\xd1a*\xf2^',
             'size': 23, 'mtime_year': 2015, 'mtime_second': 0x42a042,
             'mtime_ns': 765430000, 'extra_data': 0 },
            # size here is set large enough to require multi-byte encoding
            {'kind': 'file', 'parent': 0, 'name': b'file',
             'cid': b'P\xcd\x91\x14\x0b\x0c\xd9\x95\xfb\xd1!\xe3\xf3\x05'
                    b'\xe7\xd1[\xe6\xc8\x1b\xc5&\x99\xe3L\xe9?\xdaJ\x0eF\xde',
             'size': 7850, 'mtime_year': 2013, 'mtime_second': 0x10adba0,
             'mtime_ns': 0, 'extra_data': 0 },
            )
        tree = FakeTree()
        tree._add_directory(('path', 'to', 'db'))
        starttime = datetime.datetime(2015, 9, 5, 21, 22, 42)

        backup = datafile.create_backup_in_replacement_mode(
            tree, ('path', 'to', 'db'), starttime)
        self.append_item_sequence(items[5:], backup)
        backup.insert_item(
            0, -1, datafile.ItemSetting(b'end', b'2015-09-05T21:24:06'))
        self.assertNotIn(
            ('path', 'to', 'db', '2015', '09-05T21:22'), tree._files)
        self.assertEqual(
            True,
            tree._files[('path', 'to', 'db', '2015', '09-05T21:22.new')].locked)
        backup.commit_and_close()
        self.assertCountEqual(
            (('path', 'to', 'db', '2015'),
             ('path', 'to', 'db', '2015', '09-05T21:22.new'),
             ('path', 'to', 'db', '2015', '09-05T21:22')),
            set(tree._files_modified))
        tree._files_modified = []
        self.assertNotIn(
            ('path', 'to', 'db', '2015', '09-05T21:22.new'), tree._files)
        backup = datafile.open_backup_by_name(
            tree, ('path', 'to', 'db'), '2015-09-05T21:22')
        self.assertItemSequence(items, backup)
        self.assertRaises(StopIteration, next, backup)
        backup.close()
        self.assertCountEqual((), tree._files_modified)
Exemplo n.º 3
0
 def __init__(self, tree, dbpath, name):
     self._items = []
     self._dirs = { 0: () }
     df = datafile.open_backup_by_name(tree, dbpath, name)
     for item in df:
         if item.kind == 'directory':
             assert item.dirid not in self._dirs
             self._dirs[item.dirid] = self._full_path(item)
         self._items.append(item)
Exemplo n.º 4
0
    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)
Exemplo n.º 5
0
    def test_create_simple_backup_with_special_files(self):
        items = StandardItemData()
        items.load_backup_1()
        items.append_item(
            {'kind': 'file-symlink', 'parent': 0, 'name': b'symbolic_link',
             'cid': b':&h)\x02-\xaf`\x92\xde\x11\xbb\xd7\xaaK4\xb7\xa0E\xa1\x8d'
                    b'\xb8#(\x02"\xc2s\x01\xd6\x03\xd1',
             'size': 27, 'mtime_year': 2014, 'mtime_second': 29899012,
             'mtime_ns': 259388602, 'extra_data': 0 })
        items.append_item(
            {'kind': 'file-socket', 'parent': 0, 'name': b'fs_socket',
             'cid': b'', 'size': 0, 'mtime_year': 2014,
             'mtime_second': 24395803, 'mtime_ns': 946662039, 'extra_data': 0})
        tree = FakeTree()
        tree._add_directory(('path', 'to', 'db'))
        starttime = datetime.datetime(2015, 4, 3, 10, 46, 6)

        backup = datafile.create_backup_in_replacement_mode(
            tree, ('path', 'to', 'db'), starttime)
        self.assertEqual('setting', items.items[4]['kind'])
        self.assertEqual('directory', items.items[5]['kind'])
        self.append_item_sequence(items.items[5:], backup)
        backup.insert_item(
            0, -1, datafile.ItemSetting(b'end', b'2015-04-03T10:47:59'))
        self.assertNotIn(
            ('path', 'to', 'db', '2015', '04-03T10:46'), tree._files)
        self.assertEqual(
            True,
            tree._files[('path', 'to', 'db', '2015', '04-03T10:46.new')].locked)
        backup.commit_and_close()
        self.assertCountEqual(
            (('path', 'to', 'db', '2015'),
             ('path', 'to', 'db', '2015', '04-03T10:46.new'),
             ('path', 'to', 'db', '2015', '04-03T10:46')),
            set(tree._files_modified))
        tree._files_modified = []
        self.assertNotIn(
            ('path', 'to', 'db', '2015', '04-03T10:46.new'), tree._files)
        backup = datafile.open_backup_by_name(
            tree, ('path', 'to', 'db'), '2015-04-03T10:46')
        self.assertItemSequence(items.items, backup)
        self.assertRaises(StopIteration, next, backup)
        backup.close()
        self.assertCountEqual((), tree._files_modified)
Exemplo n.º 6
0
 def _read_file(self, bkpath, bkname):
     fs = filesys.get_file_system('local')
     dbpath = fs.path_from_string(os.path.join(bkpath, 'db'))
     with datafile.open_backup_by_name(fs, dbpath, bkname) as df:
         self._read_datafile(df)
Exemplo n.º 7
0
    def test_create_simple_backup_with_extra_file_data(self):
        items = StandardItemData()
        items.load_backup_1()
        items.change_extra_data_for_dirid(
            8, { 'owner': 'me', 'group': 'me', 'unix-access': 0o755 })
        items.change_extra_data_for_dirid(
            9, { 'owner': 'me', 'group': 'me', 'unix-access': 0o755 })
        items.change_extra_data_for_file(
            9, b'file', { 'owner': 'me', 'group': 'me', 'unix-access': 0o644 })
        items.change_extra_data_for_file(
            0, b'file', { 'owner': 'me', 'group': 'me', 'unix-access': 0o755 })
        items.append_item(
            {'kind': 'file-symlink', 'parent': 0, 'name': b'symbolic_link',
             'cid': b':&h)\x02-\xaf`\x92\xde\x11\xbb\xd7\xaaK4\xb7\xa0E\xa1\x8d'
                    b'\xb8#(\x02"\xc2s\x01\xd6\x03\xd1',
             'size': 27, 'mtime_year': 2014, 'mtime_second': 29899012,
             'mtime_ns': 259388602,
             'extra_data': {
                 'owner': 'other', 'group': 'other', 'unix-access': 0o644 } })
        items.append_item(
            {'kind': 'file-socket', 'parent': 0, 'name': b'fs_socket',
             'cid': b'', 'size': 0, 'mtime_year': 2014,
             'mtime_second': 24395803, 'mtime_ns': 946662039,
             'extra_data': {
                 'owner': 'root', 'group': 'staff', 'unix-access': 0o640 } })
        tree = FakeTree()
        tree._add_directory(('path', 'to', 'db'))
        starttime = datetime.datetime(2015, 4, 3, 10, 46, 6)

        backup = datafile.create_backup_in_replacement_mode(
            tree, ('path', 'to', 'db'), starttime)
        kvs = KeyValueDict()
        extradefs = ExtraDataDict()
        self.assertEqual('setting', items.items[4]['kind'])
        self.assertEqual('directory', items.items[5]['kind'])
        self.append_item_sequence_with_extras(
            items.items[5:], backup, kvs, extradefs)
        backup.insert_item(
            0, -1, datafile.ItemSetting(b'end', b'2015-04-03T10:47:59'))
        self.assertNotIn(
            ('path', 'to', 'db', '2015', '04-03T10:46'), tree._files)
        self.assertEqual(
            True,
            tree._files[('path', 'to', 'db', '2015', '04-03T10:46.new')].locked)
        backup.commit_and_close()
        self.assertCountEqual(
            (('path', 'to', 'db', '2015'),
             ('path', 'to', 'db', '2015', '04-03T10:46.new'),
             ('path', 'to', 'db', '2015', '04-03T10:46')),
            set(tree._files_modified))
        tree._files_modified = []
        self.assertNotIn(
            ('path', 'to', 'db', '2015', '04-03T10:46.new'), tree._files)
        backup = datafile.open_backup_by_name(
            tree, ('path', 'to', 'db'), '2015-04-03T10:46')
        kvids = {}
        xids = { 0: tuple() }
        self.assertItemSequenceWithExtras(items.items, backup, kvids, xids)
        self.assertRaises(StopIteration, next, backup)
        self.assertKeyValueDictsEqual(kvs.kvids, kvids)
        self.assertEqual(extradefs.xids, xids)
        backup.close()
        self.assertCountEqual((), tree._files_modified)
Exemplo n.º 8
0
 def open_raw_backup(tree, path, name):
     return datafile.open_backup_by_name(tree, path, name)