Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Porting 54543 #55404

Merged
merged 3 commits into from
Dec 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 3 additions & 12 deletions salt/modules/cmdmod.py
Original file line number Diff line number Diff line change
Expand Up @@ -666,21 +666,12 @@ def _get_stripped(cmd):
except (OSError, IOError) as exc:
msg = (
'Unable to run command \'{0}\' with the context \'{1}\', '
'reason: '.format(
'reason: {2}'.format(
cmd if output_loglevel is not None else 'REDACTED',
new_kwargs
new_kwargs,
exc
)
)
try:
if exc.filename is None:
msg += 'command not found'
else:
msg += '{0}: {1}'.format(exc, exc.filename)
except AttributeError:
# Both IOError and OSError have the filename attribute, so this
# is a precaution in case the exception classes in the previous
# try/except are changed.
msg += 'unknown'
raise CommandExecutionError(msg)

try:
Expand Down
14 changes: 10 additions & 4 deletions tests/unit/modules/test_cmdmod.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,23 +232,29 @@ def test_run_no_vt_os_error(self):
'''
Tests error raised when not useing vt and OSError is provided
'''
expected_error = "expect error"
with patch('salt.modules.cmdmod._is_valid_shell', MagicMock(return_value=True)):
with patch('salt.utils.platform.is_windows', MagicMock(return_value=False)):
with patch('os.path.isfile', MagicMock(return_value=True)):
with patch('os.access', MagicMock(return_value=True)):
with patch('salt.utils.timed_subprocess.TimedProc', MagicMock(side_effect=OSError)):
self.assertRaises(CommandExecutionError, cmdmod._run, 'foo')
with patch('salt.utils.timed_subprocess.TimedProc', MagicMock(side_effect=OSError(expected_error))):
with self.assertRaises(CommandExecutionError) as error:
cmdmod.run('foo')
assert error.exception.args[0].endswith(expected_error), repr(error.exception.args[0])

def test_run_no_vt_io_error(self):
'''
Tests error raised when not useing vt and IOError is provided
'''
expected_error = "expect error"
with patch('salt.modules.cmdmod._is_valid_shell', MagicMock(return_value=True)):
with patch('salt.utils.platform.is_windows', MagicMock(return_value=False)):
with patch('os.path.isfile', MagicMock(return_value=True)):
with patch('os.access', MagicMock(return_value=True)):
with patch('salt.utils.timed_subprocess.TimedProc', MagicMock(side_effect=IOError)):
self.assertRaises(CommandExecutionError, cmdmod._run, 'foo')
with patch('salt.utils.timed_subprocess.TimedProc', MagicMock(side_effect=IOError(expected_error))):
with self.assertRaises(CommandExecutionError) as error:
cmdmod.run('foo')
assert error.exception.args[0].endswith(expected_error), repr(error.exception.args[0])

@skipIf(salt.utils.platform.is_windows(), 'Do not run on Windows')
@skipIf(True, 'Test breaks unittests runs')
Expand Down