xref: /openbmc/openbmc-test-automation/bin/plug_ins/Stop/cp_stop_check (revision f7129672e8ba35d2c3bc4394444d592b92cc882d)
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
73ba8ecdcSMichael Walshimport sys
83ba8ecdcSMichael Walshimport subprocess
9b3dffe8cSMichael Sheposimport os
10*f7129672SMichael Sheposimport re
113ba8ecdcSMichael Walsh
123ba8ecdcSMichael Walshfrom gen_print import *
133ba8ecdcSMichael Walshfrom gen_valid import *
143ba8ecdcSMichael Walshfrom gen_arg import *
153ba8ecdcSMichael Walshfrom gen_misc import *
163ba8ecdcSMichael Walshfrom gen_cmd import *
173ba8ecdcSMichael Walshfrom gen_plug_in_utils import *
183ba8ecdcSMichael Walshfrom gen_call_robot import *
193ba8ecdcSMichael Walsh
202ea965ceSMichael Walsh# Set exit_on_error for gen_valid functions.
212ea965ceSMichael Walshset_exit_on_error(True)
222ea965ceSMichael Walsh
2371fec43fSMichael Walsh# Initialize default plug-in parms..
2471fec43fSMichael WalshSTOP_REST_FAIL = 0
253ba8ecdcSMichael WalshSTOP_COMMAND = ''
263ba8ecdcSMichael Walshstop_test_rc = 2
2771fec43fSMichael WalshSTOP_VERIFY_HARDWARE_FAIL = 0
2871fec43fSMichael Walsh
293ba8ecdcSMichael Walsh
303ba8ecdcSMichael Walsh# Create parser object to process command line parameters and args.
313ba8ecdcSMichael Walshparser = argparse.ArgumentParser(
323ba8ecdcSMichael Walsh    usage='%(prog)s [OPTIONS]',
333ba8ecdcSMichael Walsh    description="If the \"Stop\" plug-in is selected by the user, %(prog)s" +
343ba8ecdcSMichael Walsh    " is called by OBMC Boot Test after each boot test.  If %(prog)s returns" +
353ba8ecdcSMichael Walsh    " " + str(stop_test_rc) + ", then OBMC Boot Test will stop.  The user" +
363ba8ecdcSMichael Walsh    " may set environment variable STOP_COMMAND to contain any valid bash" +
373ba8ecdcSMichael Walsh    " command or program.  %(prog)s will run this stop command.  If the stop" +
383ba8ecdcSMichael Walsh    " command returns non-zero, then %(prog)s will return " +
393ba8ecdcSMichael Walsh    str(stop_test_rc) + ".  %(prog)s recognizes some special values for" +
403ba8ecdcSMichael Walsh    " STOP_COMMAND: 1) \"FAIL\" means that OBMC Boot Test should stop" +
413ba8ecdcSMichael Walsh    " whenever a boot test fails. 2) \"ALL\" means that OBMC Boot Test" +
423ba8ecdcSMichael Walsh    " should stop after any boot test.  If environment variable" +
433ba8ecdcSMichael Walsh    " STOP_REST_FAIL is set, OBMC Boot Test will stop if REST commands are" +
443ba8ecdcSMichael Walsh    " no longer working.",
458d6bf60fSMichael Walsh    formatter_class=argparse.ArgumentDefaultsHelpFormatter,
463ba8ecdcSMichael Walsh    prefix_chars='-+')
473ba8ecdcSMichael Walsh
48410b1787SMichael Walsh# The stock_list will be passed to gen_get_options.  We populate it with the names of stock parm options we
49410b1787SMichael Walsh# want.  These stock parms are pre-defined by gen_get_options.
501ea9b7a6SMichael Walshstock_list = [("test_mode", get_plug_default("test_mode", 0)),
513ba8ecdcSMichael Walsh              ("quiet", get_plug_default("quiet", 0)),
523ba8ecdcSMichael Walsh              ("debug", get_plug_default("debug", 0))]
533ba8ecdcSMichael Walsh
543ba8ecdcSMichael Walsh
553ba8ecdcSMichael Walshdef exit_function(signal_number=0,
563ba8ecdcSMichael Walsh                  frame=None):
573ba8ecdcSMichael Walsh    r"""
58410b1787SMichael Walsh    Execute whenever the program ends normally or with the signals that we catch (i.e. TERM, INT).
593ba8ecdcSMichael Walsh
6071fec43fSMichael Walsh    This function will be called by gen_exit_function().
6171fec43fSMichael Walsh    """
623ba8ecdcSMichael Walsh
63a0ce75a7SMichael Walsh    process_robot_output_files()
64a0ce75a7SMichael Walsh
653ba8ecdcSMichael Walsh
663ba8ecdcSMichael Walshdef validate_parms():
673ba8ecdcSMichael Walsh    r"""
6871fec43fSMichael Walsh    Validate program parameters, etc.
6971fec43fSMichael Walsh
7071fec43fSMichael Walsh    This function will be called by gen_setup().
713ba8ecdcSMichael Walsh    """
723ba8ecdcSMichael Walsh
733ba8ecdcSMichael Walsh    get_plug_vars()
743ba8ecdcSMichael Walsh
753ba8ecdcSMichael Walsh
762ce1dbaeSMichael Walshdef stop_check():
772ce1dbaeSMichael Walsh    r"""
782ce1dbaeSMichael Walsh    Stop this program with the stop check return code.
792ce1dbaeSMichael Walsh    """
802ce1dbaeSMichael Walsh
812ce1dbaeSMichael Walsh    if MASTER_PID != PROGRAM_PID:
82c213d499SMichael Walsh        save_plug_in_value(stop_check_rc=stop_test_rc)
832ce1dbaeSMichael Walsh    exit(stop_test_rc)
842ce1dbaeSMichael Walsh
852ce1dbaeSMichael Walsh
863ba8ecdcSMichael Walshdef rest_fail():
873ba8ecdcSMichael Walsh    r"""
88410b1787SMichael Walsh    If STOP_REST_FAIL, then this function will determine whether REST commands to the target are working.  If
89410b1787SMichael Walsh    not, this function will stop the program by returning stop_test_rc.
903ba8ecdcSMichael Walsh    """
913ba8ecdcSMichael Walsh
9271fec43fSMichael Walsh    if not STOP_REST_FAIL:
933ba8ecdcSMichael Walsh        return
943ba8ecdcSMichael Walsh
95b3dffe8cSMichael Shepos    REDFISH_SUPPORT_TRANS_STATE = int(os.environ.get('REDFISH_SUPPORT_TRANS_STATE', 0)) or \
96b3dffe8cSMichael Shepos        int(os.environ.get('AUTOBOOT_REDFISH_SUPPORT_TRANS_STATE', 0))
97b3dffe8cSMichael Shepos
98b3dffe8cSMichael Shepos    if REDFISH_SUPPORT_TRANS_STATE:
99b3dffe8cSMichael Shepos        interface = "redfish"
100b3dffe8cSMichael Shepos    else:
101b3dffe8cSMichael Shepos        interface = "rest"
102b3dffe8cSMichael Shepos
103b3dffe8cSMichael Shepos    print_timen("Checking to see whether %s commands are working." % interface)
1048ab9aea6SMichael Walsh    init_robot_out_parms(get_plug_in_package_name() + "." + pgm_name + ".")
105b3dffe8cSMichael Shepos    lib_file_path = init_robot_file_path("lib/utils.robot") + ":" \
1068ab9aea6SMichael Walsh        + init_robot_file_path("lib/gen_robot_print.py")
1073ba8ecdcSMichael Walsh    set_mod_global(lib_file_path)
1083ba8ecdcSMichael Walsh    timeout = '0 seconds'
1093ba8ecdcSMichael Walsh    interval = '1 second'
110b3dffe8cSMichael Shepos    keyword_string = "${match_state}=  Create Dictionary  %s=1 ;" % interface +\
1113ba8ecdcSMichael Walsh        " ${state}=  Wait State  ${match_state}  " + timeout + "  " +\
1123ba8ecdcSMichael Walsh        interval + "  quiet=${1} ; Rpvar  state"
1133ba8ecdcSMichael Walsh    set_mod_global(keyword_string)
114046fe223SMichael Walsh    cmd_buf = create_robot_cmd_string("extended/run_keyword.robot", OPENBMC_HOST, SSH_PORT, HTTPS_PORT,
115a7d6614fSMichael Shepos                                      REST_USERNAME, REST_PASSWORD, OPENBMC_USERNAME, OPENBMC_PASSWORD,
116b3dffe8cSMichael Shepos                                      REDFISH_SUPPORT_TRANS_STATE, keyword_string, lib_file_path, quiet,
117046fe223SMichael Walsh                                      test_mode, debug, outputdir, output, log, report, loglevel)
1183ba8ecdcSMichael Walsh    if not robot_cmd_fnc(cmd_buf):
119b3dffe8cSMichael Shepos        print_timen("The caller wishes to stop test execution if %s commands are failing." % interface)
1202ce1dbaeSMichael Walsh        stop_check()
121b3dffe8cSMichael Shepos    print_timen("%s commands are working so no reason as of yet to stop the test." % interface)
1223ba8ecdcSMichael Walsh
1233ba8ecdcSMichael Walsh
1243ba8ecdcSMichael Walshdef esel_stop_check():
1253ba8ecdcSMichael Walsh    r"""
126410b1787SMichael Walsh    Run the esel_stop_check program to determine whether any eSEL entries found warrant stopping the test
127410b1787SMichael Walsh    run.  See esel_stop_check help text for details.
1283ba8ecdcSMichael Walsh    """
1293ba8ecdcSMichael Walsh
1303ba8ecdcSMichael Walsh    if STOP_ESEL_STOP_FILE_PATH == "":
1313ba8ecdcSMichael Walsh        return
1323ba8ecdcSMichael Walsh
13371fec43fSMichael Walsh    cmd_buf = "esel_stop_check --esel_stop_file_path=" + STOP_ESEL_STOP_FILE_PATH
13471fec43fSMichael Walsh    shell_rc, out_buf = shell_cmd(cmd_buf, show_err=0)
1353ba8ecdcSMichael Walsh    if shell_rc == stop_test_rc:
13671fec43fSMichael Walsh        print_timen("The caller wishes to stop test execution based on the presence of certain esel entries.")
1372ce1dbaeSMichael Walsh        stop_check()
1383ba8ecdcSMichael Walsh
1393ba8ecdcSMichael Walsh
140*f7129672SMichael Sheposdef pel_stop_check():
141*f7129672SMichael Shepos    r"""
142*f7129672SMichael Shepos    Determine whether any PEL entries found warrant stopping the test
143*f7129672SMichael Shepos    run.
144*f7129672SMichael Shepos    """
145*f7129672SMichael Shepos
146*f7129672SMichael Shepos    if STOP_PEL_STOP_FILE_PATH == "":
147*f7129672SMichael Shepos        return
148*f7129672SMichael Shepos
149*f7129672SMichael Shepos    pel_txt_file_path = os.environ.get("AUTOBOOT_FFDC_DIR_PATH", "") + \
150*f7129672SMichael Shepos     os.environ.get("AUTOBOOT_FFDC_PREFIX", "") + "PEL_logs_list.json"
151*f7129672SMichael Shepos
152*f7129672SMichael Shepos    if not os.path.isfile(pel_txt_file_path):
153*f7129672SMichael Shepos        qprint_timen("The following file was not present so no further" +
154*f7129672SMichael Shepos                     " action will be taken.")
155*f7129672SMichael Shepos        qprint_var(pel_txt_file_path)
156*f7129672SMichael Shepos        return
157*f7129672SMichael Shepos
158*f7129672SMichael Shepos    default_stop_dir_path = "/afs/rchland.ibm.com/projects/esw/dvt/"
159*f7129672SMichael Shepos
160*f7129672SMichael Shepos    # If pel_stop_file_path is unqualified and cannot be found, pre-pend
161*f7129672SMichael Shepos    # default_stop_dir_path for the user.
162*f7129672SMichael Shepos    pel_stop_file_path = os.environ.get("STOP_PEL_STOP_FILE_PATH", "")
163*f7129672SMichael Shepos    if not os.path.isfile(pel_stop_file_path) and \
164*f7129672SMichael Shepos       os.path.isfile(default_stop_dir_path + pel_stop_file_path):
165*f7129672SMichael Shepos        pel_stop_file_path = default_stop_dir_path + pel_stop_file_path
166*f7129672SMichael Shepos        qprint_timen("Using default stop file path.")
167*f7129672SMichael Shepos        qprint_var(pel_stop_file_path)
168*f7129672SMichael Shepos
169*f7129672SMichael Shepos    # First, read the file in and convert it to a list.
170*f7129672SMichael Shepos    pel_stop_list = file_to_list(pel_stop_file_path, newlines=0, comments=0)
171*f7129672SMichael Shepos
172*f7129672SMichael Shepos    if len(pel_stop_list) == 0:
173*f7129672SMichael Shepos        print_timen("There are no records to process in " +
174*f7129672SMichael Shepos                    pel_stop_file_path + ".")
175*f7129672SMichael Shepos        return
176*f7129672SMichael Shepos
177*f7129672SMichael Shepos    pel_all_list = file_to_list(pel_txt_file_path, newlines=0, comments=0)
178*f7129672SMichael Shepos
179*f7129672SMichael Shepos    if len(pel_all_list) == 0:
180*f7129672SMichael Shepos        print_timen("There are no records to process in " +
181*f7129672SMichael Shepos                    pel_txt_file_path + ".")
182*f7129672SMichael Shepos        return
183*f7129672SMichael Shepos
184*f7129672SMichael Shepos    for stop_pel in pel_stop_list:
185*f7129672SMichael Shepos        for pel_all in pel_all_list:
186*f7129672SMichael Shepos            pel_match = re.search(".*SRC.*" + stop_pel + ".*", pel_all)
187*f7129672SMichael Shepos            if pel_match:
188*f7129672SMichael Shepos                print_timen("The caller wishes to stop test execution based on the presence of certain PEL entries.")
189*f7129672SMichael Shepos                stop_check()
190*f7129672SMichael Shepos
191*f7129672SMichael Shepos
1923ba8ecdcSMichael Walshdef main():
1933ba8ecdcSMichael Walsh
19471fec43fSMichael Walsh    gen_setup()
1953ba8ecdcSMichael Walsh
19680caea09SMichael Walsh    print_plug_in_header()
1973ba8ecdcSMichael Walsh
1983ba8ecdcSMichael Walsh    if STOP_COMMAND.upper() == "FAIL":
1993ba8ecdcSMichael Walsh        if AUTOBOOT_BOOT_SUCCESS == "0":
2003ba8ecdcSMichael Walsh            print_timen("The caller wishes to stop after each boot failure.")
2012ce1dbaeSMichael Walsh            stop_check()
2023ba8ecdcSMichael Walsh    elif STOP_COMMAND.upper() == "ALL":
2033ba8ecdcSMichael Walsh        print_timen("The caller wishes to stop after each boot test.")
2042ce1dbaeSMichael Walsh        stop_check()
2053ba8ecdcSMichael Walsh    elif len(STOP_COMMAND) > 0:
20671fec43fSMichael Walsh        shell_rc, out_buf = shell_cmd(STOP_COMMAND, quiet=quiet, show_err=0)
2073ba8ecdcSMichael Walsh        if shell_rc != 0:
2083ba8ecdcSMichael Walsh            print_timen("The caller wishes to stop test execution.")
2092ce1dbaeSMichael Walsh            stop_check()
2103ba8ecdcSMichael Walsh
2113c4869a2SMichael Walsh    rest_fail()
2123c4869a2SMichael Walsh
2133c4869a2SMichael Walsh    esel_stop_check()
2143c4869a2SMichael Walsh
215*f7129672SMichael Shepos    pel_stop_check()
216*f7129672SMichael Shepos
21771fec43fSMichael Walsh    if STOP_VERIFY_HARDWARE_FAIL:
21871fec43fSMichael Walsh        hardware_error_found = restore_plug_in_value(0, 'Verify_hardware')
21971fec43fSMichael Walsh        if hardware_error_found:
22071fec43fSMichael Walsh            print_timen("The caller wishes to stop test execution when the Verify_hardware plug-in detects a"
22171fec43fSMichael Walsh                        + " hardware error.")
22271fec43fSMichael Walsh            stop_check()
22371fec43fSMichael Walsh
2243ba8ecdcSMichael Walsh    qprint_timen("The caller does not wish to stop the test run.")
2253ba8ecdcSMichael Walsh
226004ad3c9SJoy Onyerikwu
22771fec43fSMichael Walshmain()
228