Skip to content

Commit

Permalink
Added a sudo method to cli.Client to avoid boilerplate.
Browse files Browse the repository at this point in the history
As part of our `less boilerplate` effort I added a new method `sudo`
to `cli.Client`.

In a lot of test cases there are code like:

```python
sudo = () if cli.is_root(cfg) else ('sudo',)
...
client.run(sudo + ('command_name', 'argument1', ...)
```

The idea is to replace the above boilerplate with

```python
client.sudo('command_name', 'argument1', ...)
```

The method name is explicly sayig what it does as `sudo` stands for `SUperuser` + `DO` and it follows the same naming seeing on other projects like Fabric and Ansible (internak modules implementation).

With this change we will be able to remove the `sudo = () if cli.is_root(cfg) else ('sudo',)` boilerplate in those Pulp-2-tests referenced:

```bash

pulp_2_tests/tests/docker/utils.py:    sudo = '' if cli.is_root(cfg) else 'sudo'
pulp_2_tests/tests/puppet/api_v2/test_install_distributor.py:        sudo = () if cli.is_root(self.cfg) else ('sudo',)
pulp_2_tests/tests/rpm/api_v2/test_content_sources.py:        sudo = () if cli.is_root(cls.cfg) else ('sudo',)
pulp_2_tests/tests/rpm/api_v2/test_content_sources.py:        sudo = () if cli.is_root(cls.cfg) else ('sudo',)
pulp_2_tests/tests/rpm/api_v2/test_content_sources.py:    sudo = '' if cli.is_root(cfg) else 'sudo'
pulp_2_tests/tests/rpm/api_v2/test_crud.py:        sudo = () if cli.is_root(self.cfg) else ('sudo',)
pulp_2_tests/tests/rpm/api_v2/test_download_policies.py:        sudo = '' if cli.is_root(cls.cfg) else 'sudo '
pulp_2_tests/tests/rpm/api_v2/test_errata.py:        # sudo = () if cli.is_root(cfg) else ('sudo',)
pulp_2_tests/tests/rpm/api_v2/test_errata.py:        sudo = () if cli.is_root(cfg) else ('sudo',)
pulp_2_tests/tests/rpm/api_v2/test_export.py:        self.__sudo = None
pulp_2_tests/tests/rpm/api_v2/test_export.py:            self.__sudo = '' if cli.is_root(self.cfg) else 'sudo '
pulp_2_tests/tests/rpm/api_v2/test_iso_sync_publish.py:        sudo = () if cli.is_root(self.cfg) else ('sudo',)
pulp_2_tests/tests/rpm/api_v2/test_modularity.py:        sudo = () if cli.is_root(cfg) else ('sudo',)
pulp_2_tests/tests/rpm/api_v2/test_republish.py:        sudo = () if cli.is_root(self.cfg) else ('sudo',)
pulp_2_tests/tests/rpm/api_v2/test_rich_weak_dependencies.py:        sudo = () if cli.is_root(cfg) else ('sudo',)
pulp_2_tests/tests/rpm/api_v2/test_rsync_distributor.py:        sudo = () if cli.is_root(cfg) else ('sudo',)
pulp_2_tests/tests/rpm/api_v2/test_rsync_distributor.py:        sudo = () if cli.is_root(cfg) else ('sudo',)
pulp_2_tests/tests/rpm/api_v2/test_rsync_distributor.py:        sudo = '' if cli.is_root(cfg) else 'sudo '
pulp_2_tests/tests/rpm/api_v2/test_service_resiliency.py:        sudo = '' if cli.is_root(self.cfg) else 'sudo'
pulp_2_tests/tests/rpm/api_v2/test_service_resiliency.py:        sudo = () if cli.is_root(self.cfg) else ('sudo',)
pulp_2_tests/tests/rpm/api_v2/utils.py:        sudo = '' if cli.is_root(cfg) else 'sudo '
pulp_2_tests/tests/rpm/api_v2/utils.py:        sudo = '' if cli.is_root(cfg) else 'sudo '
pulp_2_tests/tests/rpm/api_v2/utils.py:        sudo = () if cli.is_root(cfg) else ('sudo',)
pulp_2_tests/tests/rpm/api_v2/utils.py:        sudo = '' if cli.is_root(cfg) else 'sudo '
pulp_2_tests/tests/rpm/api_v2/utils.py:    sudo = () if cli.is_root(cfg) else ('sudo',)
pulp_2_tests/tests/rpm/cli/test_copy_units.py:        sudo = () if cli.is_root(cfg) else ('sudo',)
pulp_2_tests/tests/rpm/cli/test_process_recycling.py:    sudo = () if cli.is_root(cfg) else ('sudo',)
pulp_2_tests/tests/rpm/cli/test_process_recycling.py:        sudo = () if cli.is_root(cfg) else ('sudo',)

```
  • Loading branch information
rochacbruno committed Sep 5, 2018
1 parent 52561d9 commit 592a0d6
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
14 changes: 14 additions & 0 deletions pulp_smash/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,8 @@ def __init__(self, cfg, response_handler=None, pulp_host=None):
else:
self.response_handler = response_handler

self.cfg = cfg

def run(self, args, **kwargs):
"""Run a command and ``return self.response_handler(result)``.
Expand All @@ -234,6 +236,18 @@ def run(self, args, **kwargs):
completed_process = CompletedProcess(args, code, stdout, stderr)
return self.response_handler(completed_process)

def sudo(self, args, **kwargs):
"""Run a command as superuser.
This method is a wrapper around :meth:`pulp_smash.cli.Client.run`
which prior to execution checks if user is root and if not
adds ``sudo`` as the first argument of the command.
"""
if args[0] != 'sudo' and not is_root(self.cfg):
args = list(args)
args.insert(0, 'sudo')
return self.run(tuple(args), **kwargs)


class BaseServiceManager(metaclass=ABCMeta):
"""A base service manager.
Expand Down
13 changes: 13 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,19 @@ def test_explicit_pulp_host(self):
plumbum.machines.SshMachine.assert_called_once_with(
cfg.hosts[1].hostname)

def test_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, 'run') as run:
client.sudo(('ls',))
run.assert_called_once_with(('sudo', 'ls'))


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

0 comments on commit 592a0d6

Please sign in to comment.