Make test re-use HTTPRequest part 2

We are using the controller to do test instead of
wsgi URI now, the HTTPRequest is only a fake value
and mostly is not used. Make test class reuse
the Request to remove code complication.
Also, some method set such as 'POST', 'DELETE' etc
are not needed anymore since controller are
directly used.

This patch focus on agents, cloudpipe,
cloudpipe_update and certificates.

Partially implements blueprint v2-on-v3-api
Change-Id: I5fe718a5062ad8cc89adf1fd9983f4f1b491361d
This commit is contained in:
jichenjc 2015-01-14 18:28:05 +08:00
parent 588d72d506
commit d5840d6025
4 changed files with 35 additions and 65 deletions

View File

@ -93,9 +93,9 @@ class AgentsTestV21(test.NoDBTestCase):
self.stubs.Set(db, "agent_build_create",
fake_agent_build_create)
self.context = context.get_admin_context()
self.req = fakes.HTTPRequest.blank('')
def test_agents_create(self):
req = fakes.HTTPRequest.blank('')
body = {'agent': {'hypervisor': 'kvm',
'os': 'win',
'architecture': 'x86',
@ -109,11 +109,10 @@ class AgentsTestV21(test.NoDBTestCase):
'url': 'http://example.com/path/to/resource',
'md5hash': 'add6bb58e139be103324d04d82d8f545',
'agent_id': 1}}
res_dict = self.controller.create(req, body=body)
res_dict = self.controller.create(self.req, body=body)
self.assertEqual(res_dict, response)
def _test_agents_create_key_error(self, key):
req = fakes.HTTPRequest.blank('')
body = {'agent': {'hypervisor': 'kvm',
'os': 'win',
'architecture': 'x86',
@ -122,7 +121,7 @@ class AgentsTestV21(test.NoDBTestCase):
'md5hash': 'add6bb58e139be103324d04d82d8f545'}}
body['agent'].pop(key)
self.assertRaises(self.validation_error,
self.controller.create, req, body=body)
self.controller.create, self.req, body=body)
def test_agents_create_without_hypervisor(self):
self._test_agents_create_key_error('hypervisor')
@ -143,16 +142,14 @@ class AgentsTestV21(test.NoDBTestCase):
self._test_agents_create_key_error('md5hash')
def test_agents_create_with_wrong_type(self):
req = fakes.HTTPRequest.blank('')
body = {'agent': None}
self.assertRaises(self.validation_error,
self.controller.create, req, body=body)
self.controller.create, self.req, body=body)
def test_agents_create_with_empty_type(self):
req = fakes.HTTPRequest.blank('')
body = {}
self.assertRaises(self.validation_error,
self.controller.create, req, body=body)
self.controller.create, self.req, body=body)
def test_agents_create_with_existed_agent(self):
def fake_agent_build_create_with_exited_agent(context, values):
@ -160,18 +157,16 @@ class AgentsTestV21(test.NoDBTestCase):
self.stubs.Set(db, 'agent_build_create',
fake_agent_build_create_with_exited_agent)
req = fakes.HTTPRequest.blank('')
body = {'agent': {'hypervisor': 'kvm',
'os': 'win',
'architecture': 'x86',
'version': '7.0',
'url': 'xxx://xxxx/xxx/xxx',
'md5hash': 'add6bb58e139be103324d04d82d8f545'}}
self.assertRaises(webob.exc.HTTPConflict, self.controller.create, req,
body=body)
self.assertRaises(webob.exc.HTTPConflict, self.controller.create,
self.req, body=body)
def _test_agents_create_with_invalid_length(self, key):
req = fakes.HTTPRequest.blank('')
body = {'agent': {'hypervisor': 'kvm',
'os': 'win',
'architecture': 'x86',
@ -180,7 +175,7 @@ class AgentsTestV21(test.NoDBTestCase):
'md5hash': 'add6bb58e139be103324d04d82d8f545'}}
body['agent'][key] = 'x' * 256
self.assertRaises(self.validation_error,
self.controller.create, req, body=body)
self.controller.create, self.req, body=body)
def test_agents_create_with_invalid_length_hypervisor(self):
self._test_agents_create_with_invalid_length('hypervisor')
@ -201,19 +196,16 @@ class AgentsTestV21(test.NoDBTestCase):
self._test_agents_create_with_invalid_length('md5hash')
def test_agents_delete(self):
req = fakes.HTTPRequest.blank('')
self.controller.delete(req, 1)
self.controller.delete(self.req, 1)
def test_agents_delete_with_id_not_found(self):
with mock.patch.object(db, 'agent_build_destroy',
side_effect=exception.AgentBuildNotFound(id=1)):
req = fakes.HTTPRequest.blank('')
self.assertRaises(webob.exc.HTTPNotFound,
self.controller.delete, req, 1)
self.controller.delete, self.req, 1)
def test_agents_list(self):
req = fakes.HTTPRequest.blank('')
res_dict = self.controller.index(req)
res_dict = self.controller.index(self.req)
agents_list = [{'hypervisor': 'kvm', 'os': 'win',
'architecture': 'x86',
'version': '7.0',
@ -261,7 +253,6 @@ class AgentsTestV21(test.NoDBTestCase):
self.assertEqual(res_dict, {'agents': response})
def test_agents_update(self):
req = fakes.HTTPRequest.blank('')
body = {'para': {'version': '7.0',
'url': 'http://example.com/path/to/resource',
'md5hash': 'add6bb58e139be103324d04d82d8f545'}}
@ -269,17 +260,16 @@ class AgentsTestV21(test.NoDBTestCase):
'version': '7.0',
'url': 'http://example.com/path/to/resource',
'md5hash': 'add6bb58e139be103324d04d82d8f545'}}
res_dict = self.controller.update(req, 1, body=body)
res_dict = self.controller.update(self.req, 1, body=body)
self.assertEqual(res_dict, response)
def _test_agents_update_key_error(self, key):
req = fakes.HTTPRequest.blank('')
body = {'para': {'version': '7.0',
'url': 'xxx://xxxx/xxx/xxx',
'md5hash': 'add6bb58e139be103324d04d82d8f545'}}
body['para'].pop(key)
self.assertRaises(self.validation_error,
self.controller.update, req, 1, body=body)
self.controller.update, self.req, 1, body=body)
def test_agents_update_without_version(self):
self._test_agents_update_key_error('version')
@ -291,33 +281,29 @@ class AgentsTestV21(test.NoDBTestCase):
self._test_agents_update_key_error('md5hash')
def test_agents_update_with_wrong_type(self):
req = fakes.HTTPRequest.blank('')
body = {'agent': None}
self.assertRaises(self.validation_error,
self.controller.update, req, 1, body=body)
self.controller.update, self.req, 1, body=body)
def test_agents_update_with_empty(self):
req = fakes.HTTPRequest.blank('')
body = {}
self.assertRaises(self.validation_error,
self.controller.update, req, 1, body=body)
self.controller.update, self.req, 1, body=body)
def test_agents_update_value_error(self):
req = fakes.HTTPRequest.blank('')
body = {'para': {'version': '7.0',
'url': 1111,
'md5hash': 'add6bb58e139be103324d04d82d8f545'}}
self.assertRaises(self.validation_error,
self.controller.update, req, 1, body=body)
self.controller.update, self.req, 1, body=body)
def _test_agents_update_with_invalid_length(self, key):
req = fakes.HTTPRequest.blank('')
body = {'para': {'version': '7.0',
'url': 'http://example.com/path/to/resource',
'md5hash': 'add6bb58e139be103324d04d82d8f545'}}
body['para'][key] = 'x' * 256
self.assertRaises(self.validation_error,
self.controller.update, req, 1, body=body)
self.controller.update, self.req, 1, body=body)
def test_agents_update_with_invalid_length_version(self):
self._test_agents_update_with_invalid_length('version')
@ -331,12 +317,11 @@ class AgentsTestV21(test.NoDBTestCase):
def test_agents_update_with_id_not_found(self):
with mock.patch.object(db, 'agent_build_update',
side_effect=exception.AgentBuildNotFound(id=1)):
req = fakes.HTTPRequest.blank('')
body = {'para': {'version': '7.0',
'url': 'http://example.com/path/to/resource',
'md5hash': 'add6bb58e139be103324d04d82d8f545'}}
self.assertRaises(webob.exc.HTTPNotFound,
self.controller.update, req, 1, body=body)
self.controller.update, self.req, 1, body=body)
class AgentsTestV2(AgentsTestV21):

View File

@ -41,6 +41,7 @@ class CertificatesTestV21(test.NoDBTestCase):
super(CertificatesTestV21, self).setUp()
self.context = context.RequestContext('fake', 'fake')
self.controller = self.certificates.CertificatesController()
self.req = fakes.HTTPRequest.blank('')
def test_translate_certificate_view(self):
pk, cert = 'fakepk', 'fakecert'
@ -56,8 +57,7 @@ class CertificatesTestV21(test.NoDBTestCase):
self.mox.ReplayAll()
req = fakes.HTTPRequest.blank(self.url + '/root')
res_dict = self.controller.show(req, 'root')
res_dict = self.controller.show(self.req, 'root')
response = {'certificate': {'data': 'fakeroot', 'private_key': None}}
self.assertEqual(res_dict, response)
@ -68,9 +68,8 @@ class CertificatesTestV21(test.NoDBTestCase):
common_policy.parse_rule("!")
}
policy.set_rules(rules)
req = fakes.HTTPRequest.blank(self.url + '/root')
exc = self.assertRaises(exception.PolicyNotAuthorized,
self.controller.show, req, 'root')
self.controller.show, self.req, 'root')
self.assertIn(self.certificate_show_extension,
exc.format_message())
@ -85,8 +84,7 @@ class CertificatesTestV21(test.NoDBTestCase):
self.mox.ReplayAll()
req = fakes.HTTPRequest.blank(self.url)
res_dict = self.controller.create(req)
res_dict = self.controller.create(self.req)
response = {
'certificate': {'data': 'fakecert',
@ -100,20 +98,18 @@ class CertificatesTestV21(test.NoDBTestCase):
common_policy.parse_rule("!")
}
policy.set_rules(rules)
req = fakes.HTTPRequest.blank(self.url)
exc = self.assertRaises(exception.PolicyNotAuthorized,
self.controller.create, req)
self.controller.create, self.req)
self.assertIn(self.certificate_create_extension,
exc.format_message())
@mock.patch.object(rpcapi.CertAPI, 'fetch_ca',
side_effect=exception.CryptoCAFileNotFound(project='fake'))
def test_non_exist_certificates_show(self, mock_fetch_ca):
req = fakes.HTTPRequest.blank(self.url + '/root')
self.assertRaises(
exc.HTTPNotFound,
self.controller.show,
req, 'root')
self.req, 'root')
class CertificatesTestV2(CertificatesTestV21):

View File

@ -67,6 +67,7 @@ class CloudpipeTestV21(test.NoDBTestCase):
self.stubs.Set(self.controller.compute_api, "get_all",
compute_api_get_all_empty)
self.stubs.Set(utils, 'vpn_ping', utils_vpn_ping)
self.req = fakes.HTTPRequest.blank('')
def test_cloudpipe_list_no_network(self):
@ -77,8 +78,7 @@ class CloudpipeTestV21(test.NoDBTestCase):
fake_get_nw_info_for_instance)
self.stubs.Set(self.controller.compute_api, "get_all",
compute_api_get_all)
req = fakes.HTTPRequest.blank(self.url)
res_dict = self.controller.index(req)
res_dict = self.controller.index(self.req)
response = {'cloudpipes': [{'project_id': project_id,
'instance_id': uuid,
'created_at': '1981-10-20T00:00:00Z'}]}
@ -100,8 +100,7 @@ class CloudpipeTestV21(test.NoDBTestCase):
network_api_get)
self.stubs.Set(self.controller.compute_api, "get_all",
compute_api_get_all)
req = fakes.HTTPRequest.blank(self.url)
res_dict = self.controller.index(req)
res_dict = self.controller.index(self.req)
response = {'cloudpipes': [{'project_id': project_id,
'internal_ip': '192.168.1.100',
'public_ip': '127.0.0.1',
@ -118,8 +117,7 @@ class CloudpipeTestV21(test.NoDBTestCase):
self.stubs.Set(self.controller.cloudpipe, 'launch_vpn_instance',
launch_vpn_instance)
body = {'cloudpipe': {'project_id': project_id}}
req = fakes.HTTPRequest.blank(self.url)
res_dict = self.controller.create(req, body=body)
res_dict = self.controller.create(self.req, body=body)
response = {'instance_id': uuid}
self.assertEqual(res_dict, response)

View File

@ -46,6 +46,7 @@ class CloudpipeUpdateTestV21(test.NoDBTestCase):
self.stubs.Set(db, "project_get_networks", fake_project_get_networks)
self.stubs.Set(db, "network_update", fake_network_update)
self._setup()
self.req = fakes.HTTPRequest.blank('')
def _setup(self):
self.controller = clup_v21.CloudpipeController()
@ -54,47 +55,37 @@ class CloudpipeUpdateTestV21(test.NoDBTestCase):
self.assertEqual(expected_status, controller_methord.wsgi_code)
def test_cloudpipe_configure_project(self):
req = fakes.HTTPRequest.blank(
'/v2/fake/os-cloudpipe/configure-project')
body = {"configure_project": {"vpn_ip": "1.2.3.4", "vpn_port": 222}}
result = self.controller.update(req, 'configure-project',
body=body)
result = self.controller.update(self.req, 'configure-project',
body=body)
self._check_status(202, result, self.controller.update)
self.assertEqual(fake_networks[0]['vpn_public_address'], "1.2.3.4")
self.assertEqual(fake_networks[0]['vpn_public_port'], 222)
def test_cloudpipe_configure_project_bad_url(self):
req = fakes.HTTPRequest.blank(
'/v2/fake/os-cloudpipe/configure-projectx')
body = {"configure_project": {"vpn_ip": "1.2.3.4", "vpn_port": 222}}
self.assertRaises(webob.exc.HTTPBadRequest,
self.controller.update, req,
self.controller.update, self.req,
'configure-projectx', body=body)
def test_cloudpipe_configure_project_bad_data(self):
req = fakes.HTTPRequest.blank(
'/v2/fake/os-cloudpipe/configure-project')
body = {"configure_project": {"vpn_ipxx": "1.2.3.4", "vpn_port": 222}}
self.assertRaises(self.bad_request,
self.controller.update, req,
self.controller.update, self.req,
'configure-project', body=body)
def test_cloudpipe_configure_project_bad_vpn_port(self):
req = fakes.HTTPRequest.blank(
'/v2/fake/os-cloudpipe/configure-project')
body = {"configure_project": {"vpn_ipxx": "1.2.3.4",
"vpn_port": "foo"}}
self.assertRaises(self.bad_request,
self.controller.update, req,
self.controller.update, self.req,
'configure-project', body=body)
def test_cloudpipe_configure_project_vpn_port_with_empty_string(self):
req = fakes.HTTPRequest.blank(
'/v2/fake/os-cloudpipe/configure-project')
body = {"configure_project": {"vpn_ipxx": "1.2.3.4",
"vpn_port": ""}}
self.assertRaises(self.bad_request,
self.controller.update, req,
self.controller.update, self.req,
'configure-project', body=body)