xref: /openbmc/openbmc-test-automation/bin/plug_ins/Stop/cp_stop_check (revision 7e96b132b987fa866c9a96e3f633dbb3fed5b4d9)
13ba8ecdcSMichael Walsh#!/usr/bin/env python
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
93ba8ecdcSMichael Walsh
103ba8ecdcSMichael Walshsave_path_0 = sys.path[0]
113ba8ecdcSMichael Walshdel sys.path[0]
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
213ba8ecdcSMichael Walsh# Restore sys.path[0].
223ba8ecdcSMichael Walshsys.path.insert(0, save_path_0)
233ba8ecdcSMichael Walsh
243ba8ecdcSMichael Walsh# Initialize.
253ba8ecdcSMichael WalshSTOP_REST_FAIL = ''
263ba8ecdcSMichael WalshSTOP_COMMAND = ''
273ba8ecdcSMichael Walshstop_test_rc = 2
283ba8ecdcSMichael Walsh
293ba8ecdcSMichael Walsh# Create parser object to process command line parameters and args.
303ba8ecdcSMichael Walsh
313ba8ecdcSMichael Walsh# Create parser object.
323ba8ecdcSMichael Walshparser = argparse.ArgumentParser(
333ba8ecdcSMichael Walsh    usage='%(prog)s [OPTIONS]',
343ba8ecdcSMichael Walsh    description="If the \"Stop\" plug-in is selected by the user, %(prog)s" +
353ba8ecdcSMichael Walsh    " is called by OBMC Boot Test after each boot test.  If %(prog)s returns" +
363ba8ecdcSMichael Walsh    " " + str(stop_test_rc) + ", then OBMC Boot Test will stop.  The user" +
373ba8ecdcSMichael Walsh    " may set environment variable STOP_COMMAND to contain any valid bash" +
383ba8ecdcSMichael Walsh    " command or program.  %(prog)s will run this stop command.  If the stop" +
393ba8ecdcSMichael Walsh    " command returns non-zero, then %(prog)s will return " +
403ba8ecdcSMichael Walsh    str(stop_test_rc) + ".  %(prog)s recognizes some special values for" +
413ba8ecdcSMichael Walsh    " STOP_COMMAND: 1) \"FAIL\" means that OBMC Boot Test should stop" +
423ba8ecdcSMichael Walsh    " whenever a boot test fails. 2) \"ALL\" means that OBMC Boot Test" +
433ba8ecdcSMichael Walsh    " should stop after any boot test.  If environment variable" +
443ba8ecdcSMichael Walsh    " STOP_REST_FAIL is set, OBMC Boot Test will stop if REST commands are" +
453ba8ecdcSMichael Walsh    " no longer working.",
463ba8ecdcSMichael Walsh    formatter_class=argparse.RawTextHelpFormatter,
473ba8ecdcSMichael Walsh    prefix_chars='-+')
483ba8ecdcSMichael Walsh
493ba8ecdcSMichael Walsh# The stock_list will be passed to gen_get_options.  We populate it with the
503ba8ecdcSMichael Walsh# names of stock parm options we want.  These stock parms are pre-defined by
513ba8ecdcSMichael Walsh# gen_get_options.
523ba8ecdcSMichael Walshstock_list = [("test_mode", 0),
533ba8ecdcSMichael Walsh              ("quiet", get_plug_default("quiet", 0)),
543ba8ecdcSMichael Walsh              ("debug", get_plug_default("debug", 0))]
553ba8ecdcSMichael Walsh
563ba8ecdcSMichael Walsh
573ba8ecdcSMichael Walshdef exit_function(signal_number=0,
583ba8ecdcSMichael Walsh                  frame=None):
593ba8ecdcSMichael Walsh    r"""
603ba8ecdcSMichael Walsh    Execute whenever the program ends normally or with the signals that we
613ba8ecdcSMichael Walsh    catch (i.e. TERM, INT).
623ba8ecdcSMichael Walsh    """
633ba8ecdcSMichael Walsh
643ba8ecdcSMichael Walsh    dprint_executing()
653ba8ecdcSMichael Walsh    dprint_var(signal_number)
663ba8ecdcSMichael Walsh
67a0ce75a7SMichael Walsh    process_robot_output_files()
68a0ce75a7SMichael Walsh
693ba8ecdcSMichael Walsh    qprint_pgm_footer()
703ba8ecdcSMichael Walsh
713ba8ecdcSMichael Walsh
723ba8ecdcSMichael Walshdef signal_handler(signal_number,
733ba8ecdcSMichael Walsh                   frame):
743ba8ecdcSMichael Walsh    r"""
753ba8ecdcSMichael Walsh    Handle signals.  Without a function to catch a SIGTERM or SIGINT, our
763ba8ecdcSMichael Walsh    program would terminate immediately with return code 143 and without
773ba8ecdcSMichael Walsh    calling our exit_function.
783ba8ecdcSMichael Walsh    """
793ba8ecdcSMichael Walsh
803ba8ecdcSMichael Walsh    # Our convention is to set up exit_function with atexit.register() so
813ba8ecdcSMichael Walsh    # there is no need to explicitly call exit_function from here.
823ba8ecdcSMichael Walsh
833ba8ecdcSMichael Walsh    dprint_executing()
843ba8ecdcSMichael Walsh
853ba8ecdcSMichael Walsh    # Calling exit prevents us from returning to the code that was running
863ba8ecdcSMichael Walsh    # when we received the signal.
873ba8ecdcSMichael Walsh    exit(0)
883ba8ecdcSMichael Walsh
893ba8ecdcSMichael Walsh
903ba8ecdcSMichael Walshdef validate_parms():
913ba8ecdcSMichael Walsh    r"""
923ba8ecdcSMichael Walsh    Validate program parameters, etc.  Return True or False (i.e. pass/fail)
933ba8ecdcSMichael Walsh    accordingly.
943ba8ecdcSMichael Walsh    """
953ba8ecdcSMichael Walsh
963ba8ecdcSMichael Walsh    get_plug_vars()
973ba8ecdcSMichael Walsh
983ba8ecdcSMichael Walsh    if not valid_value(AUTOBOOT_OPENBMC_HOST, ["", None]):
993ba8ecdcSMichael Walsh        return False
1003ba8ecdcSMichael Walsh
1013ba8ecdcSMichael Walsh    gen_post_validation(exit_function, signal_handler)
1023ba8ecdcSMichael Walsh
1033ba8ecdcSMichael Walsh    return True
1043ba8ecdcSMichael Walsh
1053ba8ecdcSMichael Walsh
1063ba8ecdcSMichael Walshdef rest_fail():
1073ba8ecdcSMichael Walsh    r"""
1083ba8ecdcSMichael Walsh    If STOP_REST_FAIL, then this function will determine whether REST commands
1093ba8ecdcSMichael Walsh    to the target are working.  If not, this function will stop the program by
1103ba8ecdcSMichael Walsh    returning stop_test_rc.
1113ba8ecdcSMichael Walsh    """
1123ba8ecdcSMichael Walsh
1133ba8ecdcSMichael Walsh    if STOP_REST_FAIL != '1':
1143ba8ecdcSMichael Walsh        return
1153ba8ecdcSMichael Walsh
1163ba8ecdcSMichael Walsh    print_timen("Checking to see whether REST commands are working.")
1178ab9aea6SMichael Walsh    init_robot_out_parms(get_plug_in_package_name() + "." + pgm_name + ".")
1188ab9aea6SMichael Walsh    lib_file_path = init_robot_file_path("lib/state.py") + ":"\
1198ab9aea6SMichael Walsh        + init_robot_file_path("lib/gen_robot_print.py")
1203ba8ecdcSMichael Walsh    set_mod_global(lib_file_path)
1213ba8ecdcSMichael Walsh    timeout = '0 seconds'
1223ba8ecdcSMichael Walsh    interval = '1 second'
1233ba8ecdcSMichael Walsh    keyword_string = "${match_state}=  Create Dictionary  rest=1 ;" +\
1243ba8ecdcSMichael Walsh        " ${state}=  Wait State  ${match_state}  " + timeout + "  " +\
1253ba8ecdcSMichael Walsh        interval + "  quiet=${1} ; Rpvar  state"
1263ba8ecdcSMichael Walsh    set_mod_global(keyword_string)
1273ba8ecdcSMichael Walsh
1283ba8ecdcSMichael Walsh    cmd_buf = create_robot_cmd_string("extended/run_keyword.robot",
129*7e96b132SMichael Walsh                                      OPENBMC_HOST, keyword_string,
1303ba8ecdcSMichael Walsh                                      lib_file_path, quiet, test_mode, debug,
1313ba8ecdcSMichael Walsh                                      outputdir, output, log, report, loglevel)
1323ba8ecdcSMichael Walsh    if not robot_cmd_fnc(cmd_buf):
1333ba8ecdcSMichael Walsh        print_timen("The caller wishes to stop test execution if REST" +
1343ba8ecdcSMichael Walsh                    " commands are failing.")
135*7e96b132SMichael Walsh        exit(stop_test_rc)
1363ba8ecdcSMichael Walsh    print_timen("REST commands are working so no reason as of yet to stop" +
1373ba8ecdcSMichael Walsh                " the test.")
1383ba8ecdcSMichael Walsh
1393ba8ecdcSMichael Walsh
1403ba8ecdcSMichael Walshdef esel_stop_check():
1413ba8ecdcSMichael Walsh    r"""
1423ba8ecdcSMichael Walsh    Run the esel_stop_check program to determine whether any eSEL entries
1437732c7e2SGunnar Mills    found warrant stopping the test run.  See esel_stop_check help text for
1443ba8ecdcSMichael Walsh    details.
1453ba8ecdcSMichael Walsh    """
1463ba8ecdcSMichael Walsh
1473ba8ecdcSMichael Walsh    if STOP_ESEL_STOP_FILE_PATH == "":
1483ba8ecdcSMichael Walsh        return
1493ba8ecdcSMichael Walsh
1503ba8ecdcSMichael Walsh    cmd_buf = "esel_stop_check --esel_stop_file_path=" +\
1513ba8ecdcSMichael Walsh        STOP_ESEL_STOP_FILE_PATH
1523ba8ecdcSMichael Walsh    shell_rc, out_buf = cmd_fnc_u(cmd_buf, show_err=0)
1533ba8ecdcSMichael Walsh    if shell_rc == stop_test_rc:
1543ba8ecdcSMichael Walsh        print_timen("The caller wishes to stop test execution based on the" +
1553ba8ecdcSMichael Walsh                    " presence of certain esel entries.")
156*7e96b132SMichael Walsh        exit(stop_test_rc)
1573ba8ecdcSMichael Walsh
1583ba8ecdcSMichael Walsh
1593ba8ecdcSMichael Walshdef main():
1603ba8ecdcSMichael Walsh
1613ba8ecdcSMichael Walsh    if not gen_get_options(parser, stock_list):
1623ba8ecdcSMichael Walsh        return False
1633ba8ecdcSMichael Walsh
1643ba8ecdcSMichael Walsh    if not validate_parms():
1653ba8ecdcSMichael Walsh        return False
1663ba8ecdcSMichael Walsh
1673ba8ecdcSMichael Walsh    qprint_pgm_header()
1683ba8ecdcSMichael Walsh
1693ba8ecdcSMichael Walsh    if not debug:
1703ba8ecdcSMichael Walsh        qprint_vars(STOP_REST_FAIL, STOP_COMMAND, AUTOBOOT_BOOT_SUCCESS)
1713ba8ecdcSMichael Walsh
1723ba8ecdcSMichael Walsh    dprint_plug_vars()
1733ba8ecdcSMichael Walsh
1743ba8ecdcSMichael Walsh    rest_fail()
1753ba8ecdcSMichael Walsh
1763ba8ecdcSMichael Walsh    esel_stop_check()
1773ba8ecdcSMichael Walsh
1783ba8ecdcSMichael Walsh    if STOP_COMMAND.upper() == "FAIL":
1793ba8ecdcSMichael Walsh        if AUTOBOOT_BOOT_SUCCESS == "0":
1803ba8ecdcSMichael Walsh            print_timen("The caller wishes to stop after each boot failure.")
181*7e96b132SMichael Walsh            exit(stop_test_rc)
1823ba8ecdcSMichael Walsh    elif STOP_COMMAND.upper() == "ALL":
1833ba8ecdcSMichael Walsh        print_timen("The caller wishes to stop after each boot test.")
184*7e96b132SMichael Walsh        exit(stop_test_rc)
1853ba8ecdcSMichael Walsh    elif len(STOP_COMMAND) > 0:
1863ba8ecdcSMichael Walsh        shell_rc, out_buf = cmd_fnc_u(STOP_COMMAND, quiet=quiet, show_err=0)
1873ba8ecdcSMichael Walsh        if shell_rc != 0:
1883ba8ecdcSMichael Walsh            print_timen("The caller wishes to stop test execution.")
189*7e96b132SMichael Walsh            exit(stop_test_rc)
1903ba8ecdcSMichael Walsh
1913ba8ecdcSMichael Walsh    qprint_timen("The caller does not wish to stop the test run.")
1923ba8ecdcSMichael Walsh    return True
1933ba8ecdcSMichael Walsh
194c8483e36SMichael Walsh# Main
195004ad3c9SJoy Onyerikwu
196*7e96b132SMichael Walsh
1973ba8ecdcSMichael Walshif not main():
1983ba8ecdcSMichael Walsh    exit(1)
199