From: He Jie Xu Date: Mon, 13 May 2013 02:26:09 +0000 (+0800) Subject: Fix invalid status code assertion after handle expected exception X-Git-Url: https://review.fuel-infra.org/gitweb?a=commitdiff_plain;h=4184d30458a2eae46c2ec4db6fca26e57a8a2353;p=openstack-build%2Fneutron-build.git Fix invalid status code assertion after handle expected exception Fix bug 1179350 Change-Id: I88b511206d97f2ad607a7889c61df69a8638ced3 --- diff --git a/quantum/tests/unit/nicira/test_nicira_plugin.py b/quantum/tests/unit/nicira/test_nicira_plugin.py index 506b1c149..b2baee860 100644 --- a/quantum/tests/unit/nicira/test_nicira_plugin.py +++ b/quantum/tests/unit/nicira/test_nicira_plugin.py @@ -19,7 +19,6 @@ import os import mock import netaddr from oslo.config import cfg -import testtools import webob.exc from quantum.common import constants @@ -41,6 +40,7 @@ import quantum.tests.unit.test_extension_portsecurity as psec import quantum.tests.unit.test_extension_security_group as ext_sg from quantum.tests.unit import test_extensions import quantum.tests.unit.test_l3_plugin as test_l3_plugin +from quantum.tests.unit import testlib_api NICIRA_PKG_PATH = nvp_plugin.__name__ NICIRA_EXT_PATH = "../../plugins/nicira/extensions" @@ -186,10 +186,10 @@ class TestNiciraNetworksV2(test_plugin.TestNetworksV2, self._test_create_bridge_network(vlan_id=123) def test_create_bridge_vlan_network_outofrange_returns_400(self): - with testtools.ExpectedException( + with testlib_api.ExpectedException( webob.exc.HTTPClientError) as ctx_manager: self._test_create_bridge_network(vlan_id=5000) - self.assertEqual(ctx_manager.exception.code, 400) + self.assertEqual(ctx_manager.exception.code, 400) def test_list_networks_filter_by_id(self): # We add this unit test to cover some logic specific to the diff --git a/quantum/tests/unit/test_db_plugin.py b/quantum/tests/unit/test_db_plugin.py index 786e25479..167b9279f 100644 --- a/quantum/tests/unit/test_db_plugin.py +++ b/quantum/tests/unit/test_db_plugin.py @@ -23,7 +23,6 @@ import random import mock from oslo.config import cfg -import testtools from testtools import matchers import webob.exc @@ -1806,14 +1805,14 @@ class TestNetworksV2(QuantumDbPluginV2TestCase): def test_create_public_network_no_admin_tenant(self): name = 'public_net' - with testtools.ExpectedException( + with testlib_api.ExpectedException( webob.exc.HTTPClientError) as ctx_manager: with self.network(name=name, shared=True, tenant_id="another_tenant", set_context=True): pass - self.assertEqual(ctx_manager.exception.code, 403) + self.assertEqual(ctx_manager.exception.code, 403) def test_update_network(self): with self.network() as network: @@ -2346,13 +2345,13 @@ class TestSubnetsV2(QuantumDbPluginV2TestCase): with self.subnet(network=network, gateway_ip=gateway_ip_1, cidr=cidr_1): - with testtools.ExpectedException( + with testlib_api.ExpectedException( webob.exc.HTTPClientError) as ctx_manager: with self.subnet(network=network, gateway_ip=gateway_ip_2, cidr=cidr_2): pass - self.assertEqual(ctx_manager.exception.code, 400) + self.assertEqual(ctx_manager.exception.code, 400) def test_create_subnet_bad_V4_cidr(self): with self.network() as network: @@ -2389,12 +2388,12 @@ class TestSubnetsV2(QuantumDbPluginV2TestCase): cidr_1 = '10.0.0.0/23' cidr_2 = '10.0.0.0/24' cfg.CONF.set_override('allow_overlapping_ips', False) - with testtools.ExpectedException( + with testlib_api.ExpectedException( webob.exc.HTTPClientError) as ctx_manager: with contextlib.nested(self.subnet(cidr=cidr_1), self.subnet(cidr=cidr_2)): pass - self.assertEqual(ctx_manager.exception.code, 400) + self.assertEqual(ctx_manager.exception.code, 400) def test_create_subnets_bulk_native(self): if self._skip_native_bulk: @@ -2796,23 +2795,23 @@ class TestSubnetsV2(QuantumDbPluginV2TestCase): cidr = '10.0.0.0/24' allocation_pools = [{'start': '10.0.0.1', 'end': '10.0.0.5'}] - with testtools.ExpectedException( + with testlib_api.ExpectedException( webob.exc.HTTPClientError) as ctx_manager: self._test_create_subnet(cidr=cidr, allocation_pools=allocation_pools) - self.assertEqual(ctx_manager.exception.code, 409) + self.assertEqual(ctx_manager.exception.code, 409) def test_create_subnet_gateway_in_allocation_pool_returns_409(self): gateway_ip = '10.0.0.50' cidr = '10.0.0.0/24' allocation_pools = [{'start': '10.0.0.1', 'end': '10.0.0.100'}] - with testtools.ExpectedException( + with testlib_api.ExpectedException( webob.exc.HTTPClientError) as ctx_manager: self._test_create_subnet(gateway_ip=gateway_ip, cidr=cidr, allocation_pools=allocation_pools) - self.assertEqual(ctx_manager.exception.code, 409) + self.assertEqual(ctx_manager.exception.code, 409) def test_create_subnet_overlapping_allocation_pools_returns_409(self): gateway_ip = '10.0.0.1' @@ -2821,44 +2820,44 @@ class TestSubnetsV2(QuantumDbPluginV2TestCase): 'end': '10.0.0.150'}, {'start': '10.0.0.140', 'end': '10.0.0.180'}] - with testtools.ExpectedException( + with testlib_api.ExpectedException( webob.exc.HTTPClientError) as ctx_manager: self._test_create_subnet(gateway_ip=gateway_ip, cidr=cidr, allocation_pools=allocation_pools) - self.assertEqual(ctx_manager.exception.code, 409) + self.assertEqual(ctx_manager.exception.code, 409) def test_create_subnet_invalid_allocation_pool_returns_400(self): gateway_ip = '10.0.0.1' cidr = '10.0.0.0/24' allocation_pools = [{'start': '10.0.0.2', 'end': '10.0.0.256'}] - with testtools.ExpectedException( + with testlib_api.ExpectedException( webob.exc.HTTPClientError) as ctx_manager: self._test_create_subnet(gateway_ip=gateway_ip, cidr=cidr, allocation_pools=allocation_pools) - self.assertEqual(ctx_manager.exception.code, 400) + self.assertEqual(ctx_manager.exception.code, 400) def test_create_subnet_out_of_range_allocation_pool_returns_400(self): gateway_ip = '10.0.0.1' cidr = '10.0.0.0/24' allocation_pools = [{'start': '10.0.0.2', 'end': '10.0.1.6'}] - with testtools.ExpectedException( + with testlib_api.ExpectedException( webob.exc.HTTPClientError) as ctx_manager: self._test_create_subnet(gateway_ip=gateway_ip, cidr=cidr, allocation_pools=allocation_pools) - self.assertEqual(ctx_manager.exception.code, 400) + self.assertEqual(ctx_manager.exception.code, 400) def test_create_subnet_shared_returns_400(self): cidr = '10.0.0.0/24' - with testtools.ExpectedException( + with testlib_api.ExpectedException( webob.exc.HTTPClientError) as ctx_manager: self._test_create_subnet(cidr=cidr, shared=True) - self.assertEqual(ctx_manager.exception.code, 400) + self.assertEqual(ctx_manager.exception.code, 400) def test_create_subnet_inconsistent_ipv6_cidrv4(self): with self.network() as network: diff --git a/quantum/tests/unit/test_l3_plugin.py b/quantum/tests/unit/test_l3_plugin.py index 22c199a8f..180ccae63 100644 --- a/quantum/tests/unit/test_l3_plugin.py +++ b/quantum/tests/unit/test_l3_plugin.py @@ -24,7 +24,6 @@ import itertools import mock from oslo.config import cfg -import testtools from webob import exc import webtest @@ -1484,13 +1483,13 @@ class L3NatDBTestCase(L3NatTestCaseBase): def test_create_port_external_network_non_admin_fails(self): with self.network(router__external=True) as ext_net: with self.subnet(network=ext_net) as ext_subnet: - with testtools.ExpectedException( + with testlib_api.ExpectedException( exc.HTTPClientError) as ctx_manager: with self.port(subnet=ext_subnet, set_context='True', tenant_id='noadmin'): pass - self.assertEqual(ctx_manager.exception.code, 403) + self.assertEqual(ctx_manager.exception.code, 403) def test_create_port_external_network_admin_suceeds(self): with self.network(router__external=True) as ext_net: @@ -1500,12 +1499,12 @@ class L3NatDBTestCase(L3NatTestCaseBase): ext_net['network']['id']) def test_create_external_network_non_admin_fails(self): - with testtools.ExpectedException(exc.HTTPClientError) as ctx_manager: + with testlib_api.ExpectedException(exc.HTTPClientError) as ctx_manager: with self.network(router__external=True, set_context='True', tenant_id='noadmin'): pass - self.assertEqual(ctx_manager.exception.code, 403) + self.assertEqual(ctx_manager.exception.code, 403) def test_create_external_network_admin_suceeds(self): with self.network(router__external=True) as ext_net: diff --git a/quantum/tests/unit/test_routerserviceinsertion.py b/quantum/tests/unit/test_routerserviceinsertion.py index 60491af1a..7e1f2b281 100644 --- a/quantum/tests/unit/test_routerserviceinsertion.py +++ b/quantum/tests/unit/test_routerserviceinsertion.py @@ -15,7 +15,6 @@ # under the License. from oslo.config import cfg -import testtools import webob.exc as webexc import quantum @@ -293,11 +292,11 @@ class RouterServiceInsertionTestCase(base.BaseTestCase): } if update_service_type_id: data["router"]["service_type_id"] = _uuid() - with testtools.ExpectedException( + with testlib_api.ExpectedException( webexc.HTTPClientError) as ctx_manager: res = self._do_request( 'PUT', _get_path('routers/{0}'.format(router_id)), data) - self.assertEqual(ctx_manager.exception.code, 400) + self.assertEqual(ctx_manager.exception.code, 400) else: res = self._do_request( 'PUT', _get_path('routers/{0}'.format(router_id)), data) @@ -422,12 +421,12 @@ class RouterServiceInsertionTestCase(base.BaseTestCase): data = {res: uattrs} if update_router_id: uattrs['router_id'] = self._router_id - with testtools.ExpectedException( + with testlib_api.ExpectedException( webexc.HTTPClientError) as ctx_manager: self._do_request( 'PUT', _get_path('lb/{0}s/{1}'.format(res, obj['id'])), data) - self.assertEqual(ctx_manager.exception.code, 400) + self.assertEqual(ctx_manager.exception.code, 400) else: self._do_request( 'PUT', diff --git a/quantum/tests/unit/testlib_api.py b/quantum/tests/unit/testlib_api.py index eed2bd47c..fe0718f19 100644 --- a/quantum/tests/unit/testlib_api.py +++ b/quantum/tests/unit/testlib_api.py @@ -13,11 +13,26 @@ # License for the specific language governing permissions and limitations # under the License. +import testtools + from quantum.api.v2 import attributes from quantum.tests import base from quantum import wsgi +class ExpectedException(testtools.ExpectedException): + def __enter__(self): + return self + + def __exit__(self, exc_type, exc_value, traceback): + if super(ExpectedException, self).__exit__(exc_type, + exc_value, + traceback): + self.exception = exc_value + return True + return False + + def create_request(path, body, content_type, method='GET', query_string=None, context=None): if query_string: