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

Call bash only when necessary on macOS #54769

Merged
merged 22 commits into from
Dec 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
2b1bbc3
fix: Call bash only when necessary
cdalvaro Oct 5, 2019
e1bac5c
test: Improve test test_shell_properly_handled_on_macOS
cdalvaro Oct 5, 2019
373f59f
Merge branch 'master' into 2019.2.1
cdalvaro Oct 8, 2019
0a6b7cf
feat: skip test if platform is not macOS
cdalvaro Oct 8, 2019
35e5882
Merge branch '2019.2.1' of github.com:cdalvaro/salt into 2019.2.1
cdalvaro Oct 8, 2019
553d486
Merge branch 'master' into 2019.2.1
cdalvaro Oct 10, 2019
4585f72
Merge branch 'master' into 2019.2.1
cdalvaro Oct 22, 2019
33a7d23
Merge branch 'master' into 2019.2.1
cdalvaro Oct 24, 2019
b893c54
Merge branch 'master' into 2019.2.1
cdalvaro Oct 28, 2019
22dc6f7
Merge branch 'master' into 2019.2.1
cdalvaro Nov 4, 2019
21ee26a
Merge branch 'master' into 2019.2.1
cdalvaro Nov 10, 2019
8271861
Merge branch 'master' into 2019.2.1
cdalvaro Nov 13, 2019
eff8072
Merge branch 'master' into 2019.2.1
cdalvaro Nov 18, 2019
c51c214
Merge branch 'master' into 2019.2.1
cdalvaro Nov 20, 2019
92da667
Merge branch 'master' into 2019.2.1
cdalvaro Nov 26, 2019
cbd0fca
Merge branch 'master' into 2019.2.1
cdalvaro Dec 2, 2019
9c0b67f
Merge branch 'master' into 2019.2.1
cdalvaro Dec 9, 2019
0dee744
Merge branch 'master' into 2019.2.1
cdalvaro Dec 17, 2019
d48d68d
Merge branch 'master' into 2019.2.1
cdalvaro Dec 22, 2019
e131fe2
Merge branch 'master' into 2019.2.1
cdalvaro Dec 24, 2019
28877b6
bugfix: Solve pylint old-style class warning
cdalvaro Dec 25, 2019
6df87a5
Merge branch '2019.2.1' of github.com:cdalvaro/salt into 2019.2.1
cdalvaro Dec 25, 2019
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
8 changes: 7 additions & 1 deletion salt/modules/cmdmod.py
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,13 @@ def _get_stripped(cmd):

# Ensure environment is correct for a newly logged-in user by running
# the command under bash as a login shell
cmd = '/bin/bash -l -c {cmd}'.format(cmd=_cmd_quote(cmd))
try:
user_shell = __salt__['user.info'](runas)['shell']
if re.search('bash$', user_shell):
cmd = '{shell} -l -c {cmd}'.format(shell=user_shell,
cmd=_cmd_quote(cmd))
except KeyError:
pass

# Ensure the login is simulated correctly (note: su runs sh, not bash,
# which causes the environment to be initialised incorrectly, which is
Expand Down
53 changes: 53 additions & 0 deletions tests/unit/modules/test_cmdmod.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,59 @@ def test_os_environment_remains_intact(self):
if not salt.utils.platform.is_darwin():
getpwnam_mock.assert_called_with('foobar')

@skipIf(not salt.utils.platform.is_darwin(), 'applicable to macOS only')
def test_shell_properly_handled_on_macOS(self):
'''
cmd.run should invoke a new bash login only
when bash is the default shell for the selected user
'''
class _CommandHandler(object):
'''
Class for capturing cmd
'''
def __init__(self):
self.cmd = None

def clear(self):
self.cmd = None

cmd_handler = _CommandHandler()

def mock_proc(__cmd__, **kwargs):
cmd_handler.cmd = ' '.join(__cmd__)
return MagicMock(return_value=MockTimedProc(stdout=None, stderr=None))

with patch('pwd.getpwnam') as getpwnam_mock:
with patch('salt.utils.timed_subprocess.TimedProc', mock_proc):

# User default shell is '/usr/local/bin/bash'
user_default_shell = '/usr/local/bin/bash'
with patch.dict(cmdmod.__salt__,
{'user.info': MagicMock(return_value={'shell': user_default_shell})}):

cmd_handler.clear()
cmdmod._run('ls',
cwd=tempfile.gettempdir(),
runas='foobar',
use_vt=False)

self.assertRegex(cmd_handler.cmd, "{} -l -c".format(user_default_shell),
"cmd invokes right bash session on macOS")

# User default shell is '/bin/zsh'
user_default_shell = '/bin/zsh'
with patch.dict(cmdmod.__salt__,
{'user.info': MagicMock(return_value={'shell': user_default_shell})}):

cmd_handler.clear()
cmdmod._run('ls',
cwd=tempfile.gettempdir(),
runas='foobar',
use_vt=False)

self.assertNotRegex(cmd_handler.cmd, "bash -l -c",
"cmd does not invoke user shell on macOS")

def test_run_cwd_doesnt_exist_issue_7154(self):
'''
cmd.run should fail and raise
Expand Down