Ejemplo n.º 1
0
def vt_get_volume_type_by_name(context, name):
    """Replacement for cinder.volume.volume_types.get_volume_type_by_name.

    Overrides cinder.volume.volume_types.get_volume_type_by_name to return
    the volume type based on inspection of our fake structure, rather than
    going to the Cinder DB.
    """
    if name == fake_vt['name']:
        return fake_vt
    raise exception.VolumeTypeNotFoundByName(volume_type_name=name)
Ejemplo n.º 2
0
    def test_extract_image_volume_type_from_image_invalid_type(
            self,
            fake_get_type_id,
            fake_get_def_vol_type,
            fake_get_qos,
            fake_is_encrypted,
            fake_db_get_vol_type):

        image_volume_type = 'invalid'
        fake_image_service = fake_image.FakeImageService()
        image_id = 7
        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_mgr.MockKeyManager()

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

        fake_is_encrypted.return_value = False
        fake_get_type_id.return_value = 1
        fake_get_def_vol_type.return_value = 'fake_vol_type'
        fake_db_get_vol_type.side_effect = (
            exception.VolumeTypeNotFoundByName(volume_type_name='invalid'))
        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)
        expected_result = {'size': 1,
                           'snapshot_id': None,
                           'source_volid': None,
                           'availability_zone': 'nova',
                           'volume_type': 'fake_vol_type',
                           'volume_type_id': 1,
                           'encryption_key_id': None,
                           'qos_specs': None,
                           'source_replicaid': None,
                           'consistencygroup_id': None,
                           'cgsnapshot_id': None, }
        self.assertEqual(expected_result, result)
Ejemplo n.º 3
0
def get_default_volume_type():
    """Get the default volume type."""
    name = CONF.default_volume_type
    vol_type = {}
    ctxt = context.get_admin_context()
    if name:
        try:
            vol_type = get_volume_type_by_name(ctxt, name)
        except exception.VolumeTypeNotFoundByName:
            # Couldn't find volume type with the name in default_volume_type
            # flag, record this issue and raise exception
            # TODO(zhiteng) consider add notification to warn admin
            LOG.exception('Default volume type is not found. '
                          'Please check default_volume_type config:')
            raise exception.VolumeTypeNotFoundByName(volume_type_name=name)
    else:
        vol_type = get_volume_type_by_name(ctxt, DEFAULT_VOLUME_TYPE)

    return vol_type
Ejemplo n.º 4
0
def return_volume_types_destroy(context, name):
    if name == "777":
        raise exception.VolumeTypeNotFoundByName(volume_type_name=name)
    pass
Ejemplo n.º 5
0
def return_volume_types_get_by_name(context, name):
    if name == "777":
        raise exception.VolumeTypeNotFoundByName(volume_type_name=name)
    return stub_volume_type(int(name.split("_")[2]))
Ejemplo n.º 6
0
def return_volume_types_destroy(context, name):
    if name == fake.WILL_NOT_BE_FOUND_ID:
        raise exception.VolumeTypeNotFoundByName(volume_type_name=name)
    pass
Ejemplo n.º 7
0
def return_volume_types_get_by_name(context, name):
    if name == NOT_FOUND_VOLUME_TYPE:
        raise exception.VolumeTypeNotFoundByName(volume_type_name=name)
    return stub_volume_type(name.split("_")[2])
Ejemplo n.º 8
0
class ExtractVolumeRequestTaskValidationsTestCase(test.TestCase):
    """Test validation code.

    The ExtractVolumeRequestTask takes a set of inputs that will form a
    volume-create request and validates them, inferring values for "missing"
    inputs.

    This class tests the validation code, not the Task itself.
    """
    def setUp(self):
        super(ExtractVolumeRequestTaskValidationsTestCase, self).setUp()
        self.context = context.get_admin_context()

    fake_vol_type = 'vt-from-volume_type'
    fake_source_vol = {'volume_type_id': 'vt-from-source_vol'}
    fake_snapshot = {'volume_type_id': 'vt-from-snapshot'}
    fake_img_vol_type_id = 'vt-from-image_volume_type_id'
    fake_config_value = 'vt-from-config-value'

    big_ass_data_tuple = (
        # case 0: null params and no configured default should
        # result in the system default volume type
        {
            'param_vol_type': None,
            'param_source_vol': None,
            'param_snap': None,
            'param_img_vol_type_id': None,
            'config_value': volume_types.DEFAULT_VOLUME_TYPE,
            'expected_vol_type': volume_types.DEFAULT_VOLUME_TYPE
        },
        # case set 1: if a volume_type is passed, should always be selected
        {
            'param_vol_type': fake_vol_type,
            'param_source_vol': None,
            'param_snap': None,
            'param_img_vol_type_id': None,
            'config_value': volume_types.DEFAULT_VOLUME_TYPE,
            'expected_vol_type': 'vt-from-volume_type'
        },
        {
            'param_vol_type': fake_vol_type,
            'param_source_vol': fake_source_vol,
            'param_snap': fake_snapshot,
            'param_img_vol_type_id': fake_img_vol_type_id,
            'config_value': fake_config_value,
            'expected_vol_type': 'vt-from-volume_type'
        },
        # case set 2: if no volume_type is passed, the vt from the
        # source_volume should be selected
        {
            'param_vol_type': None,
            'param_source_vol': fake_source_vol,
            'param_snap': None,
            'param_img_vol_type_id': None,
            'config_value': volume_types.DEFAULT_VOLUME_TYPE,
            'expected_vol_type': 'vt-from-source_vol'
        },
        {
            'param_vol_type': None,
            'param_source_vol': fake_source_vol,
            'param_snap': fake_snapshot,
            'param_img_vol_type_id': fake_img_vol_type_id,
            'config_value': fake_config_value,
            'expected_vol_type': 'vt-from-source_vol'
        },
        # case set 3: no volume_type, no source_volume, so snapshot's type
        # should be selected
        {
            'param_vol_type': None,
            'param_source_vol': None,
            'param_snap': fake_snapshot,
            'param_img_vol_type_id': None,
            'config_value': volume_types.DEFAULT_VOLUME_TYPE,
            'expected_vol_type': 'vt-from-snapshot'
        },
        {
            'param_vol_type': None,
            'param_source_vol': None,
            'param_snap': fake_snapshot,
            'param_img_vol_type_id': fake_img_vol_type_id,
            'config_value': fake_config_value,
            'expected_vol_type': 'vt-from-snapshot'
        },
        # case set 4: no volume_type, no source_volume, no snapshot --
        # use the volume_type from the image metadata
        {
            'param_vol_type': None,
            'param_source_vol': None,
            'param_snap': None,
            'param_img_vol_type_id': fake_img_vol_type_id,
            'config_value': volume_types.DEFAULT_VOLUME_TYPE,
            'expected_vol_type': 'vt-from-image_volume_type_id'
        },
        {
            'param_vol_type': None,
            'param_source_vol': None,
            'param_snap': None,
            'param_img_vol_type_id': fake_img_vol_type_id,
            'config_value': fake_config_value,
            'expected_vol_type': 'vt-from-image_volume_type_id'
        },
        # case 5: params all null, should use configured volume_type
        {
            'param_vol_type': None,
            'param_source_vol': None,
            'param_snap': None,
            'param_img_vol_type_id': None,
            'config_value': fake_config_value,
            'expected_vol_type': 'vt-from-config-value'
        })

    def reflect_second(a, b):
        return b

    @ddt.data(*big_ass_data_tuple)
    @mock.patch('cinder.objects.VolumeType.get_by_name_or_id',
                side_effect=reflect_second)
    @mock.patch('cinder.volume.volume_types.get_volume_type_by_name',
                side_effect=reflect_second)
    @ddt.unpack
    def test__get_volume_type(self, mock_get_volume_type_by_name,
                              mock_get_by_name_or_id, param_vol_type,
                              param_source_vol, param_snap,
                              param_img_vol_type_id, config_value,
                              expected_vol_type):

        self.flags(default_volume_type=config_value)

        test_fn = create_volume.ExtractVolumeRequestTask._get_volume_type

        self.assertEqual(
            expected_vol_type,
            test_fn(self.context, param_vol_type, param_source_vol, param_snap,
                    param_img_vol_type_id))

    # Before the Train release, an invalid volume type specifier
    # would not raise an exception; it would log an error and you'd
    # get a volume with volume_type == None.  We want to verify that
    # specifying a non-existent volume_type always raises an exception
    smaller_data_tuple = ({
        'param_source_vol': fake_source_vol,
        'param_snap': None,
        'param_img_vol_type_id': None,
        'config_value': None
    }, {
        'param_source_vol': None,
        'param_snap': fake_snapshot,
        'param_img_vol_type_id': None,
        'config_value': None
    }, {
        'param_source_vol': None,
        'param_snap': None,
        'param_img_vol_type_id': fake_img_vol_type_id,
        'config_value': None
    }, {
        'param_source_vol': None,
        'param_snap': None,
        'param_img_vol_type_id': None,
        'config_value': fake_config_value
    })

    @ddt.data(*smaller_data_tuple)
    @mock.patch('cinder.objects.VolumeType.get_by_name_or_id',
                side_effect=exception.VolumeTypeNotFoundByName(
                    volume_type_name="get_by_name_or_id"))
    @mock.patch('cinder.volume.volume_types.get_volume_type_by_name',
                side_effect=exception.VolumeTypeNotFoundByName(
                    volume_type_name="get_by_name"))
    @ddt.unpack
    def test_neg_get_volume_type(self, mock_get_volume_type_by_name,
                                 mock_get_by_name_or_id, param_source_vol,
                                 param_snap, param_img_vol_type_id,
                                 config_value):

        self.flags(default_volume_type=config_value)

        test_fn = create_volume.ExtractVolumeRequestTask._get_volume_type

        if config_value:
            self.assertRaises(exception.VolumeTypeDefaultMisconfiguredError,
                              test_fn, self.context, None, param_source_vol,
                              param_snap, param_img_vol_type_id)
        else:
            self.assertRaises(exception.VolumeTypeNotFoundByName, test_fn,
                              self.context, None, param_source_vol, param_snap,
                              param_img_vol_type_id)