1e7e9171eSGeorge Keishing#!/usr/bin/env python3 23ba8ecdcSMichael Walsh 33ba8ecdcSMichael Walshr""" 43ba8ecdcSMichael WalshCheck for stop conditions. Return code of 2 if stop conditions are found. 53ba8ecdcSMichael Walsh""" 63ba8ecdcSMichael Walsh 7a57fef4aSPatrick Williamsimport argparse 8b3dffe8cSMichael Sheposimport os 9f7129672SMichael Sheposimport re 103ba8ecdcSMichael Walsh 11*7899a451SGeorge Keishingfrom gen_arg import * # NOQA 12*7899a451SGeorge Keishingfrom gen_call_robot import * # NOQA 13*7899a451SGeorge Keishingfrom gen_cmd import * # NOQA 14*7899a451SGeorge Keishingfrom gen_misc import * # NOQA 15*7899a451SGeorge Keishingfrom gen_plug_in_utils import * # NOQA 16*7899a451SGeorge Keishingfrom gen_print import * # NOQA 17*7899a451SGeorge Keishingfrom gen_valid import * # NOQA 183ba8ecdcSMichael Walsh 192ea965ceSMichael Walsh# Set exit_on_error for gen_valid functions. 202ea965ceSMichael Walshset_exit_on_error(True) 212ea965ceSMichael Walsh 2271fec43fSMichael Walsh# Initialize default plug-in parms.. 2371fec43fSMichael WalshSTOP_REST_FAIL = 0 2420f38712SPatrick WilliamsSTOP_COMMAND = "" 253ba8ecdcSMichael Walshstop_test_rc = 2 2671fec43fSMichael WalshSTOP_VERIFY_HARDWARE_FAIL = 0 2771fec43fSMichael Walsh 283ba8ecdcSMichael Walsh 293ba8ecdcSMichael Walsh# Create parser object to process command line parameters and args. 303ba8ecdcSMichael Walshparser = argparse.ArgumentParser( 3120f38712SPatrick Williams usage="%(prog)s [OPTIONS]", 3220f38712SPatrick Williams description='If the "Stop" plug-in is selected by the user, %(prog)s' 33a57fef4aSPatrick Williams + " is called by OBMC Boot Test after each boot test. If %(prog)s returns" 3420f38712SPatrick Williams + " " 3520f38712SPatrick Williams + str(stop_test_rc) 3620f38712SPatrick Williams + ", then OBMC Boot Test will stop. The user" 37a57fef4aSPatrick Williams + " may set environment variable STOP_COMMAND to contain any valid bash" 38a57fef4aSPatrick Williams + " command or program. %(prog)s will run this stop command. If the stop" 39a57fef4aSPatrick Williams + " command returns non-zero, then %(prog)s will return " 4020f38712SPatrick Williams + str(stop_test_rc) 4120f38712SPatrick Williams + ". %(prog)s recognizes some special values for" 4220f38712SPatrick Williams + ' STOP_COMMAND: 1) "FAIL" means that OBMC Boot Test should stop' 4320f38712SPatrick Williams + ' whenever a boot test fails. 2) "ALL" means that OBMC Boot Test' 44a57fef4aSPatrick Williams + " should stop after any boot test. If environment variable" 45a57fef4aSPatrick Williams + " STOP_REST_FAIL is set, OBMC Boot Test will stop if REST commands are" 46a57fef4aSPatrick Williams + " no longer working.", 478d6bf60fSMichael Walsh formatter_class=argparse.ArgumentDefaultsHelpFormatter, 4820f38712SPatrick Williams prefix_chars="-+", 4920f38712SPatrick Williams) 503ba8ecdcSMichael Walsh 51410b1787SMichael Walsh# The stock_list will be passed to gen_get_options. We populate it with the names of stock parm options we 52410b1787SMichael Walsh# want. These stock parms are pre-defined by gen_get_options. 5320f38712SPatrick Williamsstock_list = [ 5420f38712SPatrick Williams ("test_mode", get_plug_default("test_mode", 0)), 553ba8ecdcSMichael Walsh ("quiet", get_plug_default("quiet", 0)), 5620f38712SPatrick Williams ("debug", get_plug_default("debug", 0)), 5720f38712SPatrick Williams] 583ba8ecdcSMichael Walsh 593ba8ecdcSMichael Walsh 6020f38712SPatrick Williamsdef exit_function(signal_number=0, frame=None): 613ba8ecdcSMichael Walsh r""" 62410b1787SMichael Walsh Execute whenever the program ends normally or with the signals that we catch (i.e. TERM, INT). 633ba8ecdcSMichael Walsh 6471fec43fSMichael Walsh This function will be called by gen_exit_function(). 6571fec43fSMichael Walsh """ 663ba8ecdcSMichael Walsh 67a0ce75a7SMichael Walsh process_robot_output_files() 68a0ce75a7SMichael Walsh 693ba8ecdcSMichael Walsh 703ba8ecdcSMichael Walshdef validate_parms(): 713ba8ecdcSMichael Walsh r""" 7271fec43fSMichael Walsh Validate program parameters, etc. 7371fec43fSMichael Walsh 7471fec43fSMichael Walsh This function will be called by gen_setup(). 753ba8ecdcSMichael Walsh """ 763ba8ecdcSMichael Walsh 773ba8ecdcSMichael Walsh get_plug_vars() 783ba8ecdcSMichael Walsh 793ba8ecdcSMichael Walsh 802ce1dbaeSMichael Walshdef stop_check(): 812ce1dbaeSMichael Walsh r""" 822ce1dbaeSMichael Walsh Stop this program with the stop check return code. 832ce1dbaeSMichael Walsh """ 842ce1dbaeSMichael Walsh 852ce1dbaeSMichael Walsh if MASTER_PID != PROGRAM_PID: 86c213d499SMichael Walsh save_plug_in_value(stop_check_rc=stop_test_rc) 872ce1dbaeSMichael Walsh exit(stop_test_rc) 882ce1dbaeSMichael Walsh 892ce1dbaeSMichael Walsh 903ba8ecdcSMichael Walshdef rest_fail(): 913ba8ecdcSMichael Walsh r""" 92410b1787SMichael Walsh If STOP_REST_FAIL, then this function will determine whether REST commands to the target are working. If 93410b1787SMichael Walsh not, this function will stop the program by returning stop_test_rc. 943ba8ecdcSMichael Walsh """ 953ba8ecdcSMichael Walsh 9671fec43fSMichael Walsh if not STOP_REST_FAIL: 973ba8ecdcSMichael Walsh return 983ba8ecdcSMichael Walsh 9920f38712SPatrick Williams REDFISH_SUPPORT_TRANS_STATE = int( 10020f38712SPatrick Williams os.environ.get("REDFISH_SUPPORT_TRANS_STATE", 0) 10120f38712SPatrick Williams ) or int(os.environ.get("AUTOBOOT_REDFISH_SUPPORT_TRANS_STATE", 0)) 102b3dffe8cSMichael Shepos 103b3dffe8cSMichael Shepos if REDFISH_SUPPORT_TRANS_STATE: 104b3dffe8cSMichael Shepos interface = "redfish" 105b3dffe8cSMichael Shepos else: 106b3dffe8cSMichael Shepos interface = "rest" 107b3dffe8cSMichael Shepos 108b3dffe8cSMichael Shepos print_timen("Checking to see whether %s commands are working." % interface) 1098ab9aea6SMichael Walsh init_robot_out_parms(get_plug_in_package_name() + "." + pgm_name + ".") 11020f38712SPatrick Williams lib_file_path = ( 11120f38712SPatrick Williams init_robot_file_path("lib/utils.robot") 11220f38712SPatrick Williams + ":" 1138ab9aea6SMichael Walsh + init_robot_file_path("lib/gen_robot_print.py") 11420f38712SPatrick Williams ) 1153ba8ecdcSMichael Walsh set_mod_global(lib_file_path) 11620f38712SPatrick Williams timeout = "0 seconds" 11720f38712SPatrick Williams interval = "1 second" 11820f38712SPatrick Williams keyword_string = ( 11920f38712SPatrick Williams "${match_state}= Create Dictionary %s=1 ;" % interface 12020f38712SPatrick Williams + " ${state}= Wait State ${match_state} " 12120f38712SPatrick Williams + timeout 12220f38712SPatrick Williams + " " 12320f38712SPatrick Williams + interval 12420f38712SPatrick Williams + " quiet=${1} ; Rpvar state" 12520f38712SPatrick Williams ) 1263ba8ecdcSMichael Walsh set_mod_global(keyword_string) 12720f38712SPatrick Williams cmd_buf = create_robot_cmd_string( 12820f38712SPatrick Williams "extended/run_keyword.robot", 12920f38712SPatrick Williams OPENBMC_HOST, 13020f38712SPatrick Williams SSH_PORT, 13120f38712SPatrick Williams HTTPS_PORT, 13220f38712SPatrick Williams REST_USERNAME, 13320f38712SPatrick Williams REST_PASSWORD, 13420f38712SPatrick Williams OPENBMC_USERNAME, 13520f38712SPatrick Williams OPENBMC_PASSWORD, 13620f38712SPatrick Williams REDFISH_SUPPORT_TRANS_STATE, 13720f38712SPatrick Williams keyword_string, 13820f38712SPatrick Williams lib_file_path, 13920f38712SPatrick Williams quiet, 14020f38712SPatrick Williams test_mode, 14120f38712SPatrick Williams debug, 14220f38712SPatrick Williams outputdir, 14320f38712SPatrick Williams output, 14420f38712SPatrick Williams log, 14520f38712SPatrick Williams report, 14620f38712SPatrick Williams loglevel, 14720f38712SPatrick Williams ) 1483ba8ecdcSMichael Walsh if not robot_cmd_fnc(cmd_buf): 14920f38712SPatrick Williams print_timen( 15020f38712SPatrick Williams "The caller wishes to stop test execution if %s commands are" 15120f38712SPatrick Williams " failing." % interface 15220f38712SPatrick Williams ) 1532ce1dbaeSMichael Walsh stop_check() 15420f38712SPatrick Williams print_timen( 15520f38712SPatrick Williams "%s commands are working so no reason as of yet to stop the test." 15620f38712SPatrick Williams % interface 15720f38712SPatrick Williams ) 1583ba8ecdcSMichael Walsh 1593ba8ecdcSMichael Walsh 1603ba8ecdcSMichael Walshdef esel_stop_check(): 1613ba8ecdcSMichael Walsh r""" 162410b1787SMichael Walsh Run the esel_stop_check program to determine whether any eSEL entries found warrant stopping the test 163410b1787SMichael Walsh run. See esel_stop_check help text for details. 1643ba8ecdcSMichael Walsh """ 1653ba8ecdcSMichael Walsh 1663ba8ecdcSMichael Walsh if STOP_ESEL_STOP_FILE_PATH == "": 1673ba8ecdcSMichael Walsh return 1683ba8ecdcSMichael Walsh 16920f38712SPatrick Williams cmd_buf = ( 17020f38712SPatrick Williams "esel_stop_check --esel_stop_file_path=" + STOP_ESEL_STOP_FILE_PATH 17120f38712SPatrick Williams ) 17271fec43fSMichael Walsh shell_rc, out_buf = shell_cmd(cmd_buf, show_err=0) 1733ba8ecdcSMichael Walsh if shell_rc == stop_test_rc: 17420f38712SPatrick Williams print_timen( 17520f38712SPatrick Williams "The caller wishes to stop test execution based on the presence of" 17620f38712SPatrick Williams " certain esel entries." 17720f38712SPatrick Williams ) 1782ce1dbaeSMichael Walsh stop_check() 1793ba8ecdcSMichael Walsh 1803ba8ecdcSMichael Walsh 181f7129672SMichael Sheposdef pel_stop_check(): 182f7129672SMichael Shepos r""" 183f7129672SMichael Shepos Determine whether any PEL entries found warrant stopping the test 184f7129672SMichael Shepos run. 185f7129672SMichael Shepos """ 186f7129672SMichael Shepos 187f7129672SMichael Shepos if STOP_PEL_STOP_FILE_PATH == "": 188f7129672SMichael Shepos return 189f7129672SMichael Shepos 19020f38712SPatrick Williams pel_txt_file_path = ( 19120f38712SPatrick Williams os.environ.get("AUTOBOOT_FFDC_DIR_PATH", "") 19220f38712SPatrick Williams + os.environ.get("AUTOBOOT_FFDC_PREFIX", "") 19320f38712SPatrick Williams + "PEL_logs_list.json" 19420f38712SPatrick Williams ) 195f7129672SMichael Shepos 196f7129672SMichael Shepos if not os.path.isfile(pel_txt_file_path): 19720f38712SPatrick Williams qprint_timen( 19820f38712SPatrick Williams "The following file was not present so no further" 19920f38712SPatrick Williams + " action will be taken." 20020f38712SPatrick Williams ) 201f7129672SMichael Shepos qprint_var(pel_txt_file_path) 202f7129672SMichael Shepos return 203f7129672SMichael Shepos 204f7129672SMichael Shepos default_stop_dir_path = "/afs/rchland.ibm.com/projects/esw/dvt/" 205f7129672SMichael Shepos 206f7129672SMichael Shepos # If pel_stop_file_path is unqualified and cannot be found, pre-pend 207f7129672SMichael Shepos # default_stop_dir_path for the user. 208f7129672SMichael Shepos pel_stop_file_path = os.environ.get("STOP_PEL_STOP_FILE_PATH", "") 20920f38712SPatrick Williams if not os.path.isfile(pel_stop_file_path) and os.path.isfile( 21020f38712SPatrick Williams default_stop_dir_path + pel_stop_file_path 21120f38712SPatrick Williams ): 212f7129672SMichael Shepos pel_stop_file_path = default_stop_dir_path + pel_stop_file_path 213f7129672SMichael Shepos qprint_timen("Using default stop file path.") 214f7129672SMichael Shepos qprint_var(pel_stop_file_path) 215f7129672SMichael Shepos 216f7129672SMichael Shepos # First, read the file in and convert it to a list. 217f7129672SMichael Shepos pel_stop_list = file_to_list(pel_stop_file_path, newlines=0, comments=0) 218f7129672SMichael Shepos 219f7129672SMichael Shepos if len(pel_stop_list) == 0: 22020f38712SPatrick Williams print_timen( 22120f38712SPatrick Williams "There are no records to process in " + pel_stop_file_path + "." 22220f38712SPatrick Williams ) 223f7129672SMichael Shepos return 224f7129672SMichael Shepos 225f7129672SMichael Shepos pel_all_list = file_to_list(pel_txt_file_path, newlines=0, comments=0) 226f7129672SMichael Shepos 227f7129672SMichael Shepos if len(pel_all_list) == 0: 22820f38712SPatrick Williams print_timen( 22920f38712SPatrick Williams "There are no records to process in " + pel_txt_file_path + "." 23020f38712SPatrick Williams ) 231f7129672SMichael Shepos return 232f7129672SMichael Shepos 233f7129672SMichael Shepos for stop_pel in pel_stop_list: 234f7129672SMichael Shepos for pel_all in pel_all_list: 235f7129672SMichael Shepos pel_match = re.search(".*SRC.*" + stop_pel + ".*", pel_all) 236f7129672SMichael Shepos if pel_match: 23720f38712SPatrick Williams print_timen( 23820f38712SPatrick Williams "The caller wishes to stop test execution based on " 23920f38712SPatrick Williams + "the presence of certain PEL entries." 24020f38712SPatrick Williams ) 241f7129672SMichael Shepos stop_check() 242f7129672SMichael Shepos 243f7129672SMichael Shepos 2443ba8ecdcSMichael Walshdef main(): 24571fec43fSMichael Walsh gen_setup() 2463ba8ecdcSMichael Walsh 24780caea09SMichael Walsh print_plug_in_header() 2483ba8ecdcSMichael Walsh 2493ba8ecdcSMichael Walsh if STOP_COMMAND.upper() == "FAIL": 2503ba8ecdcSMichael Walsh if AUTOBOOT_BOOT_SUCCESS == "0": 2513ba8ecdcSMichael Walsh print_timen("The caller wishes to stop after each boot failure.") 2522ce1dbaeSMichael Walsh stop_check() 2533ba8ecdcSMichael Walsh elif STOP_COMMAND.upper() == "ALL": 2543ba8ecdcSMichael Walsh print_timen("The caller wishes to stop after each boot test.") 2552ce1dbaeSMichael Walsh stop_check() 2563ba8ecdcSMichael Walsh elif len(STOP_COMMAND) > 0: 25771fec43fSMichael Walsh shell_rc, out_buf = shell_cmd(STOP_COMMAND, quiet=quiet, show_err=0) 2583ba8ecdcSMichael Walsh if shell_rc != 0: 2593ba8ecdcSMichael Walsh print_timen("The caller wishes to stop test execution.") 2602ce1dbaeSMichael Walsh stop_check() 2613ba8ecdcSMichael Walsh 2623c4869a2SMichael Walsh rest_fail() 2633c4869a2SMichael Walsh 2643c4869a2SMichael Walsh esel_stop_check() 2653c4869a2SMichael Walsh 266f7129672SMichael Shepos pel_stop_check() 267f7129672SMichael Shepos 26871fec43fSMichael Walsh if STOP_VERIFY_HARDWARE_FAIL: 26920f38712SPatrick Williams hardware_error_found = restore_plug_in_value(0, "Verify_hardware") 27071fec43fSMichael Walsh if hardware_error_found: 27120f38712SPatrick Williams print_timen( 27220f38712SPatrick Williams "The caller wishes to stop test execution when the" 273*7899a451SGeorge Keishing " Verify_hardware plug-in detects a hardware error." 27420f38712SPatrick Williams ) 27571fec43fSMichael Walsh stop_check() 27671fec43fSMichael Walsh 2773ba8ecdcSMichael Walsh qprint_timen("The caller does not wish to stop the test run.") 2783ba8ecdcSMichael Walsh 279004ad3c9SJoy Onyerikwu 28071fec43fSMichael Walshmain() 281