unit test: mock.patch before super.setUp

There are intermittent failures with journal db. e.g. there is should
corresponding journal entry, but it didn't.
One of the possible causes is, there can be one or more journal threads
running unintentionally. In principle JournalThread is prevented from
creating thread by mocking methods that spawns such threads.
Make it sure that do mock.patch or useFixture which calls mock.patch
before calling super.setUp

Change-Id: I884c6ca5c07f330c2c416226d95304ac618bfc66
This commit is contained in:
Isaku Yamahata
2017-07-19 09:27:02 -07:00
parent 5b9b3e02c5
commit c108ef4629
13 changed files with 39 additions and 36 deletions

View File

@@ -29,15 +29,15 @@ from networking_odl.tests.unit import test_base_db
class OpenDaylightConfigBase(test_plugin.Ml2PluginV2TestCase,
test_base_db.ODLBaseDbTestCase):
def setUp(self):
self.useFixture(base.OpenDaylightJournalThreadFixture())
self.mock_mt_thread = mock.patch.object(
maintenance.MaintenanceThread, 'start').start()
self.useFixture(base.OpenDaylightRestClientFixture())
super(OpenDaylightConfigBase, self).setUp()
cfg.CONF.set_override('mechanism_drivers',
['logger', 'opendaylight_v2'], 'ml2')
cfg.CONF.set_override('extension_drivers',
['port_security', 'qos'], 'ml2')
self.useFixture(base.OpenDaylightJournalThreadFixture())
self.mock_mt_thread = mock.patch.object(
maintenance.MaintenanceThread, 'start').start()
self.thread = journal.OpenDaylightJournalThread()
def run_journal_processing(self):
@@ -47,11 +47,11 @@ class OpenDaylightConfigBase(test_plugin.Ml2PluginV2TestCase,
class OpenDaylightTestCase(OpenDaylightConfigBase):
def setUp(self):
self.mock_sendjson = mock.patch.object(client.OpenDaylightRestClient,
'sendjson').start()
super(OpenDaylightTestCase, self).setUp()
self.port_create_status = 'DOWN'
self.mech = mech_driver_v2.OpenDaylightMechanismDriver()
self.mock_sendjson = mock.patch.object(client.OpenDaylightRestClient,
'sendjson').start()
self.mock_sendjson.side_effect = self.check_sendjson
def check_sendjson(self, method, urlpath, obj):

View File

@@ -32,9 +32,9 @@ class TestOdlFeatures(base.DietTestCase):
"neutron-extensions:operational-port-status"}]}}"""
def setUp(self):
super(TestOdlFeatures, self).setUp()
self.features_fixture = base.OpenDaylightFeaturesFixture()
self.useFixture(self.features_fixture)
super(TestOdlFeatures, self).setUp()
self.features_fixture.mock_odl_features_init.stop()
@mock.patch.object(OpenDaylightRestClient, 'request')

View File

@@ -48,10 +48,10 @@ class TestWebsocketClient(base.DietTestCase):
def setUp(self):
"""Setup test."""
super(TestWebsocketClient, self).setUp()
self.useFixture(base.OpenDaylightRestClientFixture())
mock.patch.object(wsc.OpenDaylightWebsocketClient,
'start_odl_websocket_thread').start()
super(TestWebsocketClient, self).setUp()
self.mgr = wsc.OpenDaylightWebsocketClient.odl_create_websocket(
"http://localhost:8080/",

View File

@@ -44,10 +44,10 @@ from networking_odl.trunk import trunk_driver_v2 as trunk_driver
class FullSyncTestCase(test_base_db.ODLBaseDbTestCase):
def setUp(self):
super(FullSyncTestCase, self).setUp()
self.useFixture(
base.OpenDaylightRestClientGlobalFixture(full_sync._CLIENT))
super(FullSyncTestCase, self).setUp()
self._CLIENT = full_sync._CLIENT.get_client()
for plugin_name in self._get_all_resources():

View File

@@ -34,13 +34,11 @@ from networking_odl.tests import base
class RecoveryTestCase(SqlTestCaseLight):
def setUp(self):
super(RecoveryTestCase, self).setUp()
self.db_session = neutron_db_api.get_writer_session()
self.useFixture(
base.OpenDaylightRestClientGlobalFixture(recovery._CLIENT))
super(RecoveryTestCase, self).setUp()
self.db_session = neutron_db_api.get_writer_session()
self._CLIENT = recovery._CLIENT.get_client()
self.addCleanup(self._db_cleanup)
self.addCleanup(self.clean_registered_resources)

View File

@@ -49,9 +49,9 @@ PORT_ID = uuidutils.generate_uuid()
class OpenDayLightMechanismConfigTests(testlib_api.SqlTestCase):
def setUp(self):
super(OpenDayLightMechanismConfigTests, self).setUp()
self.useFixture(odl_base.OpenDaylightRestClientFixture())
self.useFixture(odl_base.OpenDaylightFeaturesFixture())
super(OpenDayLightMechanismConfigTests, self).setUp()
cfg.CONF.set_override('mechanism_drivers',
['logger', 'opendaylight_v2'], 'ml2')
cfg.CONF.set_override('port_binding_controller',

View File

@@ -124,13 +124,13 @@ class OpenDaylightTestCase(test_plugin.Ml2PluginV2TestCase):
def setUp(self):
self.useFixture(odl_base.OpenDaylightRestClientFixture())
self.useFixture(odl_base.OpenDaylightFeaturesFixture())
super(OpenDaylightTestCase, self).setUp()
self.port_create_status = 'DOWN'
self.mech = mech_driver.OpenDaylightMechanismDriver()
mock.patch.object(
client.OpenDaylightRestClient,
'sendjson',
new=self.check_sendjson).start()
super(OpenDaylightTestCase, self).setUp()
self.port_create_status = 'DOWN'
self.mech = mech_driver.OpenDaylightMechanismDriver()
def check_sendjson(self, method, urlpath, obj):
self.assertFalse(urlpath.startswith("http://"))
@@ -138,8 +138,8 @@ class OpenDaylightTestCase(test_plugin.Ml2PluginV2TestCase):
class OpenDayLightMechanismConfigTests(testlib_api.SqlTestCase):
def setUp(self):
super(OpenDayLightMechanismConfigTests, self).setUp()
self.useFixture(odl_base.OpenDaylightFeaturesFixture())
super(OpenDayLightMechanismConfigTests, self).setUp()
config.cfg.CONF.set_override('mechanism_drivers',
['logger', 'opendaylight'],
'ml2')
@@ -303,9 +303,9 @@ class OpenDaylightSyncTestCase(OpenDaylightTestCase):
class OpenDaylightMechanismDriverTestCase(base.BaseTestCase):
def setUp(self):
super(OpenDaylightMechanismDriverTestCase, self).setUp()
self.useFixture(odl_base.OpenDaylightRestClientFixture())
self.useFixture(odl_base.OpenDaylightFeaturesFixture())
super(OpenDaylightMechanismDriverTestCase, self).setUp()
config.cfg.CONF.set_override('mechanism_drivers',
['logger', 'opendaylight'], 'ml2')
self.mech = mech_driver.OpenDaylightMechanismDriver()

View File

@@ -59,15 +59,15 @@ SG_RULE_FAKE_ID = uuidutils.generate_uuid()
class OpenDayLightMechanismConfigTests(testlib_api.SqlTestCase):
def setUp(self):
super(OpenDayLightMechanismConfigTests, self).setUp()
self.useFixture(base.OpenDaylightFeaturesFixture())
self.useFixture(base.OpenDaylightJournalThreadFixture())
self.mock_mt_thread = mock.patch.object(
maintenance.MaintenanceThread, 'start').start()
super(OpenDayLightMechanismConfigTests, self).setUp()
cfg.CONF.set_override('mechanism_drivers',
['logger', 'opendaylight_v2'], 'ml2')
cfg.CONF.set_override('port_binding_controller',
'legacy-port-binding', 'ml2_odl')
self.useFixture(base.OpenDaylightFeaturesFixture())
def _set_config(self, url='http://127.0.0.1:9999', username='someuser',
password='somepass'):
@@ -154,10 +154,9 @@ class AttributeDict(dict):
class OpenDaylightMechanismDriverTestCase(base_v2.OpenDaylightConfigBase):
def setUp(self):
super(OpenDaylightMechanismDriverTestCase, self).setUp()
self.useFixture(base.OpenDaylightFeaturesFixture())
self.mock_sync_thread = mock.patch.object(
journal.OpenDaylightJournalThread, 'start_odl_sync_thread').start()
self.useFixture(base.OpenDaylightJournalThreadFixture())
super(OpenDaylightMechanismDriverTestCase, self).setUp()
self.db_session = neutron_db_api.get_writer_session()
self.mech = mech_driver_v2.OpenDaylightMechanismDriver()
self.mech.initialize()
@@ -577,13 +576,19 @@ class OpenDaylightMechanismDriverTestCase(base_v2.OpenDaylightConfigBase):
def test_thread_call(self):
"""Verify that the sync thread method is called."""
# Create any object that would spin up the sync thread via the
# decorator call_thread_on_end() used by all the event handlers.
self._call_operation_object(odl_const.ODL_CREATE,
odl_const.ODL_NETWORK)
with mock.patch.object(
journal.OpenDaylightJournalThread,
'start_odl_sync_thread') as mock_sync_thread:
self.mech = mech_driver_v2.OpenDaylightMechanismDriver()
self.mech.initialize()
# Verify that the thread call was made.
self.assertTrue(self.mock_sync_thread.called)
# Create any object that would spin up the sync thread via the
# decorator call_thread_on_end() used by all the event handlers.
self._call_operation_object(odl_const.ODL_CREATE,
odl_const.ODL_NETWORK)
# Verify that the thread call was made.
mock_sync_thread.assert_called()
def test_sg(self):
self._test_object_type(odl_const.ODL_SG, 2)

View File

@@ -56,10 +56,10 @@ class TestOdlPortStatusUpdate(base.DietTestCase):
}""")
def setUp(self):
super(TestOdlPortStatusUpdate, self).setUp()
self.useFixture(base.OpenDaylightFeaturesFixture())
self.mock_ws_client = mock.patch.object(
OpenDaylightWebsocketClient, 'odl_create_websocket')
super(TestOdlPortStatusUpdate, self).setUp()
def test_object_create(self):
OdlPortStatusUpdate()

View File

@@ -228,9 +228,9 @@ class TestPseudoAgentDBBindingController(base.DietTestCase):
def setUp(self):
"""Setup test."""
super(TestPseudoAgentDBBindingController, self).setUp()
self.useFixture(base.OpenDaylightRestClientFixture())
self.useFixture(base.OpenDaylightFeaturesFixture())
super(TestPseudoAgentDBBindingController, self).setUp()
fake_agents_db = mock.MagicMock()
fake_agents_db.create_or_update_agent = mock.MagicMock()
@@ -496,9 +496,9 @@ class TestPseudoAgentDBBindingControllerBug1608659(
]}}
def setUp(self):
self.useFixture(base.OpenDaylightRestClientFixture())
super(TestPseudoAgentDBBindingControllerBug1608659, self).setUp(
plugin='ml2')
self.useFixture(base.OpenDaylightRestClientFixture())
self.core_plugin = directory.get_plugin()
self.mgr = pseudo_agentdb_binding.PseudoAgentDBBindingController(
self.core_plugin)

View File

@@ -26,10 +26,10 @@ from networking_odl.tests.unit import base_v2
class OpenDaylightQosDriverTestCase(base_v2.OpenDaylightConfigBase):
def setUp(self):
self.useFixture(base.OpenDaylightJournalThreadFixture())
super(OpenDaylightQosDriverTestCase, self).setUp()
self.db_session = neutron_db_api.get_writer_session()
self.qos_driver = qos_driver.OpenDaylightQosDriver()
self.useFixture(base.OpenDaylightJournalThreadFixture())
def _get_mock_context(self):
current = {'tenant_id': 'tenant_id'}

View File

@@ -25,10 +25,10 @@ class TestOpenDaylightSFCFlowClassifierDriverV1(base.DietTestCase):
def setUp(self):
self.useFixture(odl_base.OpenDaylightRestClientFixture())
super(TestOpenDaylightSFCFlowClassifierDriverV1, self).setUp()
self.mocked_fc_context = patch(
'networking_sfc.services.flowclassifier.common.context'
'.FlowClassifierContext').start().return_value
super(TestOpenDaylightSFCFlowClassifierDriverV1, self).setUp()
self.driver = sfc_fc.OpenDaylightSFCFlowClassifierDriverV1()
self.driver.initialize()

View File

@@ -27,7 +27,6 @@ class TestOpenDaylightSFCDriverV1(base.DietTestCase):
def setUp(self):
self.useFixture(odl_base.OpenDaylightRestClientFixture())
super(TestOpenDaylightSFCDriverV1, self).setUp()
self.mocked_pp_context = patch(
'networking_sfc.services.sfc.common.context.PortPairContext'
).start().return_value
@@ -39,6 +38,7 @@ class TestOpenDaylightSFCDriverV1(base.DietTestCase):
self.mocked_pc_context = patch(
'networking_sfc.services.sfc.common.context.PortChainContext'
).start().return_value
super(TestOpenDaylightSFCDriverV1, self).setUp()
self.driver = sfc_driver_v1.OpenDaylightSFCDriverV1()
self.driver.initialize()