From 4434882bc41c87c52192ab3ad01d3448868603cc Mon Sep 17 00:00:00 2001 From: Isaku Yamahata Date: Tue, 25 Feb 2014 15:34:53 +0900 Subject: [PATCH] tests/unit: clean up notification driver Some tests doesn't cleanup notification driver. So some tests can run against unintended notification driver depending on execution order Change-Id: I7dbed47153be66684827b2693bbc995dddfb4c1b Related-Bug: #1281481 Partial-Bug: #1284549 --- neutron/tests/base.py | 14 ++++++++++++++ .../services/metering/test_metering_agent.py | 8 +------- neutron/tests/unit/test_extension_extraroute.py | 10 ++-------- neutron/tests/unit/test_l3_plugin.py | 17 ++--------------- 4 files changed, 19 insertions(+), 30 deletions(-) diff --git a/neutron/tests/base.py b/neutron/tests/base.py index 71b3427e67f..26c166e16c7 100644 --- a/neutron/tests/base.py +++ b/neutron/tests/base.py @@ -28,6 +28,8 @@ from oslo.config import cfg import testtools from neutron import manager +from neutron.openstack.common.notifier import api as notifier_api +from neutron.openstack.common.notifier import test_notifier from neutron.tests import post_mortem_debug @@ -52,6 +54,18 @@ class BaseTestCase(testtools.TestCase): if core_plugin is not None: cfg.CONF.set_override('core_plugin', core_plugin) + def _cleanup_test_notifier(self): + test_notifier.NOTIFICATIONS = [] + + def setup_notification_driver(self, notification_driver=None): + # to reload the drivers + self.addCleanup(notifier_api._reset_drivers) + self.addCleanup(self._cleanup_test_notifier) + notifier_api._reset_drivers() + if notification_driver is None: + notification_driver = [test_notifier.__name__] + cfg.CONF.set_override("notification_driver", notification_driver) + def setUp(self): super(BaseTestCase, self).setUp() diff --git a/neutron/tests/unit/services/metering/test_metering_agent.py b/neutron/tests/unit/services/metering/test_metering_agent.py index 0c3a410ea64..3e1d0db299e 100644 --- a/neutron/tests/unit/services/metering/test_metering_agent.py +++ b/neutron/tests/unit/services/metering/test_metering_agent.py @@ -18,7 +18,6 @@ import mock from oslo.config import cfg from neutron.agent.common import config -from neutron.openstack.common.notifier import api as notifier_api from neutron.openstack.common.notifier import test_notifier from neutron.openstack.common import uuidutils from neutron.services.metering.agents import metering_agent @@ -52,8 +51,7 @@ class TestMeteringOperations(base.BaseTestCase): cfg.CONF.set_override('measure_interval', 0) cfg.CONF.set_override('report_interval', 0) - notifier_api._drivers = None - cfg.CONF.set_override("notification_driver", [test_notifier.__name__]) + self.setup_notification_driver() metering_rpc = ('neutron.services.metering.agents.metering_agent.' 'MeteringPluginRpc._get_sync_data_metering') @@ -70,10 +68,6 @@ class TestMeteringOperations(base.BaseTestCase): self.agent = metering_agent.MeteringAgent('my agent', cfg.CONF) self.driver = self.agent.metering_driver - def tearDown(self): - test_notifier.NOTIFICATIONS = [] - super(TestMeteringOperations, self).tearDown() - def test_add_metering_label(self): self.agent.add_metering_label(None, ROUTERS) self.assertEqual(self.driver.add_metering_label.call_count, 1) diff --git a/neutron/tests/unit/test_extension_extraroute.py b/neutron/tests/unit/test_extension_extraroute.py index 48ce3f782b9..826ca80a2c0 100644 --- a/neutron/tests/unit/test_extension_extraroute.py +++ b/neutron/tests/unit/test_extension_extraroute.py @@ -24,8 +24,6 @@ from neutron.db import extraroute_db from neutron.extensions import extraroute from neutron.extensions import l3 from neutron.openstack.common import log as logging -from neutron.openstack.common.notifier import api as notifier_api -from neutron.openstack.common.notifier import test_notifier from neutron.openstack.common import uuidutils from neutron.tests.unit import test_api_v2 from neutron.tests.unit import test_l3_plugin as test_l3 @@ -470,9 +468,7 @@ class ExtraRouteDBIntTestCase(test_l3.L3NatDBIntTestCase, ext_mgr = ExtraRouteTestExtensionManager() super(test_l3.L3BaseForIntTests, self).setUp(plugin=plugin, ext_mgr=ext_mgr) - # Set to None to reload the drivers - notifier_api._drivers = None - cfg.CONF.set_override("notification_driver", [test_notifier.__name__]) + self.setup_notification_driver() class ExtraRouteDBIntTestCaseXML(ExtraRouteDBIntTestCase): @@ -497,9 +493,7 @@ class ExtraRouteDBSepTestCase(test_l3.L3NatDBSepTestCase, plugin=plugin, ext_mgr=ext_mgr, service_plugins=service_plugins) - # Set to None to reload the drivers - notifier_api._drivers = None - cfg.CONF.set_override("notification_driver", [test_notifier.__name__]) + self.setup_notification_driver() class ExtraRouteDBSepTestCaseXML(ExtraRouteDBSepTestCase): diff --git a/neutron/tests/unit/test_l3_plugin.py b/neutron/tests/unit/test_l3_plugin.py index a29ade31e28..bcaf7cefc30 100644 --- a/neutron/tests/unit/test_l3_plugin.py +++ b/neutron/tests/unit/test_l3_plugin.py @@ -35,7 +35,6 @@ from neutron.extensions import external_net from neutron.extensions import l3 from neutron.manager import NeutronManager from neutron.openstack.common import log as logging -from neutron.openstack.common.notifier import api as notifier_api from neutron.openstack.common.notifier import test_notifier from neutron.openstack.common import uuidutils from neutron.plugins.common import constants as service_constants @@ -1723,13 +1722,7 @@ class L3BaseForIntTests(test_db_plugin.NeutronDbPluginV2TestCase): super(L3BaseForIntTests, self).setUp(plugin=plugin, ext_mgr=ext_mgr, service_plugins=service_plugins) - # Set to None to reload the drivers - notifier_api._drivers = None - cfg.CONF.set_override("notification_driver", [test_notifier.__name__]) - - def tearDown(self): - test_notifier.NOTIFICATIONS = [] - super(L3BaseForIntTests, self).tearDown() + self.setup_notification_driver() class L3BaseForSepTests(test_db_plugin.NeutronDbPluginV2TestCase): @@ -1750,13 +1743,7 @@ class L3BaseForSepTests(test_db_plugin.NeutronDbPluginV2TestCase): super(L3BaseForSepTests, self).setUp(plugin=plugin, ext_mgr=ext_mgr, service_plugins=service_plugins) - # Set to None to reload the drivers - notifier_api._drivers = None - cfg.CONF.set_override("notification_driver", [test_notifier.__name__]) - - def tearDown(self): - test_notifier.NOTIFICATIONS = [] - super(L3BaseForSepTests, self).tearDown() + self.setup_notification_driver() class L3AgentDbIntTestCase(L3BaseForIntTests, L3AgentDbTestCaseBase):