Catch DevicePathInUse in attach_volume

DevicePathInUse might be raised in attach action,
we didn't catch it so a 500 error might be returned
to end user.

Change-Id: Ic9f0979b5adef28bb47756e7fc2ce5a3d6493298
Closes-Bug: 1621452
This commit is contained in:
jichenjc 2016-09-07 04:41:06 +08:00 committed by Matt Riedemann
parent c51af64dae
commit 6051499a85
2 changed files with 22 additions and 0 deletions
nova
api/openstack/compute
tests/unit/api/openstack/compute

@ -327,6 +327,8 @@ class VolumeAttachmentController(wsgi.Controller):
raise exc.HTTPNotFound(explanation=e.format_message())
except exception.InstanceIsLocked as e:
raise exc.HTTPConflict(explanation=e.format_message())
except exception.DevicePathInUse as e:
raise exc.HTTPConflict(explanation=e.format_message())
except exception.InstanceInvalidState as state_error:
common.raise_http_conflict_for_instance_invalid_state(state_error,
'attach_volume', server_id)

@ -635,6 +635,26 @@ class VolumeAttachTestsV21(test.NoDBTestCase):
self.assertRaises(self.validation_error, self.attachments.create,
req, FAKE_UUID, body=body)
@mock.patch.object(compute_api.API, 'attach_volume',
side_effect=exception.DevicePathInUse(path='/dev/sda'))
def test_attach_volume_device_in_use(self, mock_attach):
body = {
'volumeAttachment': {
'device': '/dev/sda',
'volumeId': FAKE_UUID_A,
}
}
req = fakes.HTTPRequest.blank('/v2/servers/id/os-volume_attachments')
req.method = 'POST'
req.body = jsonutils.dump_as_bytes({})
req.headers['content-type'] = 'application/json'
req.environ['nova.context'] = self.context
self.assertRaises(webob.exc.HTTPConflict, self.attachments.create,
req, FAKE_UUID, body=body)
def test_attach_volume_without_volumeId(self):
self.stubs.Set(compute_api.API,
'attach_volume',