Add a magical function testinfra_hosts_update to update host list in runtime#609
Add a magical function testinfra_hosts_update to update host list in runtime#609amarao wants to merge 1 commit intopytest-dev:mainfrom
Conversation
b9ee620 to
a3e2537
Compare
testinfra/plugin.py
Outdated
| 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) |
There was a problem hiding this comment.
This line should also be under the if no ?
There was a problem hiding this comment.
oops. Thank you! I've cleanup code and missed that.
Fixed.
| force_ansible=metafunc.config.option.force_ansible, | ||
| ) | ||
| if hasattr(metafunc.module, "testinfra_hosts_update"): | ||
| if not callable(metafunc.module.testinfra_update.callable): |
There was a problem hiding this comment.
Shouldn't this be callable(metafunc.module.testinfra_hosts_update) ?
There was a problem hiding this comment.
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.
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.
a3e2537 to
6d7094f
Compare
I thought about it. I really would like ability to specify the Actually some kind of hook to process the hostlist after it was parsed and processed by Ansible is really welcomed. Ansible inventory is really tricky thing (given the molecule control over inventory), so ability to tweak results are important, I think. The magicalily of |
This adds a support for optional magical function
testinfra_hosts_updateto each module, which allows to filter host list after it was created bytestinfra.get_hostsbefore doing an actual test parametrization. Practially, it's allow to filter/update/mangle host list after it was become a list of concrete hosts.The issue it tries to solve is that
testinfra_hostswhen used in tests for molecule/ansible environment usually contains group names (as host names are volatile in many molecule usecases), and some tests may requires to be "run_once" for a given group.The practical scenario is like this (mons is a groups for Ceph monitors):