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

Added a sudo argument to cli.Client.run to avoid boilerplate. #1132

Merged
merged 1 commit into from
Sep 6, 2018
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
11 changes: 10 additions & 1 deletion pulp_smash/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,14 +213,20 @@ def __init__(self, cfg, response_handler=None, pulp_host=None):
else:
self.response_handler = response_handler

def run(self, args, **kwargs):
self.cfg = cfg

def run(self, args, sudo=False, **kwargs):
"""Run a command and ``return self.response_handler(result)``.

This method is a thin wrapper around Plumbum's `BaseCommand.run`_
method, which is itself a thin wrapper around the standard library's
`subprocess.Popen`_ class. See their documentation for detailed usage
instructions. See :class:`pulp_smash.cli.Client` for a usage example.

:param args: Any arguments to be passed to the process (a tuple).
:param sudo: If the command should run as superuser (a boolean).
:param kwargs: Extra named arguments passed to plumbumBaseCommand.run.

.. _BaseCommand.run:
http://plumbum.readthedocs.io/en/latest/api/commands.html#plumbum.commands.base.BaseCommand.run
.. _subprocess.Popen:
Expand All @@ -230,6 +236,9 @@ def run(self, args, **kwargs):
# https://plumbum.readthedocs.io/en/latest/api/commands.html#plumbum.commands.base.BaseCommand.run
kwargs.setdefault('retcode')

if sudo and args[0] != 'sudo' and not is_root(self.cfg):
args = ('sudo',) + tuple(args)

code, stdout, stderr = self.machine[args[0]].run(args[1:], **kwargs)
completed_process = CompletedProcess(args, code, stdout, stderr)
return self.response_handler(completed_process)
Expand Down
52 changes: 52 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,58 @@ def test_explicit_pulp_host(self):
plumbum.machines.SshMachine.assert_called_once_with(
cfg.hosts[1].hostname)

def test_run(self):
"""Test run commands."""
cfg = _get_pulp_smash_config(hosts=[
config.PulpHost(
hostname=socket.getfqdn(),
roles={'shell': {}},
)
])
client = cli.Client(cfg)
with mock.patch.object(client, 'machine') as machine:

machine.__getitem__.return_value = machine
machine.run.return_value = (0, 'ok', None)

result = client.run(('ls', '-la'))

# Internal call is: `machine[args[0]].run(args[1:], **kwargs)`
# So assert `machine['ls']` is called
machine.__getitem__.assert_called_once_with('ls')
# then `.run(('-la',), **kwargs)`
machine.run.assert_called_once_with(('-la',), retcode=None)

self.assertEqual(result.returncode, 0)
self.assertEqual(result.stdout, 'ok')
self.assertIsNone(result.stderr)

def test_run_as_sudo(self):
"""Test run commands as sudo."""
cfg = _get_pulp_smash_config(hosts=[
config.PulpHost(
hostname=socket.getfqdn(),
roles={'shell': {}},
)
])
client = cli.Client(cfg)
with mock.patch.object(client, 'machine') as machine:

machine.__getitem__.return_value = machine
machine.run.return_value = (0, 'ok', None)

result = client.run(('ls', '-la'), sudo=True)

# Internal call is: `machine[args[0]].run(args[1:], **kwargs)`
# So assert `machine['sudo']` is called
machine.__getitem__.assert_called_once_with('sudo')
# then `.run(('ls', '-la'), **kwargs)`
machine.run.assert_called_once_with(('ls', '-la'), retcode=None)

self.assertEqual(result.returncode, 0)
self.assertEqual(result.stdout, 'ok')
self.assertIsNone(result.stderr)


class IsRootTestCase(unittest.TestCase):
"""Tests for :class:`pulp_smash.cli.is_root`."""
Expand Down