V3 Pause: treat case when driver does not implement the operation

Some virt drivers may not support this operation.

Change-Id: I21a3fbb6e41e4a32810fd8c76f0fabb7aeff9244
This commit is contained in:
Gary Kotton 2014-06-08 01:31:52 -07:00
parent 4d7a65d9f1
commit 1196abe0ea
3 changed files with 38 additions and 2 deletions

View File

@ -21,6 +21,7 @@ from nova.api.openstack import extensions
from nova.api.openstack import wsgi from nova.api.openstack import wsgi
from nova import compute from nova import compute
from nova import exception from nova import exception
from nova.openstack.common.gettextutils import _
from nova.openstack.common import log as logging from nova.openstack.common import log as logging
LOG = logging.getLogger(__name__) LOG = logging.getLogger(__name__)
@ -37,7 +38,7 @@ class PauseServerController(wsgi.Controller):
super(PauseServerController, self).__init__(*args, **kwargs) super(PauseServerController, self).__init__(*args, **kwargs)
self.compute_api = compute.API() self.compute_api = compute.API()
@extensions.expected_errors((404, 409)) @extensions.expected_errors((404, 409, 501))
@wsgi.action('pause') @wsgi.action('pause')
def _pause(self, req, id, body): def _pause(self, req, id, body):
"""Permit Admins to pause the server.""" """Permit Admins to pause the server."""
@ -54,9 +55,12 @@ class PauseServerController(wsgi.Controller):
'pause') 'pause')
except exception.InstanceNotFound as e: except exception.InstanceNotFound as e:
raise exc.HTTPNotFound(explanation=e.format_message()) raise exc.HTTPNotFound(explanation=e.format_message())
except NotImplementedError:
msg = _("Virt driver does not implement pause function.")
raise exc.HTTPNotImplemented(explanation=msg)
return webob.Response(status_int=202) return webob.Response(status_int=202)
@extensions.expected_errors((404, 409)) @extensions.expected_errors((404, 409, 501))
@wsgi.action('unpause') @wsgi.action('unpause')
def _unpause(self, req, id, body): def _unpause(self, req, id, body):
"""Permit Admins to unpause the server.""" """Permit Admins to unpause the server."""
@ -73,6 +77,9 @@ class PauseServerController(wsgi.Controller):
'unpause') 'unpause')
except exception.InstanceNotFound as e: except exception.InstanceNotFound as e:
raise exc.HTTPNotFound(explanation=e.format_message()) raise exc.HTTPNotFound(explanation=e.format_message())
except NotImplementedError:
msg = _("Virt driver does not implement pause function.")
raise exc.HTTPNotImplemented(explanation=msg)
return webob.Response(status_int=202) return webob.Response(status_int=202)

View File

@ -90,6 +90,28 @@ class CommonMixin(object):
self.mox.VerifyAll() self.mox.VerifyAll()
self.mox.UnsetStubs() self.mox.UnsetStubs()
def _test_not_implemented_state(self, action, method=None):
if method is None:
method = action
instance = self._stub_instance_get()
body = {}
compute_api_args_map = {}
args, kwargs = compute_api_args_map.get(action, ((), {}))
getattr(self.compute_api, method)(self.context, instance,
*args, **kwargs).AndRaise(
NotImplementedError())
self.mox.ReplayAll()
res = self._make_request('/servers/%s/action' % instance.uuid,
{action: body})
self.assertEqual(501, res.status_int)
# Do these here instead of tearDown because this method is called
# more than once for the same test case
self.mox.VerifyAll()
self.mox.UnsetStubs()
def _test_invalid_state(self, action, method=None, body_map=None, def _test_invalid_state(self, action, method=None, body_map=None,
compute_api_args_map=None): compute_api_args_map=None):
if method is None: if method is None:

View File

@ -38,6 +38,13 @@ class PauseServerTests(admin_only_action_common.CommonTests):
def test_pause_unpause(self): def test_pause_unpause(self):
self._test_actions(['pause', 'unpause']) self._test_actions(['pause', 'unpause'])
def test_actions_raise_on_not_implemented(self):
for action in ['pause', 'unpause']:
self.mox.StubOutWithMock(self.compute_api, action)
self._test_not_implemented_state(action)
# Re-mock this.
self.mox.StubOutWithMock(self.compute_api, 'get')
def test_pause_unpause_with_non_existed_instance(self): def test_pause_unpause_with_non_existed_instance(self):
self._test_actions_with_non_existed_instance(['pause', 'unpause']) self._test_actions_with_non_existed_instance(['pause', 'unpause'])