Ensure unit tests do not let looping calls roam freely
Bug 1220871 This patch does minimal changes in neutron.plugins.nicira.common.sync providing unit tests with a reference to the looping call object, so that they can control its lifecycle. Also, it perform a bit of refactoring in test_l3_agent.py in the way mocks are created and started. Change-Id: I73a1eb8ecdb7c6d46ff12afba549dd27095b7202
This commit is contained in:
parent
b60ff6b2a5
commit
a07927dbe8
@ -1,5 +1,3 @@
|
|||||||
# vim: tabstop=4 shiftwidth=4 softtabstop=4
|
|
||||||
|
|
||||||
# Copyright 2013 Nicira, Inc.
|
# Copyright 2013 Nicira, Inc.
|
||||||
# All Rights Reserved
|
# All Rights Reserved
|
||||||
#
|
#
|
||||||
@ -182,6 +180,7 @@ def _start_loopingcall(min_chunk_size, state_sync_interval, func):
|
|||||||
func, sp=SyncParameters(min_chunk_size))
|
func, sp=SyncParameters(min_chunk_size))
|
||||||
state_synchronizer.start(
|
state_synchronizer.start(
|
||||||
periodic_interval_max=state_sync_interval)
|
periodic_interval_max=state_sync_interval)
|
||||||
|
return state_synchronizer
|
||||||
|
|
||||||
|
|
||||||
class NvpSynchronizer():
|
class NvpSynchronizer():
|
||||||
@ -219,8 +218,10 @@ class NvpSynchronizer():
|
|||||||
raise nvp_exc.NvpPluginException(err_msg=err_msg)
|
raise nvp_exc.NvpPluginException(err_msg=err_msg)
|
||||||
# Backoff time in case of failures while fetching sync data
|
# Backoff time in case of failures while fetching sync data
|
||||||
self._sync_backoff = 1
|
self._sync_backoff = 1
|
||||||
_start_loopingcall(min_chunk_size, state_sync_interval,
|
# Store the looping call in an instance variable to allow unit tests
|
||||||
self._synchronize_state)
|
# for controlling its lifecycle
|
||||||
|
self._sync_looping_call = _start_loopingcall(
|
||||||
|
min_chunk_size, state_sync_interval, self._synchronize_state)
|
||||||
|
|
||||||
def _get_tag_dict(self, tags):
|
def _get_tag_dict(self, tags):
|
||||||
return dict((tag.get('scope'), tag['tag']) for tag in tags)
|
return dict((tag.get('scope'), tag['tag']) for tag in tags)
|
||||||
|
@ -248,6 +248,8 @@ class SyncLoopingCallTestCase(base.BaseTestCase):
|
|||||||
synchronizer = sync.NvpSynchronizer(None, None,
|
synchronizer = sync.NvpSynchronizer(None, None,
|
||||||
100, 0, 0)
|
100, 0, 0)
|
||||||
time.sleep(0.04999)
|
time.sleep(0.04999)
|
||||||
|
# stop looping call before asserting
|
||||||
|
synchronizer._sync_looping_call.stop()
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
5, synchronizer._synchronize_state.call_count)
|
5, synchronizer._synchronize_state.call_count)
|
||||||
|
|
||||||
|
@ -63,6 +63,10 @@ class TestMeteringOperations(base.BaseTestCase):
|
|||||||
self.driver_patch = mock.patch(self.noop_driver, autospec=True)
|
self.driver_patch = mock.patch(self.noop_driver, autospec=True)
|
||||||
self.driver_patch.start()
|
self.driver_patch.start()
|
||||||
|
|
||||||
|
loopingcall_patch = mock.patch(
|
||||||
|
'neutron.openstack.common.loopingcall.FixedIntervalLoopingCall')
|
||||||
|
loopingcall_patch.start()
|
||||||
|
|
||||||
self.agent = metering_agent.MeteringAgent('my agent', cfg.CONF)
|
self.agent = metering_agent.MeteringAgent('my agent', cfg.CONF)
|
||||||
self.driver = self.agent.metering_driver
|
self.driver = self.agent.metering_driver
|
||||||
|
|
||||||
|
@ -75,6 +75,10 @@ class TestVPNAgent(base.BaseTestCase):
|
|||||||
self.plugin_api = mock.Mock()
|
self.plugin_api = mock.Mock()
|
||||||
l3pluginApi_cls.return_value = self.plugin_api
|
l3pluginApi_cls.return_value = self.plugin_api
|
||||||
|
|
||||||
|
looping_call_p = mock.patch(
|
||||||
|
'neutron.openstack.common.loopingcall.FixedIntervalLoopingCall')
|
||||||
|
looping_call_p.start()
|
||||||
|
|
||||||
self.fake_host = 'fake_host'
|
self.fake_host = 'fake_host'
|
||||||
self.agent = agent.VPNAgent(self.fake_host)
|
self.agent = agent.VPNAgent(self.fake_host)
|
||||||
|
|
||||||
|
@ -646,41 +646,35 @@ class TestL3AgentEventHandler(base.BaseTestCase):
|
|||||||
cfg.CONF.set_override('use_namespaces', True)
|
cfg.CONF.set_override('use_namespaces', True)
|
||||||
agent_config.register_root_helper(cfg.CONF)
|
agent_config.register_root_helper(cfg.CONF)
|
||||||
|
|
||||||
self.device_exists_p = mock.patch(
|
device_exists_p = mock.patch(
|
||||||
'neutron.agent.linux.ip_lib.device_exists')
|
'neutron.agent.linux.ip_lib.device_exists')
|
||||||
self.device_exists = self.device_exists_p.start()
|
device_exists_p.start()
|
||||||
|
|
||||||
self.utils_exec_p = mock.patch(
|
utils_exec_p = mock.patch(
|
||||||
'neutron.agent.linux.utils.execute')
|
'neutron.agent.linux.utils.execute')
|
||||||
self.utils_exec = self.utils_exec_p.start()
|
utils_exec_p.start()
|
||||||
|
|
||||||
self.drv_cls_p = mock.patch('neutron.agent.linux.interface.NullDriver')
|
drv_cls_p = mock.patch('neutron.agent.linux.interface.NullDriver')
|
||||||
driver_cls = self.drv_cls_p.start()
|
driver_cls = drv_cls_p.start()
|
||||||
self.mock_driver = mock.MagicMock()
|
mock_driver = mock.MagicMock()
|
||||||
self.mock_driver.DEV_NAME_LEN = (
|
mock_driver.DEV_NAME_LEN = (
|
||||||
interface.LinuxInterfaceDriver.DEV_NAME_LEN)
|
interface.LinuxInterfaceDriver.DEV_NAME_LEN)
|
||||||
driver_cls.return_value = self.mock_driver
|
driver_cls.return_value = mock_driver
|
||||||
|
|
||||||
self.l3_plugin_p = mock.patch(
|
l3_plugin_p = mock.patch(
|
||||||
'neutron.agent.l3_agent.L3PluginApi')
|
'neutron.agent.l3_agent.L3PluginApi')
|
||||||
l3_plugin_cls = self.l3_plugin_p.start()
|
l3_plugin_cls = l3_plugin_p.start()
|
||||||
self.plugin_api = mock.Mock()
|
l3_plugin_cls.return_value = mock.Mock()
|
||||||
l3_plugin_cls.return_value = self.plugin_api
|
|
||||||
|
|
||||||
self.external_process_p = mock.patch(
|
self.external_process_p = mock.patch(
|
||||||
'neutron.agent.linux.external_process.ProcessManager'
|
'neutron.agent.linux.external_process.ProcessManager'
|
||||||
)
|
)
|
||||||
self.external_process = self.external_process_p.start()
|
self.external_process_p.start()
|
||||||
|
looping_call_p = mock.patch(
|
||||||
|
'neutron.openstack.common.loopingcall.FixedIntervalLoopingCall')
|
||||||
|
looping_call_p.start()
|
||||||
self.agent = l3_agent.L3NATAgent(HOSTNAME)
|
self.agent = l3_agent.L3NATAgent(HOSTNAME)
|
||||||
|
self.addCleanup(mock.patch.stopall)
|
||||||
def tearDown(self):
|
|
||||||
self.device_exists_p.stop()
|
|
||||||
self.utils_exec_p.stop()
|
|
||||||
self.drv_cls_p.stop()
|
|
||||||
self.l3_plugin_p.stop()
|
|
||||||
self.external_process_p.stop()
|
|
||||||
super(TestL3AgentEventHandler, self).tearDown()
|
|
||||||
|
|
||||||
def test_spawn_metadata_proxy(self):
|
def test_spawn_metadata_proxy(self):
|
||||||
router_id = _uuid()
|
router_id = _uuid()
|
||||||
|
Loading…
Reference in New Issue
Block a user