Add nova profile use block_device_mapping_v2 image check

If nova profile use block_device_mapping_v2 schema, When this
parameter specifies the special source_type and uuid, we need
to test the special source_type to detect whether the uuid is
a valid parameter.

Example the source_type value 'image', the physical vm will use
bootable volume create.

Change-Id: I409fbf52a42de0988c5aa5a1e2dd15e2b983a811
Signed-off-by: Yuanbin.Chen <cybing4@gmail.com>
This commit is contained in:
Yuanbin.Chen 2018-03-19 10:52:33 +08:00
parent 8082fbc61f
commit a456b5464c
2 changed files with 60 additions and 2 deletions

View File

@ -448,11 +448,15 @@ class ServerProfile(base.Profile):
return True
def _resolve_bdm(self, bdm):
def _resolve_bdm(self, obj, bdm, reason=None):
for bd in bdm:
for key in self.BDM2_KEYS:
if bd[key] is None:
del bd[key]
if 'uuid' in bd and 'source_type' in bd:
if bd['source_type'] == 'image':
self._validate_image(obj, bd['uuid'], reason)
return bdm
def _check_security_groups(self, nc, net_spec, result):
@ -820,7 +824,7 @@ class ServerProfile(base.Profile):
block_device_mapping_v2 = self.properties[self.BLOCK_DEVICE_MAPPING_V2]
if block_device_mapping_v2 is not None:
kwargs['block_device_mapping_v2'] = self._resolve_bdm(
block_device_mapping_v2)
obj, block_device_mapping_v2, 'create')
user_data = self.properties[self.USER_DATA]
if user_data is not None:

View File

@ -202,6 +202,57 @@ class TestNovaServerBasic(base.SenlinTestCase):
mock_image.assert_called_once_with(node_obj, 'FAKE_IMAGE', 'create')
def test_do_create_bdm_invalid_image(self):
cc = mock.Mock()
nc = mock.Mock()
node_obj = mock.Mock(id='FAKE_NODE_ID', data={}, index=123,
cluster_id='FAKE_CLUSTER_ID')
bdm_v2 = [
{
'volume_size': 1,
'uuid': '6ce0be68',
'source_type': 'image',
'destination_type': 'volume',
'boot_index': 0,
},
]
spec = {
'type': 'os.nova.server',
'version': '1.0',
'properties': {
'flavor': 'FLAV',
'name': 'FAKE_SERVER_NAME',
'security_groups': ['HIGH_SECURITY_GROUP'],
'block_device_mapping_v2': bdm_v2,
}
}
profile = server.ServerProfile('s2', spec)
profile._computeclient = cc
profile._networkclient = nc
self._stubout_profile(profile, mock_image=True, mock_flavor=True,
mock_keypair=True)
err = exc.EResourceCreation(type='server', message='FOO')
mock_volume = self.patchobject(profile, '_resolve_bdm',
side_effect=err)
self.assertRaises(exc.EResourceCreation,
profile.do_create,
node_obj)
expected_volume = [{
'guest_format': None,
'boot_index': 0,
'uuid': '6ce0be68',
'volume_size': 1,
'device_name': None,
'disk_bus': None,
'source_type': 'image',
'device_type': None,
'destination_type': 'volume',
'delete_on_termination': None
}]
mock_volume.assert_called_once_with(
node_obj, expected_volume, 'create')
def test_do_create_invalid_flavor(self):
profile = server.ServerProfile('s2', self.spec)
self._stubout_profile(profile, mock_image=True)
@ -463,6 +514,9 @@ class TestNovaServerBasic(base.SenlinTestCase):
}
cc.server_create.assert_called_once_with(**attrs)
cc.server_get.assert_called_once_with('FAKE_ID')
profile._validate_image.assert_called_once_with(
node_obj, expected_volume['uuid'], 'create')
mock_zone_info.assert_called_once_with(node_obj, fake_server)
self.assertEqual('FAKE_ID', server_id)