From: Walter A. Boring IV Date: Mon, 23 Mar 2015 23:39:37 +0000 (-0700) Subject: Be safe with getting attachment X-Git-Url: https://review.fuel-infra.org/gitweb?a=commitdiff_plain;h=bc7a284bd2fd27d0f29a8facd5a67abc90667f26;p=openstack-build%2Fcinder-build.git Be safe with getting attachment 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 --- diff --git a/cinder/tests/test_volume.py b/cinder/tests/test_volume.py index 7a0e5e90e..41b39bbfc 100644 --- a/cinder/tests/test_volume.py +++ b/cinder/tests/test_volume.py @@ -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" diff --git a/cinder/volume/manager.py b/cinder/volume/manager.py index 3d605ac03..e0235b8f4 100644 --- a/cinder/volume/manager.py +++ b/cinder/volume/manager.py @@ -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")