]> review.fuel-infra Code Review - openstack-build/cinder-build.git/commitdiff
Add exception handling for copy_volume_to_image()
authorZhengguang--reset-author <zhengguangou@gmail.com>
Tue, 6 May 2014 01:05:34 +0000 (09:05 +0800)
committerZhengguang--reset-author <zhengguangou@gmail.com>
Tue, 6 May 2014 03:57:02 +0000 (11:57 +0800)
The method require_driver_initialized occurs exception so that the
method volume_get will be not executed, and the variable 'volume'
will be not defined. In the "finally" code segment, the
volume['instance_uuid'] will get the UnboundLocalError, this patch
gets volume before require_driver_initialized to avoid the above
problem.

Change-Id: Ic8167383eb67c5016c9853da274455e0f202dc4d
Closes-Bug: #1322958

cinder/tests/test_volume.py
cinder/volume/manager.py

index a0016af73ecc9f61a9301c2f1825664822ad869a..e6c5d8188155b31ccaafef6ba3780c1111782cdf 100644 (file)
@@ -2540,6 +2540,23 @@ class CopyVolumeToImageTestCase(BaseVolumeTestCase):
         volume = db.volume_get(self.context, self.volume_id)
         self.assertEqual(volume['status'], 'available')
 
+    def test_copy_volume_to_image_driver_not_initialized(self):
+        # creating volume testdata
+        db.volume_create(self.context, self.volume_attrs)
+
+        # set initialized to False
+        self.volume.driver._initialized = False
+
+        # start test
+        self.assertRaises(exception.DriverNotInitialized,
+                          self.volume.copy_volume_to_image,
+                          self.context,
+                          self.volume_id,
+                          self.image_meta)
+
+        volume = db.volume_get(self.context, self.volume_id)
+        self.assertEqual(volume.status, 'available')
+
 
 class GetActiveByWindowTestCase(BaseVolumeTestCase):
     def setUp(self):
index 32fbfa51765565cb7c2a8887daddc9608cf02247..9535155bd4333ca1602da403e04058277752fb03 100644 (file)
@@ -673,12 +673,13 @@ class VolumeManager(manager.SchedulerDependentManager):
         """
         payload = {'volume_id': volume_id, 'image_id': image_meta['id']}
         try:
+            volume = self.db.volume_get(context, volume_id)
+
             # NOTE(flaper87): Verify the driver is enabled
             # before going forward. The exception will be caught
             # and the volume status updated.
             utils.require_driver_initialized(self.driver)
 
-            volume = self.db.volume_get(context, volume_id)
             image_service, image_id = \
                 glance.get_remote_image_service(context, image_meta['id'])
             self.driver.copy_volume_to_image(context, volume, image_service,