diff --git a/tests/conftest.py b/tests/conftest.py index b8b4e0c15c..88594395ce 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -232,6 +232,11 @@ def pytest_addoption(parser): parser.addoption("--is_parallel_leader", action="store_true", default=False, help="Is the parallel leader") parser.addoption("--parallel_followers", action="store", default=0, type=int, help="Number of parallel followers") + ############################ + # SmartSwitch options # + ############################ + parser.addoption("--dpu-pattern", action="store", default=None, help="dpu host name") + def pytest_configure(config): if config.getoption("enable_macsec"): @@ -365,10 +370,9 @@ def parallel_run_context(request): ) -def get_specified_duts(request): +def get_specified_device_info(request, device_pattern): """ - Get a list of DUT hostnames specified with the --host-pattern CLI option - or -d if using `run_tests.sh` + Get a list of device hostnames specified with the --host-pattern or --dpu-pattern CLI option """ tbname, tbinfo = get_tbinfo(request) testbed_duts = tbinfo['duts'] @@ -376,7 +380,7 @@ def get_specified_duts(request): if is_parallel_run(request): return [get_target_hostname(request)] - host_pattern = request.config.getoption("--host-pattern") + host_pattern = request.config.getoption(device_pattern) if host_pattern == 'all': return testbed_duts @@ -396,6 +400,21 @@ def get_specified_duts(request): return duts +def get_specified_duts(request): + """ + Get a list of DUT hostnames specified with the --host-pattern CLI option + or -d if using `run_tests.sh` + """ + return get_specified_device_info(request, "--host-pattern") + + +def get_specified_dpus(request): + """ + Get a list of DUT hostnames specified with the --dpu-pattern CLI option + """ + return get_specified_device_info(request, "--dpu-pattern") + + def pytest_sessionstart(session): # reset all the sonic_custom_msg keys from cache # reset here because this fixture will always be very first fixture to be called @@ -453,6 +472,45 @@ def duthost(duthosts, request): return duthost +@pytest.fixture(name="dpuhosts", scope="session") +def fixture_dpuhosts(enhance_inventory, ansible_adhoc, tbinfo, request): + """ + @summary: fixture to get DPU hosts defined in testbed. + @param ansible_adhoc: Fixture provided by the pytest-ansible package. + Source of the various device objects. It is + mandatory argument for the class constructors. + @param tbinfo: fixture provides information about testbed. + """ + try: + host = DutHosts(ansible_adhoc, tbinfo, get_specified_dpus(request), + target_hostname=get_target_hostname(request), is_parallel_leader=is_parallel_leader(request)) + return host + except BaseException as e: + logger.error("Failed to initialize dpuhosts.") + request.config.cache.set("dpuhosts_fixture_failed", True) + pt_assert(False, "!!!!!!!!!!!!!!!! dpuhosts fixture failed !!!!!!!!!!!!!!!!" + "Exception: {}".format(repr(e))) + + +@pytest.fixture(scope="session") +def dpuhost(dpuhosts, request): + ''' + @summary: Shortcut fixture for getting DPU host. For a lengthy test case, test case module can + pass a request to disable sh time out mechanis on dut in order to avoid ssh timeout. + After test case completes, the fixture will restore ssh timeout. + @param duthosts: fixture to get DPU hosts + @param request: request parameters for duphost test fixture + ''' + dpu_index = getattr(request.session, "dpu_index", 0) + assert dpu_index < len(dpuhosts), \ + "DPU index '{0}' is out of bound '{1}'".format(dpu_index, + len(dpuhosts)) + + duthost = dpuhosts[dpu_index] + + return duthost + + @pytest.fixture(scope="session") def mg_facts(duthost): return duthost.minigraph_facts(host=duthost.hostname)['ansible_facts']