Skip to content

Commit b9ee620

Browse files
committed
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.
1 parent 468f1d0 commit b9ee620

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

doc/source/invocation.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ You can also set hosts per test module::
3535
[....]
3636

3737

38+
If you need to filter or update generated host list (f.e. after ["ansible://somegroup"]),
39+
you can use testinfra_hosts_update function:
40+
41+
def testinfra_hosts_update(host_list):
42+
return [host_list[0]]
43+
3844

3945
Parallel execution
4046
~~~~~~~~~~~~~~~~~~

testinfra/plugin.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,10 @@ def pytest_generate_tests(metafunc):
117117
ansible_inventory=metafunc.config.option.ansible_inventory,
118118
force_ansible=metafunc.config.option.force_ansible,
119119
)
120+
if hasattr(metafunc.module, "testinfra_hosts_update"):
121+
if not metafunc.module.testinfra_update.callable():
122+
pytest.fail("testinfra_hosts_update must be a function")
123+
params = metafunc.module.testinfra_hosts_update(params)
120124
params = sorted(params, key=lambda x: x.backend.get_pytest_id())
121125
ids = [e.backend.get_pytest_id() for e in params]
122126
metafunc.parametrize(

0 commit comments

Comments
 (0)