Пример #1
0
    def test_copy_volume_to_image_driver_exception(self):
        self.image_meta['id'] = self.image_id

        image_service = fake_image.FakeImageService()
        # create new image in queued state
        queued_image_id = 'd5133f15-f753-41bd-920a-06b8c49275d9'
        queued_image_meta = image_service.show(self.context, self.image_id)
        queued_image_meta['id'] = queued_image_id
        queued_image_meta['status'] = 'queued'
        image_service.create(self.context, queued_image_meta)

        # create new image in saving state
        saving_image_id = '5c6eec33-bab4-4e7d-b2c9-88e2d0a5f6f2'
        saving_image_meta = image_service.show(self.context, self.image_id)
        saving_image_meta['id'] = saving_image_id
        saving_image_meta['status'] = 'saving'
        image_service.create(self.context, saving_image_meta)

        # create volume
        self.volume_attrs['status'] = 'available'
        self.volume_attrs['instance_uuid'] = None
        db.volume_create(self.context, self.volume_attrs)

        with mock.patch.object(self.volume.driver,
                               'copy_volume_to_image') as driver_copy_mock:
            driver_copy_mock.side_effect = exception.VolumeDriverException(
                "Error")

            # test with image not in queued state
            self.assertRaises(exception.VolumeDriverException,
                              self.volume.copy_volume_to_image, self.context,
                              self.volume_id, self.image_meta)
            # Make sure we are passing an OVO instance and not an ORM instance
            # to the driver
            self.assertIsInstance(driver_copy_mock.call_args[0][1],
                                  objects.Volume)
            volume = db.volume_get(self.context, self.volume_id)
            self.assertEqual('available', volume['status'])
            # image shouldn't be deleted if it is not in queued state
            image_service.show(self.context, self.image_id)

            # test with image in queued state
            self.assertRaises(exception.VolumeDriverException,
                              self.volume.copy_volume_to_image, self.context,
                              self.volume_id, queued_image_meta)
            volume = db.volume_get(self.context, self.volume_id)
            self.assertEqual('available', volume['status'])
            # queued image should be deleted
            self.assertRaises(exception.ImageNotFound, image_service.show,
                              self.context, queued_image_id)

            # test with image in saving state
            self.assertRaises(exception.VolumeDriverException,
                              self.volume.copy_volume_to_image, self.context,
                              self.volume_id, saving_image_meta)
            volume = db.volume_get(self.context, self.volume_id)
            self.assertEqual('available', volume['status'])
            # image in saving state should be deleted
            self.assertRaises(exception.ImageNotFound, image_service.show,
                              self.context, saving_image_id)
Пример #2
0
    def test_extract_availability_zone_without_fallback(
            self, fake_get_type_id, fake_get_qos, fake_is_encrypted):
        fake_image_service = fake_image.FakeImageService()
        image_id = 3
        image_meta = {}
        image_meta['id'] = image_id
        image_meta['status'] = 'active'
        image_meta['size'] = 1
        fake_image_service.create(self.ctxt, image_meta)
        fake_key_manager = mock_key_mgr.MockKeyManager()
        volume_type = 'type1'

        task = create_volume.ExtractVolumeRequestTask(fake_image_service,
                                                      {'nova'})

        fake_is_encrypted.return_value = False
        fake_get_type_id.return_value = 1
        fake_get_qos.return_value = {'qos_specs': None}
        self.assertRaises(exception.InvalidInput,
                          task.execute,
                          self.ctxt,
                          size=1,
                          snapshot=None,
                          image_id=image_id,
                          source_volume=None,
                          availability_zone='notnova',
                          volume_type=volume_type,
                          metadata=None,
                          key_manager=fake_key_manager,
                          source_replica=None,
                          consistencygroup=None,
                          cgsnapshot=None)
    def test_create_encrypted_volume_from_image(self, mock_check_size,
                                                mock_qemu_img, mock_fetch_img,
                                                mock_handle_bootable):
        fake_db = mock.MagicMock()
        fake_driver = mock.MagicMock()
        fake_volume_manager = mock.MagicMock()
        fake_manager = create_volume_manager.CreateVolumeFromSpecTask(
            fake_volume_manager, fake_db, fake_driver)
        volume = fake_volume.fake_volume_obj(
            self.ctxt, encryption_key_id=fakes.ENCRYPTION_KEY_ID)

        fake_image_service = fake_image.FakeImageService()
        image_meta = {}
        image_id = fakes.IMAGE_ID
        image_meta['id'] = image_id
        image_meta['status'] = 'active'
        image_meta['size'] = 1
        image_location = 'abc'

        fake_db.volume_update.return_value = volume
        fake_manager._create_from_image(self.ctxt, volume, image_location,
                                        image_id, image_meta,
                                        fake_image_service)

        fake_driver.create_volume.assert_called_once_with(volume)
        fake_driver.copy_image_to_encrypted_volume.assert_called_once_with(
            self.ctxt, volume, fake_image_service, image_id)
        mock_handle_bootable.assert_called_once_with(self.ctxt,
                                                     volume,
                                                     image_id=image_id,
                                                     image_meta=image_meta)
    def test_extract_volume_request_from_image_encrypted(
            self, fake_get_qos, fake_get_encryption_key,
            fake_get_volume_type_id, fake_is_encrypted):

        fake_image_service = fake_image.FakeImageService()
        image_id = 1
        image_meta = {}
        image_meta['id'] = image_id
        image_meta['status'] = 'active'
        image_meta['size'] = 1
        fake_image_service.create(self.ctxt, image_meta)
        fake_key_manager = mock_key_manager.MockKeyManager()

        task = create_volume.ExtractVolumeRequestTask(fake_image_service,
                                                      {'nova'})

        fake_is_encrypted.return_value = True
        fake_get_volume_type_id.return_value = fakes.VOLUME_TYPE_ID
        task.execute(self.ctxt,
                     size=1,
                     snapshot=None,
                     image_id=image_id,
                     source_volume=None,
                     availability_zone='nova',
                     volume_type=None,
                     metadata=None,
                     key_manager=fake_key_manager,
                     source_replica=None,
                     consistencygroup=None,
                     cgsnapshot=None,
                     group=None)
        fake_get_encryption_key.assert_called_once_with(
            fake_key_manager, self.ctxt, fakes.VOLUME_TYPE_ID, None, None)
Пример #5
0
    def _test_copy_volume_to_image_with_image_volume(self, mock_copy,
                                                     mock_create,
                                                     mock_quota_commit,
                                                     mock_quota_reserve):
        self.volume.driver.configuration.image_upload_use_cinder_backend = True
        self.addCleanup(fake_image.FakeImageService_reset)
        image_service = fake_image.FakeImageService()

        def add_location_wrapper(ctx, id, uri, metadata):
            try:
                volume = db.volume_get(ctx, id)
                self.assertEqual(ctx.project_id,
                                 volume['metadata']['image_owner'])
            except exception.VolumeNotFound:
                pass
            return image_service.add_location_orig(ctx, id, uri, metadata)

        image_service.add_location_orig = image_service.add_location
        image_service.add_location = add_location_wrapper

        image_id = '5c6eec33-bab4-4e7d-b2c9-88e2d0a5f6f2'
        self.image_meta['id'] = image_id
        self.image_meta['status'] = 'queued'
        image_service.create(self.context, self.image_meta)

        # creating volume testdata
        self.volume_attrs['instance_uuid'] = None
        self.volume_attrs['snapshot_id'] = fake.SNAPSHOT_ID
        volume_type_id = db.volume_type_create(
            self.context, {
                'name': 'test',
                'extra_specs': {
                    'image_service:store_id': 'fake_store'
                }
            }).get('id')
        self.volume_attrs['volume_type_id'] = volume_type_id
        db.volume_create(self.context, self.volume_attrs)

        def fake_create(context, volume, **kwargs):
            db.volume_update(context, volume.id, {'status': 'available'})

        mock_create.side_effect = fake_create

        # start test
        self.volume.copy_volume_to_image(self.context, self.volume_id,
                                         self.image_meta)

        volume = db.volume_get(self.context, self.volume_id)
        self.assertEqual('available', volume['status'])

        # return create image
        image = image_service.show(self.context, image_id)
        image_service.delete(self.context, image_id)
        return image
    def test_extract_image_volume_type_from_image(self, fake_get_type_id,
                                                  fake_get_vol_type,
                                                  fake_get_def_vol_type,
                                                  fake_get_qos,
                                                  fake_is_encrypted):

        image_volume_type = 'type_from_image'
        fake_image_service = fake_image.FakeImageService()
        image_id = 6
        image_meta = {}
        image_meta['id'] = image_id
        image_meta['status'] = 'active'
        image_meta['size'] = 1
        image_meta['properties'] = {}
        image_meta['properties']['cinder_img_volume_type'] = image_volume_type
        fake_image_service.create(self.ctxt, image_meta)
        fake_key_manager = mock_key_manager.MockKeyManager()

        task = create_volume.ExtractVolumeRequestTask(fake_image_service,
                                                      {'nova'})

        fake_is_encrypted.return_value = False
        fake_get_type_id.return_value = 1
        fake_get_vol_type.return_value = image_volume_type
        fake_get_def_vol_type.return_value = 'fake_vol_type'
        fake_get_qos.return_value = {'qos_specs': None}
        result = task.execute(self.ctxt,
                              size=1,
                              snapshot=None,
                              image_id=image_id,
                              source_volume=None,
                              availability_zone='nova',
                              volume_type=None,
                              metadata=None,
                              key_manager=fake_key_manager,
                              source_replica=None,
                              consistencygroup=None,
                              cgsnapshot=None,
                              group=None)
        expected_result = {
            'size': 1,
            'snapshot_id': None,
            'source_volid': None,
            'availability_zone': 'nova',
            'volume_type': image_volume_type,
            'volume_type_id': 1,
            'encryption_key_id': None,
            'qos_specs': None,
            'source_replicaid': None,
            'consistencygroup_id': None,
            'cgsnapshot_id': None,
            'group_id': None,
        }
        self.assertEqual(expected_result, result)
    def test_extract_availability_zone_with_fallback(self, fake_get_type_id,
                                                     fake_get_qos,
                                                     fake_is_encrypted):

        self.override_config('allow_availability_zone_fallback', True)

        fake_image_service = fake_image.FakeImageService()
        image_id = 4
        image_meta = {}
        image_meta['id'] = image_id
        image_meta['status'] = 'active'
        image_meta['size'] = 1
        fake_image_service.create(self.ctxt, image_meta)
        fake_key_manager = mock_key_manager.MockKeyManager()
        volume_type = 'type1'

        task = create_volume.ExtractVolumeRequestTask(fake_image_service,
                                                      {'nova'})

        fake_is_encrypted.return_value = False
        fake_get_type_id.return_value = 1
        fake_get_qos.return_value = {'qos_specs': None}
        result = task.execute(self.ctxt,
                              size=1,
                              snapshot=None,
                              image_id=image_id,
                              source_volume=None,
                              availability_zone='does_not_exist',
                              volume_type=volume_type,
                              metadata=None,
                              key_manager=fake_key_manager,
                              source_replica=None,
                              consistencygroup=None,
                              cgsnapshot=None,
                              group=None)
        expected_result = {
            'size': 1,
            'snapshot_id': None,
            'source_volid': None,
            'availability_zone': 'nova',
            'volume_type': volume_type,
            'volume_type_id': 1,
            'encryption_key_id': None,
            'qos_specs': None,
            'source_replicaid': None,
            'consistencygroup_id': None,
            'cgsnapshot_id': None,
            'group_id': None,
        }
        self.assertEqual(expected_result, result)
Пример #8
0
    def test_volume_reimage(self):
        volume = tests_utils.create_volume(self.context, status='downloading',
                                           previous_status='available')
        self.assertEqual(volume.status, 'downloading')
        self.assertEqual(volume.previous_status, 'available')
        self.volume.create_volume(self.context, volume)

        with mock.patch.object(self.volume.driver, 'copy_image_to_volume'
                               ) as mock_cp_img:
            self.volume.reimage(self.context, volume, self.image_meta)
            mock_cp_img.assert_called_once_with(self.context, volume,
                                                fake_image.FakeImageService(),
                                                self.image_meta['id'])
        self.assertEqual(volume.status, 'available')
Пример #9
0
    def test_copy_image_to_encrypted_volume_failed_fetch(
            self, excep, mock_detach_encryptor, mock_attach_encryptor,
            mock_detach_volume, mock_attach_volume, mock_fetch_to_raw,
            mock_get_connector_properties):
        properties = {}
        volume = tests_utils.create_volume(
            self.context,
            status='available',
            size=2,
            encryption_key_id=fake.ENCRYPTION_KEY_ID)
        volume_id = volume['id']
        volume = db.volume_get(context.get_admin_context(), volume_id)
        image_service = fake_image.FakeImageService()
        local_path = 'dev/sda'
        attach_info = {
            'device': {
                'path': local_path
            },
            'conn': {
                'driver_volume_type': 'iscsi',
                'data': {},
            }
        }

        mock_get_connector_properties.return_value = properties
        mock_attach_volume.return_value = [attach_info, volume]
        mock_fetch_to_raw.side_effect = excep

        encryption = {'encryption_key_id': fake.ENCRYPTION_KEY_ID}
        self.assertRaises(type(excep),
                          self.volume.driver.copy_image_to_encrypted_volume,
                          self.context, volume, image_service, fake.IMAGE_ID)

        mock_attach_volume.assert_called_once_with(self.context, volume,
                                                   properties)
        mock_attach_encryptor.assert_called_once_with(self.context,
                                                      attach_info, encryption)
        mock_fetch_to_raw.assert_called_once_with(self.context,
                                                  image_service,
                                                  fake.IMAGE_ID,
                                                  local_path,
                                                  '1M',
                                                  size=2)
        mock_detach_encryptor.assert_called_once_with(attach_info, encryption)
        mock_detach_volume.assert_called_once_with(self.context,
                                                   attach_info,
                                                   volume,
                                                   properties,
                                                   force=True)
Пример #10
0
 def test_copy_volume_to_image(self, mount_volume,
                               copy_volume_to_image,
                               unmount_volume):
     volume = fake_volume(self.ctxt)
     image_service = fake_image.FakeImageService()
     image = image_service.images[fake.IMAGE_ID]
     mount_volume.return_value = True
     copy_volume_to_image.return_value = True
     unmount_volume.return_value = True
     self.drv.copy_volume_to_image(self.ctxt, volume,
                                   image_service, image)
     mount_volume.assert_called_with(volume)
     copy_volume_to_image.assert_called_with(self.ctxt, volume,
                                             image_service, image)
     unmount_volume.assert_called_with(volume)
Пример #11
0
    def test_copy_image_to_encrypted_volume_failed_attach_encryptor(
            self, mock_detach_encryptor, mock_attach_encryptor,
            mock_detach_volume, mock_attach_volume, mock_fetch_to_raw,
            mock_get_connector_properties):
        properties = {}
        volume = tests_utils.create_volume(
            self.context,
            status='available',
            size=2,
            encryption_key_id=fake.ENCRYPTION_KEY_ID)
        volume_id = volume['id']
        volume = db.volume_get(context.get_admin_context(), volume_id)
        image_service = fake_image.FakeImageService()
        attach_info = {
            'device': {
                'path': 'dev/sda'
            },
            'conn': {
                'driver_volume_type': 'iscsi',
                'data': {},
            }
        }

        mock_get_connector_properties.return_value = properties
        mock_attach_volume.return_value = [attach_info, volume]
        raised_exception = os_brick.exception.VolumeEncryptionNotSupported(
            volume_id="123", volume_type="abc")
        mock_attach_encryptor.side_effect = raised_exception

        self.assertRaises(os_brick.exception.VolumeEncryptionNotSupported,
                          self.volume.driver.copy_image_to_encrypted_volume,
                          self.context, volume, image_service, fake.IMAGE_ID)

        encryption = {'encryption_key_id': fake.ENCRYPTION_KEY_ID}
        mock_attach_volume.assert_called_once_with(self.context, volume,
                                                   properties)
        mock_attach_encryptor.assert_called_once_with(self.context,
                                                      attach_info, encryption)
        self.assertFalse(mock_fetch_to_raw.called)
        self.assertFalse(mock_detach_encryptor.called)
        mock_detach_volume.assert_called_once_with(self.context,
                                                   attach_info,
                                                   volume,
                                                   properties,
                                                   force=True)
    def test__copy_image_to_volume(self, is_encrypted):
        fake_db = mock.MagicMock()
        fake_driver = mock.MagicMock()
        fake_volume_manager = mock.MagicMock()
        fake_manager = create_volume_manager.CreateVolumeFromSpecTask(
            fake_volume_manager, fake_db, fake_driver)
        key = fakes.ENCRYPTION_KEY_ID if is_encrypted else None
        volume = fake_volume.fake_volume_obj(self.ctxt, encryption_key_id=key)

        fake_image_service = fake_image.FakeImageService()
        image_id = fakes.IMAGE_ID
        image_location = 'abc'

        fake_manager._copy_image_to_volume(self.ctxt, volume, image_id,
                                           image_location, fake_image_service)
        if is_encrypted:
            fake_driver.copy_image_to_encrypted_volume.assert_called_once_with(
                self.ctxt, volume, fake_image_service, image_id)
        else:
            fake_driver.copy_image_to_volume.assert_called_once_with(
                self.ctxt, volume, fake_image_service, image_id)
Пример #13
0
 def setUp(self):
     super(VolumeReimageTestCase, self).setUp()
     self.patch('cinder.volume.volume_utils.clear_volume', autospec=True)
     fake_image.mock_image_service(self)
     self.image_meta = fake_image.FakeImageService().show(
         self.context, fake_constants.IMAGE_ID)