From c08211b19e2b9c38e537e155d11ffced37af0e85 Mon Sep 17 00:00:00 2001 From: Zhengguang--reset-author Date: Tue, 6 May 2014 09:05:34 +0800 Subject: [PATCH] Add exception handling for copy_volume_to_image() 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 | 17 +++++++++++++++++ cinder/volume/manager.py | 3 ++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/cinder/tests/test_volume.py b/cinder/tests/test_volume.py index a0016af73..e6c5d8188 100644 --- a/cinder/tests/test_volume.py +++ b/cinder/tests/test_volume.py @@ -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): diff --git a/cinder/volume/manager.py b/cinder/volume/manager.py index 32fbfa517..9535155bd 100644 --- a/cinder/volume/manager.py +++ b/cinder/volume/manager.py @@ -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, -- 2.45.2