Testing Ansible With Molecule

My colleague was recently assigned a task to create tests for an ansible role that she works on. She pinged me for help and we got started in figuring out what to do.

The first thing we attempted was to run tests inside docker using Ansible following the instructions in an Ansible.com blog post. The idea was we would run the role we wanted to test. Then run a second test playbook that would do a couple of asserts. I was stuck here for a bit for various reasons. The containers that are used in the blog post have not been updated in over a year. And we ran into some trouble trying to find a container with systemd running inside that was also public. The right way to do that would be to generate the container using a Dockerfile on the fly and run tests inside them. That was okay with me, but it added more complexity.

For two days or so, I briefly looked at the idea of doing this in VMs generated on the fly, but it added way too much overhead. Michael, my colleague, pointed me to molecule. His team has been using it regularly, though he himself hasn’t looked at it.

Molecule is an interesting project. It seems to do what I need, but there isn’t spectacular documentation on how to use it for a project that already exists. There are ascii videos, but I’m a fan of reading more than watching. Getting molecule to work on Fedora 28 was a bit of a pain. Ansible needs libselinux-python to work with Docker on a host that has selinux enabled. Now, you can’t installed libselinux-python from pip. It has to be installed from packages. I tried installing it in a virtualenv with site packages and installing molecule from packages, both of them failed in interesting ways that I’m yet to debug.

Eventually, I gave up and created a Centos 7 VM for this. A virtualenv with site packages actually worked inside my Centos 7 VM. This is great news, because this is the sort environment I expect to run molecule. The bit I really like about molecule is that it takes care of the harness and I can write asserts in Python. The tests will actually look like what Python tests look like. The bit I don’t like is that its documentation isn’t as thorough as I’d like. I plan to submit a pull request for the docs for a full flow on how to write tests with molecule. I found various blog posts on the internet that were far helpful. It took some guess work to realize that testinfra is it’s own python module and I should be looking at the module for documentation on how to write my own asserts. This is still a work in progress, but I expect a lot of our ansible pieces will end up being better tested now that we have this in place.


Posted

in

by

Tags:

Comments

Leave a Reply