diff --git a/nova/tests/unit/api/openstack/compute/contrib/test_evacuate.py b/nova/tests/unit/api/openstack/compute/contrib/test_evacuate.py index 4f4ce1e617c7..b4b84cf08b55 100644 --- a/nova/tests/unit/api/openstack/compute/contrib/test_evacuate.py +++ b/nova/tests/unit/api/openstack/compute/contrib/test_evacuate.py @@ -15,7 +15,6 @@ import uuid from oslo.config import cfg -from oslo.serialization import jsonutils import webob from nova.api.openstack.compute.contrib import evacuate as evacuate_v2 @@ -72,6 +71,8 @@ class EvacuateTestV21(test.NoDBTestCase): for _method in self._methods: self.stubs.Set(compute_api.API, _method, fake_compute_api) self._set_up_controller() + self.admin_req = fakes.HTTPRequest.blank('', use_admin_context=True) + self.req = fakes.HTTPRequest.blank('') def _set_up_controller(self): self.controller = evacuate_v21.EvacuateController() @@ -79,9 +80,7 @@ class EvacuateTestV21(test.NoDBTestCase): def _get_evacuate_response(self, json_load, uuid=None): base_json_load = {'evacuate': json_load} - req = fakes.HTTPRequest.blank('', use_admin_context=True) - req.body = jsonutils.dumps(base_json_load) - response = self.controller._evacuate(req, uuid or self.UUID, + response = self.controller._evacuate(self.admin_req, uuid or self.UUID, body=base_json_load) return response @@ -90,10 +89,9 @@ class EvacuateTestV21(test.NoDBTestCase): controller=None): controller = controller or self.controller body = {'evacuate': body} - req = fakes.HTTPRequest.blank('', use_admin_context=True) self.assertRaises(exception, controller._evacuate, - req, uuid or self.UUID, body=body) + self.admin_req, uuid or self.UUID, body=body) def _fake_update(self, inst, context, instance, task_state, expected_task_state): @@ -193,10 +191,9 @@ class EvacuateTestV21(test.NoDBTestCase): def test_not_admin(self): body = {'evacuate': {'host': 'my-host', 'onSharedStorage': 'False'}} - req = fakes.HTTPRequest.blank('', use_admin_context=False) self.assertRaises(exception.PolicyNotAuthorized, self.controller._evacuate, - req, self.UUID, body=body) + self.req, self.UUID, body=body) def test_evacuate_to_same_host(self): self._check_evacuate_failure(webob.exc.HTTPBadRequest, diff --git a/nova/tests/unit/api/openstack/compute/contrib/test_instance_usage_audit_log.py b/nova/tests/unit/api/openstack/compute/contrib/test_instance_usage_audit_log.py index 1a9c0a0816ac..f1ac24937117 100644 --- a/nova/tests/unit/api/openstack/compute/contrib/test_instance_usage_audit_log.py +++ b/nova/tests/unit/api/openstack/compute/contrib/test_instance_usage_audit_log.py @@ -130,6 +130,9 @@ class InstanceUsageAuditLogTestV21(test.NoDBTestCase): self.stubs.Set(db, 'task_log_get_all', fake_task_log_get_all) + self.req = fakes.HTTPRequest.blank('') + self.admin_req = fakes.HTTPRequest.blank('', use_admin_context=True) + def _set_up_controller(self): self.controller = v21_ial.InstanceUsageAuditLogController() @@ -138,9 +141,7 @@ class InstanceUsageAuditLogTestV21(test.NoDBTestCase): timeutils.clear_time_override() def test_index(self): - req = fakes.HTTPRequest.blank('/v2/fake/os-instance_usage_audit_log', - use_admin_context=True) - result = self.controller.index(req) + result = self.controller.index(self.admin_req) self.assertIn('instance_usage_audit_logs', result) logs = result['instance_usage_audit_logs'] self.assertEqual(57, logs['total_instances']) @@ -153,16 +154,11 @@ class InstanceUsageAuditLogTestV21(test.NoDBTestCase): self.assertEqual("ALL hosts done. 0 errors.", logs['overall_status']) def test_index_non_admin(self): - req = fakes.HTTPRequest.blank('/v2/fake/os-instance_usage_audit_log', - use_admin_context=False) self.assertRaises(exception.PolicyNotAuthorized, - self.controller.index, req) + self.controller.index, self.req) def test_show(self): - req = fakes.HTTPRequest.blank( - '/v2/fake/os-instance_usage_audit_log/show', - use_admin_context=True) - result = self.controller.show(req, '2012-07-05 10:00:00') + result = self.controller.show(self.admin_req, '2012-07-05 10:00:00') self.assertIn('instance_usage_audit_log', result) logs = result['instance_usage_audit_log'] self.assertEqual(57, logs['total_instances']) @@ -175,16 +171,12 @@ class InstanceUsageAuditLogTestV21(test.NoDBTestCase): self.assertEqual("ALL hosts done. 0 errors.", logs['overall_status']) def test_show_non_admin(self): - req = fakes.HTTPRequest.blank('/v2/fake/os-instance_usage_audit_log', - use_admin_context=False) self.assertRaises(exception.PolicyNotAuthorized, - self.controller.show, req, '2012-07-05 10:00:00') + self.controller.show, self.req, + '2012-07-05 10:00:00') def test_show_with_running(self): - req = fakes.HTTPRequest.blank( - '/v2/fake/os-instance_usage_audit_log/show', - use_admin_context=True) - result = self.controller.show(req, '2012-07-06 10:00:00') + result = self.controller.show(self.admin_req, '2012-07-06 10:00:00') self.assertIn('instance_usage_audit_log', result) logs = result['instance_usage_audit_log'] self.assertEqual(57, logs['total_instances']) @@ -198,10 +190,7 @@ class InstanceUsageAuditLogTestV21(test.NoDBTestCase): logs['overall_status']) def test_show_with_errors(self): - req = fakes.HTTPRequest.blank( - '/v2/fake/os-instance_usage_audit_log/show', - use_admin_context=True) - result = self.controller.show(req, '2012-07-07 10:00:00') + result = self.controller.show(self.admin_req, '2012-07-07 10:00:00') self.assertIn('instance_usage_audit_log', result) logs = result['instance_usage_audit_log'] self.assertEqual(57, logs['total_instances']) diff --git a/nova/tests/unit/api/openstack/compute/contrib/test_multiple_create.py b/nova/tests/unit/api/openstack/compute/contrib/test_multiple_create.py index 6840d71aa900..5ce8a8945919 100644 --- a/nova/tests/unit/api/openstack/compute/contrib/test_multiple_create.py +++ b/nova/tests/unit/api/openstack/compute/contrib/test_multiple_create.py @@ -17,7 +17,6 @@ import datetime import uuid from oslo.config import cfg -from oslo.serialization import jsonutils import webob from nova.api.openstack.compute import plugins @@ -144,6 +143,7 @@ class MultiCreateExtensionTestV21(test.TestCase): server_update) self.stubs.Set(manager.VlanManager, 'allocate_fixed_ip', fake_method) + self.req = fakes.HTTPRequest.blank('') def _test_create_extra(self, params, no_image=False, override_controller=None): @@ -153,14 +153,12 @@ class MultiCreateExtensionTestV21(test.TestCase): server.pop('imageRef', None) server.update(params) body = dict(server=server) - req = fakes.HTTPRequest.blank('/servers') - req.method = 'POST' - req.body = jsonutils.dumps(body) - req.headers["content-type"] = "application/json" if override_controller: - server = override_controller.create(req, body=body).obj['server'] + server = override_controller.create(self.req, + body=body).obj['server'] else: - server = self.controller.create(req, body=body).obj['server'] + server = self.controller.create(self.req, + body=body).obj['server'] def _check_multiple_create_extension_disabled(self, **kwargs): # NOTE: on v2.1 API, "create a server" API doesn't add the following @@ -236,13 +234,9 @@ class MultiCreateExtensionTestV21(test.TestCase): 'flavorRef': flavor_ref, } } - req = fakes.HTTPRequest.blank('/servers') - req.method = 'POST' - req.body = jsonutils.dumps(body) - req.headers["content-type"] = "application/json" self.assertRaises(self.validation_error, self.controller.create, - req, + self.req, body=body) def test_create_instance_invalid_negative_max(self): @@ -257,13 +251,9 @@ class MultiCreateExtensionTestV21(test.TestCase): 'flavorRef': flavor_ref, } } - req = fakes.HTTPRequest.blank('/servers') - req.method = 'POST' - req.body = jsonutils.dumps(body) - req.headers["content-type"] = "application/json" self.assertRaises(self.validation_error, self.controller.create, - req, + self.req, body=body) def test_create_instance_with_blank_min(self): @@ -278,13 +268,9 @@ class MultiCreateExtensionTestV21(test.TestCase): 'flavor_ref': flavor_ref, } } - req = fakes.HTTPRequest.blank('/servers') - req.method = 'POST' - req.body = jsonutils.dumps(body) - req.headers["content-type"] = "application/json" self.assertRaises(self.validation_error, self.controller.create, - req, + self.req, body=body) def test_create_instance_with_blank_max(self): @@ -299,13 +285,9 @@ class MultiCreateExtensionTestV21(test.TestCase): 'flavor_ref': flavor_ref, } } - req = fakes.HTTPRequest.blank('/servers') - req.method = 'POST' - req.body = jsonutils.dumps(body) - req.headers["content-type"] = "application/json" self.assertRaises(self.validation_error, self.controller.create, - req, + self.req, body=body) def test_create_instance_invalid_min_greater_than_max(self): @@ -321,13 +303,9 @@ class MultiCreateExtensionTestV21(test.TestCase): 'flavorRef': flavor_ref, } } - req = fakes.HTTPRequest.blank('/servers') - req.method = 'POST' - req.body = jsonutils.dumps(body) - req.headers["content-type"] = "application/json" self.assertRaises(webob.exc.HTTPBadRequest, self.controller.create, - req, + self.req, body=body) def test_create_instance_invalid_alpha_min(self): @@ -342,13 +320,9 @@ class MultiCreateExtensionTestV21(test.TestCase): 'flavorRef': flavor_ref, } } - req = fakes.HTTPRequest.blank('/servers') - req.method = 'POST' - req.body = jsonutils.dumps(body) - req.headers["content-type"] = "application/json" self.assertRaises(self.validation_error, self.controller.create, - req, + self.req, body=body) def test_create_instance_invalid_alpha_max(self): @@ -363,13 +337,9 @@ class MultiCreateExtensionTestV21(test.TestCase): 'flavorRef': flavor_ref, } } - req = fakes.HTTPRequest.blank('/servers') - req.method = 'POST' - req.body = jsonutils.dumps(body) - req.headers["content-type"] = "application/json" self.assertRaises(self.validation_error, self.controller.create, - req, + self.req, body=body) def test_create_multiple_instances(self): @@ -389,11 +359,7 @@ class MultiCreateExtensionTestV21(test.TestCase): } } - req = fakes.HTTPRequest.blank('/servers') - req.method = 'POST' - req.body = jsonutils.dumps(body) - req.headers["content-type"] = "application/json" - res = self.controller.create(req, body=body).obj + res = self.controller.create(self.req, body=body).obj self.assertEqual(FAKE_UUID, res["server"]["id"]) self._check_admin_password_len(res["server"]) @@ -416,11 +382,7 @@ class MultiCreateExtensionTestV21(test.TestCase): } } - req = fakes.HTTPRequest.blank('/servers') - req.method = 'POST' - req.body = jsonutils.dumps(body) - req.headers["content-type"] = "application/json" - res = self.controller.create(req, body=body).obj + res = self.controller.create(self.req, body=body).obj self.assertEqual(FAKE_UUID, res["server"]["id"]) self._check_admin_password_missing(res["server"]) @@ -452,11 +414,7 @@ class MultiCreateExtensionTestV21(test.TestCase): } } - req = fakes.HTTPRequest.blank('/servers') - req.method = 'POST' - req.body = jsonutils.dumps(body) - req.headers["content-type"] = "application/json" - res = self.controller.create(req, body=body) + res = self.controller.create(self.req, body=body) reservation_id = res.obj['reservation_id'] self.assertNotEqual(reservation_id, "") self.assertIsNotNone(reservation_id) @@ -531,12 +489,8 @@ class MultiCreateExtensionTestV21(test.TestCase): } } - req = fakes.HTTPRequest.blank('/servers') - req.method = 'POST' - req.body = jsonutils.dumps(body) - req.headers["content-type"] = "application/json" self.assertRaises(self.validation_error, - self.controller.create, req, body=body) + self.controller.create, self.req, body=body) def test_create_multiple_instance_with_non_integer_min_count(self): image_href = '76fa36fc-c930-4bf3-8c8a-ea2a2420deb6' @@ -552,12 +506,8 @@ class MultiCreateExtensionTestV21(test.TestCase): } } - req = fakes.HTTPRequest.blank('/servers') - req.method = 'POST' - req.body = jsonutils.dumps(body) - req.headers["content-type"] = "application/json" self.assertRaises(self.validation_error, - self.controller.create, req, body=body) + self.controller.create, self.req, body=body) class MultiCreateExtensionTestV2(MultiCreateExtensionTestV21): diff --git a/nova/tests/unit/api/openstack/compute/contrib/test_networks.py b/nova/tests/unit/api/openstack/compute/contrib/test_networks.py index 652a71d7ad4c..c5ac9b3cd25d 100644 --- a/nova/tests/unit/api/openstack/compute/contrib/test_networks.py +++ b/nova/tests/unit/api/openstack/compute/contrib/test_networks.py @@ -229,7 +229,6 @@ class FakeNetworkAPI(object): # NOTE(vish): tests that network create Exceptions actually return # the proper error responses class NetworkCreateExceptionsTestV21(test.TestCase): - url_prefix = '/v2/1234' class PassthroughAPI(object): def __init__(self): @@ -246,39 +245,35 @@ class NetworkCreateExceptionsTestV21(test.TestCase): fakes.stub_out_networking(self.stubs) fakes.stub_out_rate_limiting(self.stubs) self.new_network = copy.deepcopy(NEW_NETWORK) + self.req = fakes.HTTPRequest.blank('') def _setup(self): self.controller = networks_v21.NetworkController(self.PassthroughAPI()) def test_network_create_bad_vlan(self): - req = fakes.HTTPRequest.blank(self.url_prefix + '/os-networks') self.new_network['network']['vlan_start'] = 'foo' self.assertRaises(webob.exc.HTTPBadRequest, - self.controller.create, req, self.new_network) + self.controller.create, self.req, self.new_network) def test_network_create_no_cidr(self): - req = fakes.HTTPRequest.blank(self.url_prefix + '/os-networks') self.new_network['network']['cidr'] = '' self.assertRaises(webob.exc.HTTPBadRequest, - self.controller.create, req, self.new_network) + self.controller.create, self.req, self.new_network) def test_network_create_invalid_fixed_cidr(self): - req = fakes.HTTPRequest.blank(self.url_prefix + '/os-networks') self.new_network['network']['fixed_cidr'] = 'foo' self.assertRaises(webob.exc.HTTPBadRequest, - self.controller.create, req, self.new_network) + self.controller.create, self.req, self.new_network) def test_network_create_invalid_start(self): - req = fakes.HTTPRequest.blank(self.url_prefix + '/os-networks') self.new_network['network']['allowed_start'] = 'foo' self.assertRaises(webob.exc.HTTPBadRequest, - self.controller.create, req, self.new_network) + self.controller.create, self.req, self.new_network) def test_network_create_handle_network_not_created(self): - req = fakes.HTTPRequest.blank(self.url_prefix + '/os-networks') self.new_network['network']['label'] = 'fail_NetworkNotCreated' self.assertRaises(webob.exc.HTTPBadRequest, - self.controller.create, req, self.new_network) + self.controller.create, self.req, self.new_network) def test_network_create_cidr_conflict(self): @@ -291,10 +286,9 @@ class NetworkCreateExceptionsTestV21(test.TestCase): self.stubs.Set(objects.NetworkList, 'get_all', get_all) - req = fakes.HTTPRequest.blank(self.url_prefix + '/os-networks') self.new_network['network']['cidr'] = '10.0.0.0/24' self.assertRaises(webob.exc.HTTPConflict, - self.controller.create, req, self.new_network) + self.controller.create, self.req, self.new_network) class NetworkCreateExceptionsTestV2(NetworkCreateExceptionsTestV21): @@ -308,7 +302,6 @@ class NetworkCreateExceptionsTestV2(NetworkCreateExceptionsTestV21): class NetworksTestV21(test.NoDBTestCase): - url_prefix = '/v2/1234' def setUp(self): super(NetworksTestV21, self).setUp() @@ -317,6 +310,8 @@ class NetworksTestV21(test.NoDBTestCase): fakes.stub_out_networking(self.stubs) fakes.stub_out_rate_limiting(self.stubs) self.new_network = copy.deepcopy(NEW_NETWORK) + self.req = fakes.HTTPRequest.blank('') + self.admin_req = fakes.HTTPRequest.blank('', use_admin_context=True) def _setup(self): self.controller = networks_v21.NetworkController( @@ -332,26 +327,23 @@ class NetworksTestV21(test.NoDBTestCase): def test_network_list_all_as_user(self): self.maxDiff = None - req = fakes.HTTPRequest.blank(self.url_prefix + '/os-networks') - res_dict = self.controller.index(req) + res_dict = self.controller.index(self.req) self.assertEqual(res_dict, {'networks': []}) - project_id = req.environ["nova.context"].project_id - cxt = req.environ["nova.context"] + project_id = self.req.environ["nova.context"].project_id + cxt = self.req.environ["nova.context"] uuid = FAKE_NETWORKS[0]['uuid'] self.fake_network_api.associate(context=cxt, network_uuid=uuid, project=project_id) - res_dict = self.controller.index(req) + res_dict = self.controller.index(self.req) expected = [copy.deepcopy(FAKE_USER_NETWORKS[0])] for network in expected: self.network_uuid_to_id(network) self.assertEqual({'networks': expected}, res_dict) def test_network_list_all_as_admin(self): - req = fakes.HTTPRequest.blank(self.url_prefix + '/os-networks') - req.environ["nova.context"].is_admin = True - res_dict = self.controller.index(req) + res_dict = self.controller.index(self.admin_req) expected = copy.deepcopy(FAKE_NETWORKS) for network in expected: self.network_uuid_to_id(network) @@ -359,74 +351,55 @@ class NetworksTestV21(test.NoDBTestCase): def test_network_disassociate(self): uuid = FAKE_NETWORKS[0]['uuid'] - req = fakes.HTTPRequest.blank(self.url_prefix + - '/os-networks/%s/action' % uuid) res = self.controller._disassociate_host_and_project( - req, uuid, {'disassociate': None}) + self.req, uuid, {'disassociate': None}) self._check_status(res, self.controller._disassociate_host_and_project, 202) self.assertIsNone(self.fake_network_api.networks[0]['project_id']) self.assertIsNone(self.fake_network_api.networks[0]['host']) def test_network_disassociate_not_found(self): - req = fakes.HTTPRequest.blank(self.url_prefix + - '/os-networks/100/action') self.assertRaises(webob.exc.HTTPNotFound, self.controller._disassociate_host_and_project, - req, 100, {'disassociate': None}) + self.req, 100, {'disassociate': None}) def test_network_get_as_user(self): uuid = FAKE_USER_NETWORKS[0]['uuid'] - req = fakes.HTTPRequest.blank(self.url_prefix + - '/os-networks/%s' % uuid) - res_dict = self.controller.show(req, uuid) + res_dict = self.controller.show(self.req, uuid) expected = {'network': copy.deepcopy(FAKE_USER_NETWORKS[0])} self.network_uuid_to_id(expected['network']) self.assertEqual(expected, res_dict) def test_network_get_as_admin(self): uuid = FAKE_NETWORKS[0]['uuid'] - req = fakes.HTTPRequest.blank(self.url_prefix + - '/os-networks/%s' % uuid) - req.environ["nova.context"].is_admin = True - res_dict = self.controller.show(req, uuid) + res_dict = self.controller.show(self.admin_req, uuid) expected = {'network': copy.deepcopy(FAKE_NETWORKS[0])} self.network_uuid_to_id(expected['network']) self.assertEqual(expected, res_dict) def test_network_get_not_found(self): - req = fakes.HTTPRequest.blank(self.url_prefix + '/os-networks/100') self.assertRaises(webob.exc.HTTPNotFound, - self.controller.show, req, 100) + self.controller.show, self.req, 100) def test_network_delete(self): - uuid = FAKE_NETWORKS[0]['uuid'] - req = fakes.HTTPRequest.blank(self.url_prefix + - '/os-networks/%s' % uuid) - res = self.controller.delete(req, 1) + res = self.controller.delete(self.req, 1) self._check_status(res, self.controller._disassociate_host_and_project, 202) def test_network_delete_not_found(self): - req = fakes.HTTPRequest.blank(self.url_prefix + '/os-networks/100') self.assertRaises(webob.exc.HTTPNotFound, - self.controller.delete, req, 100) + self.controller.delete, self.req, 100) def test_network_delete_in_use(self): - req = fakes.HTTPRequest.blank(self.url_prefix + '/os-networks/-1') self.assertRaises(webob.exc.HTTPConflict, - self.controller.delete, req, -1) + self.controller.delete, self.req, -1) def test_network_add(self): uuid = FAKE_NETWORKS[1]['uuid'] - req = fakes.HTTPRequest.blank(self.url_prefix + '/os-networks/add') - res = self.controller.add(req, {'id': uuid}) + res = self.controller.add(self.req, {'id': uuid}) self._check_status(res, self.controller._disassociate_host_and_project, 202) - req = fakes.HTTPRequest.blank(self.url_prefix + - '/os-networks/%s' % uuid) - req.environ["nova.context"].is_admin = True - res_dict = self.controller.show(req, uuid) + res_dict = self.controller.show(self.admin_req, uuid) self.assertEqual(res_dict['network']['project_id'], 'fake') @mock.patch('nova.tests.unit.api.openstack.compute.contrib.test_networks.' @@ -434,52 +407,43 @@ class NetworksTestV21(test.NoDBTestCase): side_effect=exception.NoMoreNetworks) def test_network_add_no_more_networks_fail(self, mock_add): uuid = FAKE_NETWORKS[1]['uuid'] - req = fakes.HTTPRequest.blank(self.url_prefix + '/os-networks/add') - self.assertRaises(webob.exc.HTTPBadRequest, self.controller.add, req, - {'id': uuid}) + self.assertRaises(webob.exc.HTTPBadRequest, self.controller.add, + self.req, {'id': uuid}) @mock.patch('nova.tests.unit.api.openstack.compute.contrib.test_networks.' 'FakeNetworkAPI.add_network_to_project', side_effect=exception.NetworkNotFoundForUUID(uuid='fake_uuid')) def test_network_add_network_not_found_networks_fail(self, mock_add): uuid = FAKE_NETWORKS[1]['uuid'] - req = fakes.HTTPRequest.blank(self.url_prefix + '/os-networks/add') - self.assertRaises(webob.exc.HTTPBadRequest, self.controller.add, req, - {'id': uuid}) + self.assertRaises(webob.exc.HTTPBadRequest, self.controller.add, + self.req, {'id': uuid}) def test_network_create(self): - req = fakes.HTTPRequest.blank(self.url_prefix + '/os-networks') - res_dict = self.controller.create(req, body=self.new_network) + res_dict = self.controller.create(self.req, body=self.new_network) self.assertIn('network', res_dict) uuid = res_dict['network']['id'] - req = fakes.HTTPRequest.blank(self.url_prefix + - '/os-networks/%s' % uuid) - res_dict = self.controller.show(req, uuid) + res_dict = self.controller.show(self.req, uuid) self.assertTrue(res_dict['network']['label']. startswith(NEW_NETWORK['network']['label'])) def test_network_create_large(self): - req = fakes.HTTPRequest.blank(self.url_prefix + '/os-networks') self.new_network['network']['cidr'] = '128.0.0.0/4' - res_dict = self.controller.create(req, self.new_network) + res_dict = self.controller.create(self.req, self.new_network) self.assertEqual(res_dict['network']['cidr'], self.new_network['network']['cidr']) def test_network_create_bad_cidr(self): - req = fakes.HTTPRequest.blank(self.url_prefix + '/os-networks') self.new_network['network']['cidr'] = '128.0.0.0/900' self.assertRaises(webob.exc.HTTPBadRequest, - self.controller.create, req, self.new_network) + self.controller.create, self.req, self.new_network) def test_network_neutron_disassociate_not_implemented(self): uuid = FAKE_NETWORKS[1]['uuid'] self.flags(network_api_class='nova.network.neutronv2.api.API') controller = networks.NetworkController() - req = fakes.HTTPRequest.blank(self.url_prefix + - '/os-networks/%s/action' % uuid) self.assertRaises(webob.exc.HTTPNotImplemented, controller._disassociate_host_and_project, - req, uuid, {'disassociate': None}) + self.req, uuid, {'disassociate': None}) class NetworksTestV2(NetworksTestV21): @@ -504,9 +468,8 @@ class NetworksTestV2(NetworksTestV21): return [{}] self.stubs.Set(self.controller.network_api, 'create', no_mtu) - req = fakes.HTTPRequest.blank(self.url_prefix + '/os-networks') self.new_network['network']['mtu'] = 9000 - self.controller.create(req, self.new_network) + self.controller.create(self.req, self.new_network) class NetworksAssociateTestV21(test.NoDBTestCase): @@ -517,6 +480,8 @@ class NetworksAssociateTestV21(test.NoDBTestCase): self._setup() fakes.stub_out_networking(self.stubs) fakes.stub_out_rate_limiting(self.stubs) + self.req = fakes.HTTPRequest.blank('') + self.admin_req = fakes.HTTPRequest.blank('', use_admin_context=True) def _setup(self): self.controller = networks.NetworkController(self.fake_network_api) @@ -528,9 +493,8 @@ class NetworksAssociateTestV21(test.NoDBTestCase): def test_network_disassociate_host_only(self): uuid = FAKE_NETWORKS[0]['uuid'] - req = fakes.HTTPRequest.blank('/v2/1234/os-networks/%s/action' % uuid) res = self.associate_controller._disassociate_host_only( - req, uuid, {'disassociate_host': None}) + self.req, uuid, {'disassociate_host': None}) self._check_status(res, self.associate_controller._disassociate_host_only, 202) @@ -539,9 +503,8 @@ class NetworksAssociateTestV21(test.NoDBTestCase): def test_network_disassociate_project_only(self): uuid = FAKE_NETWORKS[0]['uuid'] - req = fakes.HTTPRequest.blank('/v2/1234/os-networks/%s/action' % uuid) res = self.associate_controller._disassociate_project_only( - req, uuid, {'disassociate_project': None}) + self.req, uuid, {'disassociate_project': None}) self._check_status( res, self.associate_controller._disassociate_project_only, 202) self.assertIsNone(self.fake_network_api.networks[0]['project_id']) @@ -566,13 +529,10 @@ class NetworksAssociateTestV21(test.NoDBTestCase): def test_network_associate_with_host(self): uuid = FAKE_NETWORKS[1]['uuid'] - req = fakes.HTTPRequest.blank('/v2/1234//os-networks/%s/action' % uuid) res = self.associate_controller._associate_host( - req, uuid, body={'associate_host': "TestHost"}) + self.req, uuid, body={'associate_host': "TestHost"}) self._check_status(res, self.associate_controller._associate_host, 202) - req = fakes.HTTPRequest.blank('/v2/1234/os-networks/%s' % uuid) - req.environ["nova.context"].is_admin = True - res_dict = self.controller.show(req, uuid) + res_dict = self.controller.show(self.admin_req, uuid) self.assertEqual(res_dict['network']['host'], 'TestHost') def test_network_neutron_associate_not_implemented(self): @@ -580,10 +540,9 @@ class NetworksAssociateTestV21(test.NoDBTestCase): self.flags(network_api_class='nova.network.neutronv2.api.API') assoc_ctrl = networks_associate.NetworkAssociateActionController() - req = fakes.HTTPRequest.blank('/v2/1234/os-networks/%s/action' % uuid) self.assertRaises(webob.exc.HTTPNotImplemented, assoc_ctrl._associate_host, - req, uuid, {'associate_host': "TestHost"}) + self.req, uuid, {'associate_host': "TestHost"}) def _test_network_neutron_associate_host_validation_failed(self, body): uuid = FAKE_NETWORKS[1]['uuid'] @@ -614,19 +573,17 @@ class NetworksAssociateTestV21(test.NoDBTestCase): self.flags(network_api_class='nova.network.neutronv2.api.API') assoc_ctrl = networks_associate.NetworkAssociateActionController() - req = fakes.HTTPRequest.blank('/v2/1234/os-networks/%s/action' % uuid) self.assertRaises(webob.exc.HTTPNotImplemented, assoc_ctrl._disassociate_project_only, - req, uuid, {'disassociate_project': None}) + self.req, uuid, {'disassociate_project': None}) def test_network_neutron_disassociate_host_not_implemented(self): uuid = FAKE_NETWORKS[1]['uuid'] self.flags(network_api_class='nova.network.neutronv2.api.API') assoc_ctrl = networks_associate.NetworkAssociateActionController() - req = fakes.HTTPRequest.blank('/v2/1234/os-networks/%s/action' % uuid) self.assertRaises(webob.exc.HTTPNotImplemented, assoc_ctrl._disassociate_host_only, - req, uuid, {'disassociate_host': None}) + self.req, uuid, {'disassociate_host': None}) class NetworksAssociateTestV2(NetworksAssociateTestV21): diff --git a/nova/tests/unit/api/openstack/compute/contrib/test_services.py b/nova/tests/unit/api/openstack/compute/contrib/test_services.py index 615318b4cf10..94e2f14d8667 100644 --- a/nova/tests/unit/api/openstack/compute/contrib/test_services.py +++ b/nova/tests/unit/api/openstack/compute/contrib/test_services.py @@ -180,6 +180,9 @@ class ServicesTestV21(test.TestCase): self.stubs.Set(db, "service_update", fake_db_service_update(fake_services_list)) + self.req = fakes.HTTPRequest.blank('') + self.admin_req = fakes.HTTPRequest.blank('', use_admin_context=True) + def _process_output(self, services, has_disabled=False, has_id=False): return services @@ -450,65 +453,56 @@ class ServicesTestV21(test.TestCase): self.stubs.Set(db, "service_update", _service_update) body = {'host': 'host1', 'binary': 'nova-compute'} - req = fakes.HTTPRequest.blank('/v2/fake/os-services/enable') - - res_dict = self.controller.update(req, "enable", body=body) + res_dict = self.controller.update(self.req, "enable", body=body) self.assertEqual(res_dict['service']['status'], 'enabled') self.assertNotIn('disabled_reason', res_dict['service']) def test_services_enable_with_invalid_host(self): body = {'host': 'invalid', 'binary': 'nova-compute'} - req = fakes.HTTPRequest.blank('/v2/fake/os-services/enable') self.assertRaises(webob.exc.HTTPNotFound, self.controller.update, - req, + self.req, "enable", body=body) def test_services_enable_with_invalid_binary(self): body = {'host': 'host1', 'binary': 'invalid'} - req = fakes.HTTPRequest.blank('/v2/fake/os-services/enable') self.assertRaises(webob.exc.HTTPNotFound, self.controller.update, - req, + self.req, "enable", body=body) def test_services_disable(self): - req = fakes.HTTPRequest.blank('/v2/fake/os-services/disable') body = {'host': 'host1', 'binary': 'nova-compute'} - res_dict = self.controller.update(req, "disable", body=body) + res_dict = self.controller.update(self.req, "disable", body=body) self.assertEqual(res_dict['service']['status'], 'disabled') self.assertNotIn('disabled_reason', res_dict['service']) def test_services_disable_with_invalid_host(self): body = {'host': 'invalid', 'binary': 'nova-compute'} - req = fakes.HTTPRequest.blank('/v2/fake/os-services/disable') self.assertRaises(webob.exc.HTTPNotFound, self.controller.update, - req, + self.req, "disable", body=body) def test_services_disable_with_invalid_binary(self): body = {'host': 'host1', 'binary': 'invalid'} - req = fakes.HTTPRequest.blank('/v2/fake/os-services/disable') self.assertRaises(webob.exc.HTTPNotFound, self.controller.update, - req, + self.req, "disable", body=body) def test_services_disable_log_reason(self): self.ext_mgr.extensions['os-extended-services'] = True - req = \ - fakes.HTTPRequest.blank('/v2/fake/os-services/disable-log-reason') body = {'host': 'host1', 'binary': 'nova-compute', 'disabled_reason': 'test-reason', } - res_dict = self.controller.update(req, + res_dict = self.controller.update(self.req, "disable-log-reason", body=body) @@ -517,48 +511,39 @@ class ServicesTestV21(test.TestCase): def test_mandatory_reason_field(self): self.ext_mgr.extensions['os-extended-services'] = True - req = \ - fakes.HTTPRequest.blank('/v2/fake/os-services/disable-log-reason') body = {'host': 'host1', 'binary': 'nova-compute', } self.assertRaises(webob.exc.HTTPBadRequest, - self.controller.update, req, "disable-log-reason", body=body) + self.controller.update, self.req, "disable-log-reason", + body=body) def test_invalid_reason_field(self): self.ext_mgr.extensions['os-extended-services'] = True - url = '/v2/fake/os-services/disable-log-reason' - req = fakes.HTTPRequest.blank(url) reason = 'a' * 256 body = {'host': 'host1', 'binary': 'nova-compute', 'disabled_reason': reason, } self.assertRaises(self.bad_request, - self.controller.update, req, "disable-log-reason", body=body) + self.controller.update, self.req, "disable-log-reason", + body=body) def test_services_delete(self): self.ext_mgr.extensions['os-extended-services-delete'] = True - request = fakes.HTTPRequest.blank('/v2/fakes/os-services/1', - use_admin_context=True) - request.method = 'DELETE' - with mock.patch.object(self.controller.host_api, 'service_delete') as service_delete: - self.controller.delete(request, '1') + self.controller.delete(self.admin_req, '1') service_delete.assert_called_once_with( - request.environ['nova.context'], '1') + self.admin_req.environ['nova.context'], '1') self.assertEqual(self.controller.delete.wsgi_code, 204) def test_services_delete_not_found(self): self.ext_mgr.extensions['os-extended-services-delete'] = True - request = fakes.HTTPRequest.blank('/v2/fakes/os-services/abc', - use_admin_context=True) - request.method = 'DELETE' self.assertRaises(webob.exc.HTTPNotFound, - self.controller.delete, request, 'abc') + self.controller.delete, self.admin_req, 'abc') # This test is just to verify that the servicegroup API gets used when # calling the API @@ -579,11 +564,8 @@ class ServicesTestV20(ServicesTestV21): self.controller = services_v2.ServiceController(self.ext_mgr) def test_services_delete_not_enabled(self): - request = fakes.HTTPRequest.blank('v2/fakes/os-services/300', - use_admin_context=True) - request.method = 'DELETE' self.assertRaises(webob.exc.HTTPMethodNotAllowed, - self.controller.delete, request, '300') + self.controller.delete, self.admin_req, '300') def _process_output(self, services, has_disabled=False, has_id=False): for service in services['services']: