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 117899a451SGeorge Keishingfrom gen_arg import * # NOQA 127899a451SGeorge Keishingfrom gen_call_robot import * # NOQA 137899a451SGeorge Keishingfrom gen_cmd import * # NOQA 147899a451SGeorge Keishingfrom gen_misc import * # NOQA 157899a451SGeorge Keishingfrom gen_plug_in_utils import * # NOQA 167899a451SGeorge Keishingfrom gen_print import * # NOQA 177899a451SGeorge 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 OPENBMC_USERNAME, 13320f38712SPatrick Williams OPENBMC_PASSWORD, 13420f38712SPatrick Williams REDFISH_SUPPORT_TRANS_STATE, 13520f38712SPatrick Williams keyword_string, 13620f38712SPatrick Williams lib_file_path, 13720f38712SPatrick Williams quiet, 13820f38712SPatrick Williams test_mode, 13920f38712SPatrick Williams debug, 14020f38712SPatrick Williams outputdir, 14120f38712SPatrick Williams output, 14220f38712SPatrick Williams log, 14320f38712SPatrick Williams report, 14420f38712SPatrick Williams loglevel, 14520f38712SPatrick Williams ) 1463ba8ecdcSMichael Walsh if not robot_cmd_fnc(cmd_buf): 14720f38712SPatrick Williams print_timen( 14820f38712SPatrick Williams "The caller wishes to stop test execution if %s commands are" 14920f38712SPatrick Williams " failing." % interface 15020f38712SPatrick Williams ) 1512ce1dbaeSMichael Walsh stop_check() 15220f38712SPatrick Williams print_timen( 15320f38712SPatrick Williams "%s commands are working so no reason as of yet to stop the test." 15420f38712SPatrick Williams % interface 15520f38712SPatrick Williams ) 1563ba8ecdcSMichael Walsh 1573ba8ecdcSMichael Walsh 1583ba8ecdcSMichael Walshdef esel_stop_check(): 1593ba8ecdcSMichael Walsh r""" 160410b1787SMichael Walsh Run the esel_stop_check program to determine whether any eSEL entries found warrant stopping the test 161410b1787SMichael Walsh run. See esel_stop_check help text for details. 1623ba8ecdcSMichael Walsh """ 1633ba8ecdcSMichael Walsh 1643ba8ecdcSMichael Walsh if STOP_ESEL_STOP_FILE_PATH == "": 1653ba8ecdcSMichael Walsh return 1663ba8ecdcSMichael Walsh 16720f38712SPatrick Williams cmd_buf = ( 16820f38712SPatrick Williams "esel_stop_check --esel_stop_file_path=" + STOP_ESEL_STOP_FILE_PATH 16920f38712SPatrick Williams ) 17071fec43fSMichael Walsh shell_rc, out_buf = shell_cmd(cmd_buf, show_err=0) 1713ba8ecdcSMichael Walsh if shell_rc == stop_test_rc: 17220f38712SPatrick Williams print_timen( 17320f38712SPatrick Williams "The caller wishes to stop test execution based on the presence of" 17420f38712SPatrick Williams " certain esel entries." 17520f38712SPatrick Williams ) 1762ce1dbaeSMichael Walsh stop_check() 1773ba8ecdcSMichael Walsh 1783ba8ecdcSMichael Walsh 179f7129672SMichael Sheposdef pel_stop_check(): 180f7129672SMichael Shepos r""" 181f7129672SMichael Shepos Determine whether any PEL entries found warrant stopping the test 182f7129672SMichael Shepos run. 183f7129672SMichael Shepos """ 184f7129672SMichael Shepos 185f7129672SMichael Shepos if STOP_PEL_STOP_FILE_PATH == "": 186f7129672SMichael Shepos return 187f7129672SMichael Shepos 18820f38712SPatrick Williams pel_txt_file_path = ( 18920f38712SPatrick Williams os.environ.get("AUTOBOOT_FFDC_DIR_PATH", "") 19020f38712SPatrick Williams + os.environ.get("AUTOBOOT_FFDC_PREFIX", "") 19120f38712SPatrick Williams + "PEL_logs_list.json" 19220f38712SPatrick Williams ) 193f7129672SMichael Shepos 194f7129672SMichael Shepos if not os.path.isfile(pel_txt_file_path): 19520f38712SPatrick Williams qprint_timen( 19620f38712SPatrick Williams "The following file was not present so no further" 19720f38712SPatrick Williams + " action will be taken." 19820f38712SPatrick Williams ) 199f7129672SMichael Shepos qprint_var(pel_txt_file_path) 200f7129672SMichael Shepos return 201f7129672SMichael Shepos 202*8e6bdae8SGeorge Keishing default_stop_dir_path = "" 203f7129672SMichael Shepos 204f7129672SMichael Shepos # If pel_stop_file_path is unqualified and cannot be found, pre-pend 205f7129672SMichael Shepos # default_stop_dir_path for the user. 206f7129672SMichael Shepos pel_stop_file_path = os.environ.get("STOP_PEL_STOP_FILE_PATH", "") 20720f38712SPatrick Williams if not os.path.isfile(pel_stop_file_path) and os.path.isfile( 20820f38712SPatrick Williams default_stop_dir_path + pel_stop_file_path 20920f38712SPatrick Williams ): 210f7129672SMichael Shepos pel_stop_file_path = default_stop_dir_path + pel_stop_file_path 211f7129672SMichael Shepos qprint_timen("Using default stop file path.") 212f7129672SMichael Shepos qprint_var(pel_stop_file_path) 213f7129672SMichael Shepos 214f7129672SMichael Shepos # First, read the file in and convert it to a list. 215f7129672SMichael Shepos pel_stop_list = file_to_list(pel_stop_file_path, newlines=0, comments=0) 216f7129672SMichael Shepos 217f7129672SMichael Shepos if len(pel_stop_list) == 0: 21820f38712SPatrick Williams print_timen( 21920f38712SPatrick Williams "There are no records to process in " + pel_stop_file_path + "." 22020f38712SPatrick Williams ) 221f7129672SMichael Shepos return 222f7129672SMichael Shepos 223f7129672SMichael Shepos pel_all_list = file_to_list(pel_txt_file_path, newlines=0, comments=0) 224f7129672SMichael Shepos 225f7129672SMichael Shepos if len(pel_all_list) == 0: 22620f38712SPatrick Williams print_timen( 22720f38712SPatrick Williams "There are no records to process in " + pel_txt_file_path + "." 22820f38712SPatrick Williams ) 229f7129672SMichael Shepos return 230f7129672SMichael Shepos 231f7129672SMichael Shepos for stop_pel in pel_stop_list: 232f7129672SMichael Shepos for pel_all in pel_all_list: 233f7129672SMichael Shepos pel_match = re.search(".*SRC.*" + stop_pel + ".*", pel_all) 234f7129672SMichael Shepos if pel_match: 23520f38712SPatrick Williams print_timen( 23620f38712SPatrick Williams "The caller wishes to stop test execution based on " 23720f38712SPatrick Williams + "the presence of certain PEL entries." 23820f38712SPatrick Williams ) 239f7129672SMichael Shepos stop_check() 240f7129672SMichael Shepos 241f7129672SMichael Shepos 2423ba8ecdcSMichael Walshdef main(): 24371fec43fSMichael Walsh gen_setup() 2443ba8ecdcSMichael Walsh 24580caea09SMichael Walsh print_plug_in_header() 2463ba8ecdcSMichael Walsh 2473ba8ecdcSMichael Walsh if STOP_COMMAND.upper() == "FAIL": 2483ba8ecdcSMichael Walsh if AUTOBOOT_BOOT_SUCCESS == "0": 2493ba8ecdcSMichael Walsh print_timen("The caller wishes to stop after each boot failure.") 2502ce1dbaeSMichael Walsh stop_check() 2513ba8ecdcSMichael Walsh elif STOP_COMMAND.upper() == "ALL": 2523ba8ecdcSMichael Walsh print_timen("The caller wishes to stop after each boot test.") 2532ce1dbaeSMichael Walsh stop_check() 2543ba8ecdcSMichael Walsh elif len(STOP_COMMAND) > 0: 25571fec43fSMichael Walsh shell_rc, out_buf = shell_cmd(STOP_COMMAND, quiet=quiet, show_err=0) 2563ba8ecdcSMichael Walsh if shell_rc != 0: 2573ba8ecdcSMichael Walsh print_timen("The caller wishes to stop test execution.") 2582ce1dbaeSMichael Walsh stop_check() 2593ba8ecdcSMichael Walsh 2603c4869a2SMichael Walsh rest_fail() 2613c4869a2SMichael Walsh 2623c4869a2SMichael Walsh esel_stop_check() 2633c4869a2SMichael Walsh 264f7129672SMichael Shepos pel_stop_check() 265f7129672SMichael Shepos 26671fec43fSMichael Walsh if STOP_VERIFY_HARDWARE_FAIL: 26720f38712SPatrick Williams hardware_error_found = restore_plug_in_value(0, "Verify_hardware") 26871fec43fSMichael Walsh if hardware_error_found: 26920f38712SPatrick Williams print_timen( 27020f38712SPatrick Williams "The caller wishes to stop test execution when the" 2717899a451SGeorge Keishing " Verify_hardware plug-in detects a hardware error." 27220f38712SPatrick Williams ) 27371fec43fSMichael Walsh stop_check() 27471fec43fSMichael Walsh 2753ba8ecdcSMichael Walsh qprint_timen("The caller does not wish to stop the test run.") 2763ba8ecdcSMichael Walsh 277004ad3c9SJoy Onyerikwu 27871fec43fSMichael Walshmain() 279