]> review.fuel-infra Code Review - openstack-build/cinder-build.git/commitdiff
Filter scheduler: Fix KeyError on invalid create request
authorEric Harney <eharney@redhat.com>
Tue, 8 Sep 2015 22:40:53 +0000 (18:40 -0400)
committerEric Harney <eharney@redhat.com>
Tue, 8 Sep 2015 22:46:32 +0000 (18:46 -0400)
An invalid request can generate a KeyError here rather
than the intended failure message.

Closes-Bug: #1493580

Change-Id: Ia73d8c2834e564ee14d72303001e11be2ea14900

cinder/scheduler/filter_scheduler.py
cinder/tests/unit/scheduler/test_filter_scheduler.py

index 3e1a81cafaf8c4ceeeed93dd4b9d52262aeda151..66604f3c467c67af2cfcdfac22d80993a36638aa 100644 (file)
@@ -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,
index b2c7dfc43b7639ef624ba04df0db1db64b27b6fd..26bf6508428463fd9157f8beba518ece481b438c 100644 (file)
@@ -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):