Fixtures for testinfra and molecule
Project description
About
With the Pytest plugin Testinfra you can write unit tests in Python to test your servers configured by the management tool Ansible. Testinfra is the default verifier of Molecule testing environment.
The Pytest plugin Testaid provides helper functions and fixtures to facilitate the use of Testinfra. It helps to not only unit test your Ansible roles but to integration and system test your whole Ansible project.
License
Quick start
Install the testaid plugin using pip:
$ pip install testaid
Tests
Run molecule test by invoking tox:
$ tox
Example
Have a look at test/debian for a complete example of a molecule project using ansible, testinfra and testaid. The molecule project doubles as a test for the testaid plugin.
Boilerplate
As a boilerplate for testinfra tests it is enough to do:
import testaid
testinfra_hosts = testaid.hosts()
Fixture testpass
You can access gopass secrets by using the testpass fixture:
def test_mytest(host, testpass):
my_password = testpass('my_project/my_password')
Fixture testvars
Arguably the most useful feature of the testaid plugin is the testvars fixture. The fixture exposes and resolves the role defaults as well as the project variables as a dictionary:
def test_mytest(host, testvars):
my_password = testpass['my_variable']
Internally, the fixture uses the Ansible debug module to resolve templates. Thus, it can resolve any kind of template that the debug module can resolve including jinja2 code and invoking lookup plugins.
As resolving the templates is very slow the fixture will cache the results using the Pytest cache plugin. This allows fast test-driven development but remember to clear the cache when you add or change an Ansible variable:
pytest --cache-clear; molecule verify
The cache will use the molecule ephemeral directory as the cache key which is unique for each molecule instance. When using the boilerplate you can inspect the cache by running:
pytest --cache-show
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.