Skip to content

Commit 437cadc

Browse files
Zuulopenstack-gerrit
authored andcommitted
Merge "Add logging details of the http response in the unit tests" into stable/2023.1
2 parents ad285c4 + 04b56f4 commit 437cadc

File tree

9 files changed

+32
-52
lines changed

9 files changed

+32
-52
lines changed

neutron/tests/unit/db/test_db_base_plugin_v2.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,14 @@ def _req(self, method, resource, data=None, fmt=None, id=None, params=None,
246246
query_string=params, context=context,
247247
headers=headers)
248248

249+
def _check_http_response(self, res):
250+
# Things can go wrong - raise HTTP exc with res code only
251+
# so it can be caught by unit tests
252+
if res.status_int >= webob.exc.HTTPClientError.code:
253+
res.charset = 'utf8'
254+
raise webob.exc.HTTPClientError(explanation=str(res),
255+
code=res.status_int)
256+
249257
def new_create_request(self, resource, data, fmt=None, id=None,
250258
subresource=None, context=None):
251259
return self._req('POST', resource, data, fmt, id=id,
@@ -523,10 +531,7 @@ def _make_network(self, fmt, name, admin_state_up, **kwargs):
523531
res = self._create_network(fmt, name, admin_state_up, **kwargs)
524532
# TODO(salvatore-orlando): do exception handling in this test module
525533
# in a uniform way (we do it differently for ports, subnets, and nets
526-
# Things can go wrong - raise HTTP exc with res code only
527-
# so it can be caught by unit tests
528-
if res.status_int >= webob.exc.HTTPClientError.code:
529-
raise webob.exc.HTTPClientError(code=res.status_int)
534+
self._check_http_response(res)
530535
return self.deserialize(fmt, res)
531536

532537
def _make_subnet(self, fmt, network, gateway, cidr, subnetpool_id=None,
@@ -551,10 +556,7 @@ def _make_subnet(self, fmt, network, gateway, cidr, subnetpool_id=None,
551556
ipv6_ra_mode=ipv6_ra_mode,
552557
ipv6_address_mode=ipv6_address_mode,
553558
set_context=set_context)
554-
# Things can go wrong - raise HTTP exc with res code only
555-
# so it can be caught by unit tests
556-
if res.status_int >= webob.exc.HTTPClientError.code:
557-
raise webob.exc.HTTPClientError(code=res.status_int)
559+
self._check_http_response(res)
558560
return self.deserialize(fmt, res)
559561

560562
def _make_v6_subnet(self, network, ra_addr_mode, ipv6_pd=False):
@@ -580,16 +582,14 @@ def _make_subnetpool(self, fmt, prefixes, admin=False, **kwargs):
580582
**kwargs)
581583
# Things can go wrong - raise HTTP exc with res code only
582584
# so it can be caught by unit tests
583-
if res.status_int >= webob.exc.HTTPClientError.code:
584-
raise webob.exc.HTTPClientError(code=res.status_int)
585+
self._check_http_response(res)
585586
return self.deserialize(fmt, res)
586587

587588
def _make_port(self, fmt, net_id, expected_res_status=None, **kwargs):
588589
res = self._create_port(fmt, net_id, expected_res_status, **kwargs)
589590
# Things can go wrong - raise HTTP exc with res code only
590591
# so it can be caught by unit tests
591-
if res.status_int >= webob.exc.HTTPClientError.code:
592-
raise webob.exc.HTTPClientError(code=res.status_int)
592+
self._check_http_response(res)
593593
return self.deserialize(fmt, res)
594594

595595
def _create_qos_rule(self, fmt, qos_policy_id, rule_type, max_kbps=None,

neutron/tests/unit/db/test_l3_db.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
from neutron_lib.plugins import utils as plugin_utils
3333
from oslo_utils import uuidutils
3434
import testtools
35-
import webob.exc
3635

3736
from neutron.db import extraroute_db
3837
from neutron.db import l3_db
@@ -951,8 +950,7 @@ def _create_external_network(self, name=None, **kwargs):
951950
res = self._create_network(
952951
self.fmt, name, True,
953952
arg_list=(extnet_apidef.EXTERNAL,), **kwargs)
954-
if res.status_int >= webob.exc.HTTPClientError.code:
955-
raise webob.exc.HTTPClientError(code=res.status_int)
953+
self._check_http_response(res)
956954
return self.deserialize(self.fmt, res)
957955

958956
def test_update_router_gw_notify(self):

neutron/tests/unit/extensions/test_address_group.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,7 @@ def _create_address_group(self, **kwargs):
5252
self._tenant_id))
5353
req.environ['neutron.context'] = neutron_context
5454
res = req.get_response(self.ext_api)
55-
if res.status_int >= webob.exc.HTTPClientError.code:
56-
raise webob.exc.HTTPClientError(code=res.status_int)
55+
self._check_http_response(res)
5756
return res
5857

5958
def _test_create_address_group(self, expected=None, **kwargs):

neutron/tests/unit/extensions/test_address_scope.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,7 @@ def _create_address_scope(self, fmt, ip_version=constants.IP_VERSION_4,
7171
def _make_address_scope(self, fmt, ip_version, admin=False, **kwargs):
7272
res = self._create_address_scope(fmt, ip_version,
7373
admin=admin, **kwargs)
74-
if res.status_int >= webob.exc.HTTPClientError.code:
75-
raise webob.exc.HTTPClientError(code=res.status_int)
74+
self._check_http_response(res)
7675
return self.deserialize(fmt, res)
7776

7877
@contextlib.contextmanager

neutron/tests/unit/extensions/test_local_ip.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,7 @@ def _create_local_ip(self, **kwargs):
5151
'', kwargs.get('project_id', self._tenant_id), is_admin=True)
5252
req.environ['neutron.context'] = neutron_context
5353
res = req.get_response(self.ext_api)
54-
if res.status_int >= webob.exc.HTTPClientError.code:
55-
raise webob.exc.HTTPClientError(code=res.status_int)
54+
self._check_http_response(res)
5655
return self.deserialize(self.fmt, res)
5756

5857
def _update_local_ip(self, lip_id, data):
@@ -61,8 +60,7 @@ def _update_local_ip(self, lip_id, data):
6160
update_req.environ['neutron.context'] = context.Context(
6261
'', self._tenant_id)
6362
res = update_req.get_response(self.ext_api)
64-
if res.status_int >= webob.exc.HTTPClientError.code:
65-
raise webob.exc.HTTPClientError(code=res.status_int)
63+
self._check_http_response(res)
6664
return self.deserialize(self.fmt, res)
6765

6866
def _create_local_ip_association(self, local_ip_id, fixed_port_id,
@@ -77,8 +75,7 @@ def _create_local_ip_association(self, local_ip_id, fixed_port_id,
7775
neutron_context = context.Context('', self._tenant_id)
7876
req.environ['neutron.context'] = neutron_context
7977
res = req.get_response(self.ext_api)
80-
if res.status_int >= webob.exc.HTTPClientError.code:
81-
raise webob.exc.HTTPClientError(code=res.status_int)
78+
self._check_http_response(res)
8279
return self.deserialize(self.fmt, res)
8380

8481
@contextlib.contextmanager

neutron/tests/unit/extensions/test_network_segment_range.py

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,7 @@ def _create_network_segment_range(self, fmt, expected_res_status=None,
6767

6868
def network_segment_range(self, **kwargs):
6969
res = self._create_network_segment_range(self.fmt, **kwargs)
70-
if res.status_int >= webob.exc.HTTPClientError.code:
71-
raise webob.exc.HTTPClientError(code=res.status_int)
70+
self._check_http_response(res)
7271
return self.deserialize(self.fmt, res)
7372

7473
def _test_create_network_segment_range(self, expected=None, **kwargs):
@@ -162,7 +161,7 @@ def test_create_network_segment_range_unsupported_network_type(self):
162161
self._test_create_network_segment_range,
163162
network_type='foo-network-type')
164163
self.assertEqual(webob.exc.HTTPClientError.code, exc.code)
165-
self.assertIn('The server could not comply with the request',
164+
self.assertIn('foo-network-type is not in valid_values',
166165
exc.explanation)
167166

168167
def test_create_network_segment_range_no_physical_network(self):
@@ -212,16 +211,14 @@ def test_create_network_segment_range_failed_with_vlan_minimum_id(self):
212211
self._test_create_network_segment_range,
213212
minimum=0)
214213
self.assertEqual(webob.exc.HTTPClientError.code, exc.code)
215-
self.assertIn('The server could not comply with the request',
216-
exc.explanation)
214+
self.assertIn('Invalid input for minimum', exc.explanation)
217215

218216
def test_create_network_segment_range_failed_with_vlan_maximum_id(self):
219217
exc = self.assertRaises(webob.exc.HTTPClientError,
220218
self._test_create_network_segment_range,
221219
minimum=4095)
222220
self.assertEqual(webob.exc.HTTPServerError.code, exc.code)
223-
self.assertIn('The server could not comply with the request',
224-
exc.explanation)
221+
self.assertIn('Invalid network VLAN range', exc.explanation)
225222

226223
def test_create_network_segment_range_failed_with_tunnel_minimum_id(self):
227224
tunnel_type = [constants.TYPE_VXLAN,
@@ -234,8 +231,7 @@ def test_create_network_segment_range_failed_with_tunnel_minimum_id(self):
234231
physical_network=None,
235232
minimum=0)
236233
self.assertEqual(webob.exc.HTTPClientError.code, exc.code)
237-
self.assertIn('The server could not comply with the request',
238-
exc.explanation)
234+
self.assertIn('Invalid input for minimum', exc.explanation)
239235

240236
def test_create_network_segment_range_failed_with_tunnel_maximum_id(self):
241237
expected_res = [(constants.TYPE_VXLAN, 2 ** 24),
@@ -249,10 +245,10 @@ def test_create_network_segment_range_failed_with_tunnel_maximum_id(self):
249245
maximum=max_id)
250246
if network_type == constants.TYPE_GRE:
251247
self.assertEqual(webob.exc.HTTPClientError.code, exc.code)
248+
self.assertIn('Invalid input for maximum', exc.explanation)
252249
else:
253250
self.assertEqual(webob.exc.HTTPServerError.code, exc.code)
254-
self.assertIn('The server could not comply with the request',
255-
exc.explanation)
251+
self.assertIn('Invalid network tunnel range', exc.explanation)
256252

257253
def test_update_network_segment_range_set_name(self):
258254
network_segment_range = self._test_create_network_segment_range()

neutron/tests/unit/extensions/test_segment.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,7 @@ def _create_segment(self, fmt, expected_res_status=None, **kwargs):
121121

122122
def _make_segment(self, fmt, **kwargs):
123123
res = self._create_segment(fmt, **kwargs)
124-
if res.status_int >= webob.exc.HTTPClientError.code:
125-
res.charset = 'utf8'
126-
raise webob.exc.HTTPClientError(
127-
code=res.status_int, explanation=str(res))
124+
self._check_http_response(res)
128125
return self.deserialize(fmt, res)
129126

130127
def segment(self, **kwargs):

neutron/tests/unit/plugins/ml2/test_port_binding.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -374,8 +374,7 @@ def _create_port_binding(self, fmt, port_id, host, tenant_id=None,
374374

375375
def _make_port_binding(self, fmt, port_id, host, **kwargs):
376376
res = self._create_port_binding(fmt, port_id, host, **kwargs)
377-
if res.status_int >= webob.exc.HTTPClientError.code:
378-
raise webob.exc.HTTPClientError(code=res.status_int)
377+
self._check_http_response(res)
379378
return self.deserialize(fmt, res)
380379

381380
def _update_port_binding(self, fmt, port_id, host, **kwargs):
@@ -387,8 +386,7 @@ def _update_port_binding(self, fmt, port_id, host, **kwargs):
387386

388387
def _do_update_port_binding(self, fmt, port_id, host, **kwargs):
389388
res = self._update_port_binding(fmt, port_id, host, **kwargs)
390-
if res.status_int >= webob.exc.HTTPClientError.code:
391-
raise webob.exc.HTTPClientError(code=res.status_int)
389+
self._check_http_response(res)
392390
return self.deserialize(fmt, res)
393391

394392
def _activate_port_binding(self, port_id, host, raw_response=True):
@@ -401,8 +399,7 @@ def _activate_port_binding(self, port_id, host, raw_response=True):
401399
def _check_code_and_serialize(self, response, raw_response):
402400
if raw_response:
403401
return response
404-
if response.status_int >= webob.exc.HTTPClientError.code:
405-
raise webob.exc.HTTPClientError(code=response.status_int)
402+
self._check_http_response(response)
406403
return self.deserialize(self.fmt, response)
407404

408405
def _list_port_bindings(self, port_id, params=None, raw_response=True):

neutron/tests/unit/services/qos/test_qos_plugin.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1929,26 +1929,23 @@ def _update_rule(self, rule_type, rule_id, **kwargs):
19291929
rule_type.replace('_', '-'))
19301930
request = self.new_update_request(resource, data, rule_id, self.fmt)
19311931
res = request.get_response(self.ext_api)
1932-
if res.status_int >= webob.exc.HTTPClientError.code:
1933-
raise webob.exc.HTTPClientError(code=res.status_int)
1932+
self._check_http_response(res)
19341933
return self.deserialize(self.fmt, res)
19351934

19361935
def _show_rule(self, rule_type, rule_id):
19371936
resource = '%s/alias-%s-rules' % (qos.ALIAS,
19381937
rule_type.replace('_', '-'))
19391938
request = self.new_show_request(resource, rule_id, self.fmt)
19401939
res = request.get_response(self.ext_api)
1941-
if res.status_int >= webob.exc.HTTPClientError.code:
1942-
raise webob.exc.HTTPClientError(code=res.status_int)
1940+
self._check_http_response(res)
19431941
return self.deserialize(self.fmt, res)
19441942

19451943
def _delete_rule(self, rule_type, rule_id):
19461944
resource = '%s/alias-%s-rules' % (qos.ALIAS,
19471945
rule_type.replace('_', '-'))
19481946
request = self.new_delete_request(resource, rule_id, self.fmt)
19491947
res = request.get_response(self.ext_api)
1950-
if res.status_int >= webob.exc.HTTPClientError.code:
1951-
raise webob.exc.HTTPClientError(code=res.status_int)
1948+
self._check_http_response(res)
19521949

19531950
@mock.patch.object(qos_plugin.QoSPlugin, "update_policy_rule")
19541951
def test_update_rule(self, update_policy_rule_mock):

0 commit comments

Comments
 (0)