I’ve been looking at improving the quality of the testing I do with Molecule and Testinfra. Simple checks like service.is_running or package.is_installed have their place but they’re pretty limited as to what assurances they provide us. Part of the issue I have is that some tests need a fair bit of setup to make them worthwhile. Attempting to tackle that with raw Python might be a little bit tricky. A better approach is to use the Ansible module available within TestInfra. We can call an ansible module with the following Python code:

ansible = host.ansible(module name,
                       module arguments,
                       check mode,

Some of the stuff I do involves Nagios Plugins that parse log files for interesting content. I can test the the plugin is doing what it’s supposed to by setting log file content and then running the plugin and checking the output. Here’s an example of that…

def test_nagios_plugin_test(host):
    with host.sudo():
        cmd = host.run("rm -f /var/log/mylogfile.log")
        assert cmd.rc == 0

    with host.sudo("reaper"):
        ansible = host.ansible("copy",
                               "src=dummy_log_content.log dest=/var/log/mylogfile.log",
        assert ansible['changed'] == True

    with host.sudo("nrpe"):
        cmd = host.run("/usr/lib64/nagios/plugins/check_logfile_custom")
        assert cmd.rc == 0
        assert cmd.stdout.strip() == "OK: All looks good in /var/log/mylogfile.log"

The dummy_log_content.log is placed into the roles molecule/default/files directory. By providing a range of dummy log files I can ensure the Nagios Plugin is working as expected.