Open Source For You — December 2017

(Steven Felgate) #1
How To Admin

http://www.OpenSourceForU.com | OPEN SOURCE FOR YOU | DECEMBER 2017 | 35

Figure 4: Molecule test run

By: Ranajit Jana
The author is a senior architect in service transformation
(open source) at Wipro Technologies. He is interested
in all the technologies related to microservices—
containerisation, monitoring, DevOps, etc. You can contact
him at [email protected]

[1] https://github.com/metacloud/molecule
[2] http://testinfra.readthedocs.io
[3] https://github.com/dj-wasabi/ansible-zabbix-agent
[4] https://blog.opsfactory.rocks/testing-ansible-roles-with-
molecule-97ceca46736a
[5] https://medium.com/@elgallego/ansible-role-testing-
molecule-7a64f43d95cb
[6] http://giovannitorres.me/testing-ansible-roles-with-
molecule-testinfra-and-vagrant.html#installing-
packages-with-pip

References

tests. The file contains assertions as shown in Figure 3.
As can be seen, these assertions are declarative; hence
it requires no effort to add them. The overall test is run with
the Molecule test command as shown in Figure 4. It runs
steps in an opinionated way—cleaning and provisioning the
infrastructure, checking idempotency, testing assertions and
at the end, cleaning resources when the tests are finished.
Molecule can be extended to test complex scenarios and
distributed clustered environments, too, in test machines.
As seen from the steps, Molecule can spin up a Docker
container, do all the testing required and destroy the
container after cleaning the infrastructure.
Figure 3: Sample assertions in test file


The sample Molecule file is shown in Figure 1. One needs
to modify it as per the project’s requirement.

Note: Docker images used for testing need to be
systemd enabled and should have privileges.

Take a look at the playbook. Make sure that you have
made all the changes needed for all the parameters to be
passed to the role.
Now we are ready to write test scripts using TestInfra
(http://testinfra.readthedocs.io/en/latest/).
We need to go to the folder Molecule/default/tests. You
can create a test assertion.
Test assertion is one of the most important steps in a
testing framework. Molecule uses TestInfra (http://testinfra.
readthedocs.io/en/latest) as its test validation framework. The
tests are located in the Python file located at ./molecule/default/

Figure 2: playbook.yml sample file


Visit http://www.electronicsb2b.com


For More


Test and Equipment Stories


Visit http://www.electronicsb2b.com


For More


Test and Equipment Stories


Test and Measurement Stories

Free download pdf