Merge "Make linux.utils.execute log error on return codes"

This commit is contained in:
Jenkins 2014-05-30 23:31:12 +00:00 committed by Gerrit Code Review
commit d8c790bb0c
2 changed files with 41 additions and 3 deletions

View File

@ -71,9 +71,12 @@ def execute(cmd, root_helper=None, process_input=None, addl_env=None,
m = _("\nCommand: %(cmd)s\nExit code: %(code)s\nStdout: %(stdout)r\n" m = _("\nCommand: %(cmd)s\nExit code: %(code)s\nStdout: %(stdout)r\n"
"Stderr: %(stderr)r") % {'cmd': cmd, 'code': obj.returncode, "Stderr: %(stderr)r") % {'cmd': cmd, 'code': obj.returncode,
'stdout': _stdout, 'stderr': _stderr} 'stdout': _stdout, 'stderr': _stderr}
LOG.debug(m) if obj.returncode:
if obj.returncode and check_exit_code: LOG.error(m)
raise RuntimeError(m) if check_exit_code:
raise RuntimeError(m)
else:
LOG.debug(m)
finally: finally:
# NOTE(termie): this appears to be necessary to let the subprocess # NOTE(termie): this appears to be necessary to let the subprocess
# call clean something up in between calls, without # call clean something up in between calls, without

View File

@ -20,6 +20,19 @@ from neutron.agent.linux import utils
from neutron.tests import base from neutron.tests import base
class FakeCreateProcess(object):
class FakeStdin(object):
def close(self):
pass
def __init__(self, returncode):
self.returncode = returncode
self.stdin = self.FakeStdin()
def communicate(self, process_input=None):
return '', ''
class AgentUtilsExecuteTest(base.BaseTestCase): class AgentUtilsExecuteTest(base.BaseTestCase):
def setUp(self): def setUp(self):
super(AgentUtilsExecuteTest, self).setUp() super(AgentUtilsExecuteTest, self).setUp()
@ -75,6 +88,28 @@ class AgentUtilsExecuteTest(base.BaseTestCase):
addl_env={'foo': 'bar'}) addl_env={'foo': 'bar'})
self.assertEqual(result, expected) self.assertEqual(result, expected)
def test_return_code_log_error_raise_runtime(self):
with mock.patch.object(utils, 'create_process') as create_process:
create_process.return_value = FakeCreateProcess(1), 'ls'
with mock.patch.object(utils, 'LOG') as log:
self.assertRaises(RuntimeError, utils.execute,
['ls'])
self.assertTrue(log.error.called)
def test_return_code_log_error_no_raise_runtime(self):
with mock.patch.object(utils, 'create_process') as create_process:
create_process.return_value = FakeCreateProcess(1), 'ls'
with mock.patch.object(utils, 'LOG') as log:
utils.execute(['ls'], check_exit_code=False)
self.assertTrue(log.error.called)
def test_return_code_log_debug(self):
with mock.patch.object(utils, 'create_process') as create_process:
create_process.return_value = FakeCreateProcess(0), 'ls'
with mock.patch.object(utils, 'LOG') as log:
utils.execute(['ls'])
self.assertTrue(log.debug.called)
class AgentUtilsGetInterfaceMAC(base.BaseTestCase): class AgentUtilsGetInterfaceMAC(base.BaseTestCase):
def test_get_interface_mac(self): def test_get_interface_mac(self):