diff options
| author | Johannes Erdfelt <johannes.erdfelt@rackspace.com> | 2011-11-01 14:47:51 +0000 |
|---|---|---|
| committer | Johannes Erdfelt <johannes.erdfelt@rackspace.com> | 2011-11-04 18:19:19 +0000 |
| commit | 0d36d2bf9eba28123c172fd24780f0ebe95db10a (patch) | |
| tree | 151d389c236e0855d63655b21d3d2de01205a296 | |
| parent | 67a1c257f9e4be774da5acf2c1b703d196e0a2cf (diff) | |
| download | nova-0d36d2bf9eba28123c172fd24780f0ebe95db10a.tar.gz nova-0d36d2bf9eba28123c172fd24780f0ebe95db10a.tar.xz nova-0d36d2bf9eba28123c172fd24780f0ebe95db10a.zip | |
Speed up tests a further 35 seconds
This changes more OSAPI tests to using their Controller directly, bypassing
the slow wsgi setup process.
Change-Id: Ic014c32786cb10c541f7d367573adb7a069c1a88
| -rw-r--r-- | nova/api/openstack/contrib/quotas.py | 4 | ||||
| -rw-r--r-- | nova/tests/api/openstack/contrib/test_floating_ips.py | 103 | ||||
| -rw-r--r-- | nova/tests/api/openstack/contrib/test_quotas.py | 77 | ||||
| -rw-r--r-- | nova/tests/api/openstack/test_consoles.py | 171 | ||||
| -rw-r--r-- | nova/tests/api/openstack/test_flavors_extra_specs.py | 116 | ||||
| -rw-r--r-- | nova/tests/api/openstack/test_users.py | 157 | ||||
| -rw-r--r-- | nova/tests/api/openstack/test_volume_types.py | 74 | ||||
| -rw-r--r-- | nova/tests/api/openstack/test_volume_types_extra_specs.py | 110 | ||||
| -rw-r--r-- | nova/tests/api/openstack/test_zones.py | 227 |
9 files changed, 407 insertions, 632 deletions
diff --git a/nova/api/openstack/contrib/quotas.py b/nova/api/openstack/contrib/quotas.py index 83d75394a..184b4d12f 100644 --- a/nova/api/openstack/contrib/quotas.py +++ b/nova/api/openstack/contrib/quotas.py @@ -49,7 +49,7 @@ class QuotaSetsController(object): return self._format_quota_set(id, quota.get_project_quotas(context, id)) except exception.NotAuthorized: - return webob.Response(status_int=403) + raise webob.exc.HTTPForbidden() def update(self, req, id, body): context = req.environ['nova.context'] @@ -65,7 +65,7 @@ class QuotaSetsController(object): except exception.ProjectQuotaNotFound: db.quota_create(context, project_id, key, value) except exception.AdminRequired: - return webob.Response(status_int=403) + raise webob.exc.HTTPForbidden() return {'quota_set': quota.get_project_quotas(context, project_id)} def defaults(self, req, id): diff --git a/nova/tests/api/openstack/contrib/test_floating_ips.py b/nova/tests/api/openstack/contrib/test_floating_ips.py index e0008c3f8..63831f31f 100644 --- a/nova/tests/api/openstack/contrib/test_floating_ips.py +++ b/nova/tests/api/openstack/contrib/test_floating_ips.py @@ -27,7 +27,7 @@ from nova.tests.api.openstack import fakes from nova.tests.api.openstack import test_servers -from nova.api.openstack.contrib.floating_ips import FloatingIPController +from nova.api.openstack.contrib import floating_ips from nova.api.openstack.contrib.floating_ips import _translate_floating_ip_view @@ -94,6 +94,11 @@ def fake_instance_get(context, instance_id): "project_id": '123'} +class StubExtensionManager(object): + def register(self, *args): + pass + + class FloatingIpTest(test.TestCase): address = "10.10.10.10" @@ -127,6 +132,9 @@ class FloatingIpTest(test.TestCase): self.context = context.get_admin_context() self._create_floating_ip() + self.controller = floating_ips.FloatingIPController() + self.manager = floating_ips.Floating_ips(StubExtensionManager()) + def tearDown(self): self._delete_floating_ip() super(FloatingIpTest, self).tearDown() @@ -148,10 +156,9 @@ class FloatingIpTest(test.TestCase): self.assertTrue('floating_ip' in view) def test_floating_ips_list(self): - req = webob.Request.blank('/v1.1/123/os-floating-ips') - res = req.get_response(fakes.wsgi_app()) - self.assertEqual(res.status_int, 200) - res_dict = json.loads(res.body) + req = fakes.HTTPRequest.blank('/v1.1/123/os-floating-ips') + res_dict = self.controller.index(req) + response = {'floating_ips': [{'instance_id': 1, 'ip': '10.10.10.10', 'fixed_ip': '10.0.0.1', @@ -163,10 +170,9 @@ class FloatingIpTest(test.TestCase): self.assertEqual(res_dict, response) def test_floating_ip_show(self): - req = webob.Request.blank('/v1.1/123/os-floating-ips/1') - res = req.get_response(fakes.wsgi_app()) - self.assertEqual(res.status_int, 200) - res_dict = json.loads(res.body) + req = fakes.HTTPRequest.blank('/v1.1/123/os-floating-ips/1') + res_dict = self.controller.show(req, 1) + self.assertEqual(res_dict['floating_ip']['id'], 1) self.assertEqual(res_dict['floating_ip']['ip'], '10.10.10.10') self.assertEqual(res_dict['floating_ip']['instance_id'], None) @@ -177,10 +183,9 @@ class FloatingIpTest(test.TestCase): 'fixed_ip': {'address': '10.0.0.1', 'instance_id': 1}} self.stubs.Set(network.api.API, "get_floating_ip", get_floating_ip) - req = webob.Request.blank('/v1.1/123/os-floating-ips/1') - res = req.get_response(fakes.wsgi_app()) - self.assertEqual(res.status_int, 200) - res_dict = json.loads(res.body) + req = fakes.HTTPRequest.blank('/v1.1/123/os-floating-ips/1') + res_dict = self.controller.show(req, 1) + self.assertEqual(res_dict['floating_ip']['id'], 1) self.assertEqual(res_dict['floating_ip']['ip'], '10.10.10.10') self.assertEqual(res_dict['floating_ip']['instance_id'], 1) @@ -191,11 +196,10 @@ class FloatingIpTest(test.TestCase): raise(rpc.RemoteError('NoMoreFloatingIps', '', '')) self.stubs.Set(rpc, "call", fake_call) - req = webob.Request.blank('/v1.1/123/os-floating-ips') - req.method = 'POST' - req.headers['Content-Type'] = 'application/json' - res = req.get_response(fakes.wsgi_app()) - self.assertEqual(res.status_int, 400) + + req = fakes.HTTPRequest.blank('/v1.1/123/os-floating-ips') + self.assertRaises(webob.exc.HTTPBadRequest, self.controller.create, + req) def test_floating_ip_allocate(self): def fake1(*args, **kwargs): @@ -208,12 +212,11 @@ class FloatingIpTest(test.TestCase): fake1) self.stubs.Set(network.api.API, "get_floating_ip_by_address", fake2) - req = webob.Request.blank('/v1.1/123/os-floating-ips') - req.method = 'POST' - req.headers['Content-Type'] = 'application/json' - res = req.get_response(fakes.wsgi_app()) - self.assertEqual(res.status_int, 200) - ip = json.loads(res.body)['floating_ip'] + + req = fakes.HTTPRequest.blank('/v1.1/123/os-floating-ips') + res_dict = self.controller.create(req) + + ip = res_dict['floating_ip'] expected = { "id": 1, @@ -223,61 +226,45 @@ class FloatingIpTest(test.TestCase): self.assertEqual(ip, expected) def test_floating_ip_release(self): - req = webob.Request.blank('/v1.1/123/os-floating-ips/1') - req.method = 'DELETE' - res = req.get_response(fakes.wsgi_app()) - self.assertEqual(res.status_int, 202) + req = fakes.HTTPRequest.blank('/v1.1/123/os-floating-ips/1') + self.controller.delete(req, 1) # test floating ip add/remove -> associate/disassociate def test_floating_ip_associate(self): body = dict(addFloatingIp=dict(address=self.address)) - req = webob.Request.blank('/v1.1/123/servers/test_inst/action') - req.method = "POST" - req.body = json.dumps(body) - req.headers["content-type"] = "application/json" - resp = req.get_response(fakes.wsgi_app()) - self.assertEqual(resp.status_int, 202) + req = fakes.HTTPRequest.blank('/v1.1/123/servers/test_inst/action') + self.manager._add_floating_ip(body, req, 'test_inst') def test_floating_ip_disassociate(self): body = dict(removeFloatingIp=dict(address='10.10.10.10')) - req = webob.Request.blank('/v1.1/123/servers/test_inst/action') - req.method = "POST" - req.body = json.dumps(body) - req.headers["content-type"] = "application/json" - resp = req.get_response(fakes.wsgi_app()) - self.assertEqual(resp.status_int, 202) + req = fakes.HTTPRequest.blank('/v1.1/123/servers/test_inst/action') + self.manager._remove_floating_ip(body, req, 'test_inst') # these are a few bad param tests def test_bad_address_param_in_remove_floating_ip(self): body = dict(removeFloatingIp=dict(badparam='11.0.0.1')) - req = webob.Request.blank('/v1.1/123/servers/test_inst/action') - req.method = "POST" - req.body = json.dumps(body) - req.headers["content-type"] = "application/json" - resp = req.get_response(fakes.wsgi_app()) - self.assertEqual(resp.status_int, 400) + req = fakes.HTTPRequest.blank('/v1.1/123/servers/test_inst/action') + self.assertRaises(webob.exc.HTTPBadRequest, + self.manager._add_floating_ip, body, req, + 'test_inst') def test_missing_dict_param_in_remove_floating_ip(self): body = dict(removeFloatingIp='11.0.0.1') - req = webob.Request.blank('/v1.1/123/servers/test_inst/action') - req.method = "POST" - req.body = json.dumps(body) - req.headers["content-type"] = "application/json" - resp = req.get_response(fakes.wsgi_app()) - self.assertEqual(resp.status_int, 400) + req = fakes.HTTPRequest.blank('/v1.1/123/servers/test_inst/action') + self.assertRaises(webob.exc.HTTPBadRequest, + self.manager._remove_floating_ip, body, req, + 'test_inst') def test_missing_dict_param_in_add_floating_ip(self): body = dict(addFloatingIp='11.0.0.1') - req = webob.Request.blank('/v1.1/123/servers/test_inst/action') - req.method = "POST" - req.body = json.dumps(body) - req.headers["content-type"] = "application/json" - resp = req.get_response(fakes.wsgi_app()) - self.assertEqual(resp.status_int, 400) + req = fakes.HTTPRequest.blank('/v1.1/123/servers/test_inst/action') + self.assertRaises(webob.exc.HTTPBadRequest, + self.manager._add_floating_ip, body, req, + 'test_inst') diff --git a/nova/tests/api/openstack/contrib/test_quotas.py b/nova/tests/api/openstack/contrib/test_quotas.py index 7faef08b2..6374dfd93 100644 --- a/nova/tests/api/openstack/contrib/test_quotas.py +++ b/nova/tests/api/openstack/contrib/test_quotas.py @@ -79,12 +79,10 @@ class QuotaSetsTest(test.TestCase): def test_quotas_defaults(self): uri = '/v1.1/fake_tenant/os-quota-sets/fake_tenant/defaults' - req = webob.Request.blank(uri) - req.method = 'GET' - req.headers['Content-Type'] = 'application/json' - res = req.get_response(fakes.wsgi_app()) - self.assertEqual(res.status_int, 200) + req = fakes.HTTPRequest.blank(uri) + res_dict = self.controller.defaults(req, 'fake_tenant') + expected = {'quota_set': { 'id': 'fake_tenant', 'instances': 10, @@ -97,57 +95,40 @@ class QuotaSetsTest(test.TestCase): 'injected_files': 5, 'injected_file_content_bytes': 10240}} - self.assertEqual(json.loads(res.body), expected) + self.assertEqual(res_dict, expected) def test_quotas_show_as_admin(self): - req = webob.Request.blank('/v1.1/1234/os-quota-sets/1234') - req.method = 'GET' - req.headers['Content-Type'] = 'application/json' - res = req.get_response(fakes.wsgi_app( - fake_auth_context=self.admin_context)) + req = fakes.HTTPRequest.blank('/v1.1/1234/os-quota-sets/1234', + use_admin_context=True) + res_dict = self.controller.show(req, 1234) - self.assertEqual(res.status_int, 200) - self.assertEqual(json.loads(res.body), quota_set('1234')) + self.assertEqual(res_dict, quota_set('1234')) def test_quotas_show_as_unauthorized_user(self): - req = webob.Request.blank('/v1.1/fake/os-quota-sets/1234') - req.method = 'GET' - req.headers['Content-Type'] = 'application/json' - res = req.get_response(fakes.wsgi_app( - fake_auth_context=self.user_context)) - - self.assertEqual(res.status_int, 403) + req = fakes.HTTPRequest.blank('/v1.1/1234/os-quota-sets/1234') + self.assertRaises(webob.exc.HTTPForbidden, self.controller.show, + req, 1234) def test_quotas_update_as_admin(self): - updated_quota_set = {'quota_set': {'instances': 50, - 'cores': 50, 'ram': 51200, 'volumes': 10, - 'gigabytes': 1000, 'floating_ips': 10, - 'metadata_items': 128, 'injected_files': 5, - 'injected_file_content_bytes': 10240}} - - req = webob.Request.blank('/v1.1/1234/os-quota-sets/update_me') - req.method = 'PUT' - req.body = json.dumps(updated_quota_set) - req.headers['Content-Type'] = 'application/json' + body = {'quota_set': {'instances': 50, 'cores': 50, + 'ram': 51200, 'volumes': 10, + 'gigabytes': 1000, 'floating_ips': 10, + 'metadata_items': 128, 'injected_files': 5, + 'injected_file_content_bytes': 10240}} - res = req.get_response(fakes.wsgi_app( - fake_auth_context=self.admin_context)) + req = fakes.HTTPRequest.blank('/v1.1/1234/os-quota-sets/update_me', + use_admin_context=True) + res_dict = self.controller.update(req, 'update_me', body) - self.assertEqual(json.loads(res.body), updated_quota_set) + self.assertEqual(res_dict, body) def test_quotas_update_as_user(self): - updated_quota_set = {'quota_set': {'instances': 50, - 'cores': 50, 'ram': 51200, 'volumes': 10, - 'gigabytes': 1000, 'floating_ips': 10, - 'metadata_items': 128, 'injected_files': 5, - 'injected_file_content_bytes': 10240}} - - req = webob.Request.blank('/v1.1/1234/os-quota-sets/update_me') - req.method = 'PUT' - req.body = json.dumps(updated_quota_set) - req.headers['Content-Type'] = 'application/json' - - res = req.get_response(fakes.wsgi_app( - fake_auth_context=self.user_context)) - - self.assertEqual(res.status_int, 403) + body = {'quota_set': {'instances': 50, 'cores': 50, + 'ram': 51200, 'volumes': 10, + 'gigabytes': 1000, 'floating_ips': 10, + 'metadata_items': 128, 'injected_files': 5, + 'injected_file_content_bytes': 10240}} + + req = fakes.HTTPRequest.blank('/v1.1/1234/os-quota-sets/update_me') + self.assertRaises(webob.exc.HTTPForbidden, self.controller.update, + req, 'update_me', body) diff --git a/nova/tests/api/openstack/test_consoles.py b/nova/tests/api/openstack/test_consoles.py index 679419916..0b682be0a 100644 --- a/nova/tests/api/openstack/test_consoles.py +++ b/nova/tests/api/openstack/test_consoles.py @@ -126,9 +126,9 @@ def stub_instance(id, user_id='fake', project_id='fake', host=None, return instance -class ConsolesTest(test.TestCase): +class ConsolesControllerTest(test.TestCase): def setUp(self): - super(ConsolesTest, self).setUp() + super(ConsolesControllerTest, self).setUp() self.flags(verbose=True) self.instance_db = FakeInstanceDB() self.stubs.Set(db, 'instance_get', @@ -137,17 +137,16 @@ class ConsolesTest(test.TestCase): self.instance_db.return_server_by_uuid) self.uuid = str(utils.gen_uuid()) self.url = '/v1.1/fake/servers/%s/consoles' % self.uuid + self.controller = consoles.Controller() def test_create_console(self): def fake_create_console(cons_self, context, instance_id): - self.assertTrue(instance_id, '10') + self.assertEqual(instance_id, self.uuid) return {} self.stubs.Set(console.API, 'create_console', fake_create_console) - req = webob.Request.blank(self.url) - req.method = "POST" - res = req.get_response(fakes.wsgi_app()) - self.assertEqual(res.status_int, 200) + req = fakes.HTTPRequest.blank(self.url) + self.controller.create(req, self.uuid) def test_show_console(self): def fake_get_console(cons_self, context, instance_id, console_id): @@ -166,45 +165,19 @@ class ConsolesTest(test.TestCase): self.stubs.Set(console.API, 'get_console', fake_get_console) - req = webob.Request.blank(self.url + '/20') - res = req.get_response(fakes.wsgi_app()) - self.assertEqual(res.status_int, 200) - res_dict = json.loads(res.body) + req = fakes.HTTPRequest.blank(self.url + '/20') + res_dict = self.controller.show(req, self.uuid, '20') self.assertDictMatch(res_dict, expected) - def test_show_console_xml(self): - def fake_get_console(cons_self, context, instance_id, console_id): - self.assertEqual(instance_id, self.uuid) - self.assertEqual(console_id, 20) - pool = dict(console_type='fake_type', - public_hostname='fake_hostname') - return dict(id=console_id, password='fake_password', - port='fake_port', pool=pool) - - self.stubs.Set(console.API, 'get_console', fake_get_console) - - req = webob.Request.blank(self.url + '/20.xml') - res = req.get_response(fakes.wsgi_app()) - self.assertEqual(res.status_int, 200) - - res_tree = etree.fromstring(res.body) - self.assertEqual(res_tree.tag, 'console') - self.assertEqual(res_tree.xpath('id')[0].text, '20') - self.assertEqual(res_tree.xpath('port')[0].text, 'fake_port') - self.assertEqual(res_tree.xpath('host')[0].text, 'fake_hostname') - self.assertEqual(res_tree.xpath('password')[0].text, 'fake_password') - self.assertEqual(res_tree.xpath('console_type')[0].text, - 'fake_type') - def test_show_console_unknown_console(self): def fake_get_console(cons_self, context, instance_id, console_id): raise exception.ConsoleNotFound(console_id=console_id) self.stubs.Set(console.API, 'get_console', fake_get_console) - req = webob.Request.blank(self.url + '/20') - res = req.get_response(fakes.wsgi_app()) - self.assertEqual(res.status_int, 404) + req = fakes.HTTPRequest.blank(self.url + '/20') + self.assertRaises(webob.exc.HTTPNotFound, self.controller.show, + req, self.uuid, '20') def test_show_console_unknown_instance(self): def fake_get_console(cons_self, context, instance_id, console_id): @@ -212,9 +185,9 @@ class ConsolesTest(test.TestCase): self.stubs.Set(console.API, 'get_console', fake_get_console) - req = webob.Request.blank(self.url + '/20') - res = req.get_response(fakes.wsgi_app()) - self.assertEqual(res.status_int, 404) + req = fakes.HTTPRequest.blank(self.url + '/20') + self.assertRaises(webob.exc.HTTPNotFound, self.controller.show, + req, self.uuid, '20') def test_list_consoles(self): def fake_get_consoles(cons_self, context, instance_id): @@ -236,52 +209,10 @@ class ConsolesTest(test.TestCase): self.stubs.Set(console.API, 'get_consoles', fake_get_consoles) - req = webob.Request.blank(self.url) - res = req.get_response(fakes.wsgi_app()) - self.assertEqual(res.status_int, 200) - res_dict = json.loads(res.body) + req = fakes.HTTPRequest.blank(self.url) + res_dict = self.controller.index(req, self.uuid) self.assertDictMatch(res_dict, expected) - def test_list_consoles_xml(self): - def fake_get_consoles(cons_self, context, instance_id): - self.assertEqual(instance_id, self.uuid) - - pool1 = dict(console_type='fake_type', - public_hostname='fake_hostname') - cons1 = dict(id=10, password='fake_password', - port='fake_port', pool=pool1) - pool2 = dict(console_type='fake_type2', - public_hostname='fake_hostname2') - cons2 = dict(id=11, password='fake_password2', - port='fake_port2', pool=pool2) - return [cons1, cons2] - - expected = {'consoles': - [{'console': {'id': 10, 'console_type': 'fake_type'}}, - {'console': {'id': 11, 'console_type': 'fake_type2'}}]} - - self.stubs.Set(console.API, 'get_consoles', fake_get_consoles) - - req = webob.Request.blank(self.url + '.xml') - res = req.get_response(fakes.wsgi_app()) - self.assertEqual(res.status_int, 200) - - res_tree = etree.fromstring(res.body) - self.assertEqual(res_tree.tag, 'consoles') - self.assertEqual(len(res_tree), 2) - self.assertEqual(res_tree[0].tag, 'console') - self.assertEqual(res_tree[1].tag, 'console') - self.assertEqual(len(res_tree[0]), 1) - self.assertEqual(res_tree[0][0].tag, 'console') - self.assertEqual(len(res_tree[1]), 1) - self.assertEqual(res_tree[1][0].tag, 'console') - self.assertEqual(res_tree[0][0].xpath('id')[0].text, '10') - self.assertEqual(res_tree[1][0].xpath('id')[0].text, '11') - self.assertEqual(res_tree[0][0].xpath('console_type')[0].text, - 'fake_type') - self.assertEqual(res_tree[1][0].xpath('console_type')[0].text, - 'fake_type2') - def test_delete_console(self): def fake_get_console(cons_self, context, instance_id, console_id): self.assertEqual(instance_id, self.uuid) @@ -298,29 +229,71 @@ class ConsolesTest(test.TestCase): self.stubs.Set(console.API, 'get_console', fake_get_console) self.stubs.Set(console.API, 'delete_console', fake_delete_console) - req = webob.Request.blank(self.url + '/20') - req.method = "DELETE" - res = req.get_response(fakes.wsgi_app()) - self.assertEqual(res.status_int, 202) + req = fakes.HTTPRequest.blank(self.url + '/20') + self.controller.delete(req, self.uuid, '20') - def test_show_console_unknown_console(self): + def test_delete_console_unknown_console(self): def fake_delete_console(cons_self, context, instance_id, console_id): raise exception.ConsoleNotFound(console_id=console_id) self.stubs.Set(console.API, 'delete_console', fake_delete_console) - req = webob.Request.blank(self.url + '/20') - req.method = "DELETE" - res = req.get_response(fakes.wsgi_app()) - self.assertEqual(res.status_int, 404) + req = fakes.HTTPRequest.blank(self.url + '/20') + self.assertRaises(webob.exc.HTTPNotFound, self.controller.delete, + req, self.uuid, '20') - def test_show_console_unknown_instance(self): + def test_delete_console_unknown_instance(self): def fake_delete_console(cons_self, context, instance_id, console_id): raise exception.InstanceNotFound(instance_id=instance_id) self.stubs.Set(console.API, 'delete_console', fake_delete_console) - req = webob.Request.blank(self.url + '/20') - req.method = "DELETE" - res = req.get_response(fakes.wsgi_app()) - self.assertEqual(res.status_int, 404) + req = fakes.HTTPRequest.blank(self.url + '/20') + self.assertRaises(webob.exc.HTTPNotFound, self.controller.delete, + req, self.uuid, '20') + + +class TestConsolesXMLSerializer(test.TestCase): + + serializer = consoles.ConsoleXMLSerializer() + + def test_show(self): + fixture = {'console': {'id': 20, + 'password': 'fake_password', + 'port': 'fake_port', + 'host': 'fake_hostname', + 'console_type': 'fake_type'}} + + output = self.serializer.serialize(fixture, 'show') + res_tree = etree.XML(output) + + self.assertEqual(res_tree.tag, 'console') + self.assertEqual(res_tree.xpath('id')[0].text, '20') + self.assertEqual(res_tree.xpath('port')[0].text, 'fake_port') + self.assertEqual(res_tree.xpath('host')[0].text, 'fake_hostname') + self.assertEqual(res_tree.xpath('password')[0].text, 'fake_password') + self.assertEqual(res_tree.xpath('console_type')[0].text, 'fake_type') + + def test_index(self): + fixture = {'consoles': [{'console': {'id': 10, + 'console_type': 'fake_type'}}, + {'console': {'id': 11, + 'console_type': 'fake_type2'}}]} + + output = self.serializer.serialize(fixture, 'index') + res_tree = etree.XML(output) + + self.assertEqual(res_tree.tag, 'consoles') + self.assertEqual(len(res_tree), 2) + self.assertEqual(res_tree[0].tag, 'console') + self.assertEqual(res_tree[1].tag, 'console') + self.assertEqual(len(res_tree[0]), 1) + self.assertEqual(res_tree[0][0].tag, 'console') + self.assertEqual(len(res_tree[1]), 1) + self.assertEqual(res_tree[1][0].tag, 'console') + self.assertEqual(res_tree[0][0].xpath('id')[0].text, '10') + self.assertEqual(res_tree[1][0].xpath('id')[0].text, '11') + self.assertEqual(res_tree[0][0].xpath('console_type')[0].text, + 'fake_type') + self.assertEqual(res_tree[1][0].xpath('console_type')[0].text, + 'fake_type2') diff --git a/nova/tests/api/openstack/test_flavors_extra_specs.py b/nova/tests/api/openstack/test_flavors_extra_specs.py index 766bb2d45..5784743ee 100644 --- a/nova/tests/api/openstack/test_flavors_extra_specs.py +++ b/nova/tests/api/openstack/test_flavors_extra_specs.py @@ -24,6 +24,7 @@ import os.path from nova import test from nova.api import openstack from nova.api.openstack import extensions +from nova.api.openstack.contrib import flavorextraspecs from nova.tests.api.openstack import fakes import nova.wsgi @@ -59,119 +60,112 @@ class FlavorsExtraSpecsTest(test.TestCase): def setUp(self): super(FlavorsExtraSpecsTest, self).setUp() fakes.stub_out_key_pair_funcs(self.stubs) + self.controller = flavorextraspecs.FlavorExtraSpecsController() def test_index(self): self.stubs.Set(nova.db, 'instance_type_extra_specs_get', return_flavor_extra_specs) - request = webob.Request.blank('/v1.1/123/flavors/1/os-extra_specs') - res = request.get_response(fakes.wsgi_app()) - self.assertEqual(200, res.status_int) - res_dict = json.loads(res.body) - self.assertEqual('application/json', res.headers['Content-Type']) + + req = fakes.HTTPRequest.blank('/v1.1/123/flavors/1/os-extra_specs') + res_dict = self.controller.index(req, 1) + self.assertEqual('value1', res_dict['extra_specs']['key1']) def test_index_no_data(self): self.stubs.Set(nova.db, 'instance_type_extra_specs_get', return_empty_flavor_extra_specs) - req = webob.Request.blank('/v1.1/123/flavors/1/os-extra_specs') - res = req.get_response(fakes.wsgi_app()) - res_dict = json.loads(res.body) - self.assertEqual(200, res.status_int) - self.assertEqual('application/json', res.headers['Content-Type']) + + req = fakes.HTTPRequest.blank('/v1.1/123/flavors/1/os-extra_specs') + res_dict = self.controller.index(req, 1) + self.assertEqual(0, len(res_dict['extra_specs'])) def test_show(self): self.stubs.Set(nova.db, 'instance_type_extra_specs_get', return_flavor_extra_specs) - req = webob.Request.blank('/v1.1/123/flavors/1/os-extra_specs/key5') - res = req.get_response(fakes.wsgi_app()) - self.assertEqual(200, res.status_int) - res_dict = json.loads(res.body) - self.assertEqual('application/json', res.headers['Content-Type']) + + req = fakes.HTTPRequest.blank('/v1.1/123/flavors/1/os-extra_specs' + + '/key5') + res_dict = self.controller.show(req, 1, 'key5') + self.assertEqual('value5', res_dict['key5']) def test_show_spec_not_found(self): self.stubs.Set(nova.db, 'instance_type_extra_specs_get', return_empty_flavor_extra_specs) - req = webob.Request.blank('/v1.1/123/flavors/1/os-extra_specs/key6') - res = req.get_response(fakes.wsgi_app()) - res_dict = json.loads(res.body) - self.assertEqual(404, res.status_int) + + req = fakes.HTTPRequest.blank('/v1.1/123/flavors/1/os-extra_specs' + + '/key6') + self.assertRaises(webob.exc.HTTPNotFound, self.controller.show, + req, 1, 'key6') def test_delete(self): self.stubs.Set(nova.db, 'instance_type_extra_specs_delete', delete_flavor_extra_specs) - req = webob.Request.blank('/v1.1/123/flavors/1/os-extra_specs/key5') - req.method = 'DELETE' - res = req.get_response(fakes.wsgi_app()) - self.assertEqual(200, res.status_int) + + req = fakes.HTTPRequest.blank('/v1.1/123/flavors/1/os-extra_specs' + + '/key5') + self.controller.delete(req, 1, 'key5') def test_create(self): self.stubs.Set(nova.db, 'instance_type_extra_specs_update_or_create', return_create_flavor_extra_specs) - req = webob.Request.blank('/v1.1/123/flavors/1/os-extra_specs') - req.method = 'POST' - req.body = '{"extra_specs": {"key1": "value1"}}' - req.headers["content-type"] = "application/json" - res = req.get_response(fakes.wsgi_app()) - res_dict = json.loads(res.body) - self.assertEqual(200, res.status_int) - self.assertEqual('application/json', res.headers['Content-Type']) + body = {"extra_specs": {"key1": "value1"}} + + req = fakes.HTTPRequest.blank('/v1.1/123/flavors/1/os-extra_specs') + res_dict = self.controller.create(req, 1, body) + self.assertEqual('value1', res_dict['extra_specs']['key1']) def test_create_empty_body(self): self.stubs.Set(nova.db, 'instance_type_extra_specs_update_or_create', return_create_flavor_extra_specs) - req = webob.Request.blank('/v1.1/123/flavors/1/os-extra_specs') - req.method = 'POST' - req.headers["content-type"] = "application/json" - res = req.get_response(fakes.wsgi_app()) - self.assertEqual(400, res.status_int) + + req = fakes.HTTPRequest.blank('/v1.1/123/flavors/1/os-extra_specs') + self.assertRaises(webob.exc.HTTPBadRequest, self.controller.create, + req, 1, '') def test_update_item(self): self.stubs.Set(nova.db, 'instance_type_extra_specs_update_or_create', return_create_flavor_extra_specs) - req = webob.Request.blank('/v1.1/123/flavors/1/os-extra_specs/key1') - req.method = 'PUT' - req.body = '{"key1": "value1"}' - req.headers["content-type"] = "application/json" - res = req.get_response(fakes.wsgi_app()) - self.assertEqual(200, res.status_int) - self.assertEqual('application/json', res.headers['Content-Type']) - res_dict = json.loads(res.body) + body = {"key1": "value1"} + + req = fakes.HTTPRequest.blank('/v1.1/123/flavors/1/os-extra_specs' + + '/key1') + res_dict = self.controller.update(req, 1, 'key1', body) + self.assertEqual('value1', res_dict['key1']) def test_update_item_empty_body(self): self.stubs.Set(nova.db, 'instance_type_extra_specs_update_or_create', return_create_flavor_extra_specs) - req = webob.Request.blank('/v1.1/123/flavors/1/os-extra_specs/key1') - req.method = 'PUT' - req.headers["content-type"] = "application/json" - res = req.get_response(fakes.wsgi_app()) - self.assertEqual(400, res.status_int) + + req = fakes.HTTPRequest.blank('/v1.1/123/flavors/1/os-extra_specs' + + '/key1') + self.assertRaises(webob.exc.HTTPBadRequest, self.controller.update, + req, 1, 'key1', '') def test_update_item_too_many_keys(self): self.stubs.Set(nova.db, 'instance_type_extra_specs_update_or_create', return_create_flavor_extra_specs) - req = webob.Request.blank('/v1.1/123/flavors/1/os-extra_specs/key1') - req.method = 'PUT' - req.body = '{"key1": "value1", "key2": "value2"}' - req.headers["content-type"] = "application/json" - res = req.get_response(fakes.wsgi_app()) - self.assertEqual(400, res.status_int) + body = {"key1": "value1", "key2": "value2"} + + req = fakes.HTTPRequest.blank('/v1.1/123/flavors/1/os-extra_specs' + + '/key1') + self.assertRaises(webob.exc.HTTPBadRequest, self.controller.update, + req, 1, 'key1', body) def test_update_item_body_uri_mismatch(self): self.stubs.Set(nova.db, 'instance_type_extra_specs_update_or_create', return_create_flavor_extra_specs) - req = webob.Request.blank('/v1.1/123/flavors/1/os-extra_specs/bad') - req.method = 'PUT' - req.body = '{"key1": "value1"}' - req.headers["content-type"] = "application/json" - res = req.get_response(fakes.wsgi_app()) - self.assertEqual(400, res.status_int) + body = {"key1": "value1"} + + req = fakes.HTTPRequest.blank('/v1.1/123/flavors/1/os-extra_specs/bad') + self.assertRaises(webob.exc.HTTPBadRequest, self.controller.update, + req, 1, 'bad', body) diff --git a/nova/tests/api/openstack/test_users.py b/nova/tests/api/openstack/test_users.py index cc77d7d26..82fce68ff 100644 --- a/nova/tests/api/openstack/test_users.py +++ b/nova/tests/api/openstack/test_users.py @@ -56,72 +56,29 @@ class UsersTest(test.TestCase): fakemgr.add_user(User('id1', 'guy1', 'acc1', 'secret1', False)) fakemgr.add_user(User('id2', 'guy2', 'acc2', 'secret2', True)) + self.controller = users.Controller() + def test_get_user_list(self): - req = webob.Request.blank('/v1.1/fake/users') - res = req.get_response(fakes.wsgi_app()) - res_dict = json.loads(res.body) + req = fakes.HTTPRequest.blank('/v1.1/fake/users') + res_dict = self.controller.index(req) - self.assertEqual(res.status_int, 200) self.assertEqual(len(res_dict['users']), 2) - def test_get_user_list_xml(self): - req = webob.Request.blank('/v1.1/fake/users.xml') - res = req.get_response(fakes.wsgi_app()) - res_tree = etree.fromstring(res.body) - - self.assertEqual(res.status_int, 200) - self.assertEqual(res_tree.tag, 'users') - self.assertEqual(len(res_tree), 2) - self.assertEqual(res_tree[0].tag, 'user') - self.assertEqual(res_tree[0].get('id'), 'id1') - self.assertEqual(res_tree[1].tag, 'user') - self.assertEqual(res_tree[1].get('id'), 'id2') - def test_get_user_by_id(self): - req = webob.Request.blank('/v1.1/fake/users/id2') - res = req.get_response(fakes.wsgi_app()) - res_dict = json.loads(res.body) + req = fakes.HTTPRequest.blank('/v1.1/fake/users/id2') + res_dict = self.controller.show(req, 'id2') self.assertEqual(res_dict['user']['id'], 'id2') self.assertEqual(res_dict['user']['name'], 'guy2') self.assertEqual(res_dict['user']['secret'], 'secret2') self.assertEqual(res_dict['user']['admin'], True) - self.assertEqual(res.status_int, 200) - - def test_get_user_by_id_xml(self): - req = webob.Request.blank('/v1.1/fake/users/id2.xml') - res = req.get_response(fakes.wsgi_app()) - res_tree = etree.fromstring(res.body) - - self.assertEqual(res.status_int, 200) - self.assertEqual(res_tree.tag, 'user') - self.assertEqual(res_tree.get('id'), 'id2') - self.assertEqual(res_tree.get('name'), 'guy2') - self.assertEqual(res_tree.get('secret'), 'secret2') - self.assertEqual(res_tree.get('admin'), 'True') def test_user_delete(self): - # Check the user exists - req = webob.Request.blank('/v1.1/fake/users/id1') - res = req.get_response(fakes.wsgi_app()) - res_dict = json.loads(res.body) - - self.assertEqual(res_dict['user']['id'], 'id1') - self.assertEqual(res.status_int, 200) - - # Delete the user - req = webob.Request.blank('/v1.1/fake/users/id1') - req.method = 'DELETE' - res = req.get_response(fakes.wsgi_app()) + req = fakes.HTTPRequest.blank('/v1.1/fake/users/id1') + res_dict = self.controller.delete(req, 'id1') + self.assertTrue('id1' not in [u.id for u in fakes.FakeAuthManager.auth_data]) - self.assertEqual(res.status_int, 200) - - # Check the user is not returned (and returns 404) - req = webob.Request.blank('/v1.1/fake/users/id1') - res = req.get_response(fakes.wsgi_app()) - res_dict = json.loads(res.body) - self.assertEqual(res.status_int, 404) def test_user_create(self): secret = utils.generate_password() @@ -129,15 +86,8 @@ class UsersTest(test.TestCase): access='acc3', secret=secret, admin=True)) - req = webob.Request.blank('/v1.1/fake/users') - req.headers["Content-Type"] = "application/json" - req.method = 'POST' - req.body = json.dumps(body) - - res = req.get_response(fakes.wsgi_app()) - res_dict = json.loads(res.body) - - self.assertEqual(res.status_int, 200) + req = fakes.HTTPRequest.blank('/v1.1/fake/users') + res_dict = self.controller.create(req, body) # NOTE(justinsb): This is a questionable assertion in general # fake sets id=name, but others might not... @@ -151,72 +101,57 @@ class UsersTest(test.TestCase): fakes.FakeAuthManager.auth_data]) self.assertEqual(len(fakes.FakeAuthManager.auth_data), 3) - def test_user_create_xml(self): - secret = utils.generate_password() - body = dict(user=dict(name='test_guy', - access='acc3', - secret=secret, - admin=True)) - req = webob.Request.blank('/v1.1/fake/users.xml') - req.headers["Content-Type"] = "application/json" - req.method = 'POST' - req.body = json.dumps(body) - - res = req.get_response(fakes.wsgi_app()) - res_tree = etree.fromstring(res.body) - - self.assertEqual(res.status_int, 200) - - # NOTE(justinsb): This is a questionable assertion in general - # fake sets id=name, but others might not... - self.assertEqual(res_tree.tag, 'user') - self.assertEqual(res_tree.get('id'), 'test_guy') - - self.assertEqual(res_tree.get('name'), 'test_guy') - self.assertEqual(res_tree.get('access'), 'acc3') - self.assertEqual(res_tree.get('secret'), secret) - self.assertEqual(res_tree.get('admin'), 'True') - self.assertTrue('test_guy' in [u.id for u in - fakes.FakeAuthManager.auth_data]) - self.assertEqual(len(fakes.FakeAuthManager.auth_data), 3) - def test_user_update(self): new_secret = utils.generate_password() body = dict(user=dict(name='guy2', access='acc2', secret=new_secret)) - req = webob.Request.blank('/v1.1/fake/users/id2') - req.headers["Content-Type"] = "application/json" - req.method = 'PUT' - req.body = json.dumps(body) - res = req.get_response(fakes.wsgi_app()) - res_dict = json.loads(res.body) + req = fakes.HTTPRequest.blank('/v1.1/fake/users/id2') + res_dict = self.controller.update(req, 'id2', body) - self.assertEqual(res.status_int, 200) self.assertEqual(res_dict['user']['id'], 'id2') self.assertEqual(res_dict['user']['name'], 'guy2') self.assertEqual(res_dict['user']['access'], 'acc2') self.assertEqual(res_dict['user']['secret'], new_secret) self.assertEqual(res_dict['user']['admin'], True) - def test_user_update_xml(self): - new_secret = utils.generate_password() - body = dict(user=dict(name='guy2', - access='acc2', - secret=new_secret)) - req = webob.Request.blank('/v1.1/fake/users/id2.xml') - req.headers["Content-Type"] = "application/json" - req.method = 'PUT' - req.body = json.dumps(body) - res = req.get_response(fakes.wsgi_app()) - res_tree = etree.fromstring(res.body) +class TestUsersXMLSerializer(test.TestCase): + + serializer = users.UserXMLSerializer() + + def test_index(self): + fixture = {'users': [{'id': 'id1', + 'name': 'guy1', + 'secret': 'secret1', + 'admin': False}, + {'id': 'id2', + 'name': 'guy2', + 'secret': 'secret2', + 'admin': True}]} + + output = self.serializer.serialize(fixture, 'index') + res_tree = etree.XML(output) + + self.assertEqual(res_tree.tag, 'users') + self.assertEqual(len(res_tree), 2) + self.assertEqual(res_tree[0].tag, 'user') + self.assertEqual(res_tree[0].get('id'), 'id1') + self.assertEqual(res_tree[1].tag, 'user') + self.assertEqual(res_tree[1].get('id'), 'id2') + + def test_show(self): + fixture = {'user': {'id': 'id2', + 'name': 'guy2', + 'secret': 'secret2', + 'admin': True}} + + output = self.serializer.serialize(fixture, 'show') + res_tree = etree.XML(output) - self.assertEqual(res.status_int, 200) self.assertEqual(res_tree.tag, 'user') self.assertEqual(res_tree.get('id'), 'id2') self.assertEqual(res_tree.get('name'), 'guy2') - self.assertEqual(res_tree.get('access'), 'acc2') - self.assertEqual(res_tree.get('secret'), new_secret) + self.assertEqual(res_tree.get('secret'), 'secret2') self.assertEqual(res_tree.get('admin'), 'True') diff --git a/nova/tests/api/openstack/test_volume_types.py b/nova/tests/api/openstack/test_volume_types.py index 192e66854..ec1c44854 100644 --- a/nova/tests/api/openstack/test_volume_types.py +++ b/nova/tests/api/openstack/test_volume_types.py @@ -21,6 +21,7 @@ from nova import exception from nova import context from nova import test from nova import log as logging +from nova.api.openstack.contrib import volumetypes from nova.volume import volume_types from nova.tests.api.openstack import fakes @@ -75,6 +76,7 @@ class VolumeTypesApiTest(test.TestCase): def setUp(self): super(VolumeTypesApiTest, self).setUp() fakes.stub_out_key_pair_funcs(self.stubs) + self.controller = volumetypes.VolumeTypesController() def tearDown(self): self.stubs.UnsetAll() @@ -83,11 +85,9 @@ class VolumeTypesApiTest(test.TestCase): def test_volume_types_index(self): self.stubs.Set(volume_types, 'get_all_types', return_volume_types_get_all_types) - req = webob.Request.blank('/v1.1/123/os-volume-types') - res = req.get_response(fakes.wsgi_app()) - self.assertEqual(200, res.status_int) - res_dict = json.loads(res.body) - self.assertEqual('application/json', res.headers['Content-Type']) + + req = fakes.HTTPRequest.blank('/v1.1/123/os-volume-types') + res_dict = self.controller.index(req) self.assertEqual(3, len(res_dict)) for name in ['vol_type_1', 'vol_type_2', 'vol_type_3']: @@ -97,65 +97,60 @@ class VolumeTypesApiTest(test.TestCase): def test_volume_types_index_no_data(self): self.stubs.Set(volume_types, 'get_all_types', return_empty_volume_types_get_all_types) - req = webob.Request.blank('/v1.1/123/os-volume-types') - res = req.get_response(fakes.wsgi_app()) - res_dict = json.loads(res.body) - self.assertEqual(200, res.status_int) - self.assertEqual('application/json', res.headers['Content-Type']) + + req = fakes.HTTPRequest.blank('/v1.1/123/os-volume-types') + res_dict = self.controller.index(req) + self.assertEqual(0, len(res_dict)) def test_volume_types_show(self): self.stubs.Set(volume_types, 'get_volume_type', return_volume_types_get_volume_type) - req = webob.Request.blank('/v1.1/123/os-volume-types/1') - res = req.get_response(fakes.wsgi_app()) - self.assertEqual(200, res.status_int) - res_dict = json.loads(res.body) - self.assertEqual('application/json', res.headers['Content-Type']) + + req = fakes.HTTPRequest.blank('/v1.1/123/os-volume-types/1') + res_dict = self.controller.show(req, 1) + self.assertEqual(1, len(res_dict)) self.assertEqual('vol_type_1', res_dict['volume_type']['name']) def test_volume_types_show_not_found(self): self.stubs.Set(volume_types, 'get_volume_type', return_volume_types_get_volume_type) - req = webob.Request.blank('/v1.1/123/os-volume-types/777') - res = req.get_response(fakes.wsgi_app()) - self.assertEqual(404, res.status_int) + + req = fakes.HTTPRequest.blank('/v1.1/123/os-volume-types/777') + self.assertRaises(webob.exc.HTTPNotFound, self.controller.show, + req, '777') def test_volume_types_delete(self): self.stubs.Set(volume_types, 'get_volume_type', return_volume_types_get_volume_type) self.stubs.Set(volume_types, 'destroy', return_volume_types_destroy) - req = webob.Request.blank('/v1.1/123/os-volume-types/1') - req.method = 'DELETE' - res = req.get_response(fakes.wsgi_app()) - self.assertEqual(200, res.status_int) + + req = fakes.HTTPRequest.blank('/v1.1/123/os-volume-types/1') + self.controller.delete(req, 1) def test_volume_types_delete_not_found(self): self.stubs.Set(volume_types, 'get_volume_type', return_volume_types_get_volume_type) self.stubs.Set(volume_types, 'destroy', return_volume_types_destroy) - req = webob.Request.blank('/v1.1/123/os-volume-types/777') - req.method = 'DELETE' - res = req.get_response(fakes.wsgi_app()) - self.assertEqual(404, res.status_int) + + req = fakes.HTTPRequest.blank('/v1.1/123/os-volume-types/777') + self.assertRaises(webob.exc.HTTPNotFound, self.controller.delete, + req, '777') def test_create(self): self.stubs.Set(volume_types, 'create', return_volume_types_create) self.stubs.Set(volume_types, 'get_volume_type_by_name', return_volume_types_get_by_name) - req = webob.Request.blank('/v1.1/123/os-volume-types') - req.method = 'POST' - req.body = '{"volume_type": {"name": "vol_type_1", '\ - '"extra_specs": {"key1": "value1"}}}' - req.headers["content-type"] = "application/json" - res = req.get_response(fakes.wsgi_app()) - self.assertEqual(200, res.status_int) - res_dict = json.loads(res.body) - self.assertEqual('application/json', res.headers['Content-Type']) + + body = {"volume_type": {"name": "vol_type_1", + "extra_specs": {"key1": "value1"}}} + req = fakes.HTTPRequest.blank('/v1.1/123/os-volume-types') + res_dict = self.controller.create(req, body) + self.assertEqual(1, len(res_dict)) self.assertEqual('vol_type_1', res_dict['volume_type']['name']) @@ -164,8 +159,7 @@ class VolumeTypesApiTest(test.TestCase): return_volume_types_create) self.stubs.Set(volume_types, 'get_volume_type_by_name', return_volume_types_get_by_name) - req = webob.Request.blank('/v1.1/123/os-volume-types') - req.method = 'POST' - req.headers["content-type"] = "application/json" - res = req.get_response(fakes.wsgi_app()) - self.assertEqual(400, res.status_int) + + req = fakes.HTTPRequest.blank('/v1.1/123/os-volume-types') + self.assertRaises(webob.exc.HTTPUnprocessableEntity, + self.controller.create, req, '') diff --git a/nova/tests/api/openstack/test_volume_types_extra_specs.py b/nova/tests/api/openstack/test_volume_types_extra_specs.py index e458c5e19..796478838 100644 --- a/nova/tests/api/openstack/test_volume_types_extra_specs.py +++ b/nova/tests/api/openstack/test_volume_types_extra_specs.py @@ -26,6 +26,7 @@ import os.path from nova import test from nova.api import openstack from nova.api.openstack import extensions +from nova.api.openstack.contrib import volumetypes from nova.tests.api.openstack import fakes import nova.wsgi @@ -63,119 +64,106 @@ class VolumeTypesExtraSpecsTest(test.TestCase): super(VolumeTypesExtraSpecsTest, self).setUp() fakes.stub_out_key_pair_funcs(self.stubs) self.api_path = '/v1.1/123/os-volume-types/1/extra_specs' + self.controller = volumetypes.VolumeTypeExtraSpecsController() def test_index(self): self.stubs.Set(nova.db, 'volume_type_extra_specs_get', return_volume_type_extra_specs) - request = webob.Request.blank(self.api_path) - res = request.get_response(fakes.wsgi_app()) - self.assertEqual(200, res.status_int) - res_dict = json.loads(res.body) - self.assertEqual('application/json', res.headers['Content-Type']) + + req = fakes.HTTPRequest.blank(self.api_path) + res_dict = self.controller.index(req, 1) + self.assertEqual('value1', res_dict['extra_specs']['key1']) def test_index_no_data(self): self.stubs.Set(nova.db, 'volume_type_extra_specs_get', return_empty_volume_type_extra_specs) - req = webob.Request.blank(self.api_path) - res = req.get_response(fakes.wsgi_app()) - res_dict = json.loads(res.body) - self.assertEqual(200, res.status_int) - self.assertEqual('application/json', res.headers['Content-Type']) + + req = fakes.HTTPRequest.blank(self.api_path) + res_dict = self.controller.index(req, 1) + self.assertEqual(0, len(res_dict['extra_specs'])) def test_show(self): self.stubs.Set(nova.db, 'volume_type_extra_specs_get', return_volume_type_extra_specs) - req = webob.Request.blank(self.api_path + '/key5') - res = req.get_response(fakes.wsgi_app()) - self.assertEqual(200, res.status_int) - res_dict = json.loads(res.body) - self.assertEqual('application/json', res.headers['Content-Type']) + + req = fakes.HTTPRequest.blank(self.api_path + '/key5') + res_dict = self.controller.show(req, 1, 'key5') + self.assertEqual('value5', res_dict['key5']) def test_show_spec_not_found(self): self.stubs.Set(nova.db, 'volume_type_extra_specs_get', return_empty_volume_type_extra_specs) - req = webob.Request.blank(self.api_path + '/key6') - res = req.get_response(fakes.wsgi_app()) - res_dict = json.loads(res.body) - self.assertEqual(404, res.status_int) + + req = fakes.HTTPRequest.blank(self.api_path + '/key6') + self.assertRaises(webob.exc.HTTPNotFound, self.controller.show, + req, 1, 'key6') def test_delete(self): self.stubs.Set(nova.db, 'volume_type_extra_specs_delete', delete_volume_type_extra_specs) - req = webob.Request.blank(self.api_path + '/key5') - req.method = 'DELETE' - res = req.get_response(fakes.wsgi_app()) - self.assertEqual(200, res.status_int) + + req = fakes.HTTPRequest.blank(self.api_path + '/key5') + self.controller.delete(req, 1, 'key5') def test_create(self): self.stubs.Set(nova.db, 'volume_type_extra_specs_update_or_create', return_create_volume_type_extra_specs) - req = webob.Request.blank(self.api_path) - req.method = 'POST' - req.body = '{"extra_specs": {"key1": "value1"}}' - req.headers["content-type"] = "application/json" - res = req.get_response(fakes.wsgi_app()) - res_dict = json.loads(res.body) - self.assertEqual(200, res.status_int) - self.assertEqual('application/json', res.headers['Content-Type']) + body = {"extra_specs": {"key1": "value1"}} + + req = fakes.HTTPRequest.blank(self.api_path) + res_dict = self.controller.create(req, 1, body) + self.assertEqual('value1', res_dict['extra_specs']['key1']) def test_create_empty_body(self): self.stubs.Set(nova.db, 'volume_type_extra_specs_update_or_create', return_create_volume_type_extra_specs) - req = webob.Request.blank(self.api_path) - req.method = 'POST' - req.headers["content-type"] = "application/json" - res = req.get_response(fakes.wsgi_app()) - self.assertEqual(400, res.status_int) + + req = fakes.HTTPRequest.blank(self.api_path) + self.assertRaises(webob.exc.HTTPBadRequest, self.controller.create, + req, 1, '') def test_update_item(self): self.stubs.Set(nova.db, 'volume_type_extra_specs_update_or_create', return_create_volume_type_extra_specs) - req = webob.Request.blank(self.api_path + '/key1') - req.method = 'PUT' - req.body = '{"key1": "value1"}' - req.headers["content-type"] = "application/json" - res = req.get_response(fakes.wsgi_app()) - self.assertEqual(200, res.status_int) - self.assertEqual('application/json', res.headers['Content-Type']) - res_dict = json.loads(res.body) + body = {"key1": "value1"} + + req = fakes.HTTPRequest.blank(self.api_path + '/key1') + res_dict = self.controller.update(req, 1, 'key1', body) + self.assertEqual('value1', res_dict['key1']) def test_update_item_empty_body(self): self.stubs.Set(nova.db, 'volume_type_extra_specs_update_or_create', return_create_volume_type_extra_specs) - req = webob.Request.blank(self.api_path + '/key1') - req.method = 'PUT' - req.headers["content-type"] = "application/json" - res = req.get_response(fakes.wsgi_app()) - self.assertEqual(400, res.status_int) + + req = fakes.HTTPRequest.blank(self.api_path + '/key1') + self.assertRaises(webob.exc.HTTPBadRequest, self.controller.update, + req, 1, 'key1', '') def test_update_item_too_many_keys(self): self.stubs.Set(nova.db, 'volume_type_extra_specs_update_or_create', return_create_volume_type_extra_specs) - req = webob.Request.blank(self.api_path + '/key1') - req.method = 'PUT' - req.body = '{"key1": "value1", "key2": "value2"}' - req.headers["content-type"] = "application/json" - res = req.get_response(fakes.wsgi_app()) - self.assertEqual(400, res.status_int) + body = {"key1": "value1", "key2": "value2"} + + req = fakes.HTTPRequest.blank(self.api_path + '/key1') + self.assertRaises(webob.exc.HTTPBadRequest, self.controller.update, + req, 1, 'key1', body) def test_update_item_body_uri_mismatch(self): self.stubs.Set(nova.db, 'volume_type_extra_specs_update_or_create', return_create_volume_type_extra_specs) - req = webob.Request.blank(self.api_path + '/bad') - req.method = 'PUT' - req.body = '{"key1": "value1"}' - req.headers["content-type"] = "application/json" - res = req.get_response(fakes.wsgi_app()) - self.assertEqual(400, res.status_int) + body = {"key1": "value1"} + + req = fakes.HTTPRequest.blank(self.api_path + '/bad') + self.assertRaises(webob.exc.HTTPBadRequest, self.controller.update, + req, 1, 'bad', body) diff --git a/nova/tests/api/openstack/test_zones.py b/nova/tests/api/openstack/test_zones.py index af762d3d6..496c3e8e6 100644 --- a/nova/tests/api/openstack/test_zones.py +++ b/nova/tests/api/openstack/test_zones.py @@ -36,7 +36,8 @@ FLAGS = flags.FLAGS def zone_get(context, zone_id): return dict(id=1, api_url='http://example.com', username='bob', - password='xxx', weight_scale=1.0, weight_offset=0.0) + password='xxx', weight_scale=1.0, weight_offset=0.0, + name='darksecret') def zone_create(context, values): @@ -106,198 +107,81 @@ class ZonesTest(test.TestCase): self.stubs.Set(nova.db, 'zone_create', zone_create) self.stubs.Set(nova.db, 'zone_delete', zone_delete) + self.controller = zones.Controller() + def test_get_zone_list_scheduler(self): self.stubs.Set(api, '_call_scheduler', zone_get_all_scheduler) - req = webob.Request.blank('/v1.1/fake/zones') - res = req.get_response(fakes.wsgi_app()) - res_dict = json.loads(res.body) - - self.assertEqual(res.status_int, 200) - self.assertEqual(len(res_dict['zones']), 2) - def test_get_zone_list_scheduler_xml(self): - self.stubs.Set(api, '_call_scheduler', zone_get_all_scheduler) - req = webob.Request.blank('/v1.1/fake/zones.xml') - res = req.get_response(fakes.wsgi_app()) - res_tree = etree.fromstring(res.body) + req = fakes.HTTPRequest.blank('/v1.1/fake/zones') + res_dict = self.controller.index(req) - self.assertEqual(res.status_int, 200) - self.assertEqual(res_tree.tag, '{%s}zones' % xmlutil.XMLNS_V10) - self.assertEqual(len(res_tree), 2) - self.assertEqual(res_tree[0].tag, '{%s}zone' % xmlutil.XMLNS_V10) - self.assertEqual(res_tree[1].tag, '{%s}zone' % xmlutil.XMLNS_V10) + self.assertEqual(len(res_dict['zones']), 2) def test_get_zone_list_db(self): self.stubs.Set(api, '_call_scheduler', zone_get_all_scheduler_empty) self.stubs.Set(nova.db, 'zone_get_all', zone_get_all_db) - req = webob.Request.blank('/v1.1/fake/zones') - req.headers["Content-Type"] = "application/json" - res = req.get_response(fakes.wsgi_app()) - self.assertEqual(res.status_int, 200) - res_dict = json.loads(res.body) - self.assertEqual(len(res_dict['zones']), 2) + req = fakes.HTTPRequest.blank('/v1.1/fake/zones') + res_dict = self.controller.index(req) - def test_get_zone_list_db_xml(self): - self.stubs.Set(api, '_call_scheduler', zone_get_all_scheduler_empty) - self.stubs.Set(nova.db, 'zone_get_all', zone_get_all_db) - req = webob.Request.blank('/v1.1/fake/zones.xml') - req.headers["Content-Type"] = "application/json" - res = req.get_response(fakes.wsgi_app()) - - self.assertEqual(res.status_int, 200) - res_tree = etree.fromstring(res.body) - self.assertEqual(res_tree.tag, '{%s}zones' % xmlutil.XMLNS_V10) - self.assertEqual(len(res_tree), 2) - self.assertEqual(res_tree[0].tag, '{%s}zone' % xmlutil.XMLNS_V10) - self.assertEqual(res_tree[1].tag, '{%s}zone' % xmlutil.XMLNS_V10) + self.assertEqual(len(res_dict['zones']), 2) def test_get_zone_by_id(self): - req = webob.Request.blank('/v1.1/fake/zones/1') - req.headers["Content-Type"] = "application/json" - res = req.get_response(fakes.wsgi_app()) + req = fakes.HTTPRequest.blank('/v1.1/fake/zones/1') + res_dict = self.controller.show(req, 1) - self.assertEqual(res.status_int, 200) - res_dict = json.loads(res.body) self.assertEqual(res_dict['zone']['id'], 1) self.assertEqual(res_dict['zone']['api_url'], 'http://example.com') self.assertFalse('password' in res_dict['zone']) - def test_get_zone_by_id_xml(self): - req = webob.Request.blank('/v1.1/fake/zones/1.xml') - req.headers["Content-Type"] = "application/json" - res = req.get_response(fakes.wsgi_app()) - res_tree = etree.fromstring(res.body) - - self.assertEqual(res.status_int, 200) - self.assertEqual(res_tree.tag, '{%s}zone' % xmlutil.XMLNS_V10) - self.assertEqual(res_tree.get('id'), '1') - self.assertEqual(res_tree.get('api_url'), 'http://example.com') - self.assertEqual(res_tree.get('password'), None) - def test_zone_delete(self): - req = webob.Request.blank('/v1.1/fake/zones/1') - req.headers["Content-Type"] = "application/json" - res = req.get_response(fakes.wsgi_app()) - - self.assertEqual(res.status_int, 200) + req = fakes.HTTPRequest.blank('/v1.1/fake/zones/1') + self.controller.delete(req, 1) def test_zone_create(self): body = dict(zone=dict(api_url='http://example.com', username='fred', password='fubar')) - req = webob.Request.blank('/v1.1/fake/zones') - req.headers["Content-Type"] = "application/json" - req.method = 'POST' - req.body = json.dumps(body) - res = req.get_response(fakes.wsgi_app()) + req = fakes.HTTPRequest.blank('/v1.1/fake/zones') + res_dict = self.controller.create(req, body) - self.assertEqual(res.status_int, 200) - res_dict = json.loads(res.body) self.assertEqual(res_dict['zone']['id'], 1) self.assertEqual(res_dict['zone']['api_url'], 'http://example.com') self.assertFalse('username' in res_dict['zone']) - def test_zone_create_xml(self): - body = dict(zone=dict(api_url='http://example.com', username='fred', - password='fubar')) - req = webob.Request.blank('/v1.1/fake/zones.xml') - req.headers["Content-Type"] = "application/json" - req.method = 'POST' - req.body = json.dumps(body) - - res = req.get_response(fakes.wsgi_app()) - - self.assertEqual(res.status_int, 200) - res_tree = etree.fromstring(res.body) - self.assertEqual(res_tree.tag, '{%s}zone' % xmlutil.XMLNS_V10) - self.assertEqual(res_tree.get('id'), '1') - self.assertEqual(res_tree.get('api_url'), 'http://example.com') - self.assertEqual(res_tree.get('username'), None) - def test_zone_update(self): body = dict(zone=dict(username='zeb', password='sneaky')) - req = webob.Request.blank('/v1.1/fake/zones/1') - req.headers["Content-Type"] = "application/json" - req.method = 'PUT' - req.body = json.dumps(body) - res = req.get_response(fakes.wsgi_app()) + req = fakes.HTTPRequest.blank('/v1.1/fake/zones/1') + res_dict = self.controller.update(req, 1, body) - self.assertEqual(res.status_int, 200) - res_dict = json.loads(res.body) self.assertEqual(res_dict['zone']['id'], 1) self.assertEqual(res_dict['zone']['api_url'], 'http://example.com') self.assertFalse('username' in res_dict['zone']) - def test_zone_update_xml(self): - body = dict(zone=dict(username='zeb', password='sneaky')) - req = webob.Request.blank('/v1.1/fake/zones/1.xml') - req.headers["Content-Type"] = "application/json" - req.method = 'PUT' - req.body = json.dumps(body) - - res = req.get_response(fakes.wsgi_app()) - - self.assertEqual(res.status_int, 200) - res_tree = etree.fromstring(res.body) - self.assertEqual(res_tree.tag, '{%s}zone' % xmlutil.XMLNS_V10) - self.assertEqual(res_tree.get('id'), '1') - self.assertEqual(res_tree.get('api_url'), 'http://example.com') - self.assertEqual(res_tree.get('username'), None) - def test_zone_info(self): caps = ['cap1=a;b', 'cap2=c;d'] self.flags(zone_name='darksecret', zone_capabilities=caps) self.stubs.Set(api, '_call_scheduler', zone_capabilities) - body = dict(zone=dict(username='zeb', password='sneaky')) - req = webob.Request.blank('/v1.1/fake/zones/info') + req = fakes.HTTPRequest.blank('/v1.1/fake/zones/info') + res_dict = self.controller.info(req) - res = req.get_response(fakes.wsgi_app()) - res_dict = json.loads(res.body) - self.assertEqual(res.status_int, 200) self.assertEqual(res_dict['zone']['name'], 'darksecret') self.assertEqual(res_dict['zone']['cap1'], 'a;b') self.assertEqual(res_dict['zone']['cap2'], 'c;d') - def test_zone_info_xml(self): - caps = ['cap1=a;b', 'cap2=c;d'] - self.flags(zone_name='darksecret', zone_capabilities=caps) - self.stubs.Set(api, '_call_scheduler', zone_capabilities) - - body = dict(zone=dict(username='zeb', password='sneaky')) - req = webob.Request.blank('/v1.1/fake/zones/info.xml') - - res = req.get_response(fakes.wsgi_app()) - res_tree = etree.fromstring(res.body) - self.assertEqual(res.status_int, 200) - self.assertEqual(res_tree.tag, '{%s}zone' % xmlutil.XMLNS_V10) - self.assertEqual(res_tree.get('name'), 'darksecret') - for elem in res_tree: - self.assertEqual(elem.tag in ('{%s}cap1' % xmlutil.XMLNS_V10, - '{%s}cap2' % xmlutil.XMLNS_V10), - True) - if elem.tag == '{%s}cap1' % xmlutil.XMLNS_V10: - self.assertEqual(elem.text, 'a;b') - elif elem.tag == '{%s}cap2' % xmlutil.XMLNS_V10: - self.assertEqual(elem.text, 'c;d') - def test_zone_select(self): key = 'c286696d887c9aa0611bbb3e2025a45a' self.flags(build_plan_encryption_key=key) self.stubs.Set(api, 'select', zone_select) - req = webob.Request.blank('/v1.1/fake/zones/select') - req.method = 'POST' - req.headers["Content-Type"] = "application/json" # Select queries end up being JSON encoded twice. # Once to a string and again as an HTTP POST Body - req.body = json.dumps(json.dumps({})) + body = json.dumps({}) - res = req.get_response(fakes.wsgi_app()) - res_dict = json.loads(res.body) - self.assertEqual(res.status_int, 200) + req = fakes.HTTPRequest.blank('/v1.1/fake/zones/select') + res_dict = self.controller.select(req, body) self.assertTrue('weights' in res_dict) @@ -317,21 +201,23 @@ class ZonesTest(test.TestCase): self.assertEqual(len(item), 2) self.assertTrue('weight' in item) - def test_zone_select_xml(self): + +class TestZonesXMLSerializer(test.TestCase): + + serializer = zones.ZonesXMLSerializer() + + def test_select(self): key = 'c286696d887c9aa0611bbb3e2025a45a' - self.flags(build_plan_encryption_key=key) - self.stubs.Set(api, 'select', zone_select) - req = webob.Request.blank('/v1.1/fake/zones/select.xml') - req.method = 'POST' - req.headers["Content-Type"] = "application/json" - # Select queries end up being JSON encoded twice. - # Once to a string and again as an HTTP POST Body - req.body = json.dumps(json.dumps({})) + encrypt = crypto.encryptor(key) + decrypt = crypto.decryptor(key) + + item = GLOBAL_BUILD_PLAN[0] + fixture = {'weights': {'blob': encrypt(json.dumps(item)), + 'weight': item['weight']}} - res = req.get_response(fakes.wsgi_app()) - res_tree = etree.fromstring(res.body) - self.assertEqual(res.status_int, 200) + output = self.serializer.serialize(fixture, 'select') + res_tree = etree.XML(output) self.assertEqual(res_tree.tag, '{%s}weights' % xmlutil.XMLNS_V10) @@ -345,7 +231,6 @@ class ZonesTest(test.TestCase): elif chld.tag.endswith('weight'): weight = chld.text - decrypt = crypto.decryptor(FLAGS.build_plan_encryption_key) secret_item = json.loads(decrypt(blob)) found = False for original_item in GLOBAL_BUILD_PLAN: @@ -358,3 +243,41 @@ class ZonesTest(test.TestCase): self.assertTrue(found) self.assertEqual(len(item), 2) self.assertTrue(weight) + + def test_index(self): + fixture = {'zones': zone_get_all_scheduler()} + + output = self.serializer.serialize(fixture, 'index') + res_tree = etree.XML(output) + + self.assertEqual(res_tree.tag, '{%s}zones' % xmlutil.XMLNS_V10) + self.assertEqual(len(res_tree), 2) + self.assertEqual(res_tree[0].tag, '{%s}zone' % xmlutil.XMLNS_V10) + self.assertEqual(res_tree[1].tag, '{%s}zone' % xmlutil.XMLNS_V10) + + def test_show(self): + zone = {'id': 1, + 'api_url': 'http://example.com', + 'name': 'darksecret', + 'cap1': 'a;b', + 'cap2': 'c;d'} + fixture = {'zone': zone} + + output = self.serializer.serialize(fixture, 'show') + print repr(output) + res_tree = etree.XML(output) + + self.assertEqual(res_tree.tag, '{%s}zone' % xmlutil.XMLNS_V10) + self.assertEqual(res_tree.get('id'), '1') + self.assertEqual(res_tree.get('api_url'), 'http://example.com') + self.assertEqual(res_tree.get('password'), None) + + self.assertEqual(res_tree.get('name'), 'darksecret') + for elem in res_tree: + self.assertEqual(elem.tag in ('{%s}cap1' % xmlutil.XMLNS_V10, + '{%s}cap2' % xmlutil.XMLNS_V10), + True) + if elem.tag == '{%s}cap1' % xmlutil.XMLNS_V10: + self.assertEqual(elem.text, 'a;b') + elif elem.tag == '{%s}cap2' % xmlutil.XMLNS_V10: + self.assertEqual(elem.text, 'c;d') |
