diff --git a/tests/common/fixtures/conn_graph_facts.py b/tests/common/fixtures/conn_graph_facts.py index a965da21bd8..ac7b3440ecb 100644 --- a/tests/common/fixtures/conn_graph_facts.py +++ b/tests/common/fixtures/conn_graph_facts.py @@ -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 diff --git a/tests/conftest.py b/tests/conftest.py index de7f0c7ea01..eb58b40e386 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -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" + pytest --inventory inventory1,inventory2 + + 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): """