xref: /openbmc/openbmc-test-automation/bin/plug_ins/Stop/cp_stop_check (revision a57fef4a6c9c813f18edf96b707e19cb265c8785)
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
7*a57fef4aSPatrick Williamsimport argparse
83ba8ecdcSMichael Walshimport sys
93ba8ecdcSMichael Walshimport subprocess
10b3dffe8cSMichael Sheposimport os
11f7129672SMichael Sheposimport re
123ba8ecdcSMichael Walsh
133ba8ecdcSMichael Walshfrom gen_print import *
143ba8ecdcSMichael Walshfrom gen_valid import *
153ba8ecdcSMichael Walshfrom gen_arg import *
163ba8ecdcSMichael Walshfrom gen_misc import *
173ba8ecdcSMichael Walshfrom gen_cmd import *
183ba8ecdcSMichael Walshfrom gen_plug_in_utils import *
193ba8ecdcSMichael Walshfrom gen_call_robot 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
263ba8ecdcSMichael WalshSTOP_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(
333ba8ecdcSMichael Walsh    usage='%(prog)s [OPTIONS]',
34*a57fef4aSPatrick Williams    description="If the \"Stop\" plug-in is selected by the user, %(prog)s"
35*a57fef4aSPatrick Williams    + " is called by OBMC Boot Test after each boot test.  If %(prog)s returns"
36*a57fef4aSPatrick Williams    + " " + str(stop_test_rc) + ", then OBMC Boot Test will stop.  The user"
37*a57fef4aSPatrick Williams    + " may set environment variable STOP_COMMAND to contain any valid bash"
38*a57fef4aSPatrick Williams    + " command or program.  %(prog)s will run this stop command.  If the stop"
39*a57fef4aSPatrick Williams    + " command returns non-zero, then %(prog)s will return "
40*a57fef4aSPatrick Williams    + str(stop_test_rc) + ".  %(prog)s recognizes some special values for"
41*a57fef4aSPatrick Williams    + " STOP_COMMAND: 1) \"FAIL\" means that OBMC Boot Test should stop"
42*a57fef4aSPatrick Williams    + " whenever a boot test fails. 2) \"ALL\" means that OBMC Boot Test"
43*a57fef4aSPatrick Williams    + " should stop after any boot test.  If environment variable"
44*a57fef4aSPatrick Williams    + " STOP_REST_FAIL is set, OBMC Boot Test will stop if REST commands are"
45*a57fef4aSPatrick Williams    + " no longer working.",
468d6bf60fSMichael Walsh    formatter_class=argparse.ArgumentDefaultsHelpFormatter,
473ba8ecdcSMichael Walsh    prefix_chars='-+')
483ba8ecdcSMichael Walsh
49410b1787SMichael Walsh# The stock_list will be passed to gen_get_options.  We populate it with the names of stock parm options we
50410b1787SMichael Walsh# want.  These stock parms are pre-defined by gen_get_options.
511ea9b7a6SMichael Walshstock_list = [("test_mode", get_plug_default("test_mode", 0)),
523ba8ecdcSMichael Walsh              ("quiet", get_plug_default("quiet", 0)),
533ba8ecdcSMichael Walsh              ("debug", get_plug_default("debug", 0))]
543ba8ecdcSMichael Walsh
553ba8ecdcSMichael Walsh
563ba8ecdcSMichael Walshdef exit_function(signal_number=0,
573ba8ecdcSMichael Walsh                  frame=None):
583ba8ecdcSMichael Walsh    r"""
59410b1787SMichael Walsh    Execute whenever the program ends normally or with the signals that we catch (i.e. TERM, INT).
603ba8ecdcSMichael Walsh
6171fec43fSMichael Walsh    This function will be called by gen_exit_function().
6271fec43fSMichael Walsh    """
633ba8ecdcSMichael Walsh
64a0ce75a7SMichael Walsh    process_robot_output_files()
65a0ce75a7SMichael Walsh
663ba8ecdcSMichael Walsh
673ba8ecdcSMichael Walshdef validate_parms():
683ba8ecdcSMichael Walsh    r"""
6971fec43fSMichael Walsh    Validate program parameters, etc.
7071fec43fSMichael Walsh
7171fec43fSMichael Walsh    This function will be called by gen_setup().
723ba8ecdcSMichael Walsh    """
733ba8ecdcSMichael Walsh
743ba8ecdcSMichael Walsh    get_plug_vars()
753ba8ecdcSMichael Walsh
763ba8ecdcSMichael Walsh
772ce1dbaeSMichael Walshdef stop_check():
782ce1dbaeSMichael Walsh    r"""
792ce1dbaeSMichael Walsh    Stop this program with the stop check return code.
802ce1dbaeSMichael Walsh    """
812ce1dbaeSMichael Walsh
822ce1dbaeSMichael Walsh    if MASTER_PID != PROGRAM_PID:
83c213d499SMichael Walsh        save_plug_in_value(stop_check_rc=stop_test_rc)
842ce1dbaeSMichael Walsh    exit(stop_test_rc)
852ce1dbaeSMichael Walsh
862ce1dbaeSMichael Walsh
873ba8ecdcSMichael Walshdef rest_fail():
883ba8ecdcSMichael Walsh    r"""
89410b1787SMichael Walsh    If STOP_REST_FAIL, then this function will determine whether REST commands to the target are working.  If
90410b1787SMichael Walsh    not, this function will stop the program by returning stop_test_rc.
913ba8ecdcSMichael Walsh    """
923ba8ecdcSMichael Walsh
9371fec43fSMichael Walsh    if not STOP_REST_FAIL:
943ba8ecdcSMichael Walsh        return
953ba8ecdcSMichael Walsh
96b3dffe8cSMichael Shepos    REDFISH_SUPPORT_TRANS_STATE = int(os.environ.get('REDFISH_SUPPORT_TRANS_STATE', 0)) or \
97b3dffe8cSMichael Shepos        int(os.environ.get('AUTOBOOT_REDFISH_SUPPORT_TRANS_STATE', 0))
98b3dffe8cSMichael Shepos
99b3dffe8cSMichael Shepos    if REDFISH_SUPPORT_TRANS_STATE:
100b3dffe8cSMichael Shepos        interface = "redfish"
101b3dffe8cSMichael Shepos    else:
102b3dffe8cSMichael Shepos        interface = "rest"
103b3dffe8cSMichael Shepos
104b3dffe8cSMichael Shepos    print_timen("Checking to see whether %s commands are working." % interface)
1058ab9aea6SMichael Walsh    init_robot_out_parms(get_plug_in_package_name() + "." + pgm_name + ".")
106b3dffe8cSMichael Shepos    lib_file_path = init_robot_file_path("lib/utils.robot") + ":" \
1078ab9aea6SMichael Walsh        + init_robot_file_path("lib/gen_robot_print.py")
1083ba8ecdcSMichael Walsh    set_mod_global(lib_file_path)
1093ba8ecdcSMichael Walsh    timeout = '0 seconds'
1103ba8ecdcSMichael Walsh    interval = '1 second'
111b3dffe8cSMichael Shepos    keyword_string = "${match_state}=  Create Dictionary  %s=1 ;" % interface +\
1123ba8ecdcSMichael Walsh        " ${state}=  Wait State  ${match_state}  " + timeout + "  " +\
1133ba8ecdcSMichael Walsh        interval + "  quiet=${1} ; Rpvar  state"
1143ba8ecdcSMichael Walsh    set_mod_global(keyword_string)
115046fe223SMichael Walsh    cmd_buf = create_robot_cmd_string("extended/run_keyword.robot", OPENBMC_HOST, SSH_PORT, HTTPS_PORT,
116a7d6614fSMichael Shepos                                      REST_USERNAME, REST_PASSWORD, OPENBMC_USERNAME, OPENBMC_PASSWORD,
117b3dffe8cSMichael Shepos                                      REDFISH_SUPPORT_TRANS_STATE, keyword_string, lib_file_path, quiet,
118046fe223SMichael Walsh                                      test_mode, debug, outputdir, output, log, report, loglevel)
1193ba8ecdcSMichael Walsh    if not robot_cmd_fnc(cmd_buf):
120b3dffe8cSMichael Shepos        print_timen("The caller wishes to stop test execution if %s commands are failing." % interface)
1212ce1dbaeSMichael Walsh        stop_check()
122b3dffe8cSMichael Shepos    print_timen("%s commands are working so no reason as of yet to stop the test." % interface)
1233ba8ecdcSMichael Walsh
1243ba8ecdcSMichael Walsh
1253ba8ecdcSMichael Walshdef esel_stop_check():
1263ba8ecdcSMichael Walsh    r"""
127410b1787SMichael Walsh    Run the esel_stop_check program to determine whether any eSEL entries found warrant stopping the test
128410b1787SMichael Walsh    run.  See esel_stop_check help text for details.
1293ba8ecdcSMichael Walsh    """
1303ba8ecdcSMichael Walsh
1313ba8ecdcSMichael Walsh    if STOP_ESEL_STOP_FILE_PATH == "":
1323ba8ecdcSMichael Walsh        return
1333ba8ecdcSMichael Walsh
13471fec43fSMichael Walsh    cmd_buf = "esel_stop_check --esel_stop_file_path=" + STOP_ESEL_STOP_FILE_PATH
13571fec43fSMichael Walsh    shell_rc, out_buf = shell_cmd(cmd_buf, show_err=0)
1363ba8ecdcSMichael Walsh    if shell_rc == stop_test_rc:
13771fec43fSMichael Walsh        print_timen("The caller wishes to stop test execution based on the presence of certain esel entries.")
1382ce1dbaeSMichael Walsh        stop_check()
1393ba8ecdcSMichael Walsh
1403ba8ecdcSMichael Walsh
141f7129672SMichael Sheposdef pel_stop_check():
142f7129672SMichael Shepos    r"""
143f7129672SMichael Shepos    Determine whether any PEL entries found warrant stopping the test
144f7129672SMichael Shepos    run.
145f7129672SMichael Shepos    """
146f7129672SMichael Shepos
147f7129672SMichael Shepos    if STOP_PEL_STOP_FILE_PATH == "":
148f7129672SMichael Shepos        return
149f7129672SMichael Shepos
150f7129672SMichael Shepos    pel_txt_file_path = os.environ.get("AUTOBOOT_FFDC_DIR_PATH", "") + \
151f7129672SMichael Shepos        os.environ.get("AUTOBOOT_FFDC_PREFIX", "") + "PEL_logs_list.json"
152f7129672SMichael Shepos
153f7129672SMichael Shepos    if not os.path.isfile(pel_txt_file_path):
154*a57fef4aSPatrick Williams        qprint_timen("The following file was not present so no further"
155*a57fef4aSPatrick Williams                     + " action will be taken.")
156f7129672SMichael Shepos        qprint_var(pel_txt_file_path)
157f7129672SMichael Shepos        return
158f7129672SMichael Shepos
159f7129672SMichael Shepos    default_stop_dir_path = "/afs/rchland.ibm.com/projects/esw/dvt/"
160f7129672SMichael Shepos
161f7129672SMichael Shepos    # If pel_stop_file_path is unqualified and cannot be found, pre-pend
162f7129672SMichael Shepos    # default_stop_dir_path for the user.
163f7129672SMichael Shepos    pel_stop_file_path = os.environ.get("STOP_PEL_STOP_FILE_PATH", "")
164f7129672SMichael Shepos    if not os.path.isfile(pel_stop_file_path) and \
165f7129672SMichael Shepos       os.path.isfile(default_stop_dir_path + pel_stop_file_path):
166f7129672SMichael Shepos        pel_stop_file_path = default_stop_dir_path + pel_stop_file_path
167f7129672SMichael Shepos        qprint_timen("Using default stop file path.")
168f7129672SMichael Shepos        qprint_var(pel_stop_file_path)
169f7129672SMichael Shepos
170f7129672SMichael Shepos    # First, read the file in and convert it to a list.
171f7129672SMichael Shepos    pel_stop_list = file_to_list(pel_stop_file_path, newlines=0, comments=0)
172f7129672SMichael Shepos
173f7129672SMichael Shepos    if len(pel_stop_list) == 0:
174*a57fef4aSPatrick Williams        print_timen("There are no records to process in "
175*a57fef4aSPatrick Williams                    + pel_stop_file_path + ".")
176f7129672SMichael Shepos        return
177f7129672SMichael Shepos
178f7129672SMichael Shepos    pel_all_list = file_to_list(pel_txt_file_path, newlines=0, comments=0)
179f7129672SMichael Shepos
180f7129672SMichael Shepos    if len(pel_all_list) == 0:
181*a57fef4aSPatrick Williams        print_timen("There are no records to process in "
182*a57fef4aSPatrick Williams                    + pel_txt_file_path + ".")
183f7129672SMichael Shepos        return
184f7129672SMichael Shepos
185f7129672SMichael Shepos    for stop_pel in pel_stop_list:
186f7129672SMichael Shepos        for pel_all in pel_all_list:
187f7129672SMichael Shepos            pel_match = re.search(".*SRC.*" + stop_pel + ".*", pel_all)
188f7129672SMichael Shepos            if pel_match:
189*a57fef4aSPatrick Williams                print_timen("The caller wishes to stop test execution based on "
190*a57fef4aSPatrick Williams                            + "the presence of certain PEL entries.")
191f7129672SMichael Shepos                stop_check()
192f7129672SMichael Shepos
193f7129672SMichael Shepos
1943ba8ecdcSMichael Walshdef main():
1953ba8ecdcSMichael Walsh
19671fec43fSMichael Walsh    gen_setup()
1973ba8ecdcSMichael Walsh
19880caea09SMichael Walsh    print_plug_in_header()
1993ba8ecdcSMichael Walsh
2003ba8ecdcSMichael Walsh    if STOP_COMMAND.upper() == "FAIL":
2013ba8ecdcSMichael Walsh        if AUTOBOOT_BOOT_SUCCESS == "0":
2023ba8ecdcSMichael Walsh            print_timen("The caller wishes to stop after each boot failure.")
2032ce1dbaeSMichael Walsh            stop_check()
2043ba8ecdcSMichael Walsh    elif STOP_COMMAND.upper() == "ALL":
2053ba8ecdcSMichael Walsh        print_timen("The caller wishes to stop after each boot test.")
2062ce1dbaeSMichael Walsh        stop_check()
2073ba8ecdcSMichael Walsh    elif len(STOP_COMMAND) > 0:
20871fec43fSMichael Walsh        shell_rc, out_buf = shell_cmd(STOP_COMMAND, quiet=quiet, show_err=0)
2093ba8ecdcSMichael Walsh        if shell_rc != 0:
2103ba8ecdcSMichael Walsh            print_timen("The caller wishes to stop test execution.")
2112ce1dbaeSMichael Walsh            stop_check()
2123ba8ecdcSMichael Walsh
2133c4869a2SMichael Walsh    rest_fail()
2143c4869a2SMichael Walsh
2153c4869a2SMichael Walsh    esel_stop_check()
2163c4869a2SMichael Walsh
217f7129672SMichael Shepos    pel_stop_check()
218f7129672SMichael Shepos
21971fec43fSMichael Walsh    if STOP_VERIFY_HARDWARE_FAIL:
22071fec43fSMichael Walsh        hardware_error_found = restore_plug_in_value(0, 'Verify_hardware')
22171fec43fSMichael Walsh        if hardware_error_found:
22271fec43fSMichael Walsh            print_timen("The caller wishes to stop test execution when the Verify_hardware plug-in detects a"
22371fec43fSMichael Walsh                        + " hardware error.")
22471fec43fSMichael Walsh            stop_check()
22571fec43fSMichael Walsh
2263ba8ecdcSMichael Walsh    qprint_timen("The caller does not wish to stop the test run.")
2273ba8ecdcSMichael Walsh
228004ad3c9SJoy Onyerikwu
22971fec43fSMichael Walshmain()
230