Skip to content

Commit 8314f2f

Browse files
committed
Relocate log_custom_msg to assure test results to be logged
Instead of fixture, log_custom_msg is now called in one of hook (pytest_runtest_makereport). This is to prevent unexpected sequence of fixture teardown observed in nightly. Calling log_custom_msg from pytest_runtest_makereport will be one of the very last hooks to be called for each test.
1 parent 7be107b commit 8314f2f

File tree

2 files changed

+52
-53
lines changed

2 files changed

+52
-53
lines changed

tests/common/plugins/sanity_check/__init__.py

Lines changed: 1 addition & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,13 @@
1818
logger = logging.getLogger(__name__)
1919

2020
SUPPORTED_CHECKS = checks.CHECK_ITEMS
21-
DUT_CHEK_LIST = ['core_dump_check_pass', 'config_db_check_pass']
22-
CACHE_LIST = ['core_dump_check_pass', 'config_db_check_pass',
23-
'pre_sanity_recovered', 'post_sanity_recovered']
2421

2522

2623
def pytest_sessionfinish(session, exitstatus):
2724

2825
pre_sanity_failed = session.config.cache.get("pre_sanity_check_failed", None)
2926
post_sanity_failed = session.config.cache.get("post_sanity_check_failed", None)
3027

31-
if pre_sanity_failed:
32-
session.config.cache.set("pre_sanity_check_failed", None)
33-
if post_sanity_failed:
34-
session.config.cache.set("post_sanity_check_failed", None)
35-
for key in CACHE_LIST:
36-
session.config.cache.set(key, None)
37-
3828
if pre_sanity_failed and not post_sanity_failed:
3929
session.exitstatus = constants.PRE_SANITY_CHECK_FAILED_RC
4030
elif not pre_sanity_failed and post_sanity_failed:
@@ -125,47 +115,6 @@ def do_checks(request, check_items, *args, **kwargs):
125115
return check_results
126116

127117

128-
@pytest.fixture(scope="module", autouse=True)
129-
def log_custom_msg(request):
130-
yield
131-
module_name = request.node.name
132-
items = request.session.items
133-
for item in items:
134-
if item.module.__name__ + ".py" == module_name.split("/")[-1]:
135-
customMsgDict = {}
136-
dutChekResults = {}
137-
for key in DUT_CHEK_LIST:
138-
if request.config.cache.get(key, None) is False:
139-
dutChekResults[key] = False
140-
if dutChekResults:
141-
customMsgDict['DutChekResult'] = dutChekResults
142-
143-
# Check pre_sanity_checks results
144-
preSanityCheckResults = {}
145-
if request.config.cache.get("pre_sanity_check_failed", None):
146-
preSanityCheckResults['pre_sanity_check_failed'] = True
147-
# pre_sanity_recovered should be None in healthy case, record either True/False
148-
if request.config.cache.get("pre_sanity_recovered", None) is not None:
149-
preSanityCheckResults['pre_sanity_recovered'] = request.config.cache.get("pre_sanity_recovered", None)
150-
if preSanityCheckResults:
151-
customMsgDict['PreSanityCheckResults'] = preSanityCheckResults
152-
153-
# Check post_sanity_checks results
154-
postSanityCheckResults = {}
155-
if request.config.cache.get("post_sanity_check_failed", None):
156-
postSanityCheckResults['post_sanity_check_failed'] = True
157-
# post_sanity_recovered should be None in healthy case, record either True/False
158-
if request.config.cache.get("post_sanity_recovered", None) is not None:
159-
preSanityCheckResults['post_sanity_recovered'] = request.config.cache.get("post_sanity_recovered", None)
160-
if postSanityCheckResults:
161-
customMsgDict['PostSanityCheckResults'] = postSanityCheckResults
162-
163-
# if we have any custom message to log, append it to user_properties
164-
if customMsgDict:
165-
logger.debug("customMsgDict: {}".format(customMsgDict))
166-
item.user_properties.append(('CustomMsg', json.dumps(customMsgDict)))
167-
168-
169118
@pytest.fixture(scope="module")
170119
def sanity_check_full(localhost, duthosts, request, fanouthosts, nbrhosts, tbinfo):
171120
logger.info("Prepare sanity check")
@@ -371,9 +320,8 @@ def recover_on_sanity_check_failure(duthosts, failed_results, fanouthosts, local
371320
request.config.cache.set(recovery_cache_key, True)
372321

373322

374-
# make sure teardown of log_custom_msg happens after sanity_check
375323
@pytest.fixture(scope="module", autouse=True)
376-
def sanity_check(request, parallel_run_context, log_custom_msg):
324+
def sanity_check(request, parallel_run_context):
377325
is_par_run, target_hostname, is_par_leader, par_followers, par_state_file = parallel_run_context
378326
initial_check_state = InitialCheckState(par_followers, par_state_file) if is_par_run else None
379327

tests/conftest.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@
7676
cache = FactsCache()
7777

7878
DUTHOSTS_FIXTURE_FAILED_RC = 15
79+
DUT_CHEK_LIST = ['core_dump_check_pass', 'config_db_check_pass']
80+
CACHE_LIST = ['core_dump_check_pass', 'config_db_check_pass',
81+
'pre_sanity_recovered', 'post_sanity_recovered',
82+
'pre_sanity_check_failed', 'post_sanity_check_failed']
7983

8084
pytest_plugins = ('tests.common.plugins.ptfadapter',
8185
'tests.common.plugins.ansible_fixtures',
@@ -914,12 +918,58 @@ def creds_all_duts(duthosts):
914918
return creds_all_duts
915919

916920

921+
def log_custom_msg(item):
922+
# temp log output to track module name
923+
logger.debug("[log_custom_msg] item: {}".format(item))
924+
customMsgDict = {}
925+
dutChekResults = {}
926+
for key in DUT_CHEK_LIST:
927+
if item.session.config.cache.get(key, None):
928+
dutChekResults[key] = item.session.config.cache.get(key, None)
929+
item.session.config.cache.set(key, None)
930+
if dutChekResults:
931+
customMsgDict['DutChekResult'] = dutChekResults
932+
# Check pre_sanity_checks results
933+
preSanityCheckResults = {}
934+
if item.session.config.cache.get("pre_sanity_check_failed", None):
935+
preSanityCheckResults['pre_sanity_check_failed'] = True
936+
item.session.config.cache.set("pre_sanity_check_failed", None)
937+
# pre_sanity_recovered should be None in healthy case, record either True/False
938+
if item.session.config.cache.get("pre_sanity_recovered", None) is not None:
939+
preSanityCheckResults['pre_sanity_recovered'] = item.session.config.cache.get("pre_sanity_recovered", None)
940+
item.session.config.cache.set("pre_sanity_recovered", None)
941+
if preSanityCheckResults:
942+
customMsgDict['PreSanityCheckResults'] = preSanityCheckResults
943+
# Check post_sanity_checks results
944+
postSanityCheckResults = {}
945+
if item.session.config.cache.get("post_sanity_check_failed", None):
946+
postSanityCheckResults['post_sanity_check_failed'] = True
947+
item.session.config.cache.set("post_sanity_check_failed", None)
948+
# post_sanity_recovered should be None in healthy case, record either True/False
949+
if item.session.config.cache.get("post_sanity_recovered", None) is not None:
950+
preSanityCheckResults['post_sanity_recovered'] = item.session.config.cache.get("post_sanity_recovered", None)
951+
item.session.config.cache.set("post_sanity_recovered", None)
952+
if postSanityCheckResults:
953+
customMsgDict['PostSanityCheckResults'] = postSanityCheckResults
954+
# if we have any custom message to log, append it to user_properties
955+
if customMsgDict:
956+
logger.debug("customMsgDict: {}".format(customMsgDict))
957+
item.user_properties.append(('CustomMsg', json.dumps(customMsgDict)))
958+
959+
960+
# log_custog_msg has to be the very last function to be called in every test case.
961+
# use this hook to call log_custom_msg, so we guarantee its execution and its sequence is the last.
917962
@pytest.hookimpl(tryfirst=True, hookwrapper=True)
918963
def pytest_runtest_makereport(item, call):
919964

965+
logger.debug("debugging - pytest_runtest_makereport call: {}, item_name: {}".format(call.when, item))
920966
if call.when == 'setup':
921967
item.user_properties.append(('start', str(datetime.fromtimestamp(call.start))))
922968
elif call.when == 'teardown':
969+
for key in CACHE_LIST: # temporary log output to track cache status
970+
if item.session.config.cache.get(key, None):
971+
logger.debug("debugging - before log [{}] : {}".format(key, item.session.config.cache.get(key, None)))
972+
log_custom_msg(item)
923973
item.user_properties.append(('end', str(datetime.fromtimestamp(call.stop))))
924974

925975
# Filter out unnecessary logs captured on "stdout" and "stderr"
@@ -2492,6 +2542,7 @@ def _remove_entry(table_name, key_name, config):
24922542
logger.info("Core dump and config check passed for {}".format(module_name))
24932543

24942544
if check_result:
2545+
logger.debug("core_dump_and_config_check failed, check_result: {}".format(json.dumps(check_result)))
24952546
request.config.cache.set("core_dump_check_pass", core_dump_check_pass)
24962547
request.config.cache.set("config_db_check_pass", config_db_check_pass)
24972548

0 commit comments

Comments
 (0)