From 5a1934da8e2a58943cdad337b53aab8a5fcbc7ea Mon Sep 17 00:00:00 2001 From: Isaku Yamahata Date: Tue, 10 Apr 2018 11:22:47 -0700 Subject: [PATCH] unit test: unbreak test cases for callbacks.subscribe patch set of [1] added callback priority so that a new argument, priority, was added to subscribe method. Thus its signature was changed. It caused to break some unit tests.[2] Those test cases check arguments passed to subscribe method. The fix to those test cases needs to be compatible with older version of neutron-lib. So add a helper method, get_subscribe_args, to produce argument to subscribe by checking if neutron-lib supports callback priority or not. and apply it to broken test cases. [1] https://review.openstack.org/#/c/541766/ [2] http://logs.openstack.org/periodic/git.openstack.org/openstack/neutron/master/openstack-tox-py35-with-neutron-lib-master/66d328a/testr_results.html.gz test_capabilities.CapabilitiesTest.test_register test_ovs_capabilities.CapabilitiesTest.test_register test_backend.ServerSideRpcBackendTest.test___init__ Change-Id: I94066ff8a8dae5c5637478aa52883b3b451f2857 --- neutron/tests/tools.py | 21 ++++++++++++ .../ml2/drivers/agent/test_capabilities.py | 7 ++-- .../agent/test_ovs_capabilities.py | 7 ++-- .../unit/services/trunk/rpc/test_backend.py | 33 ++++++++++++------- 4 files changed, 50 insertions(+), 18 deletions(-) diff --git a/neutron/tests/tools.py b/neutron/tests/tools.py index df54634a2ee..82b2d493032 100644 --- a/neutron/tests/tools.py +++ b/neutron/tests/tools.py @@ -34,6 +34,17 @@ from neutron.common import constants as n_const from neutron.services.logapi.common import constants as log_const +# NOTE(yamahata): from neutron-lib 1.9.1, callback priority was added and +# priority_group module was added for constants of priority. +# test the existence of the module of priority_group to check if +# callback priority is supported or not. +CALLBACK_PRIORITY_SUPPORTED = True +try: + from neutron_lib.callbacks import priority_group # noqa +except ImportError as e: + CALLBACK_PRIORITY_SUPPORTED = False + + class WarningsFixture(fixtures.Fixture): """Filters out warnings during test runs.""" @@ -135,6 +146,16 @@ def verify_mock_calls(mocked_call, expected_calls_and_values, mocked_call.assert_has_calls(expected_calls, any_order=any_order) +def get_subscribe_args(*args): + # NOTE(yamahata): from neutron-lib 1.9.1, callback priority was added. + # old signature: (callback, resource, event) + # new signature: (callback, resource, event, priority=PRIORITY_DEFAULT) + if len(args) == 3 and CALLBACK_PRIORITY_SUPPORTED: + args = list(args) # don't modify original list + args.append(priority_group.PRIORITY_DEFAULT) + return args + + def fail(msg=None): """Fail immediately, with the given message. diff --git a/neutron/tests/unit/plugins/ml2/drivers/agent/test_capabilities.py b/neutron/tests/unit/plugins/ml2/drivers/agent/test_capabilities.py index 79f56de8124..90b932af441 100644 --- a/neutron/tests/unit/plugins/ml2/drivers/agent/test_capabilities.py +++ b/neutron/tests/unit/plugins/ml2/drivers/agent/test_capabilities.py @@ -17,6 +17,7 @@ from neutron_lib import fixture from neutron.plugins.ml2.drivers.agent import capabilities from neutron.tests import base +from neutron.tests import tools class CapabilitiesTest(base.BaseTestCase): @@ -40,6 +41,6 @@ class CapabilitiesTest(base.BaseTestCase): mock_callback = mock.Mock() mock_agent_type = mock.Mock() capabilities.register(mock_callback, mock_agent_type) - self._mgr.subscribe.assert_called_with(mock_callback, - mock_agent_type, - events.AFTER_INIT) + args = tools.get_subscribe_args( + mock_callback, mock_agent_type, events.AFTER_INIT) + self._mgr.subscribe.assert_called_with(*args) diff --git a/neutron/tests/unit/plugins/ml2/drivers/openvswitch/agent/test_ovs_capabilities.py b/neutron/tests/unit/plugins/ml2/drivers/openvswitch/agent/test_ovs_capabilities.py index 6b20c86f4e2..75947922311 100644 --- a/neutron/tests/unit/plugins/ml2/drivers/openvswitch/agent/test_ovs_capabilities.py +++ b/neutron/tests/unit/plugins/ml2/drivers/openvswitch/agent/test_ovs_capabilities.py @@ -19,6 +19,7 @@ from neutron_lib import fixture from neutron.plugins.ml2.drivers.openvswitch.agent import ovs_capabilities from neutron.services.trunk.drivers.openvswitch.agent import driver from neutron.tests import base +from neutron.tests import tools from neutron_lib import constants @@ -32,6 +33,6 @@ class CapabilitiesTest(base.BaseTestCase): def test_register(self): ovs_capabilities.register() - self._mgr.subscribe.assert_called_with(driver.init_handler, - constants.AGENT_TYPE_OVS, - events.AFTER_INIT) + args = tools.get_subscribe_args( + driver.init_handler, constants.AGENT_TYPE_OVS, events.AFTER_INIT) + self._mgr.subscribe.assert_called_with(*args) diff --git a/neutron/tests/unit/services/trunk/rpc/test_backend.py b/neutron/tests/unit/services/trunk/rpc/test_backend.py index 00ec02ed847..ea1ba32c45c 100644 --- a/neutron/tests/unit/services/trunk/rpc/test_backend.py +++ b/neutron/tests/unit/services/trunk/rpc/test_backend.py @@ -20,6 +20,7 @@ from neutron.services.trunk import callbacks from neutron.services.trunk import constants as trunk_consts from neutron.services.trunk.rpc import backend from neutron.tests import base +from neutron.tests import tools class ServerSideRpcBackendTest(base.BaseTestCase): @@ -35,18 +36,26 @@ class ServerSideRpcBackendTest(base.BaseTestCase): def test___init__(self,): test_obj = backend.ServerSideRpcBackend() - calls = [mock.call(test_obj.process_event, - trunk_consts.TRUNK, - events.AFTER_CREATE), - mock.call(test_obj.process_event, - trunk_consts.TRUNK, - events.AFTER_DELETE), - mock.call(test_obj.process_event, - trunk_consts.SUBPORTS, - events.AFTER_CREATE), - mock.call(test_obj.process_event, - trunk_consts.SUBPORTS, - events.AFTER_DELETE) + calls = [mock.call( + *tools.get_subscribe_args( + test_obj.process_event, + trunk_consts.TRUNK, + events.AFTER_CREATE)), + mock.call( + *tools.get_subscribe_args( + test_obj.process_event, + trunk_consts.TRUNK, + events.AFTER_DELETE)), + mock.call( + *tools.get_subscribe_args( + test_obj.process_event, + trunk_consts.SUBPORTS, + events.AFTER_CREATE)), + mock.call( + *tools.get_subscribe_args( + test_obj.process_event, + trunk_consts.SUBPORTS, + events.AFTER_DELETE)) ] self._mgr.subscribe.assert_has_calls(calls, any_order=True)