def test_upgrade_0(self):
     """Test the upgrade from the first shelf layout version."""
     self._build_layout_version_0()
     old_shelf = LegacyShareFileShelf(self.share_md_dir)
     # add the root_uuid key
     root_share = _Share(path=self.root_dir)
     root_share.access_level = ACCESS_LEVEL_RW
     old_shelf[request.ROOT] = root_share
     for idx in range(1, 10):
         sid = str(uuid.uuid4())
         old_shelf[sid] = _Share(
             path=os.path.join(self.shares_dir, str(idx)), share_id=sid)
     # ShareFileShelf.keys returns a generator
     old_keys = [key for key in old_shelf.keys()]
     self.assertEqual(10, len(old_keys))
     if self.md_version_None:
         self.set_md_version('')
     # set the ro permissions
     self._set_permissions()
     self.main = FakeMain(self.new_root_dir, self.new_shares_dir,
                          self.data_dir, self.partials_dir)
     new_keys = [new_key for new_key in self.main.vm.shares.keys()]
     self.assertEqual(10, len(new_keys))
     for new_key in new_keys:
         self.assertIn(new_key, old_keys)
     # check the old data is still there (in the backup)
     bkp_dir = os.path.join(os.path.dirname(self.vm_data_dir),
                            '5.bkp', '0.bkp')
     backup_shelf = LegacyShareFileShelf(bkp_dir)
     backup_keys = [key for key in backup_shelf.keys()]
     for old_key in old_keys:
         self.assertIn(old_key, backup_keys)
     for new_key in new_keys:
         self.assertIn(new_key, backup_keys)
     self.check_version()
    def test_missing_version_file_with_version_non_0(self):
        """Test the upgrade from the first shelf layout version
        while the metadata sould be in v3 or greater format.

        """
        self._build_layout_version_1()
        maybe_old_shelf = LegacyShareFileShelf(self.share_md_dir)
        # add the root_uuid key
        root_share = _Share(path=self.root_dir)
        root_share.access_level = ACCESS_LEVEL_RW
        maybe_old_shelf[request.ROOT] = root_share
        for idx in range(1, 10):
            share_id = str(uuid.uuid4())
            maybe_old_shelf[share_id] = _Share(
                share_id=share_id, path=os.path.join(self.shares_dir, str(idx))
            )
        # ShareFileShelf.keys returns a generator
        maybe_old_keys = [key for key in maybe_old_shelf.keys()]
        self.assertEqual(10, len(maybe_old_keys))
        if self.md_version_None:
            self.set_md_version('')
        self.main = FakeMain(self.new_root_dir, self.new_shares_dir,
                             self.data_dir, self.partials_dir)
        new_keys = [new_key for new_key in self.main.vm.shares.keys()]
        self.assertEqual(10, len(new_keys))
        for new_key in new_keys:
            self.assertIn(new_key, maybe_old_keys)
        # as we didn't actually upgrade the shelf, just the .version file
        # check the empty 0.bkp
        # check the old data is still there (in the backup)
        backup_shelf = LegacyShareFileShelf(os.path.join(self.vm_data_dir,
                                                         '0.bkp'))
        backup_keys = [key for key in backup_shelf.keys()]
        self.assertEqual(0, len(backup_keys))
        self.check_version()
    def test_upgrade_5_critical_error(self):
        """Test the migration from version 5 with a critical error."""
        # build a fake version 5 state
        self._build_layout_version_4()
        self.set_md_version('5')
        # create some old shares and shared metadata
        legacy_shares = LegacyShareFileShelf(self.share_md_dir)
        root_share = _Share(path=self.root_dir, share_id=request.ROOT,
                            access_level=ACCESS_LEVEL_RW)
        legacy_shares[request.ROOT] = root_share
        for idx, name in enumerate(['share'] * 10):
            sid = str(uuid.uuid4())
            share_name = name + '_' + str(idx)
            share = _Share(path=os.path.join(self.shares_dir, share_name),
                           share_id=sid, name=share_name,
                           node_id=str(uuid.uuid4()),
                           other_username='******' + str(idx),
                           other_visible_name='visible name ' + str(idx))
            if idx % 2:
                share.access_level = ACCESS_LEVEL_RW
            else:
                share.access_level = ACCESS_LEVEL_RO
            legacy_shares[sid] = share
        # create shared shares
        legacy_shared = LegacyShareFileShelf(self.shared_md_dir)
        for idx, name in enumerate(['dir'] * 5):
            sid = str(uuid.uuid4())
            share_name = name + '_' + str(idx)
            share = _Share(path=os.path.join(self.root_dir, share_name),
                           share_id=sid, node_id=str(uuid.uuid4()),
                           name=share_name, other_username='******',
                           other_visible_name='hola')
            if idx % 2:
                share.access_level = ACCESS_LEVEL_RW
            else:
                share.access_level = ACCESS_LEVEL_RO
            legacy_shared[sid] = share

        # keep a copy of the current shares and shared metadata to check
        # the upgrade went ok
        legacy_shares = dict(legacy_shares.items())
        legacy_shared = dict(legacy_shared.items())

        if self.md_version_None:
            self.set_md_version('')
        # upgrade it!
        old_upgrade_share_to_volume = MetadataUpgrader._upgrade_share_to_volume

        def upgrade_share_to_volume(share, shared=False):
            raise ValueError('FAIL!')
        MetadataUpgrader._upgrade_share_to_volume = upgrade_share_to_volume
        try:
            self.assertRaises(ValueError, FakeMain, self.root_dir,
                              self.shares_dir, self.data_dir,
                              self.partials_dir)
        finally:
            MetadataUpgrader._upgrade_share_to_volume = \
                    old_upgrade_share_to_volume

        shares = LegacyShareFileShelf(self.share_md_dir)
        self.assertEqual(len(list(shares.keys())), len(legacy_shares.keys()))
        for sid, share in shares.iteritems():
            old_share = legacy_shares[sid]
            self.assertTrue(isinstance(share, _Share))
            self.assertTrue(isinstance(old_share, _Share))
        shared = LegacyShareFileShelf(self.shared_md_dir)
        self.assertEqual(len(list(shared.keys())), len(legacy_shared.keys()))
        for sid, share in shared.iteritems():
            old_share = legacy_shared[sid]
            self.assertTrue(isinstance(share, _Share))
            self.assertTrue(isinstance(old_share, _Share))
    def test_upgrade_5_partial_upgrade(self):
        """Test migration from version 5 with upgrade to 6 unfinished."""
        # build a fake version 5 state
        self._build_layout_version_4()
        self.set_md_version('5')
        self.udfs_md_dir = os.path.join(self.vm_data_dir, 'udfs')
        # create some old shares and shared metadata
        legacy_shares = LegacyShareFileShelf(self.share_md_dir)
        root_share = _Share(path=self.root_dir, share_id=request.ROOT,
                            access_level=ACCESS_LEVEL_RW, node_id=str(
                                uuid.uuid4()))
        legacy_shares[request.ROOT] = root_share
        for idx, name in enumerate(['share'] * 3):
            sid = str(uuid.uuid4())
            share_name = name + '_' + str(idx)
            share = _Share(path=os.path.join(self.shares_dir, share_name),
                           share_id=sid, name=share_name,
                           node_id=str(uuid.uuid4()),
                           other_username='******' + str(idx),
                           other_visible_name='visible name ' + str(idx))
            if idx == 0:
                share.access_level = ACCESS_LEVEL_RW
                legacy_shares[sid] = share
            elif idx == 1:
                share.access_level = ACCESS_LEVEL_RO
                legacy_shares[sid] = share
            else:
                # add a 'new' Share dict to the shelf
                share.access_level = ACCESS_LEVEL_RW
                share = Share(
                    path=share.path, volume_id=share.id, name=share.name,
                    access_level=share.access_level,
                    other_username=share.other_username,
                    other_visible_name=share.other_visible_name,
                    node_id=share.subtree)
                legacy_shares[sid] = share.__dict__

        # create shared shares
        legacy_shared = LegacyShareFileShelf(self.shared_md_dir)
        for idx, name in enumerate(['dir'] * 3):
            sid = str(uuid.uuid4())
            share_name = name + '_' + str(idx)
            share = _Share(path=os.path.join(self.root_dir, share_name),
                           share_id=sid, node_id=str(uuid.uuid4()),
                           name=share_name, other_username='******',
                           other_visible_name='hola')
            if idx == 0:
                share.access_level = ACCESS_LEVEL_RW
                legacy_shares[sid] = share
            elif idx == 1:
                share.access_level = ACCESS_LEVEL_RO
                legacy_shares[sid] = share
            else:
                # add a 'new' Shared dict to the shelf
                share.access_level = ACCESS_LEVEL_RW
                share = Shared(path=share.path,
                               volume_id=share.id, name=share.name,
                               access_level=share.access_level,
                               other_username=share.other_username,
                               other_visible_name=share.other_visible_name,
                               node_id=share.subtree)
                legacy_shares[sid] = share.__dict__

        # keep a copy of the current shares and shared metadata to check
        # the upgrade went ok
        legacy_shares = dict(legacy_shares.items())
        legacy_shared = dict(legacy_shared.items())

        if self.md_version_None:
            self.set_md_version('')
        # upgrade it!
        self.main = FakeMain(self.root_dir, self.shares_dir,
                             self.data_dir, self.partials_dir)
        vm = self.main.vm

        def compare_share(share, old_share):
            """Compare two shares, new and old"""
            old_id = getattr(old_share, 'id', None)
            if old_id is None:
                old_id = old_share['volume_id']
            self.assertEqual(share.volume_id, old_id)
            self.assertEqual(
                share.path,
                getattr(old_share, 'path', None) or old_share['path'])
            self.assertEqual(
                share.node_id,
                getattr(old_share, 'subtree', None) or old_share['node_id'])
            if not isinstance(share, Root):
                self.assertEqual(
                    share.name,
                    getattr(old_share, 'name', None) or old_share['name'])
                username = (getattr(old_share, 'other_username', None) or
                            old_share['other_username'])
                self.assertEqual(share.other_username, username)
                name = (getattr(old_share, 'other_visible_name', None) or
                        old_share['other_visible_name'])
                self.assertEqual(share.other_visible_name, name)
                level = (getattr(old_share, 'access_level', None) or
                         old_share['access_level'])
                self.assertEqual(share.access_level, level)

        for sid in vm.shares:
            old_share = legacy_shares[sid]
            share = vm.shares[sid]
            self.assertTrue(
                isinstance(share, Share) or isinstance(share, Root))
            compare_share(share, old_share)

        for sid in vm.shared:
            old_share = legacy_shared[sid]
            share = vm.shared[sid]
            self.assertTrue(isinstance(share, Shared))
            compare_share(share, old_share)
    def test_upgrade_5_with_udfs(self):
        """Test the migration from version 5 with old UDFs."""
        # build a fake version 5 state
        self._build_layout_version_4()
        self.set_md_version('5')
        self.udfs_md_dir = os.path.join(self.vm_data_dir, 'udfs')
        # create some old shares and shared metadata
        legacy_shares = LegacyShareFileShelf(self.share_md_dir)
        root_share = _Share(path=self.root_dir, share_id=request.ROOT,
                            access_level=ACCESS_LEVEL_RW)
        legacy_shares[request.ROOT] = root_share
        for idx, name in enumerate(['share'] * 10):
            sid = str(uuid.uuid4())
            share_name = name + '_' + str(idx)
            share = _Share(path=os.path.join(self.shares_dir, share_name),
                           share_id=sid, name=share_name,
                           node_id=str(uuid.uuid4()),
                           other_username='******' + str(idx),
                           other_visible_name='visible name ' + str(idx))
            if idx % 2:
                share.access_level = ACCESS_LEVEL_RW
            else:
                share.access_level = ACCESS_LEVEL_RO
            legacy_shares[sid] = share

        # create shared shares
        legacy_shared = LegacyShareFileShelf(self.shared_md_dir)
        for idx, name in enumerate(['dir'] * 5):
            sid = str(uuid.uuid4())
            share_name = name + '_' + str(idx)
            share = _Share(path=os.path.join(self.root_dir, share_name),
                           share_id=sid, node_id=str(uuid.uuid4()),
                           name=share_name, other_username='******',
                           other_visible_name='hola')
            if idx % 2:
                share.access_level = ACCESS_LEVEL_RW
            else:
                share.access_level = ACCESS_LEVEL_RO
            legacy_shared[sid] = share

        # create some udfs
        legacy_udfs = LegacyShareFileShelf(self.udfs_md_dir)
        for idx, name in enumerate(['dir'] * 5):
            udf_id = str(uuid.uuid4())
            udf_name = name + '_' + str(idx)
            udf = _UDF(
                udf_id, str(uuid.uuid4()), u'~/' + udf_name.decode('utf-8'),
                os.path.join(self.home_dir, udf_name))
            if idx % 2:
                udf.subscribed = True
            else:
                udf.subscribed = False
            legacy_udfs[sid] = udf

        # keep a copy of the current shares and shared metadata to check
        # the upgrade went ok
        legacy_shares = dict(legacy_shares.items())
        legacy_shared = dict(legacy_shared.items())
        legacy_udfs = dict(legacy_udfs.items())

        if self.md_version_None:
            self.set_md_version('')
        # upgrade it!
        self.main = FakeMain(self.root_dir, self.shares_dir,
                             self.data_dir, self.partials_dir)
        vm = self.main.vm

        def compare_share(share, old_share):
            """Compare two shares, new and old"""
            self.assertEqual(share.volume_id, old_share.id)
            self.assertEqual(share.path, old_share.path)
            self.assertEqual(share.node_id, old_share.subtree)
            if not isinstance(share, Root):
                self.assertEqual(share.name, old_share.name)
                self.assertEqual(
                    share.other_username, old_share.other_username)
                self.assertEqual(
                    share.other_visible_name, old_share.other_visible_name)
                self.assertEqual(share.access_level, old_share.access_level)

        for sid in vm.shares:
            old_share = legacy_shares[sid]
            share = vm.shares[sid]
            self.assertTrue(
                isinstance(share, Share) or isinstance(share, Root))
            compare_share(share, old_share)

        for sid in vm.shared:
            old_share = legacy_shared[sid]
            share = vm.shared[sid]
            self.assertTrue(isinstance(share, Shared))
            compare_share(share, old_share)

        for udf_id in vm.udfs:
            old_udf = legacy_udfs[udf_id]
            udf = vm.udfs[udf_id]
            self.assertTrue(isinstance(udf, UDF))
            self.assertEqual(udf.volume_id, old_udf.id)
            self.assertEqual(udf.path, old_udf.path)
            self.assertEqual(udf.node_id, old_udf.node_id)
            self.assertEqual(udf.suggested_path, old_udf.suggested_path)
            self.assertEqual(
                type(udf.suggested_path), type(old_udf.suggested_path))
            self.assertEqual(udf.subscribed, old_udf.subscribed)