Skip to content

Commit

Permalink
Merge pull request #54956 from aplanas/backport_50302
Browse files Browse the repository at this point in the history
(Backport 50302) cmd: Add root parameter for wait and run states
  • Loading branch information
dwoz authored Dec 11, 2019
2 parents 83d859d + 5947392 commit 8c2cde3
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
18 changes: 15 additions & 3 deletions salt/states/cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,7 @@ def wait(name,
unless=None,
creates=None,
cwd=None,
root=None,
runas=None,
shell=None,
env=(),
Expand Down Expand Up @@ -437,6 +438,10 @@ def wait(name,
The current working directory to execute the command in, defaults to
/root
root
Path to the root of the jail to use. If this parameter is set, the command
will run inside a chroot
runas
The user name to run the command as
Expand Down Expand Up @@ -677,6 +682,7 @@ def run(name,
unless=None,
creates=None,
cwd=None,
root=None,
runas=None,
shell=None,
env=None,
Expand Down Expand Up @@ -710,6 +716,10 @@ def run(name,
The current working directory to execute the command in, defaults to
/root
root
Path to the root of the jail to use. If this parameter is set, the command
will run inside a chroot
runas
The user name to run the command as
Expand Down Expand Up @@ -887,6 +897,7 @@ def run(name,

cmd_kwargs = copy.deepcopy(kwargs)
cmd_kwargs.update({'cwd': cwd,
'root': root,
'runas': runas,
'use_vt': use_vt,
'shell': shell or __grains__['shell'],
Expand Down Expand Up @@ -917,10 +928,11 @@ def run(name,

# Wow, we passed the test, run this sucker!
try:
cmd_all = __salt__['cmd.run_all'](
name, timeout=timeout, python_shell=True, **cmd_kwargs
run_cmd = 'cmd.run_all' if not root else 'cmd.run_chroot'
cmd_all = __salt__[run_cmd](
cmd=name, timeout=timeout, python_shell=True, **cmd_kwargs
)
except CommandExecutionError as err:
except Exception as err:
ret['comment'] = six.text_type(err)
return ret

Expand Down
23 changes: 23 additions & 0 deletions tests/unit/states/test_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,29 @@ def test_run(self):
'skip_watch': True})
self.assertDictEqual(cmd.run(name, onlyif=''), ret)

def test_run_root(self):
'''
Test to run a command with a different root
'''
name = 'cmd.script'

ret = {'name': name,
'result': False,
'changes': {},
'comment': ''}

with patch.dict(cmd.__grains__, {'shell': 'shell'}):
with patch.dict(cmd.__opts__, {'test': False}):
mock = MagicMock(side_effect=[CommandExecutionError,
{'retcode': 1}])
with patch.dict(cmd.__salt__, {'cmd.run_chroot': mock}):
ret.update({'comment': '', 'result': False})
self.assertDictEqual(cmd.run(name, root='/mnt'), ret)

ret.update({'comment': 'Command "cmd.script" run',
'result': False, 'changes': {'retcode': 1}})
self.assertDictEqual(cmd.run(name, root='/mnt'), ret)

# 'script' function tests: 1

def test_script(self):
Expand Down

0 comments on commit 8c2cde3

Please sign in to comment.