diff --git a/tests/conftest.py b/tests/conftest.py index dcb93f44a1b..fd5fc45d799 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -50,10 +50,13 @@ from tests.platform_tests.args.normal_reboot_args import add_normal_reboot_args from ptf import testutils # lgtm[py/unused-import] from tests.common.config_reload import config_reload +from tests.common.helpers.assertions import pytest_assert as pt_assert logger = logging.getLogger(__name__) cache = FactsCache() +DUTHOSTS_FIXTURE_FAILED_RC = 15 + pytest_plugins = ('tests.common.plugins.ptfadapter', 'tests.common.plugins.ansible_fixtures', 'tests.common.plugins.dut_monitor', @@ -272,6 +275,11 @@ def get_specified_duts(request): return duts +def pytest_sessionfinish(session, exitstatus): + if session.config.cache.get("duthosts_fixture_failed", None): + session.config.cache.set("duthosts_fixture_failed", None) + session.exitstatus = DUTHOSTS_FIXTURE_FAILED_RC + @pytest.fixture(name="duthosts", scope="session") def fixture_duthosts(enhance_inventory, ansible_adhoc, tbinfo, request): @@ -282,7 +290,14 @@ def fixture_duthosts(enhance_inventory, ansible_adhoc, tbinfo, request): mandatory argument for the class constructors. @param tbinfo: fixture provides information about testbed. """ - return DutHosts(ansible_adhoc, tbinfo, get_specified_duts(request)) + try: + host = DutHosts(ansible_adhoc, tbinfo, get_specified_duts(request)) + return host + except BaseException as e: + logger.error("Failed to initialize duthosts.") + request.config.cache.set("duthosts_fixture_failed", True) + pt_assert(False, "!!!!!!!!!!!!!!!! duthosts fixture failed !!!!!!!!!!!!!!!!" + "Exception: {}".format(repr(e))) @pytest.fixture(scope="session") diff --git a/tests/run_tests.sh b/tests/run_tests.sh index ebe7ced1543..8b61e5c2320 100755 --- a/tests/run_tests.sh +++ b/tests/run_tests.sh @@ -309,6 +309,11 @@ function run_individual_tests() echo "=== Sanity check failed for $test_script. Skip rest of the scripts if there is any. ===" return ${ret_code} fi + # rc 15 means duthosts fixture failed + if [ ${ret_code} -eq 15 ]; then + echo "=== duthosts fixture failed for $test_script. Skip rest of the scripts if there is any. ===" + return ${ret_code} + fi EXIT_CODE=1 if [[ ${TEST_MAX_FAIL} != 0 ]]; then