Merge "Be safe with getting attachment"

This commit is contained in:
Jenkins 2015-04-01 22:24:06 +00:00 committed by Gerrit Code Review
commit ebe3234d9e
2 changed files with 17 additions and 1 deletions

View File

@ -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"

View File

@ -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")