1414from tests .common .plugins .sanity_check .recover import recover , recover_chassis
1515from tests .common .plugins .sanity_check .constants import STAGE_PRE_TEST , STAGE_POST_TEST
1616from tests .common .helpers .assertions import pytest_assert as pt_assert
17+ from tests .common .helpers .custom_msg_utils import add_custom_msg
18+ from tests .common .helpers .constants import (
19+ DUT_CHECK_NAMESPACE
20+ )
1721
1822logger = logging .getLogger (__name__ )
1923
2024SUPPORTED_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' ]
25+ CUSTOM_MSG_PREFIX = "sonic_custom_msg"
2426
2527
2628def pytest_sessionfinish (session , exitstatus ):
@@ -32,8 +34,6 @@ def pytest_sessionfinish(session, exitstatus):
3234 session .config .cache .set ("pre_sanity_check_failed" , None )
3335 if post_sanity_failed :
3436 session .config .cache .set ("post_sanity_check_failed" , None )
35- for key in CACHE_LIST :
36- session .config .cache .set (key , None )
3737
3838 if pre_sanity_failed and not post_sanity_failed :
3939 session .exitstatus = constants .PRE_SANITY_CHECK_FAILED_RC
@@ -125,47 +125,6 @@ def do_checks(request, check_items, *args, **kwargs):
125125 return check_results
126126
127127
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-
169128@pytest .fixture (scope = "module" )
170129def prepare_parallel_run (request , parallel_run_context ):
171130 is_par_run , target_hostname , is_par_leader , par_followers , par_state_file = parallel_run_context
@@ -330,6 +289,7 @@ def sanity_check_full(prepare_parallel_run, localhost, duthosts, request, fanout
330289 if failed_results :
331290 if not allow_recover :
332291 request .config .cache .set ("pre_sanity_check_failed" , True )
292+ add_custom_msg (request , f"{ DUT_CHECK_NAMESPACE } .pre_sanity_check_failed" , True )
333293 pt_assert (False , "!!!!!!!!!!!!!!!!Pre-test sanity check failed: !!!!!!!!!!!!!!!!\n {}"
334294 .format (json .dumps (failed_results , indent = 4 , default = fallback_serializer )))
335295 else :
@@ -351,15 +311,16 @@ def sanity_check_full(prepare_parallel_run, localhost, duthosts, request, fanout
351311 logger .debug ("Post-test sanity check results:\n %s" %
352312 json .dumps (post_check_results , indent = 4 , default = fallback_serializer ))
353313
354- post_failed_results = [result for result in post_check_results if result ['failed' ]]
355- if post_failed_results :
356- if not allow_recover :
357- request .config .cache .set ("post_sanity_check_failed" , True )
358- pt_assert (False , "!!!!!!!!!!!!!!!! Post-test sanity check failed: !!!!!!!!!!!!!!!!\n {}"
359- .format (json .dumps (post_failed_results , indent = 4 , default = fallback_serializer )))
360- else :
361- recover_on_sanity_check_failure (duthosts , post_failed_results , fanouthosts , localhost , nbrhosts ,
362- post_check_items , recover_method , request , tbinfo , STAGE_POST_TEST )
314+ post_failed_results = [result for result in post_check_results if result ['failed' ]]
315+ if post_failed_results :
316+ if not allow_recover :
317+ request .config .cache .set ("post_sanity_check_failed" , True )
318+ add_custom_msg (request , f"{ DUT_CHECK_NAMESPACE } .post_sanity_check_failed" , True )
319+ pt_assert (False , "!!!!!!!!!!!!!!!! Post-test sanity check failed: !!!!!!!!!!!!!!!!\n {}"
320+ .format (json .dumps (post_failed_results , indent = 4 , default = fallback_serializer )))
321+ else :
322+ recover_on_sanity_check_failure (duthosts , post_failed_results , fanouthosts , localhost , nbrhosts ,
323+ post_check_items , recover_method , request , tbinfo , STAGE_POST_TEST )
363324
364325 logger .info ("Done post-test sanity check" )
365326 else :
@@ -401,7 +362,8 @@ def recover_on_sanity_check_failure(duthosts, failed_results, fanouthosts, local
401362
402363 except BaseException as e :
403364 request .config .cache .set (cache_key , True )
404- request .config .cache .set (recovery_cache_key , False )
365+ add_custom_msg (request , f"{ DUT_CHECK_NAMESPACE } .{ cache_key } " , True )
366+ add_custom_msg (request , f"{ DUT_CHECK_NAMESPACE } .{ recovery_cache_key } " , False )
405367
406368 logger .error (f"Recovery of sanity check failed with exception: { repr (e )} " )
407369 pt_assert (
@@ -416,17 +378,18 @@ def recover_on_sanity_check_failure(duthosts, failed_results, fanouthosts, local
416378 new_failed_results = [result for result in new_check_results if result ['failed' ]]
417379 if new_failed_results :
418380 request .config .cache .set (cache_key , True )
419- request .config .cache .set (recovery_cache_key , False )
381+ add_custom_msg (request , f"{ DUT_CHECK_NAMESPACE } .{ cache_key } " , True )
382+ add_custom_msg (request , f"{ DUT_CHECK_NAMESPACE } .{ recovery_cache_key } " , False )
420383 pt_assert (False ,
421384 f"!!!!!!!!!!!!!!!! { sanity_check_stage } sanity check after recovery failed: !!!!!!!!!!!!!!!!\n "
422385 f"{ json .dumps (new_failed_results , indent = 4 , default = fallback_serializer )} " )
423386 # Record recovery success
424- request . config . cache . set ( recovery_cache_key , True )
387+ add_custom_msg ( request , f" { DUT_CHECK_NAMESPACE } . { recovery_cache_key } " , True )
425388
426389
427- # make sure teardown of log_custom_msg happens after sanity_check
428390@pytest .fixture (scope = "module" , autouse = True )
429- def sanity_check (request , parallel_run_context , log_custom_msg ):
391+ def sanity_check (request , parallel_run_context ):
392+
430393 is_par_run , target_hostname , is_par_leader , par_followers , par_state_file = parallel_run_context
431394 initial_check_state = InitialCheckState (par_followers , par_state_file ) if is_par_run else None
432395 if is_par_run :
0 commit comments