Skip to content

Add a magical function testinfra_hosts_update to update host list in runtime #609

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions doc/source/invocation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
~~~~~~~~~~~~~~~~~~
Expand Down
4 changes: 4 additions & 0 deletions testinfra/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be callable(metafunc.module.testinfra_hosts_update) ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I fixed bad offset from comment above, now it looks like this:

        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)

The logic is that we complains if there is something called testinfra_hosts_update but it's not 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(
Expand Down