Use ironic_lib's execute()
Use ironic_lib's execute() instead of copying that code. Change-Id: If40c2da5423d0f90cc6661f8cdf2bec9567e1f7d Partial-Bug: #1587199
This commit is contained in:
parent
913c2a1ea5
commit
81ca8a8744
ironic_python_agent
@ -24,6 +24,7 @@ import tarfile
|
|||||||
import tempfile
|
import tempfile
|
||||||
import testtools
|
import testtools
|
||||||
|
|
||||||
|
from ironic_lib import utils as ironic_utils
|
||||||
import mock
|
import mock
|
||||||
from oslo_concurrency import processutils
|
from oslo_concurrency import processutils
|
||||||
from oslotest import base as test_base
|
from oslotest import base as test_base
|
||||||
@ -32,101 +33,13 @@ from ironic_python_agent import errors
|
|||||||
from ironic_python_agent import utils
|
from ironic_python_agent import utils
|
||||||
|
|
||||||
|
|
||||||
class ExecuteTestCase(testtools.TestCase):
|
class ExecuteTestCase(test_base.BaseTestCase):
|
||||||
"""This class is a copy of the same class in openstack/ironic."""
|
|
||||||
|
|
||||||
def test_retry_on_failure(self):
|
@mock.patch.object(ironic_utils, 'execute', autospec=True)
|
||||||
fd, tmpfilename = tempfile.mkstemp()
|
def test_execute(self, mock_execute):
|
||||||
_, 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):
|
|
||||||
utils.execute('/usr/bin/env', 'false', check_exit_code=False)
|
utils.execute('/usr/bin/env', 'false', check_exit_code=False)
|
||||||
self.assertRaises(processutils.ProcessExecutionError,
|
mock_execute.assert_called_once_with('/usr/bin/env', 'false',
|
||||||
utils.execute,
|
check_exit_code=False)
|
||||||
'/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)
|
|
||||||
|
|
||||||
|
|
||||||
class GetAgentParamsTestCase(test_base.BaseTestCase):
|
class GetAgentParamsTestCase(test_base.BaseTestCase):
|
||||||
|
@ -24,6 +24,7 @@ import tarfile
|
|||||||
import tempfile
|
import tempfile
|
||||||
import time
|
import time
|
||||||
|
|
||||||
|
from ironic_lib import utils as ironic_utils
|
||||||
from oslo_concurrency import processutils
|
from oslo_concurrency import processutils
|
||||||
from oslo_log import log as logging
|
from oslo_log import log as logging
|
||||||
from oslo_utils import units
|
from oslo_utils import units
|
||||||
@ -59,26 +60,11 @@ COLLECT_LOGS_COMMANDS = {
|
|||||||
|
|
||||||
|
|
||||||
def execute(*cmd, **kwargs):
|
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
|
Executes and logs results from a system command.
|
||||||
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)
|
|
||||||
"""
|
"""
|
||||||
log_stdout = kwargs.pop('log_stdout', True)
|
return ironic_utils.execute(*cmd, **kwargs)
|
||||||
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
|
|
||||||
|
|
||||||
|
|
||||||
def try_execute(*cmd, **kwargs):
|
def try_execute(*cmd, **kwargs):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user