lundi 1 janvier 2018

Testing multiple vagrant-boxes using TestInfra

I have a little project to learn about ansible and testing. My setup is:

I've defined 3 different vagrant-boxes (basically a webserver-, database and dns/logging-box). Every box gets provisioned by a locally installed ansible-instance. I want to test these boxes using Testinfra. For this, I made a folder with tests for every box. Since different configurations are done for different boxes, I want to run the test "test_webserver.py" only on the webserver-box, the "test_database.py" only on the database-box. Now I found a mechanism that should adress this issue in the testinfra-docs: here

You can also set hosts per test module:

testinfra_hosts = ["localhost", "root@webserver:2222"]

def test_foo(host): [....]

I tried to implement this, e.g. with this simple test-file:

testinfra_hosts = ["vagrant@192.168.100.102"]

def test_nginx_running(host):
    webserver_running = host.service("nginx")
    assert webserver_running

def test_web_port_listening(host):
    web_port = host.socket("tcp://0.0.0.0:80")
    assert web_port.is_listening

Now, what I want to do is: Just run one single command, to execute all the tests, similar to this:

py.test [...] --hosts=dns,web,db test/

At the moment, if I run this command, despite having defined the specific testinfra_hosts-variable in every test-file, every test gets run on all of the vagrant-boxes. My question is now, is there a clean way, to run every test only on a specified boxes? Ideally I would like to have one simple py.test-call and everything else inside of the appropriate files. Since I play to add more capabilities to those boxes, it would be really good, to have a clean seperation.

Thank you :).

Aucun commentaire:

Enregistrer un commentaire