def test_detach_invalid_attachment_id(self):
"""Make sure if the attachment id isn't found we raise."""
attachment_id = "notfoundid"
- volume_id = "abc123"
- fake_volume = {'id': volume_id,
- 'status': 'available'}
- with mock.patch.object(db, 'volume_get') as mock_volume_get:
- mock_volume_get.return_value = fake_volume
- self.assertRaises(exception.VolumeAttachmentNotFound,
- self.volume.detach_volume,
- self.context,
- volume_id,
- attachment_id)
+ volume = tests_utils.create_volume(self.context,
+ admin_metadata={'readonly': 'True'},
+ multiattach=False,
+ **self.volume_params)
+ self.volume.detach_volume(self.context, volume['id'],
+ attachment_id)
+ volume = db.volume_get(self.context, volume['id'])
+ self.assertEqual('available', volume['status'])
+
+ instance_uuid = '12345678-1234-5678-1234-567812345678'
+ attached_host = 'fake_host'
+ mountpoint = '/dev/fake'
+ tests_utils.attach_volume(self.context, volume['id'],
+ instance_uuid, attached_host,
+ mountpoint)
+ self.volume.detach_volume(self.context, volume['id'],
+ attachment_id)
+ volume = db.volume_get(self.context, volume['id'])
+ self.assertEqual('in-use', volume['status'])
def test_detach_no_attachments(self):
+ self.volume_params['status'] = 'detaching'
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'])
+ self.volume.detach_volume(self.context, volume['id'])
+ volume = db.volume_get(self.context, volume['id'])
+ self.assertEqual('available', volume['status'])
def test_run_attach_detach_volume_for_instance_no_attachment_id(self):
"""Make sure volume can be attached and detached from instance."""
attachment = self.db.volume_attachment_get(context,
attachment_id)
except exception.VolumeAttachmentNotFound:
- LOG.error(_LE("Find attachment in detach_volume failed."),
- resource=volume)
- raise
+ LOG.info(_LI("Volume detach called, but volume not attached."),
+ resource=volume)
+ # We need to make sure the volume status is set to the correct
+ # status. It could be in detaching status now, and we don't
+ # want to leave it there.
+ self.db.volume_detached(context, volume_id, attachment_id)
+ return
else:
# We can try and degrade gracefully here by trying to detach
# a volume without the attachment_id here if the volume only has
attachment = attachments[0]
else:
# there aren't any attachments for this volume.
- msg = _("Detach volume failed, because there are currently no "
- "active attachments.")
- LOG.error(msg, resource=volume)
- raise exception.InvalidVolume(reason=msg)
+ # so set the status to available and move on.
+ LOG.info(_LI("Volume detach called, but volume not attached."),
+ resource=volume)
+ self.db.volume_update(context, volume_id,
+ {'status': 'available',
+ 'attach_status': 'detached'})
+ return
self._notify_about_volume_usage(context, volume, "detach.start")
try: