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

Conversation

rochacbruno
Copy link
Member

@rochacbruno rochacbruno commented Sep 5, 2018

Fix #1037

As part of our less boilerplate effort I added a new method sudo argument
to cli.Client.run.

In a lot of test cases there are code like:

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

The idea is to replace the above boilerplate with

client.run(('command_name', 'argument1', ...), sudo=True)

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

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',)

@rochacbruno rochacbruno self-assigned this Sep 5, 2018
@rochacbruno rochacbruno force-pushed the add_sudo_function branch 2 times, most recently from 592a0d6 to a5dcf46 Compare September 5, 2018 13:36
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):
Copy link

@nixocio nixocio Sep 5, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rochacbruno, thanks for this PR.

I have seen this idiom being used.

 sudo = '' if cli.is_root(cls.cfg) else 'sudo '

I have not looked into details your PR, but about the space after sudo like the example above. Did you cover this case as well?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kersommoura The space there is because the use of .split() on a string. As run will accept only a list/tuple it is not a problem, we can replace those calls with just client.sudo.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rochacbruno, great.

Copy link
Contributor

@omaciel omaciel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if the run method accepted an optional sudo flag instead? To me, client.run() is very intuitive, and so is client.sudo() but since they both do the same action but with different "permissions", we could have the same result if we were to change the existing method to something like:

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

Thoughts?

As part of our `less boilerplate` effort I added a new method `sudo` argument
to `cli.Client.run`.

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.run(('command_name', 'argument1', ...), sudo=True)
```

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',)

```
Copy link
Contributor

@omaciel omaciel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK

@rochacbruno rochacbruno changed the title Added a sudo method to cli.Client to avoid boilerplate. Added a sudo argument to cli.Client.run to avoid boilerplate. Sep 6, 2018
@nixocio nixocio merged commit f8a21c3 into pulp:master Sep 6, 2018
@rochacbruno rochacbruno deleted the add_sudo_function branch September 6, 2018 20:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants