Add strict Boolean checking for volume create
There is no strict boolean checking for the parameter "multiattach" of API /volumes, so that any invalid boolean value can be specified. This patch adds a strict checking for it to prevent invalid value, and adds tests for this change as well. Change-Id: I2a5dc45a3b238a2d875d7fbf7159eb91d1f111bf Partial-Bug: #1594261
This commit is contained in:
parent
9af8e7125b
commit
505ccfd62f
|
@ -279,8 +279,7 @@ class VolumeController(wsgi.Controller):
|
||||||
|
|
||||||
kwargs['availability_zone'] = volume.get('availability_zone', None)
|
kwargs['availability_zone'] = volume.get('availability_zone', None)
|
||||||
kwargs['scheduler_hints'] = volume.get('scheduler_hints', None)
|
kwargs['scheduler_hints'] = volume.get('scheduler_hints', None)
|
||||||
multiattach = volume.get('multiattach', False)
|
kwargs['multiattach'] = utils.get_bool_param('multiattach', volume)
|
||||||
kwargs['multiattach'] = multiattach
|
|
||||||
|
|
||||||
new_volume = self.volume_api.create(context,
|
new_volume = self.volume_api.create(context,
|
||||||
size,
|
size,
|
||||||
|
|
|
@ -21,6 +21,7 @@ from cinder import objects
|
||||||
from cinder.objects import fields
|
from cinder.objects import fields
|
||||||
from cinder.tests.unit import fake_constants as fake
|
from cinder.tests.unit import fake_constants as fake
|
||||||
from cinder.tests.unit import fake_volume
|
from cinder.tests.unit import fake_volume
|
||||||
|
from cinder import utils
|
||||||
|
|
||||||
|
|
||||||
DEFAULT_VOL_NAME = "displayname"
|
DEFAULT_VOL_NAME = "displayname"
|
||||||
|
@ -89,6 +90,7 @@ def stub_volume_create(self, context, size, name, description, snapshot=None,
|
||||||
vol['source_volid'] = source_volume.get('id')
|
vol['source_volid'] = source_volume.get('id')
|
||||||
vol['bootable'] = False
|
vol['bootable'] = False
|
||||||
vol['volume_attachment'] = []
|
vol['volume_attachment'] = []
|
||||||
|
vol['multiattach'] = utils.get_bool_param('multiattach', param)
|
||||||
try:
|
try:
|
||||||
vol['snapshot_id'] = snapshot['id']
|
vol['snapshot_id'] = snapshot['id']
|
||||||
except (KeyError, TypeError):
|
except (KeyError, TypeError):
|
||||||
|
|
|
@ -137,7 +137,8 @@ class VolumeApiTest(test.TestCase):
|
||||||
consistencygroup_id=None,
|
consistencygroup_id=None,
|
||||||
volume_type=None,
|
volume_type=None,
|
||||||
image_ref=None,
|
image_ref=None,
|
||||||
image_id=None):
|
image_id=None,
|
||||||
|
multiattach=False):
|
||||||
vol = {"size": size,
|
vol = {"size": size,
|
||||||
"name": name,
|
"name": name,
|
||||||
"description": description,
|
"description": description,
|
||||||
|
@ -147,6 +148,7 @@ class VolumeApiTest(test.TestCase):
|
||||||
"source_replica": source_replica,
|
"source_replica": source_replica,
|
||||||
"consistencygroup_id": consistencygroup_id,
|
"consistencygroup_id": consistencygroup_id,
|
||||||
"volume_type": volume_type,
|
"volume_type": volume_type,
|
||||||
|
"multiattach": multiattach,
|
||||||
}
|
}
|
||||||
|
|
||||||
if image_id is not None:
|
if image_id is not None:
|
||||||
|
@ -169,7 +171,8 @@ class VolumeApiTest(test.TestCase):
|
||||||
attachments=None,
|
attachments=None,
|
||||||
volume_type=stubs.DEFAULT_VOL_TYPE,
|
volume_type=stubs.DEFAULT_VOL_TYPE,
|
||||||
status=stubs.DEFAULT_VOL_STATUS,
|
status=stubs.DEFAULT_VOL_STATUS,
|
||||||
with_migration_status=False):
|
with_migration_status=False,
|
||||||
|
multiattach=False):
|
||||||
metadata = metadata or {}
|
metadata = metadata or {}
|
||||||
attachments = attachments or []
|
attachments = attachments or []
|
||||||
volume = {'volume':
|
volume = {'volume':
|
||||||
|
@ -193,7 +196,7 @@ class VolumeApiTest(test.TestCase):
|
||||||
'metadata': metadata,
|
'metadata': metadata,
|
||||||
'name': name,
|
'name': name,
|
||||||
'replication_status': 'disabled',
|
'replication_status': 'disabled',
|
||||||
'multiattach': False,
|
'multiattach': multiattach,
|
||||||
'size': size,
|
'size': size,
|
||||||
'snapshot_id': snapshot_id,
|
'snapshot_id': snapshot_id,
|
||||||
'source_volid': source_volid,
|
'source_volid': source_volid,
|
||||||
|
@ -563,6 +566,37 @@ class VolumeApiTest(test.TestCase):
|
||||||
req,
|
req,
|
||||||
body)
|
body)
|
||||||
|
|
||||||
|
def test_volume_create_with_invalid_multiattach(self):
|
||||||
|
vol = self._vol_in_request_body(multiattach="InvalidBool")
|
||||||
|
body = {"volume": vol}
|
||||||
|
req = fakes.HTTPRequest.blank('/v2/volumes')
|
||||||
|
|
||||||
|
self.assertRaises(exception.InvalidParameterValue,
|
||||||
|
self.controller.create,
|
||||||
|
req,
|
||||||
|
body)
|
||||||
|
|
||||||
|
@mock.patch.object(volume_api.API, 'create', autospec=True)
|
||||||
|
@mock.patch.object(volume_api.API, 'get', autospec=True)
|
||||||
|
@mock.patch.object(db.sqlalchemy.api, '_volume_type_get_full',
|
||||||
|
autospec=True)
|
||||||
|
def test_volume_create_with_valid_multiattach(self,
|
||||||
|
volume_type_get,
|
||||||
|
get, create):
|
||||||
|
create.side_effect = stubs.stub_volume_api_create
|
||||||
|
get.side_effect = stubs.stub_volume_get
|
||||||
|
volume_type_get.side_effect = stubs.stub_volume_type_get
|
||||||
|
|
||||||
|
vol = self._vol_in_request_body(multiattach=True)
|
||||||
|
body = {"volume": vol}
|
||||||
|
|
||||||
|
ex = self._expected_vol_from_controller(multiattach=True)
|
||||||
|
|
||||||
|
req = fakes.HTTPRequest.blank('/v2/volumes')
|
||||||
|
res_dict = self.controller.create(req, body)
|
||||||
|
|
||||||
|
self.assertEqual(ex, res_dict)
|
||||||
|
|
||||||
@mock.patch(
|
@mock.patch(
|
||||||
'cinder.api.openstack.wsgi.Controller.validate_name_and_description')
|
'cinder.api.openstack.wsgi.Controller.validate_name_and_description')
|
||||||
def test_volume_update(self, mock_validate):
|
def test_volume_update(self, mock_validate):
|
||||||
|
|
Loading…
Reference in New Issue