def test_get_cached_image(self, mock_create_path, mock_synchronized,
                              mock_join, mock_exists):
        mock_join.return_value = self._FAKE_BASE_PATH
        mock_exists.side_effect = ([False] * len(constants.ALL_DISK_FORMATS) +
                                   [True])
        imagecache.get_cached_image(self._context, self._instance)
        mock_synchronized.assert_called_once_with(self._FAKE_BASE_PATH)
        mock_synchronized().assert_called_once_with(imagecache._fetch_image)
        mock_synchronized.reset_mock()

        imagecache.get_cached_image(self._context, self._instance)
        self.assertEqual(0, mock_synchronized.call_count)
        self.assertEqual(2, mock_create_path.call_count)
    def test_get_cached_image(self, mock_create_path, mock_synchronized,
                              mock_join, mock_exists):
        mock_join.return_value = self._FAKE_BASE_PATH
        mock_exists.side_effect = ([False] * len(constants.ALL_DISK_FORMATS) +
                                   [True])
        imagecache.get_cached_image(self._context, self._instance)
        mock_synchronized.assert_called_once_with(self._FAKE_BASE_PATH)
        mock_synchronized().assert_called_once_with(imagecache._fetch_image)
        mock_synchronized.reset_mock()

        imagecache.get_cached_image(self._context, self._instance)
        self.assertEqual(0, mock_synchronized.call_count)
        self.assertEqual(2, mock_create_path.call_count)
    def finish_migration(self, context, migration, instance, disk_info,
                         network_info, image_meta, resize_instance=False,
                         block_device_info=None):
        """Completes a resize."""
        LOG.debug("`Finish migration` method called.")
        if volumeutils.ebs_root_in_block_devices(block_device_info):
            root_path = None
        else:
            root_path = pathutils.lookup_root_vhd_path(instance)
            if not root_path:
                raise vbox_exc.VBoxException(
                    i18n._("Cannot find boot VHD file for instance: %s") %
                    instance.name)
            base_disk_path = imagecache.get_cached_image(context, instance)
            self._check_disk(root_path, base_disk_path)

        ephemeral_path = pathutils.lookup_ephemeral_vhd_path(instance)
        if not ephemeral_path:
            ephemeral_path = self._vbox_ops.create_ephemeral_disk(instance)

        if resize_instance:
            self._resize_disk(instance, instance.root_gb * units.Ki, root_path)
            self._resize_disk(instance,
                              instance.get('ephemeral_gb', 0) * units.Ki,
                              ephemeral_path)

        self._vbox_ops.create_instance(instance, image_meta, network_info,
                                       overwrite=False)
        self._vbox_ops.storage_setup(instance, root_path, ephemeral_path,
                                     block_device_info)
    def create_root_disk(self, context, instance):
        base_vhd_path = imagecache.get_cached_image(context, instance)
        base_info = vhdutils.disk_info(base_vhd_path)
        root_vhd_path = pathutils.root_disk_path(
            instance, disk_format=base_info[constants.VHD_IMAGE_TYPE])

        if CONF.use_cow_images:
            LOG.debug(
                "Creating differencing VHD. Parent: %(parent)s, "
                "Target: %(target)s", {
                    'parent': base_vhd_path,
                    'target': root_vhd_path
                },
                instance=instance)
            self._vbox_manage.create_hd(
                filename=root_vhd_path,
                # disk_format=base_info[constants.VHD_IMAGE_TYPE],
                variant=constants.VARIANT_STANDARD,
                parent=base_vhd_path)

        else:
            LOG.debug(
                "Cloning VHD image %(base)s to target: %(target)s", {
                    'base': base_vhd_path,
                    'target': root_vhd_path
                },
                instance=instance)
            self._vbox_manage.clone_hd(
                vhd_path=base_vhd_path,
                new_vdh_path=root_vhd_path,
                disk_format=base_info[constants.VHD_IMAGE_TYPE],
                variant=constants.VARIANT_STANDARD)

        # Resize image if is necesary
        disk_format = vhdutils.get_image_type(root_vhd_path)
        if instance.root_gb and disk_format in (constants.DISK_FORMAT_VDI,
                                                constants.DISK_FORMAT_VHD):
            base_vhd_size = base_info[constants.VHD_CAPACITY] / units.Mi
            root_vhd_size = instance.root_gb * units.Ki
            if vhdutils.is_resize_required(
                    disk_path=root_vhd_path,
                    old_size=base_vhd_size,
                    new_size=root_vhd_size,
                    instance=instance):
                self._vbox_manage.modify_hd(
                    root_vhd_path, constants.FIELD_HD_RESIZE_MB, root_vhd_size)

        return root_vhd_path
Exemple #5
0
    def create_root_disk(self, context, instance):
        base_vhd_path = imagecache.get_cached_image(context, instance)
        base_info = vhdutils.disk_info(base_vhd_path)
        root_vhd_path = pathutils.root_disk_path(
            instance, disk_format=base_info[constants.VHD_IMAGE_TYPE])

        if CONF.use_cow_images:
            LOG.debug(
                "Creating differencing VHD. Parent: %(parent)s, "
                "Target: %(target)s", {
                    'parent': base_vhd_path,
                    'target': root_vhd_path
                },
                instance=instance)
            self._vbox_manage.create_hd(
                filename=root_vhd_path,
                # disk_format=base_info[constants.VHD_IMAGE_TYPE],
                variant=constants.VARIANT_STANDARD,
                parent=base_vhd_path)

        else:
            LOG.debug("Cloning VHD image %(base)s to target: %(target)s", {
                'base': base_vhd_path,
                'target': root_vhd_path
            },
                      instance=instance)
            self._vbox_manage.clone_hd(
                vhd_path=base_vhd_path,
                new_vdh_path=root_vhd_path,
                disk_format=base_info[constants.VHD_IMAGE_TYPE],
                variant=constants.VARIANT_STANDARD)

        # Resize image if is necesary
        disk_format = vhdutils.get_image_type(root_vhd_path)
        if instance.root_gb and disk_format in (constants.DISK_FORMAT_VDI,
                                                constants.DISK_FORMAT_VHD):
            base_vhd_size = base_info[constants.VHD_CAPACITY] / units.Mi
            root_vhd_size = instance.root_gb * units.Ki
            if vhdutils.is_resize_required(disk_path=root_vhd_path,
                                           old_size=base_vhd_size,
                                           new_size=root_vhd_size,
                                           instance=instance):
                self._vbox_manage.modify_hd(root_vhd_path,
                                            constants.FIELD_HD_RESIZE_MB,
                                            root_vhd_size)

        return root_vhd_path
Exemple #6
0
    def finish_migration(self,
                         context,
                         migration,
                         instance,
                         disk_info,
                         network_info,
                         image_meta,
                         resize_instance=False,
                         block_device_info=None):
        """Completes a resize."""
        LOG.debug("`Finish migration` method called.")
        if volumeutils.ebs_root_in_block_devices(block_device_info):
            root_path = None
        else:
            root_path = pathutils.lookup_root_vhd_path(instance)
            if not root_path:
                raise vbox_exc.VBoxException(
                    i18n._("Cannot find boot VHD file for instance: %s") %
                    instance.name)
            base_disk_path = imagecache.get_cached_image(context, instance)
            self._check_disk(root_path, base_disk_path)

        ephemeral_path = pathutils.lookup_ephemeral_vhd_path(instance)
        if not ephemeral_path:
            ephemeral_path = self._vbox_ops.create_ephemeral_disk(instance)

        if resize_instance:
            self._resize_disk(instance, instance.root_gb * units.Ki, root_path)
            self._resize_disk(instance,
                              instance.get('ephemeral_gb', 0) * units.Ki,
                              ephemeral_path)

        self._vbox_ops.create_instance(instance,
                                       image_meta,
                                       network_info,
                                       overwrite=False)
        self._vbox_ops.storage_setup(instance, root_path, ephemeral_path,
                                     block_device_info)