diff --git a/cinder/tests/test_volume.py b/cinder/tests/test_volume.py index 71bcf437889..fc511040e61 100644 --- a/cinder/tests/test_volume.py +++ b/cinder/tests/test_volume.py @@ -1879,6 +1879,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 69bb372e1cf..63df538b846 100644 --- a/cinder/volume/manager.py +++ b/cinder/volume/manager.py @@ -854,8 +854,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")