Skip to content

Commit

Permalink
Merge pull request #51012 from cdalvaro/bugifx/cwd_in_combination_wit…
Browse files Browse the repository at this point in the history
…h_runas_cmd.run

Fix cmd.run with cwd and runas on macOS
  • Loading branch information
dwoz authored Dec 31, 2018
2 parents 99966fc + 68b5724 commit 5f8eae7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
2 changes: 1 addition & 1 deletion salt/modules/cmdmod.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ def _get_stripped(cmd):
if isinstance(cmd, (list, tuple)):
cmd = ' '.join(map(_cmd_quote, cmd))

cmd = 'su -l {0} -c "{1}"'.format(runas, cmd)
cmd = 'su -l {0} -c "cd {1}; {2}"'.format(runas, cwd, cmd)
# set runas to None, because if you try to run `su -l` as well as
# simulate the environment macOS will prompt for the password of the
# user and will cause salt to hang.
Expand Down
14 changes: 14 additions & 0 deletions tests/unit/modules/test_cmdmod.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,20 @@ def test_run_cwd_doesnt_exist_issue_7154(self):
else:
raise RuntimeError

def test_run_cwd_in_combination_with_runas(self):
'''
cmd.run executes command in the cwd directory
when the runas parameter is specified
'''
cmd = 'pwd'
cwd = '/tmp'
runas = 'foobar'

with patch('pwd.getpwnam') as getpwnam_mock, \
patch.dict(cmdmod.__grains__, {'os': 'Darwin', 'os_family': 'Solaris'}):
stdout = cmdmod._run(cmd, cwd=cwd, runas=runas).get('stdout')
self.assertEqual(stdout, cwd)

def test_run_all_binary_replace(self):
'''
Test for failed decoding of binary data, for instance when doing
Expand Down

0 comments on commit 5f8eae7

Please sign in to comment.