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 10*20f38712SPatrick Williamsimport subprocess 11*20f38712SPatrick Williamsimport sys 123ba8ecdcSMichael Walsh 13*20f38712SPatrick Williamsfrom gen_arg import * 14*20f38712SPatrick Williamsfrom gen_call_robot import * 15*20f38712SPatrick Williamsfrom gen_cmd import * 16*20f38712SPatrick Williamsfrom gen_misc import * 17*20f38712SPatrick Williamsfrom gen_plug_in_utils import * 183ba8ecdcSMichael Walshfrom gen_print import * 193ba8ecdcSMichael Walshfrom gen_valid import * 203ba8ecdcSMichael Walsh 212ea965ceSMichael Walsh# Set exit_on_error for gen_valid functions. 222ea965ceSMichael Walshset_exit_on_error(True) 232ea965ceSMichael Walsh 2471fec43fSMichael Walsh# Initialize default plug-in parms.. 2571fec43fSMichael WalshSTOP_REST_FAIL = 0 26*20f38712SPatrick WilliamsSTOP_COMMAND = "" 273ba8ecdcSMichael Walshstop_test_rc = 2 2871fec43fSMichael WalshSTOP_VERIFY_HARDWARE_FAIL = 0 2971fec43fSMichael Walsh 303ba8ecdcSMichael Walsh 313ba8ecdcSMichael Walsh# Create parser object to process command line parameters and args. 323ba8ecdcSMichael Walshparser = argparse.ArgumentParser( 33*20f38712SPatrick Williams usage="%(prog)s [OPTIONS]", 34*20f38712SPatrick Williams description='If the "Stop" plug-in is selected by the user, %(prog)s' 35a57fef4aSPatrick Williams + " is called by OBMC Boot Test after each boot test. If %(prog)s returns" 36*20f38712SPatrick Williams + " " 37*20f38712SPatrick Williams + str(stop_test_rc) 38*20f38712SPatrick Williams + ", then OBMC Boot Test will stop. The user" 39a57fef4aSPatrick Williams + " may set environment variable STOP_COMMAND to contain any valid bash" 40a57fef4aSPatrick Williams + " command or program. %(prog)s will run this stop command. If the stop" 41a57fef4aSPatrick Williams + " command returns non-zero, then %(prog)s will return " 42*20f38712SPatrick Williams + str(stop_test_rc) 43*20f38712SPatrick Williams + ". %(prog)s recognizes some special values for" 44*20f38712SPatrick Williams + ' STOP_COMMAND: 1) "FAIL" means that OBMC Boot Test should stop' 45*20f38712SPatrick Williams + ' whenever a boot test fails. 2) "ALL" means that OBMC Boot Test' 46a57fef4aSPatrick Williams + " should stop after any boot test. If environment variable" 47a57fef4aSPatrick Williams + " STOP_REST_FAIL is set, OBMC Boot Test will stop if REST commands are" 48a57fef4aSPatrick Williams + " no longer working.", 498d6bf60fSMichael Walsh formatter_class=argparse.ArgumentDefaultsHelpFormatter, 50*20f38712SPatrick Williams prefix_chars="-+", 51*20f38712SPatrick Williams) 523ba8ecdcSMichael Walsh 53410b1787SMichael Walsh# The stock_list will be passed to gen_get_options. We populate it with the names of stock parm options we 54410b1787SMichael Walsh# want. These stock parms are pre-defined by gen_get_options. 55*20f38712SPatrick Williamsstock_list = [ 56*20f38712SPatrick Williams ("test_mode", get_plug_default("test_mode", 0)), 573ba8ecdcSMichael Walsh ("quiet", get_plug_default("quiet", 0)), 58*20f38712SPatrick Williams ("debug", get_plug_default("debug", 0)), 59*20f38712SPatrick Williams] 603ba8ecdcSMichael Walsh 613ba8ecdcSMichael Walsh 62*20f38712SPatrick Williamsdef exit_function(signal_number=0, frame=None): 633ba8ecdcSMichael Walsh r""" 64410b1787SMichael Walsh Execute whenever the program ends normally or with the signals that we catch (i.e. TERM, INT). 653ba8ecdcSMichael Walsh 6671fec43fSMichael Walsh This function will be called by gen_exit_function(). 6771fec43fSMichael Walsh """ 683ba8ecdcSMichael Walsh 69a0ce75a7SMichael Walsh process_robot_output_files() 70a0ce75a7SMichael Walsh 713ba8ecdcSMichael Walsh 723ba8ecdcSMichael Walshdef validate_parms(): 733ba8ecdcSMichael Walsh r""" 7471fec43fSMichael Walsh Validate program parameters, etc. 7571fec43fSMichael Walsh 7671fec43fSMichael Walsh This function will be called by gen_setup(). 773ba8ecdcSMichael Walsh """ 783ba8ecdcSMichael Walsh 793ba8ecdcSMichael Walsh get_plug_vars() 803ba8ecdcSMichael Walsh 813ba8ecdcSMichael Walsh 822ce1dbaeSMichael Walshdef stop_check(): 832ce1dbaeSMichael Walsh r""" 842ce1dbaeSMichael Walsh Stop this program with the stop check return code. 852ce1dbaeSMichael Walsh """ 862ce1dbaeSMichael Walsh 872ce1dbaeSMichael Walsh if MASTER_PID != PROGRAM_PID: 88c213d499SMichael Walsh save_plug_in_value(stop_check_rc=stop_test_rc) 892ce1dbaeSMichael Walsh exit(stop_test_rc) 902ce1dbaeSMichael Walsh 912ce1dbaeSMichael Walsh 923ba8ecdcSMichael Walshdef rest_fail(): 933ba8ecdcSMichael Walsh r""" 94410b1787SMichael Walsh If STOP_REST_FAIL, then this function will determine whether REST commands to the target are working. If 95410b1787SMichael Walsh not, this function will stop the program by returning stop_test_rc. 963ba8ecdcSMichael Walsh """ 973ba8ecdcSMichael Walsh 9871fec43fSMichael Walsh if not STOP_REST_FAIL: 993ba8ecdcSMichael Walsh return 1003ba8ecdcSMichael Walsh 101*20f38712SPatrick Williams REDFISH_SUPPORT_TRANS_STATE = int( 102*20f38712SPatrick Williams os.environ.get("REDFISH_SUPPORT_TRANS_STATE", 0) 103*20f38712SPatrick Williams ) or int(os.environ.get("AUTOBOOT_REDFISH_SUPPORT_TRANS_STATE", 0)) 104b3dffe8cSMichael Shepos 105b3dffe8cSMichael Shepos if REDFISH_SUPPORT_TRANS_STATE: 106b3dffe8cSMichael Shepos interface = "redfish" 107b3dffe8cSMichael Shepos else: 108b3dffe8cSMichael Shepos interface = "rest" 109b3dffe8cSMichael Shepos 110b3dffe8cSMichael Shepos print_timen("Checking to see whether %s commands are working." % interface) 1118ab9aea6SMichael Walsh init_robot_out_parms(get_plug_in_package_name() + "." + pgm_name + ".") 112*20f38712SPatrick Williams lib_file_path = ( 113*20f38712SPatrick Williams init_robot_file_path("lib/utils.robot") 114*20f38712SPatrick Williams + ":" 1158ab9aea6SMichael Walsh + init_robot_file_path("lib/gen_robot_print.py") 116*20f38712SPatrick Williams ) 1173ba8ecdcSMichael Walsh set_mod_global(lib_file_path) 118*20f38712SPatrick Williams timeout = "0 seconds" 119*20f38712SPatrick Williams interval = "1 second" 120*20f38712SPatrick Williams keyword_string = ( 121*20f38712SPatrick Williams "${match_state}= Create Dictionary %s=1 ;" % interface 122*20f38712SPatrick Williams + " ${state}= Wait State ${match_state} " 123*20f38712SPatrick Williams + timeout 124*20f38712SPatrick Williams + " " 125*20f38712SPatrick Williams + interval 126*20f38712SPatrick Williams + " quiet=${1} ; Rpvar state" 127*20f38712SPatrick Williams ) 1283ba8ecdcSMichael Walsh set_mod_global(keyword_string) 129*20f38712SPatrick Williams cmd_buf = create_robot_cmd_string( 130*20f38712SPatrick Williams "extended/run_keyword.robot", 131*20f38712SPatrick Williams OPENBMC_HOST, 132*20f38712SPatrick Williams SSH_PORT, 133*20f38712SPatrick Williams HTTPS_PORT, 134*20f38712SPatrick Williams REST_USERNAME, 135*20f38712SPatrick Williams REST_PASSWORD, 136*20f38712SPatrick Williams OPENBMC_USERNAME, 137*20f38712SPatrick Williams OPENBMC_PASSWORD, 138*20f38712SPatrick Williams REDFISH_SUPPORT_TRANS_STATE, 139*20f38712SPatrick Williams keyword_string, 140*20f38712SPatrick Williams lib_file_path, 141*20f38712SPatrick Williams quiet, 142*20f38712SPatrick Williams test_mode, 143*20f38712SPatrick Williams debug, 144*20f38712SPatrick Williams outputdir, 145*20f38712SPatrick Williams output, 146*20f38712SPatrick Williams log, 147*20f38712SPatrick Williams report, 148*20f38712SPatrick Williams loglevel, 149*20f38712SPatrick Williams ) 1503ba8ecdcSMichael Walsh if not robot_cmd_fnc(cmd_buf): 151*20f38712SPatrick Williams print_timen( 152*20f38712SPatrick Williams "The caller wishes to stop test execution if %s commands are" 153*20f38712SPatrick Williams " failing." % interface 154*20f38712SPatrick Williams ) 1552ce1dbaeSMichael Walsh stop_check() 156*20f38712SPatrick Williams print_timen( 157*20f38712SPatrick Williams "%s commands are working so no reason as of yet to stop the test." 158*20f38712SPatrick Williams % interface 159*20f38712SPatrick Williams ) 1603ba8ecdcSMichael Walsh 1613ba8ecdcSMichael Walsh 1623ba8ecdcSMichael Walshdef esel_stop_check(): 1633ba8ecdcSMichael Walsh r""" 164410b1787SMichael Walsh Run the esel_stop_check program to determine whether any eSEL entries found warrant stopping the test 165410b1787SMichael Walsh run. See esel_stop_check help text for details. 1663ba8ecdcSMichael Walsh """ 1673ba8ecdcSMichael Walsh 1683ba8ecdcSMichael Walsh if STOP_ESEL_STOP_FILE_PATH == "": 1693ba8ecdcSMichael Walsh return 1703ba8ecdcSMichael Walsh 171*20f38712SPatrick Williams cmd_buf = ( 172*20f38712SPatrick Williams "esel_stop_check --esel_stop_file_path=" + STOP_ESEL_STOP_FILE_PATH 173*20f38712SPatrick Williams ) 17471fec43fSMichael Walsh shell_rc, out_buf = shell_cmd(cmd_buf, show_err=0) 1753ba8ecdcSMichael Walsh if shell_rc == stop_test_rc: 176*20f38712SPatrick Williams print_timen( 177*20f38712SPatrick Williams "The caller wishes to stop test execution based on the presence of" 178*20f38712SPatrick Williams " certain esel entries." 179*20f38712SPatrick Williams ) 1802ce1dbaeSMichael Walsh stop_check() 1813ba8ecdcSMichael Walsh 1823ba8ecdcSMichael Walsh 183f7129672SMichael Sheposdef pel_stop_check(): 184f7129672SMichael Shepos r""" 185f7129672SMichael Shepos Determine whether any PEL entries found warrant stopping the test 186f7129672SMichael Shepos run. 187f7129672SMichael Shepos """ 188f7129672SMichael Shepos 189f7129672SMichael Shepos if STOP_PEL_STOP_FILE_PATH == "": 190f7129672SMichael Shepos return 191f7129672SMichael Shepos 192*20f38712SPatrick Williams pel_txt_file_path = ( 193*20f38712SPatrick Williams os.environ.get("AUTOBOOT_FFDC_DIR_PATH", "") 194*20f38712SPatrick Williams + os.environ.get("AUTOBOOT_FFDC_PREFIX", "") 195*20f38712SPatrick Williams + "PEL_logs_list.json" 196*20f38712SPatrick Williams ) 197f7129672SMichael Shepos 198f7129672SMichael Shepos if not os.path.isfile(pel_txt_file_path): 199*20f38712SPatrick Williams qprint_timen( 200*20f38712SPatrick Williams "The following file was not present so no further" 201*20f38712SPatrick Williams + " action will be taken." 202*20f38712SPatrick Williams ) 203f7129672SMichael Shepos qprint_var(pel_txt_file_path) 204f7129672SMichael Shepos return 205f7129672SMichael Shepos 206f7129672SMichael Shepos default_stop_dir_path = "/afs/rchland.ibm.com/projects/esw/dvt/" 207f7129672SMichael Shepos 208f7129672SMichael Shepos # If pel_stop_file_path is unqualified and cannot be found, pre-pend 209f7129672SMichael Shepos # default_stop_dir_path for the user. 210f7129672SMichael Shepos pel_stop_file_path = os.environ.get("STOP_PEL_STOP_FILE_PATH", "") 211*20f38712SPatrick Williams if not os.path.isfile(pel_stop_file_path) and os.path.isfile( 212*20f38712SPatrick Williams default_stop_dir_path + pel_stop_file_path 213*20f38712SPatrick Williams ): 214f7129672SMichael Shepos pel_stop_file_path = default_stop_dir_path + pel_stop_file_path 215f7129672SMichael Shepos qprint_timen("Using default stop file path.") 216f7129672SMichael Shepos qprint_var(pel_stop_file_path) 217f7129672SMichael Shepos 218f7129672SMichael Shepos # First, read the file in and convert it to a list. 219f7129672SMichael Shepos pel_stop_list = file_to_list(pel_stop_file_path, newlines=0, comments=0) 220f7129672SMichael Shepos 221f7129672SMichael Shepos if len(pel_stop_list) == 0: 222*20f38712SPatrick Williams print_timen( 223*20f38712SPatrick Williams "There are no records to process in " + pel_stop_file_path + "." 224*20f38712SPatrick Williams ) 225f7129672SMichael Shepos return 226f7129672SMichael Shepos 227f7129672SMichael Shepos pel_all_list = file_to_list(pel_txt_file_path, newlines=0, comments=0) 228f7129672SMichael Shepos 229f7129672SMichael Shepos if len(pel_all_list) == 0: 230*20f38712SPatrick Williams print_timen( 231*20f38712SPatrick Williams "There are no records to process in " + pel_txt_file_path + "." 232*20f38712SPatrick Williams ) 233f7129672SMichael Shepos return 234f7129672SMichael Shepos 235f7129672SMichael Shepos for stop_pel in pel_stop_list: 236f7129672SMichael Shepos for pel_all in pel_all_list: 237f7129672SMichael Shepos pel_match = re.search(".*SRC.*" + stop_pel + ".*", pel_all) 238f7129672SMichael Shepos if pel_match: 239*20f38712SPatrick Williams print_timen( 240*20f38712SPatrick Williams "The caller wishes to stop test execution based on " 241*20f38712SPatrick Williams + "the presence of certain PEL entries." 242*20f38712SPatrick Williams ) 243f7129672SMichael Shepos stop_check() 244f7129672SMichael Shepos 245f7129672SMichael Shepos 2463ba8ecdcSMichael Walshdef main(): 24771fec43fSMichael Walsh gen_setup() 2483ba8ecdcSMichael Walsh 24980caea09SMichael Walsh print_plug_in_header() 2503ba8ecdcSMichael Walsh 2513ba8ecdcSMichael Walsh if STOP_COMMAND.upper() == "FAIL": 2523ba8ecdcSMichael Walsh if AUTOBOOT_BOOT_SUCCESS == "0": 2533ba8ecdcSMichael Walsh print_timen("The caller wishes to stop after each boot failure.") 2542ce1dbaeSMichael Walsh stop_check() 2553ba8ecdcSMichael Walsh elif STOP_COMMAND.upper() == "ALL": 2563ba8ecdcSMichael Walsh print_timen("The caller wishes to stop after each boot test.") 2572ce1dbaeSMichael Walsh stop_check() 2583ba8ecdcSMichael Walsh elif len(STOP_COMMAND) > 0: 25971fec43fSMichael Walsh shell_rc, out_buf = shell_cmd(STOP_COMMAND, quiet=quiet, show_err=0) 2603ba8ecdcSMichael Walsh if shell_rc != 0: 2613ba8ecdcSMichael Walsh print_timen("The caller wishes to stop test execution.") 2622ce1dbaeSMichael Walsh stop_check() 2633ba8ecdcSMichael Walsh 2643c4869a2SMichael Walsh rest_fail() 2653c4869a2SMichael Walsh 2663c4869a2SMichael Walsh esel_stop_check() 2673c4869a2SMichael Walsh 268f7129672SMichael Shepos pel_stop_check() 269f7129672SMichael Shepos 27071fec43fSMichael Walsh if STOP_VERIFY_HARDWARE_FAIL: 271*20f38712SPatrick Williams hardware_error_found = restore_plug_in_value(0, "Verify_hardware") 27271fec43fSMichael Walsh if hardware_error_found: 273*20f38712SPatrick Williams print_timen( 274*20f38712SPatrick Williams "The caller wishes to stop test execution when the" 275*20f38712SPatrick Williams " Verify_hardware plug-in detects a" 276*20f38712SPatrick Williams + " hardware error." 277*20f38712SPatrick Williams ) 27871fec43fSMichael Walsh stop_check() 27971fec43fSMichael Walsh 2803ba8ecdcSMichael Walsh qprint_timen("The caller does not wish to stop the test run.") 2813ba8ecdcSMichael Walsh 282004ad3c9SJoy Onyerikwu 28371fec43fSMichael Walshmain() 284