diff --git a/ironic_python_agent/tests/unit/test_utils.py b/ironic_python_agent/tests/unit/test_utils.py index 729916a64..ebfd3071d 100644 --- a/ironic_python_agent/tests/unit/test_utils.py +++ b/ironic_python_agent/tests/unit/test_utils.py @@ -24,6 +24,7 @@ import tarfile import tempfile import testtools +from ironic_lib import utils as ironic_utils import mock from oslo_concurrency import processutils from oslotest import base as test_base @@ -32,101 +33,13 @@ from ironic_python_agent import errors from ironic_python_agent import utils -class ExecuteTestCase(testtools.TestCase): - """This class is a copy of the same class in openstack/ironic.""" +class ExecuteTestCase(test_base.BaseTestCase): - def test_retry_on_failure(self): - fd, tmpfilename = tempfile.mkstemp() - _, tmpfilename2 = tempfile.mkstemp() - try: - fp = os.fdopen(fd, 'w+') - fp.write('''#!/bin/sh -# If stdin fails to get passed during one of the runs, make a note. -if ! grep -q foo -then - echo 'failure' > "$1" -fi -# If stdin has failed to get passed during this or a previous run, exit early. -if grep failure "$1" -then - exit 1 -fi -runs="$(cat $1)" -if [ -z "$runs" ] -then - runs=0 -fi -runs=$(($runs + 1)) -echo $runs > "$1" -exit 1 -''') - fp.close() - os.chmod(tmpfilename, 0o755) - try: - self.assertRaises(processutils.ProcessExecutionError, - utils.execute, - tmpfilename, tmpfilename2, attempts=10, - process_input=b'foo', - delay_on_retry=False) - except OSError as e: - if e.errno == errno.EACCES: - self.skipTest("Permissions error detected. " - "Are you running with a noexec /tmp?") - else: - raise - fp = open(tmpfilename2, 'r') - runs = fp.read() - fp.close() - self.assertNotEqual(runs.strip(), 'failure', - 'stdin did not always get passed correctly') - runs = int(runs.strip()) - self.assertEqual(10, runs, - 'Ran %d times instead of 10.' % (runs,)) - finally: - os.unlink(tmpfilename) - os.unlink(tmpfilename2) - - def test_unknown_kwargs_raises_error(self): - self.assertRaises(processutils.UnknownArgumentError, - utils.execute, - '/usr/bin/env', 'true', - this_is_not_a_valid_kwarg=True) - - def test_check_exit_code_boolean(self): + @mock.patch.object(ironic_utils, 'execute', autospec=True) + def test_execute(self, mock_execute): utils.execute('/usr/bin/env', 'false', check_exit_code=False) - self.assertRaises(processutils.ProcessExecutionError, - utils.execute, - '/usr/bin/env', 'false', check_exit_code=True) - - def test_no_retry_on_success(self): - fd, tmpfilename = tempfile.mkstemp() - _, tmpfilename2 = tempfile.mkstemp() - try: - fp = os.fdopen(fd, 'w+') - fp.write('''#!/bin/sh -# If we've already run, bail out. -grep -q foo "$1" && exit 1 -# Mark that we've run before. -echo foo > "$1" -# Check that stdin gets passed correctly. -grep foo -''') - fp.close() - os.chmod(tmpfilename, 0o755) - try: - utils.execute(tmpfilename, - tmpfilename2, - process_input=b'foo', - attempts=2) - except OSError as e: - if e.errno == errno.EACCES: - self.skipTest("Permissions error detected. " - "Are you running with a noexec /tmp?") - else: - raise - finally: - os.unlink(tmpfilename) - os.unlink(tmpfilename2) + mock_execute.assert_called_once_with('/usr/bin/env', 'false', + check_exit_code=False) class GetAgentParamsTestCase(test_base.BaseTestCase): diff --git a/ironic_python_agent/utils.py b/ironic_python_agent/utils.py index 8e50987fc..421af0b17 100644 --- a/ironic_python_agent/utils.py +++ b/ironic_python_agent/utils.py @@ -24,6 +24,7 @@ import tarfile import tempfile import time +from ironic_lib import utils as ironic_utils from oslo_concurrency import processutils from oslo_log import log as logging from oslo_utils import units @@ -59,26 +60,11 @@ COLLECT_LOGS_COMMANDS = { def execute(*cmd, **kwargs): - """Convenience wrapper around oslo's execute() method. + """Convenience wrapper around ironic_lib's execute() method. - Executes and logs results from a system command. See docs for - oslo_concurrency.processutils.execute for usage. - - :param *cmd: positional arguments to pass to processutils.execute() - :param log_stdout: keyword-only argument: whether to log the output - :param **kwargs: keyword arguments to pass to processutils.execute() - :raises: UnknownArgumentError on receiving unknown arguments - :raises: ProcessExecutionError - :raises: OSError - :returns: tuple of (stdout, stderr) + Executes and logs results from a system command. """ - log_stdout = kwargs.pop('log_stdout', True) - result = processutils.execute(*cmd, **kwargs) - LOG.debug('Execution completed, command line is "%s"', ' '.join(cmd)) - if log_stdout: - LOG.debug('Command stdout is: "%s"', result[0]) - LOG.debug('Command stderr is: "%s"', result[1]) - return result + return ironic_utils.execute(*cmd, **kwargs) def try_execute(*cmd, **kwargs):