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

Documentation & requirements-test.txt as test requirements file #216

Merged
merged 2 commits into from
Aug 22, 2023
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
24 changes: 20 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,22 @@ commands = true

Used without caution, this configuration can result in unexpected behavior, and possible false positive or false negative test results.

## Passing command line arguments to ansible-test / pytest

The behavior of the `ansible-test` (for `sanity-*` environments) or `pytest` (for `unit-*` and `integration-*` environments) commands can be customized by passing further command line arguments to it, e.g., by invoking `tox` like this:

```bash
tox -f sanity --ansible --conf tox-ansible.ini -- --test validate-modules -vvv
```

The arguments after the `--` will be passed to the `ansible-test` command. Thus in this example only the `validate-modules` sanity test will run, but with an increased verbosity.

Same can be done to pass arguments to the `pytest` commands for the `unit-*` and `integration-*` environments:

```bash
tox -e unit-py3.11-2.15 --ansible --conf tox-ansible.ini -- --junit-xml=tests/output/junit/unit.xml
```

## Usage in a CI/CD pipeline

The repo contains a github workflow that can be used in a github actions CI/CD pipeline. The workflow will run all tests across all available environments, unless limited by the `skip` option in the `tox-ansible.ini` file.
Expand Down Expand Up @@ -191,7 +207,7 @@ This can be accomplished by presenting molecule scenarios as integration tests a

Assuming the following collection directory structure:

```
```bash
namespace.name
├── extensions
│ ├── molecule
Expand All @@ -216,10 +232,10 @@ namespace.name
│ ├── action
│ │ └── action_plugin.py
│ ├── modules
│ │ └── action_plugin.py
│ │ └── module.py
├── tests
│ ├── integration
| | ── targets
── targets
│ │ │ ├── success
│ │ │ │ └── tasks
│ │ │ │ └── main.yaml
Expand Down Expand Up @@ -256,7 +272,7 @@ This approach provides the flexibility of running the `molecule` scenarios direc

## How does it work?

`tox` will, by default, create a python virtual environment for a given environment. `tox-ansible` adds ansible collection specific build and test logic to tox. The collection is copied into the virtual environment created by tox, built, and installed into the virtual environment. The installation of the collection will include the collection's collection dependencies. `tox-ansible` will also install any python dependencies from a `test-requirements.txt` and `requirements.txt` file. The virtual environment's temporary directory is used, so the copy, build and install steps are performed with each test run ensuring the current collection code is used.
`tox` will, by default, create a python virtual environment for a given environment. `tox-ansible` adds ansible collection specific build and test logic to tox. The collection is copied into the virtual environment created by tox, built, and installed into the virtual environment. The installation of the collection will include the collection's collection dependencies. `tox-ansible` will also install any python dependencies from a `test-requirements.txt` (or `requirements-test.txt`) and `requirements.txt` file. The virtual environment's temporary directory is used, so the copy, build and install steps are performed with each test run ensuring the current collection code is used.

`tox-ansible` also sets the `ANSIBLE_COLLECTIONS_PATH` environment variable to point to the virtual environment's temporary directory. This ensures that the collection under test is used when running tests. The `pytest-ansible-units` pytest plugin injects the `ANSIBLE_COLLECTIONS_PATH` environment variable into the collection loader so ansible-core can locate the collection.

Expand Down
5 changes: 5 additions & 0 deletions src/tox_ansible/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,11 @@ def conf_deps(env_conf: EnvConfigSet, test_type: str) -> str:
deps.extend(fileh.read().splitlines())
except FileNotFoundError:
pass
try:
with (TOX_WORK_DIR / "requirements-test.txt").open() as fileh:
deps.extend(fileh.read().splitlines())
except FileNotFoundError:
pass
try:
with (TOX_WORK_DIR / "requirements.txt").open() as fileh:
deps.extend(fileh.read().splitlines())
Expand Down