From 6d7094fcea532bcfc9d5889ead4743b4b9a8ca81 Mon Sep 17 00:00:00 2001 From: George Shuklin Date: Wed, 7 Apr 2021 17:05:51 +0300 Subject: [PATCH] Add a magical function testinfra_hosts_update to update list of hosts inside module. The key useful scenario is filtering out hosts from automatically generated host lists (f.e. from ansible inventory based on group names). This allows to create a tool to update this list, f.e. to implement 'run_once' policy. --- doc/source/invocation.rst | 6 ++++++ testinfra/plugin.py | 4 ++++ 2 files changed, 10 insertions(+) diff --git a/doc/source/invocation.rst b/doc/source/invocation.rst index 1d744e54..bee788a3 100644 --- a/doc/source/invocation.rst +++ b/doc/source/invocation.rst @@ -35,6 +35,12 @@ You can also set hosts per test module:: [....] +If you need to filter or update generated host list (f.e. after ["ansible://somegroup"]), +you can use testinfra_hosts_update function: + + def testinfra_hosts_update(host_list): + return [host_list[0]] + Parallel execution ~~~~~~~~~~~~~~~~~~ diff --git a/testinfra/plugin.py b/testinfra/plugin.py index bbad24a1..170b26ee 100644 --- a/testinfra/plugin.py +++ b/testinfra/plugin.py @@ -117,6 +117,10 @@ def pytest_generate_tests(metafunc): ansible_inventory=metafunc.config.option.ansible_inventory, force_ansible=metafunc.config.option.force_ansible, ) + if hasattr(metafunc.module, "testinfra_hosts_update"): + if not callable(metafunc.module.testinfra_update.callable): + pytest.fail("testinfra_hosts_update must be a function") + params = metafunc.module.testinfra_hosts_update(params) params = sorted(params, key=lambda x: x.backend.get_pytest_id()) ids = [e.backend.get_pytest_id() for e in params] metafunc.parametrize(