[UT] Fix ``APIv2TestBase`` derived test classes

The method test calling ``_post_request`` with create or update
operations, were not properly patching the ML2 plugin
``create_network`` and `update_network`` methods. With
Python3.12, the mock object passed (instead of a dictionary with a
reduced network definition) don't have a ``__json__`` method and
fails during the encoding.

NOTE: this has been manually tested with Python3.12. Currently this
patch cannot be tested in the CI.

Closes-Bug: #2051928
Change-Id: Ie30af0c655d2f27d9039ff7933e81603047da6f4
This commit is contained in:
Rodolfo Alonso Hernandez 2024-02-07 08:15:03 +00:00
parent a097f363d1
commit 4df5f2f41b
2 changed files with 12 additions and 2 deletions

View File

@ -1208,11 +1208,13 @@ class NotificationTest(APIv2TestBase):
instance.get_networks_count.return_value = 0
expected_code = exc.HTTPCreated.code
if opname == 'create':
instance.create_network.return_value = network_obj
res = self._post_request(
_get_path('networks'),
initial_input, expect_errors=expected_errors,
req_tenant_id=tenant_id)
if opname == 'update':
instance.update_network.return_value = network_obj
op_input = {resource: {'name': 'myname'}}
res = self._put_request(
_get_path('networks', id=tenant_id),
@ -1266,10 +1268,12 @@ class RegistryNotificationTest(APIv2TestBase):
expected_code = exc.HTTPCreated.code
with mock.patch.object(registry, 'publish') as publish:
if opname == 'create':
instance.create_network.return_value = initial_input
res = self.api.post_json(
_get_path('networks'),
initial_input)
if opname == 'update':
instance.update_network.return_value = initial_input
res = self.api.put_json(
_get_path('networks', id=_uuid()),
initial_input)
@ -1305,6 +1309,8 @@ class QuotaTest(APIv2TestBase):
def test_create_network_quota_exceeded(self):
initial_input = {'network': {'name': 'net1', 'tenant_id': _uuid()}}
instance = self.plugin.return_value
instance.create_network.return_value = initial_input
with mock.patch.object(quota.QUOTAS, 'make_reservation',
side_effect=n_exc.OverQuota(overs='network')):
res = self.api.post_json(
@ -1314,6 +1320,8 @@ class QuotaTest(APIv2TestBase):
def test_create_network_quota_without_limit(self):
initial_input = {'network': {'name': 'net1', 'tenant_id': _uuid()}}
instance = self.plugin.return_value
instance.create_network.return_value = initial_input
with mock.patch.object(quota.QUOTAS, 'make_reservation'), \
mock.patch.object(quota.QUOTAS, 'commit_reservation'):
res = self.api.post_json(

View File

@ -141,8 +141,9 @@ class ProvidernetExtensionTestCase(testlib_api.WebTestCase):
ctx = context.get_admin_context()
tenant_id = 'an_admin'
ctx.tenant_id = tenant_id
res, data = self._post_network_with_provider_attrs(ctx)
instance = self.plugin.return_value
instance.create_network.return_value = {}
res, data = self._post_network_with_provider_attrs(ctx)
exp_input = {'network': data}
exp_input['network'].update({'admin_state_up': True,
'tenant_id': tenant_id,
@ -163,8 +164,9 @@ class ProvidernetExtensionTestCase(testlib_api.WebTestCase):
def test_network_update_with_provider_attrs(self):
ctx = context.get_admin_context()
ctx.tenant_id = 'an_admin'
res, data, net_id = self._put_network_with_provider_attrs(ctx)
instance = self.plugin.return_value
instance.update_network.return_value = {}
res, data, net_id = self._put_network_with_provider_attrs(ctx)
exp_input = {'network': data}
instance.update_network.assert_called_with(mock.ANY,
net_id,