From 2eb5fcfa506e0d0967d8f91d20ba6686749c59a0 Mon Sep 17 00:00:00 2001 From: Hui HX Xiang Date: Fri, 11 Oct 2013 21:45:45 -0700 Subject: [PATCH] Add error log for SystemExit in dhcp-agent The SystemExit exception should be logged in dhcp-agent, the operator may miss the useful exception messages to debug. * Add error log for SystemExit in dhcp-agent. * Add addCleanup(mock.patch.stopall) in setup() * Remove teardown() * Add unit test for None and nonexistent interface_driver. Closes-Bug #1239030 Change-Id: I01bef69adecaaf8d8a919034d33c60a43e4469a7 --- neutron/agent/linux/dhcp.py | 5 ++++- neutron/tests/unit/test_dhcp_agent.py | 26 ++++++++++++++++++++------ 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/neutron/agent/linux/dhcp.py b/neutron/agent/linux/dhcp.py index 9e7481d7c4..277eccba6e 100644 --- a/neutron/agent/linux/dhcp.py +++ b/neutron/agent/linux/dhcp.py @@ -553,7 +553,9 @@ class DeviceManager(object): self.root_helper = root_helper self.plugin = plugin if not conf.interface_driver: - raise SystemExit(_('You must specify an interface driver')) + msg = _('An interface driver must be specified') + LOG.error(msg) + raise SystemExit(msg) try: self.driver = importutils.import_object( conf.interface_driver, conf) @@ -561,6 +563,7 @@ class DeviceManager(object): msg = (_("Error importing interface driver '%(driver)s': " "%(inner)s") % {'driver': conf.interface_driver, 'inner': e}) + LOG.error(msg) raise SystemExit(msg) def get_interface_name(self, network, port): diff --git a/neutron/tests/unit/test_dhcp_agent.py b/neutron/tests/unit/test_dhcp_agent.py index 4cc6b2537e..5c9fb3a937 100644 --- a/neutron/tests/unit/test_dhcp_agent.py +++ b/neutron/tests/unit/test_dhcp_agent.py @@ -139,12 +139,7 @@ class TestDhcpAgent(base.BaseTestCase): self.driver_cls.return_value = self.driver self.mock_makedirs_p = mock.patch("os.makedirs") self.mock_makedirs = self.mock_makedirs_p.start() - - def tearDown(self): - self.driver_cls_p.stop() - self.mock_makedirs_p.stop() - cfg.CONF.reset() - super(TestDhcpAgent, self).tearDown() + self.addCleanup(mock.patch.stopall) def test_dhcp_agent_manager(self): state_rpc_str = 'neutron.agent.rpc.PluginReportStateAPI' @@ -319,6 +314,25 @@ class TestDhcpAgent(base.BaseTestCase): self.assertEqual(set(networks), set(dhcp.cache.get_network_ids())) + def test_none_interface_driver(self): + cfg.CONF.set_override('interface_driver', None) + with mock.patch.object(dhcp, 'LOG') as log: + self.assertRaises(SystemExit, dhcp.DeviceManager, + cfg.CONF, 'sudo', None) + msg = 'An interface driver must be specified' + log.error.assert_called_once_with(msg) + + def test_nonexistent_interface_driver(self): + # Temporarily turn off mock, so could use the real import_class + # to import interface_driver. + self.driver_cls_p.stop() + self.addCleanup(self.driver_cls_p.start) + cfg.CONF.set_override('interface_driver', 'foo') + with mock.patch.object(dhcp, 'LOG') as log: + self.assertRaises(SystemExit, dhcp.DeviceManager, + cfg.CONF, 'sudo', None) + log.error.assert_called_once() + class TestLogArgs(base.BaseTestCase):