Fix invalid status code assertion after handle expected exception

Fix bug 1179350

Change-Id: I88b511206d97f2ad607a7889c61df69a8638ced3
This commit is contained in:
He Jie Xu
2013-05-13 10:26:09 +08:00
parent 47c399b135
commit 4184d30458
5 changed files with 44 additions and 32 deletions

View File

@@ -19,7 +19,6 @@ import os
import mock import mock
import netaddr import netaddr
from oslo.config import cfg from oslo.config import cfg
import testtools
import webob.exc import webob.exc
from quantum.common import constants 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 import quantum.tests.unit.test_extension_security_group as ext_sg
from quantum.tests.unit import test_extensions from quantum.tests.unit import test_extensions
import quantum.tests.unit.test_l3_plugin as test_l3_plugin 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_PKG_PATH = nvp_plugin.__name__
NICIRA_EXT_PATH = "../../plugins/nicira/extensions" NICIRA_EXT_PATH = "../../plugins/nicira/extensions"
@@ -186,10 +186,10 @@ class TestNiciraNetworksV2(test_plugin.TestNetworksV2,
self._test_create_bridge_network(vlan_id=123) self._test_create_bridge_network(vlan_id=123)
def test_create_bridge_vlan_network_outofrange_returns_400(self): def test_create_bridge_vlan_network_outofrange_returns_400(self):
with testtools.ExpectedException( with testlib_api.ExpectedException(
webob.exc.HTTPClientError) as ctx_manager: webob.exc.HTTPClientError) as ctx_manager:
self._test_create_bridge_network(vlan_id=5000) 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): def test_list_networks_filter_by_id(self):
# We add this unit test to cover some logic specific to the # We add this unit test to cover some logic specific to the

View File

@@ -23,7 +23,6 @@ import random
import mock import mock
from oslo.config import cfg from oslo.config import cfg
import testtools
from testtools import matchers from testtools import matchers
import webob.exc import webob.exc
@@ -1806,14 +1805,14 @@ class TestNetworksV2(QuantumDbPluginV2TestCase):
def test_create_public_network_no_admin_tenant(self): def test_create_public_network_no_admin_tenant(self):
name = 'public_net' name = 'public_net'
with testtools.ExpectedException( with testlib_api.ExpectedException(
webob.exc.HTTPClientError) as ctx_manager: webob.exc.HTTPClientError) as ctx_manager:
with self.network(name=name, with self.network(name=name,
shared=True, shared=True,
tenant_id="another_tenant", tenant_id="another_tenant",
set_context=True): set_context=True):
pass pass
self.assertEqual(ctx_manager.exception.code, 403) self.assertEqual(ctx_manager.exception.code, 403)
def test_update_network(self): def test_update_network(self):
with self.network() as network: with self.network() as network:
@@ -2346,13 +2345,13 @@ class TestSubnetsV2(QuantumDbPluginV2TestCase):
with self.subnet(network=network, with self.subnet(network=network,
gateway_ip=gateway_ip_1, gateway_ip=gateway_ip_1,
cidr=cidr_1): cidr=cidr_1):
with testtools.ExpectedException( with testlib_api.ExpectedException(
webob.exc.HTTPClientError) as ctx_manager: webob.exc.HTTPClientError) as ctx_manager:
with self.subnet(network=network, with self.subnet(network=network,
gateway_ip=gateway_ip_2, gateway_ip=gateway_ip_2,
cidr=cidr_2): cidr=cidr_2):
pass pass
self.assertEqual(ctx_manager.exception.code, 400) self.assertEqual(ctx_manager.exception.code, 400)
def test_create_subnet_bad_V4_cidr(self): def test_create_subnet_bad_V4_cidr(self):
with self.network() as network: with self.network() as network:
@@ -2389,12 +2388,12 @@ class TestSubnetsV2(QuantumDbPluginV2TestCase):
cidr_1 = '10.0.0.0/23' cidr_1 = '10.0.0.0/23'
cidr_2 = '10.0.0.0/24' cidr_2 = '10.0.0.0/24'
cfg.CONF.set_override('allow_overlapping_ips', False) cfg.CONF.set_override('allow_overlapping_ips', False)
with testtools.ExpectedException( with testlib_api.ExpectedException(
webob.exc.HTTPClientError) as ctx_manager: webob.exc.HTTPClientError) as ctx_manager:
with contextlib.nested(self.subnet(cidr=cidr_1), with contextlib.nested(self.subnet(cidr=cidr_1),
self.subnet(cidr=cidr_2)): self.subnet(cidr=cidr_2)):
pass pass
self.assertEqual(ctx_manager.exception.code, 400) self.assertEqual(ctx_manager.exception.code, 400)
def test_create_subnets_bulk_native(self): def test_create_subnets_bulk_native(self):
if self._skip_native_bulk: if self._skip_native_bulk:
@@ -2796,23 +2795,23 @@ class TestSubnetsV2(QuantumDbPluginV2TestCase):
cidr = '10.0.0.0/24' cidr = '10.0.0.0/24'
allocation_pools = [{'start': '10.0.0.1', allocation_pools = [{'start': '10.0.0.1',
'end': '10.0.0.5'}] 'end': '10.0.0.5'}]
with testtools.ExpectedException( with testlib_api.ExpectedException(
webob.exc.HTTPClientError) as ctx_manager: webob.exc.HTTPClientError) as ctx_manager:
self._test_create_subnet(cidr=cidr, self._test_create_subnet(cidr=cidr,
allocation_pools=allocation_pools) 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): def test_create_subnet_gateway_in_allocation_pool_returns_409(self):
gateway_ip = '10.0.0.50' gateway_ip = '10.0.0.50'
cidr = '10.0.0.0/24' cidr = '10.0.0.0/24'
allocation_pools = [{'start': '10.0.0.1', allocation_pools = [{'start': '10.0.0.1',
'end': '10.0.0.100'}] 'end': '10.0.0.100'}]
with testtools.ExpectedException( with testlib_api.ExpectedException(
webob.exc.HTTPClientError) as ctx_manager: webob.exc.HTTPClientError) as ctx_manager:
self._test_create_subnet(gateway_ip=gateway_ip, self._test_create_subnet(gateway_ip=gateway_ip,
cidr=cidr, cidr=cidr,
allocation_pools=allocation_pools) 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): def test_create_subnet_overlapping_allocation_pools_returns_409(self):
gateway_ip = '10.0.0.1' gateway_ip = '10.0.0.1'
@@ -2821,44 +2820,44 @@ class TestSubnetsV2(QuantumDbPluginV2TestCase):
'end': '10.0.0.150'}, 'end': '10.0.0.150'},
{'start': '10.0.0.140', {'start': '10.0.0.140',
'end': '10.0.0.180'}] 'end': '10.0.0.180'}]
with testtools.ExpectedException( with testlib_api.ExpectedException(
webob.exc.HTTPClientError) as ctx_manager: webob.exc.HTTPClientError) as ctx_manager:
self._test_create_subnet(gateway_ip=gateway_ip, self._test_create_subnet(gateway_ip=gateway_ip,
cidr=cidr, cidr=cidr,
allocation_pools=allocation_pools) 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): def test_create_subnet_invalid_allocation_pool_returns_400(self):
gateway_ip = '10.0.0.1' gateway_ip = '10.0.0.1'
cidr = '10.0.0.0/24' cidr = '10.0.0.0/24'
allocation_pools = [{'start': '10.0.0.2', allocation_pools = [{'start': '10.0.0.2',
'end': '10.0.0.256'}] 'end': '10.0.0.256'}]
with testtools.ExpectedException( with testlib_api.ExpectedException(
webob.exc.HTTPClientError) as ctx_manager: webob.exc.HTTPClientError) as ctx_manager:
self._test_create_subnet(gateway_ip=gateway_ip, self._test_create_subnet(gateway_ip=gateway_ip,
cidr=cidr, cidr=cidr,
allocation_pools=allocation_pools) 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): def test_create_subnet_out_of_range_allocation_pool_returns_400(self):
gateway_ip = '10.0.0.1' gateway_ip = '10.0.0.1'
cidr = '10.0.0.0/24' cidr = '10.0.0.0/24'
allocation_pools = [{'start': '10.0.0.2', allocation_pools = [{'start': '10.0.0.2',
'end': '10.0.1.6'}] 'end': '10.0.1.6'}]
with testtools.ExpectedException( with testlib_api.ExpectedException(
webob.exc.HTTPClientError) as ctx_manager: webob.exc.HTTPClientError) as ctx_manager:
self._test_create_subnet(gateway_ip=gateway_ip, self._test_create_subnet(gateway_ip=gateway_ip,
cidr=cidr, cidr=cidr,
allocation_pools=allocation_pools) 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): def test_create_subnet_shared_returns_400(self):
cidr = '10.0.0.0/24' cidr = '10.0.0.0/24'
with testtools.ExpectedException( with testlib_api.ExpectedException(
webob.exc.HTTPClientError) as ctx_manager: webob.exc.HTTPClientError) as ctx_manager:
self._test_create_subnet(cidr=cidr, self._test_create_subnet(cidr=cidr,
shared=True) shared=True)
self.assertEqual(ctx_manager.exception.code, 400) self.assertEqual(ctx_manager.exception.code, 400)
def test_create_subnet_inconsistent_ipv6_cidrv4(self): def test_create_subnet_inconsistent_ipv6_cidrv4(self):
with self.network() as network: with self.network() as network:

View File

@@ -24,7 +24,6 @@ import itertools
import mock import mock
from oslo.config import cfg from oslo.config import cfg
import testtools
from webob import exc from webob import exc
import webtest import webtest
@@ -1484,13 +1483,13 @@ class L3NatDBTestCase(L3NatTestCaseBase):
def test_create_port_external_network_non_admin_fails(self): def test_create_port_external_network_non_admin_fails(self):
with self.network(router__external=True) as ext_net: with self.network(router__external=True) as ext_net:
with self.subnet(network=ext_net) as ext_subnet: with self.subnet(network=ext_net) as ext_subnet:
with testtools.ExpectedException( with testlib_api.ExpectedException(
exc.HTTPClientError) as ctx_manager: exc.HTTPClientError) as ctx_manager:
with self.port(subnet=ext_subnet, with self.port(subnet=ext_subnet,
set_context='True', set_context='True',
tenant_id='noadmin'): tenant_id='noadmin'):
pass pass
self.assertEqual(ctx_manager.exception.code, 403) self.assertEqual(ctx_manager.exception.code, 403)
def test_create_port_external_network_admin_suceeds(self): def test_create_port_external_network_admin_suceeds(self):
with self.network(router__external=True) as ext_net: with self.network(router__external=True) as ext_net:
@@ -1500,12 +1499,12 @@ class L3NatDBTestCase(L3NatTestCaseBase):
ext_net['network']['id']) ext_net['network']['id'])
def test_create_external_network_non_admin_fails(self): 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, with self.network(router__external=True,
set_context='True', set_context='True',
tenant_id='noadmin'): tenant_id='noadmin'):
pass pass
self.assertEqual(ctx_manager.exception.code, 403) self.assertEqual(ctx_manager.exception.code, 403)
def test_create_external_network_admin_suceeds(self): def test_create_external_network_admin_suceeds(self):
with self.network(router__external=True) as ext_net: with self.network(router__external=True) as ext_net:

View File

@@ -15,7 +15,6 @@
# under the License. # under the License.
from oslo.config import cfg from oslo.config import cfg
import testtools
import webob.exc as webexc import webob.exc as webexc
import quantum import quantum
@@ -293,11 +292,11 @@ class RouterServiceInsertionTestCase(base.BaseTestCase):
} }
if update_service_type_id: if update_service_type_id:
data["router"]["service_type_id"] = _uuid() data["router"]["service_type_id"] = _uuid()
with testtools.ExpectedException( with testlib_api.ExpectedException(
webexc.HTTPClientError) as ctx_manager: webexc.HTTPClientError) as ctx_manager:
res = self._do_request( res = self._do_request(
'PUT', _get_path('routers/{0}'.format(router_id)), data) 'PUT', _get_path('routers/{0}'.format(router_id)), data)
self.assertEqual(ctx_manager.exception.code, 400) self.assertEqual(ctx_manager.exception.code, 400)
else: else:
res = self._do_request( res = self._do_request(
'PUT', _get_path('routers/{0}'.format(router_id)), data) 'PUT', _get_path('routers/{0}'.format(router_id)), data)
@@ -422,12 +421,12 @@ class RouterServiceInsertionTestCase(base.BaseTestCase):
data = {res: uattrs} data = {res: uattrs}
if update_router_id: if update_router_id:
uattrs['router_id'] = self._router_id uattrs['router_id'] = self._router_id
with testtools.ExpectedException( with testlib_api.ExpectedException(
webexc.HTTPClientError) as ctx_manager: webexc.HTTPClientError) as ctx_manager:
self._do_request( self._do_request(
'PUT', 'PUT',
_get_path('lb/{0}s/{1}'.format(res, obj['id'])), data) _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: else:
self._do_request( self._do_request(
'PUT', 'PUT',

View File

@@ -13,11 +13,26 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
import testtools
from quantum.api.v2 import attributes from quantum.api.v2 import attributes
from quantum.tests import base from quantum.tests import base
from quantum import wsgi 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', def create_request(path, body, content_type, method='GET',
query_string=None, context=None): query_string=None, context=None):
if query_string: if query_string: