From 28b5c7e101d45e2897480fe587aff7c7def2f161 Mon Sep 17 00:00:00 2001 From: xiexs Date: Wed, 6 Jul 2016 11:04:23 -0400 Subject: [PATCH] Add strict Boolean checking for volume manage There is no strict boolean checking for the parameter "bootable" of API /os-volume-manage, so that any invalid boolean value can be specified. This patch adds a strict checking for it to prevent invalid value, and adds a test for this change as well. Change-Id: I0d79a0bb173aaeeea0fe6d735213c70c109ccd69 Partial-Bug: #1594261 --- cinder/api/contrib/volume_manage.py | 2 +- cinder/tests/unit/api/contrib/test_volume_manage.py | 11 ++++++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/cinder/api/contrib/volume_manage.py b/cinder/api/contrib/volume_manage.py index 855437032ef..89ed5a82567 100644 --- a/cinder/api/contrib/volume_manage.py +++ b/cinder/api/contrib/volume_manage.py @@ -137,7 +137,7 @@ class VolumeManageController(wsgi.Controller): kwargs['description'] = volume.get('description', None) kwargs['metadata'] = volume.get('metadata', None) kwargs['availability_zone'] = volume.get('availability_zone', None) - kwargs['bootable'] = volume.get('bootable', False) + kwargs['bootable'] = utils.get_bool_param('bootable', volume) try: new_volume = self.volume_api.manage_existing(context, volume['host'], diff --git a/cinder/tests/unit/api/contrib/test_volume_manage.py b/cinder/tests/unit/api/contrib/test_volume_manage.py index 250178de1e5..3b3e805d2b8 100644 --- a/cinder/tests/unit/api/contrib/test_volume_manage.py +++ b/cinder/tests/unit/api/contrib/test_volume_manage.py @@ -202,6 +202,14 @@ class VolumeManageTest(test.TestCase): self.assertEqual(400, res.status_int) pass + def test_manage_volume_with_invalid_bootable(self): + """Test correct failure when invalid bool value is specified.""" + body = {'volume': {'host': 'host_ok', + 'ref': 'fake_ref', + 'bootable': 'InvalidBool'}} + res = self._get_resp_post(body) + self.assertEqual(400, res.status_int) + @mock.patch('cinder.volume.api.API.manage_existing', api_manage) @mock.patch( 'cinder.api.openstack.wsgi.Controller.validate_name_and_description') @@ -213,7 +221,8 @@ class VolumeManageTest(test.TestCase): """ body = {'volume': {'host': 'host_ok', 'ref': 'fake_ref', - 'volume_type': fake.VOLUME_TYPE_ID}} + 'volume_type': fake.VOLUME_TYPE_ID, + 'bootable': True}} res = self._get_resp_post(body) self.assertEqual(202, res.status_int, res) self.assertTrue(mock_validate.called)