Merge "Remove mox used in tests/unit/api/openstack/compute/test_server_start_stop"
This commit is contained in:
@@ -12,7 +12,7 @@
|
|||||||
# License for the specific language governing permissions and limitations
|
# License for the specific language governing permissions and limitations
|
||||||
# under the License.
|
# under the License.
|
||||||
|
|
||||||
from mox3 import mox
|
import mock
|
||||||
import six
|
import six
|
||||||
import webob
|
import webob
|
||||||
|
|
||||||
@@ -22,6 +22,7 @@ from nova.api.openstack.compute import extension_info
|
|||||||
from nova.api.openstack.compute import servers \
|
from nova.api.openstack.compute import servers \
|
||||||
as server_v21
|
as server_v21
|
||||||
from nova.compute import api as compute_api
|
from nova.compute import api as compute_api
|
||||||
|
from nova import db
|
||||||
from nova import exception
|
from nova import exception
|
||||||
from nova import policy
|
from nova import policy
|
||||||
from nova import test
|
from nova import test
|
||||||
@@ -29,30 +30,6 @@ from nova.tests.unit.api.openstack import fakes
|
|||||||
from nova.tests import uuidsentinel as uuids
|
from nova.tests import uuidsentinel as uuids
|
||||||
|
|
||||||
|
|
||||||
def fake_instance_get(context, instance_id,
|
|
||||||
columns_to_join=None, use_slave=False):
|
|
||||||
result = fakes.stub_instance(id=1, uuid=instance_id)
|
|
||||||
result['created_at'] = None
|
|
||||||
result['deleted_at'] = None
|
|
||||||
result['updated_at'] = None
|
|
||||||
result['deleted'] = 0
|
|
||||||
result['info_cache'] = {'network_info': '[]',
|
|
||||||
'instance_uuid': result['uuid']}
|
|
||||||
return result
|
|
||||||
|
|
||||||
|
|
||||||
def fake_start_stop_not_ready(self, context, instance):
|
|
||||||
raise exception.InstanceNotReady(instance_id=instance["uuid"])
|
|
||||||
|
|
||||||
|
|
||||||
def fake_start_stop_locked_server(self, context, instance):
|
|
||||||
raise exception.InstanceIsLocked(instance_uuid=instance['uuid'])
|
|
||||||
|
|
||||||
|
|
||||||
def fake_start_stop_invalid_state(self, context, instance):
|
|
||||||
raise exception.InstanceIsLocked(instance_uuid=instance['uuid'])
|
|
||||||
|
|
||||||
|
|
||||||
class ServerStartStopTestV21(test.TestCase):
|
class ServerStartStopTestV21(test.TestCase):
|
||||||
start_policy = "os_compute_api:servers:start"
|
start_policy = "os_compute_api:servers:start"
|
||||||
stop_policy = "os_compute_api:servers:stop"
|
stop_policy = "os_compute_api:servers:stop"
|
||||||
@@ -61,104 +38,110 @@ class ServerStartStopTestV21(test.TestCase):
|
|||||||
super(ServerStartStopTestV21, self).setUp()
|
super(ServerStartStopTestV21, self).setUp()
|
||||||
self._setup_controller()
|
self._setup_controller()
|
||||||
self.req = fakes.HTTPRequest.blank('')
|
self.req = fakes.HTTPRequest.blank('')
|
||||||
|
self.stub_out('nova.db.instance_get_by_uuid',
|
||||||
|
fakes.fake_instance_get())
|
||||||
|
|
||||||
def _setup_controller(self):
|
def _setup_controller(self):
|
||||||
ext_info = extension_info.LoadedExtensionInfo()
|
ext_info = extension_info.LoadedExtensionInfo()
|
||||||
self.controller = server_v21.ServersController(
|
self.controller = server_v21.ServersController(
|
||||||
extension_info=ext_info)
|
extension_info=ext_info)
|
||||||
|
|
||||||
def test_start(self):
|
@mock.patch.object(compute_api.API, 'start')
|
||||||
self.stub_out('nova.db.instance_get_by_uuid', fake_instance_get)
|
def test_start(self, start_mock):
|
||||||
self.mox.StubOutWithMock(compute_api.API, 'start')
|
|
||||||
compute_api.API.start(mox.IgnoreArg(), mox.IgnoreArg())
|
|
||||||
self.mox.ReplayAll()
|
|
||||||
|
|
||||||
body = dict(start="")
|
body = dict(start="")
|
||||||
self.controller._start_server(self.req, uuids.instance, body)
|
self.controller._start_server(self.req, uuids.instance, body)
|
||||||
|
start_mock.assert_called_once_with(mock.ANY, mock.ANY)
|
||||||
|
|
||||||
def test_start_policy_failed(self):
|
def test_start_policy_failed(self):
|
||||||
rules = {
|
rules = {
|
||||||
self.start_policy: "project_id:non_fake"
|
self.start_policy: "project_id:non_fake"
|
||||||
}
|
}
|
||||||
policy.set_rules(oslo_policy.Rules.from_dict(rules))
|
policy.set_rules(oslo_policy.Rules.from_dict(rules))
|
||||||
self.stub_out('nova.db.instance_get_by_uuid', fake_instance_get)
|
|
||||||
body = dict(start="")
|
body = dict(start="")
|
||||||
exc = self.assertRaises(exception.PolicyNotAuthorized,
|
exc = self.assertRaises(exception.PolicyNotAuthorized,
|
||||||
self.controller._start_server,
|
self.controller._start_server,
|
||||||
self.req, uuids.instance, body)
|
self.req, uuids.instance, body)
|
||||||
self.assertIn(self.start_policy, exc.format_message())
|
self.assertIn(self.start_policy, exc.format_message())
|
||||||
|
|
||||||
def test_start_not_ready(self):
|
@mock.patch.object(compute_api.API, 'start',
|
||||||
self.stub_out('nova.db.instance_get_by_uuid', fake_instance_get)
|
side_effect=exception.InstanceNotReady(
|
||||||
self.stubs.Set(compute_api.API, 'start', fake_start_stop_not_ready)
|
instance_id=uuids.instance))
|
||||||
|
def test_start_not_ready(self, start_mock):
|
||||||
body = dict(start="")
|
body = dict(start="")
|
||||||
self.assertRaises(webob.exc.HTTPConflict,
|
self.assertRaises(webob.exc.HTTPConflict,
|
||||||
self.controller._start_server, self.req, uuids.instance, body)
|
self.controller._start_server, self.req, uuids.instance, body)
|
||||||
|
|
||||||
def test_start_locked_server(self):
|
@mock.patch.object(compute_api.API, 'start',
|
||||||
self.stub_out('nova.db.instance_get_by_uuid', fake_instance_get)
|
side_effect=exception.InstanceIsLocked(
|
||||||
self.stubs.Set(compute_api.API, 'start', fake_start_stop_locked_server)
|
instance_uuid=uuids.instance))
|
||||||
|
def test_start_locked_server(self, start_mock):
|
||||||
body = dict(start="")
|
body = dict(start="")
|
||||||
self.assertRaises(webob.exc.HTTPConflict,
|
self.assertRaises(webob.exc.HTTPConflict,
|
||||||
self.controller._start_server, self.req, uuids.instance, body)
|
self.controller._start_server, self.req, uuids.instance, body)
|
||||||
|
|
||||||
def test_start_invalid_state(self):
|
@mock.patch.object(compute_api.API, 'start',
|
||||||
self.stub_out('nova.db.instance_get_by_uuid', fake_instance_get)
|
side_effect=exception.InstanceIsLocked(
|
||||||
self.stubs.Set(compute_api.API, 'start', fake_start_stop_invalid_state)
|
instance_uuid=uuids.instance))
|
||||||
|
def test_start_invalid_state(self, start_mock):
|
||||||
body = dict(start="")
|
body = dict(start="")
|
||||||
ex = self.assertRaises(webob.exc.HTTPConflict,
|
ex = self.assertRaises(webob.exc.HTTPConflict,
|
||||||
self.controller._start_server, self.req, uuids.instance, body)
|
self.controller._start_server, self.req, uuids.instance, body)
|
||||||
self.assertIn('is locked', six.text_type(ex))
|
self.assertIn('is locked', six.text_type(ex))
|
||||||
|
|
||||||
def test_stop(self):
|
@mock.patch.object(compute_api.API, 'stop')
|
||||||
self.stub_out('nova.db.instance_get_by_uuid', fake_instance_get)
|
def test_stop(self, stop_mock):
|
||||||
self.mox.StubOutWithMock(compute_api.API, 'stop')
|
|
||||||
compute_api.API.stop(mox.IgnoreArg(), mox.IgnoreArg())
|
|
||||||
self.mox.ReplayAll()
|
|
||||||
|
|
||||||
body = dict(stop="")
|
body = dict(stop="")
|
||||||
self.controller._stop_server(self.req, uuids.instance, body)
|
self.controller._stop_server(self.req, uuids.instance, body)
|
||||||
|
stop_mock.assert_called_once_with(mock.ANY, mock.ANY)
|
||||||
|
|
||||||
def test_stop_policy_failed(self):
|
def test_stop_policy_failed(self):
|
||||||
rules = {
|
rules = {
|
||||||
self.stop_policy: "project_id:non_fake"
|
self.stop_policy: "project_id:non_fake"
|
||||||
}
|
}
|
||||||
policy.set_rules(oslo_policy.Rules.from_dict(rules))
|
policy.set_rules(oslo_policy.Rules.from_dict(rules))
|
||||||
self.stub_out('nova.db.instance_get_by_uuid', fake_instance_get)
|
|
||||||
body = dict(stop="")
|
body = dict(stop="")
|
||||||
exc = self.assertRaises(exception.PolicyNotAuthorized,
|
exc = self.assertRaises(exception.PolicyNotAuthorized,
|
||||||
self.controller._stop_server,
|
self.controller._stop_server,
|
||||||
self.req, uuids.instance, body)
|
self.req, uuids.instance, body)
|
||||||
self.assertIn(self.stop_policy, exc.format_message())
|
self.assertIn(self.stop_policy, exc.format_message())
|
||||||
|
|
||||||
def test_stop_not_ready(self):
|
@mock.patch.object(compute_api.API, 'stop',
|
||||||
self.stub_out('nova.db.instance_get_by_uuid', fake_instance_get)
|
side_effect=exception.InstanceNotReady(
|
||||||
self.stubs.Set(compute_api.API, 'stop', fake_start_stop_not_ready)
|
instance_id=uuids.instance))
|
||||||
|
def test_stop_not_ready(self, stop_mock):
|
||||||
body = dict(stop="")
|
body = dict(stop="")
|
||||||
self.assertRaises(webob.exc.HTTPConflict,
|
self.assertRaises(webob.exc.HTTPConflict,
|
||||||
self.controller._stop_server, self.req, uuids.instance, body)
|
self.controller._stop_server, self.req, uuids.instance, body)
|
||||||
|
|
||||||
def test_stop_locked_server(self):
|
@mock.patch.object(compute_api.API, 'stop',
|
||||||
self.stub_out('nova.db.instance_get_by_uuid', fake_instance_get)
|
side_effect=exception.InstanceIsLocked(
|
||||||
self.stubs.Set(compute_api.API, 'stop', fake_start_stop_locked_server)
|
instance_uuid=uuids.instance))
|
||||||
|
def test_stop_locked_server(self, stop_mock):
|
||||||
body = dict(stop="")
|
body = dict(stop="")
|
||||||
ex = self.assertRaises(webob.exc.HTTPConflict,
|
ex = self.assertRaises(webob.exc.HTTPConflict,
|
||||||
self.controller._stop_server, self.req, uuids.instance, body)
|
self.controller._stop_server, self.req, uuids.instance, body)
|
||||||
self.assertIn('is locked', six.text_type(ex))
|
self.assertIn('is locked', six.text_type(ex))
|
||||||
|
|
||||||
def test_stop_invalid_state(self):
|
@mock.patch.object(compute_api.API, 'stop',
|
||||||
self.stub_out('nova.db.instance_get_by_uuid', fake_instance_get)
|
side_effect=exception.InstanceIsLocked(
|
||||||
self.stubs.Set(compute_api.API, 'stop', fake_start_stop_invalid_state)
|
instance_uuid=uuids.instance))
|
||||||
|
def test_stop_invalid_state(self, stop_mock):
|
||||||
body = dict(start="")
|
body = dict(start="")
|
||||||
self.assertRaises(webob.exc.HTTPConflict,
|
self.assertRaises(webob.exc.HTTPConflict,
|
||||||
self.controller._stop_server, self.req, uuids.instance, body)
|
self.controller._stop_server, self.req, uuids.instance, body)
|
||||||
|
|
||||||
def test_start_with_bogus_id(self):
|
@mock.patch.object(db, 'instance_get_by_uuid',
|
||||||
|
side_effect=exception.InstanceNotFound(
|
||||||
|
instance_uuid=uuids.instance))
|
||||||
|
def test_start_with_bogus_id(self, get_mock):
|
||||||
body = dict(start="")
|
body = dict(start="")
|
||||||
self.assertRaises(webob.exc.HTTPNotFound,
|
self.assertRaises(webob.exc.HTTPNotFound,
|
||||||
self.controller._start_server, self.req, uuids.instance, body)
|
self.controller._start_server, self.req, uuids.instance, body)
|
||||||
|
|
||||||
def test_stop_with_bogus_id(self):
|
@mock.patch.object(db, 'instance_get_by_uuid',
|
||||||
|
side_effect=exception.InstanceNotFound(
|
||||||
|
instance_uuid=uuids.InstanceNotFound))
|
||||||
|
def test_stop_with_bogus_id(self, get_mock):
|
||||||
body = dict(stop="")
|
body = dict(stop="")
|
||||||
self.assertRaises(webob.exc.HTTPNotFound,
|
self.assertRaises(webob.exc.HTTPNotFound,
|
||||||
self.controller._stop_server, self.req, uuids.instance, body)
|
self.controller._stop_server, self.req, uuids.instance, body)
|
||||||
|
|||||||
Reference in New Issue
Block a user