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