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
This commit is contained in:
Hui HX Xiang 2013-10-11 21:45:45 -07:00
parent 7d31234638
commit 2eb5fcfa50
2 changed files with 24 additions and 7 deletions

View File

@ -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):

View File

@ -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):