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

Fix cmd.run with cwd and runas on macOS #51012

Merged
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
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