def test_upgrade_6(self):
        """Test the migration from version 6."""
        # build a fake version 5 state
        self._build_layout_version_4()
        self.set_md_version('6')
        self.udfs_md_dir = os.path.join(self.vm_data_dir, 'udfs')
        # create some old shares and shared metadata
        legacy_shares = VMFileShelf(self.share_md_dir)
        root = Root(path=self.root_dir)
        legacy_shares[request.ROOT] = root
        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),
                volume_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 = VMFileShelf(self.shared_md_dir)
        for idx, name in enumerate(['dir'] * 5):
            sid = str(uuid.uuid4())
            share_name = name + '_' + str(idx)
            share = Shared(path=os.path.join(self.root_dir, share_name),
                           volume_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 = VMFileShelf(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

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

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

        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.__dict__, old_udf.__dict__)
    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)