Skip to content

Commit

Permalink
Add ansible_test_platform option to tox.ini (#131)
Browse files Browse the repository at this point in the history
  • Loading branch information
maxhoesel authored May 23, 2022
1 parent 55b129c commit 0b8d4eb
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 7 deletions.
28 changes: 25 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,15 @@ ansible-test
This plugin saves you this trouble by allowing you the freedom to run
these commands tranparently. For example you can run `tox -e sanity` which
will install the collection, change current directory and execute
`ansible-test sanity --python X.Y`. You can even add posargs that endup being
`ansible-test sanity --python X.Y`. You can even add posargs that end up being
passed to the executed command, like `tox -e sanity -- --help`.

By default tox-ansible will also limit execution of ansible-test to the
current python version used by tox.
By default, tox-ansible will also limit execution of ansible-test to the
current python version used by tox. In addition, tox-ansible will try to
automatically determine the best environment to run these tests in
(docker if present on the host, virtualenv, etc.).

You can disable this auto-detection feature in the `tox.ini`, see below for details.

```shell
$ tox -va
Expand All @@ -32,6 +36,7 @@ sanity -> Auto-generated for: ansible-test sanity
Only those enviroments that are detected will be listed. At least sanity will
always be visible as it does not require adding new files.


More details
------------

Expand Down Expand Up @@ -168,6 +173,23 @@ environments specified in the config file. To suppress it, specify at least one
in the envlist entry within tox.ini

### tox.ini examples
The `ansible_test_platform` option controls the platform (docker, venv, python version) that ansible-test targets run in.
By default, this is set to `auto` for automatic detection. You can also set this option
to `docker` or `venv` explicitly, or disable the Python and platform auto-detection by setting
this option to `posargs`:

```ini
[ansible]
ansible_test_platform = posargs # Disable auto-detection
```

Note that if you do this, you will have to add your own platform parameters to ansible-test via posargs,
as discussed above. For example, to use a separate container for the controller and target hosts,
you can use the following command:

`$ tox -e integration -- --controller docker:default --target docker:centos7`


To add arguments to every molecule invocation, add the
following segment to tox.ini. Each argument needs to be on a separate line, which allows
spaces and other special characters to be used without needing shell-style escapes:
Expand Down
15 changes: 11 additions & 4 deletions src/tox_ansible/ansible/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,15 @@ def tox_cases(self):
# pylint: disable=fixme
# TODO(ssbarnea): Detect and enable only those tests that do exist
# to avoid confusing tox user.

if use_docker():
docker_present = use_docker()
platform = self.options.ansible_test_platform
if (platform == "auto" and docker_present) or platform == "docker":
opts = ["--docker", "default"]
else:
elif platform in ("auto", "venv"):
opts = ["--venv"]
else:
opts = []

# We use --venv because otherwise we risk getting errors from the
# system environment, especially as one of tests performed is
# 'pip check'.
Expand Down Expand Up @@ -169,7 +173,10 @@ def tox_cases(self):
value["args"] = copy.deepcopy(self.tox.posargs)
else:
value["args"].extend(self.tox.posargs)
if "--python" not in self.tox.posargs:
if (
"--python" not in self.tox.posargs
and self.options.ansible_test_platform == "auto"
):
value["args"].extend(
["--python", f"{sys.version_info[0]}.{sys.version_info[1]}"]
)
Expand Down
16 changes: 16 additions & 0 deletions src/tox_ansible/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
INI_SECTION = "ansible"
INI_PYTHON_VERSIONS = "python"
INI_ANSIBLE_VERSIONS = "ansible"
INI_ANSIBLE_TEST_PLATFORM = "ansible_test_platform"
INI_ANSIBLE_TEST_PLATFORM_DEFAULT = "auto"
INI_ANSIBLE_TEST_PLATFORM_CHOICES = ["auto", "posargs", "docker", "venv"]
INI_MOLECULE_GLOBAL_OPTS = "molecule_opts"
INI_IGNORE_PATHS = "ignore_path"
INI_ANSIBLE_LINT_CONFIG = "ansible_lint_config"
Expand Down Expand Up @@ -50,6 +53,19 @@ def __init__(self, tox):
ansible = _split_env(ansible)
if ansible:
self.matrix.add_axis(AnsibleAxis(ansible))
self.ansible_test_platform = self.reader.getstring(
INI_ANSIBLE_TEST_PLATFORM, INI_ANSIBLE_TEST_PLATFORM_DEFAULT
)
if self.ansible_test_platform not in INI_ANSIBLE_TEST_PLATFORM_CHOICES:
raise ValueError(
"Invalid value for 'ansible_test_platform': %s. "
"Supported values are: %s"
% (
self.ansible_test_platform,
", ".join(INI_ANSIBLE_TEST_PLATFORM_CHOICES),
)
)

pythons = self.reader.getlist(INI_PYTHON_VERSIONS)
pythons = _split_env(pythons)
if pythons:
Expand Down
1 change: 1 addition & 0 deletions tests/test_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ def opts(mocker):
reader = mocker.Mock()
c.get_reader.return_value = reader
reader.getlist.return_value = ["2.10", "3.9"]
reader.getstring.return_value = "auto"
opts = Options(c)
return opts

Expand Down
1 change: 1 addition & 0 deletions tests/test_tox_molecule_case.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def opts(mocker):
reader = mocker.Mock()
config.get_reader.return_value = reader
reader.getlist.return_value = ["2.10", "3.9"]
reader.getstring.return_value = "auto"
return Options(config)


Expand Down

0 comments on commit 0b8d4eb

Please sign in to comment.