Skip to content

Commit

Permalink
Merge pull request #55404 from joechainz/porting-54543
Browse files Browse the repository at this point in the history
Porting 54543
  • Loading branch information
dwoz authored Dec 3, 2019
2 parents 8fb2548 + 6cdb9e5 commit 0cb3304
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 16 deletions.
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

0 comments on commit 0cb3304

Please sign in to comment.