]> review.fuel-infra Code Review - openstack-build/cinder-build.git/commitdiff
Be safe with getting attachment
authorWalter A. Boring IV <walter.boring@hp.com>
Mon, 23 Mar 2015 23:39:37 +0000 (16:39 -0700)
committerWalter A. Boring IV <walter.boring@hp.com>
Wed, 25 Mar 2015 17:29:52 +0000 (10:29 -0700)
The volume manager wasn't being safe with trying to pull an
attachment out of the attachments list.  It's possible someone
is calling the manager to detach a volume that has no attachments.

This patch raises an InvalidVolume exception if we detect that
there are no attachments left to detach.

Change-Id: I684ed870d13a4efc6f64b0144c987e355428bc54
Closes-Bug: #1435574

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

index 7a0e5e90e14c86ca40c9559ceb46e59e8b2b1a66..41b39bbfc11488f0c9b951b9ad86491429736d70 100644 (file)
@@ -1819,6 +1819,16 @@ class VolumeTestCase(BaseVolumeTestCase):
                           volume_id,
                           attachment_id)
 
+    def test_detach_no_attachments(self):
+        volume = tests_utils.create_volume(self.context,
+                                           admin_metadata={'readonly': 'True'},
+                                           multiattach=False,
+                                           **self.volume_params)
+        self.assertRaises(exception.InvalidVolume,
+                          self.volume.detach_volume,
+                          self.context,
+                          volume['id'])
+
     def test_run_attach_detach_volume_for_instance_no_attachment_id(self):
         """Make sure volume can be attached and detached from instance."""
         mountpoint = "/dev/sdf"
index 3d605ac038a3a6067016178a78d72b86708e811a..e0235b8f43e08234852c69d660b6a4e3b36c7136 100644 (file)
@@ -836,8 +836,14 @@ class VolumeManager(manager.SchedulerDependentManager):
                         " this volume") % {'id': volume_id}
                 LOG.error(msg)
                 raise exception.InvalidVolume(reason=msg)
-            else:
+            elif len(attachments) == 1:
                 attachment = attachments[0]
+            else:
+                # there aren't any attachments for this volume.
+                msg = _("Volume %(id)s doesn't have any attachments "
+                        "to detach") % {'id': volume_id}
+                LOG.error(msg)
+                raise exception.InvalidVolume(reason=msg)
 
         volume = self.db.volume_get(context, volume_id)
         self._notify_about_volume_usage(context, volume, "detach.start")