Skip to content
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

Add support of specifying multiple inventory files #1645

Merged
merged 1 commit into from
May 7, 2020
Merged
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
20 changes: 16 additions & 4 deletions tests/common/fixtures/conn_graph_facts.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,21 @@ def conn_graph_facts(duthost, testbed_devices):
if os.path.exists(inv_mapping_file):
with open(inv_mapping_file) as fd:
inv_map = json.load(fd)
inv_file = duthost.host.options['inventory'].split('/')[-1]
if inv_map and inv_file in inv_map:
lab_conn_graph_file = os.path.join(base_path, "../../../ansible/files/{}".format(inv_map[inv_file]))
inv_opt = duthost.host.options['inventory']
inv_files = []
if isinstance(inv_opt, str):
inv_files = (duthost.host.options['inventory']) # Make it iterable for later use
elif isinstance(inv_opt, list) or isinstance(inv_opt, tuple):
inv_files = duthost.host.options['inventory']

conn_graph_facts = localhost.conn_graph_facts(host=duthost.hostname, filename=lab_conn_graph_file)['ansible_facts']
for inv_file in inv_files:
inv_file = os.path.basename(inv_file)

# Loop through the list of inventory files supplied in --inventory argument.
# For the first inventory file that has a mapping in inv_mapping.json, return
# its conn_graph_facts.
if inv_map and inv_file in inv_map:
lab_conn_graph_file = os.path.join(base_path, "../../../ansible/files/{}".format(inv_map[inv_file]))
conn_graph_facts = localhost.conn_graph_facts(host=duthost.hostname, filename=lab_conn_graph_file)['ansible_facts']
return conn_graph_facts
return conn_graph_facts
22 changes: 22 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,28 @@ def pytest_addoption(parser):
help="number of minutes for show techsupport command")


@pytest.fixture(scope="session", autouse=True)
def enhance_inventory(request):
"""
This fixture is to enhance the capability of parsing the value of pytest cli argument '--inventory'.
The pytest-ansible plugin always assumes that the value of cli argument '--inventory' is a single
inventory file. With this enhancement, we can pass in multiple inventory files using the cli argument
'--inventory'. The multiple inventory files can be separated by comma ','.

For example:
pytest --inventory "inventory1, inventory2" <other arguments>
pytest --inventory inventory1,inventory2 <other arguments>

This fixture is automatically applied, you don't need to declare it in your test script.
"""
inv_opt = request.config.getoption("ansible_inventory")
inv_files = [inv_file.strip() for inv_file in inv_opt.split(",")]
try:
setattr(request.config.option, "ansible_inventory", inv_files)
except AttributeError:
logger.error("Failed to set enhanced 'ansible_inventory' to request.config.option")


@pytest.fixture(scope="session")
def testbed(request):
"""
Expand Down