Merge "show stdout/err from failed command execution"

This commit is contained in:
Jenkins 2014-08-29 03:57:06 +00:00 committed by Gerrit Code Review
commit 11b8a0b035
3 changed files with 67 additions and 1 deletions

View File

@ -272,10 +272,21 @@ def get_id_from_href(href):
def execute_with_timeout(*args, **kwargs):
time = kwargs.pop('timeout', 30)
log_output_on_error = kwargs.pop('log_output_on_error', False)
timeout = Timeout(time)
try:
return execute(*args, **kwargs)
except exception.ProcessExecutionError as e:
if log_output_on_error:
LOG.error(
_("Command '%(cmd)s' failed. %(description)s "
"Exit code: %(exit_code)s\nstderr: %(stderr)s\n"
"stdout: %(stdout)s") %
{'cmd': e.cmd, 'description': e.description or '',
'exit_code': e.exit_code, 'stderr': e.stderr,
'stdout': e.stdout})
raise
except Timeout as t:
if t is not timeout:
LOG.error(_("Got a timeout but not the one expected."))

View File

@ -165,7 +165,8 @@ class MySqlAppStatus(service.BaseDbStatus):
try:
out, err = utils.execute_with_timeout(
"/usr/bin/mysqladmin",
"ping", run_as_root=True, root_helper="sudo")
"ping", run_as_root=True, root_helper="sudo",
log_output_on_error=True)
LOG.info(_("MySQL Service Status is RUNNING."))
return rd_instance.ServiceStatuses.RUNNING
except exception.ProcessExecutionError:

View File

@ -0,0 +1,54 @@
# Copyright 2014 SUSE Linux GmbH.
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
#
from trove.common import exception
import trove.common.utils as utils
from mock import Mock
import testtools
from testtools import ExpectedException
class TestTroveExecuteWithTimeout(testtools.TestCase):
def test_throws_process_execution_error(self):
utils.execute = Mock(
side_effect=exception.ProcessExecutionError(
description='test-desc', exit_code=42, stderr='err',
stdout='out', cmd='test'))
with ExpectedException(
exception.ProcessExecutionError,
"test-desc\nCommand: test\nExit code: 42\n"
"Stdout: 'out'\nStderr: 'err'"):
utils.execute_with_timeout('/usr/bin/foo')
def test_log_error_when_log_output_on_error_is_true(self):
utils.execute = Mock(
side_effect=exception.ProcessExecutionError(
description='test-desc', exit_code=42, stderr='err',
stdout='out', cmd='test'))
utils.LOG.error = Mock()
with ExpectedException(
exception.ProcessExecutionError,
"test-desc\nCommand: test\nExit code: 42\n"
"Stdout: 'out'\nStderr: 'err'"):
utils.execute_with_timeout(
'/usr/bin/foo', log_output_on_error=True)
utils.LOG.error.assert_called_with(
u"Command 'test' failed. test-desc Exit code: 42\n"
"stderr: err\nstdout: out")