From: Eric Harney Date: Tue, 8 Sep 2015 22:40:53 +0000 (-0400) Subject: Filter scheduler: Fix KeyError on invalid create request X-Git-Url: https://review.fuel-infra.org/gitweb?a=commitdiff_plain;h=364e8cb92b0584de84a99bb9e5a664d3d94657da;p=openstack-build%2Fcinder-build.git Filter scheduler: Fix KeyError on invalid create request An invalid request can generate a KeyError here rather than the intended failure message. Closes-Bug: #1493580 Change-Id: Ia73d8c2834e564ee14d72303001e11be2ea14900 --- diff --git a/cinder/scheduler/filter_scheduler.py b/cinder/scheduler/filter_scheduler.py index 3e1a81caf..66604f3c4 100644 --- a/cinder/scheduler/filter_scheduler.py +++ b/cinder/scheduler/filter_scheduler.py @@ -111,9 +111,10 @@ class FilterScheduler(driver.Scheduler): if host_state.host == host: return host_state + volume_id = request_spec.get('volume_id', '??volume_id missing??') raise exception.NoValidHost(reason=_('Cannot place volume %(id)s on ' '%(host)s') % - {'id': request_spec['volume_id'], + {'id': volume_id, 'host': host}) def find_retype_host(self, context, request_spec, filter_properties=None, diff --git a/cinder/tests/unit/scheduler/test_filter_scheduler.py b/cinder/tests/unit/scheduler/test_filter_scheduler.py index b2c7dfc43..26bf65084 100644 --- a/cinder/tests/unit/scheduler/test_filter_scheduler.py +++ b/cinder/tests/unit/scheduler/test_filter_scheduler.py @@ -118,6 +118,21 @@ class FilterSchedulerTestCase(test_scheduler.SchedulerTestCase): self.assertRaises(exception.NoValidHost, sched.schedule_create_volume, fake_context, request_spec, {}) + def test_create_volume_no_hosts_invalid_req(self): + sched = fakes.FakeFilterScheduler() + + fake_context = context.RequestContext('user', 'project') + + # request_spec is missing 'volume_id' + request_spec = {'volume_properties': {'project_id': 1, + 'size': 1}, + 'volume_type': {'name': 'LVM_iSCSI'}} + self.assertRaises(exception.NoValidHost, + sched.schedule_create_volume, + fake_context, + request_spec, + {}) + @mock.patch('cinder.scheduler.host_manager.HostManager.' 'get_all_host_states') def test_create_volume_non_admin(self, _mock_get_all_host_states):