xref: /openbmc/openbmc-test-automation/bin/plug_ins/Stop/cp_stop_check (revision 3c4869a2d88e43f961914dfb309ffe76dff8226d)
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
1062ce1dbaeSMichael Walshdef stop_check():
1072ce1dbaeSMichael Walsh    r"""
1082ce1dbaeSMichael Walsh    Stop this program with the stop check return code.
1092ce1dbaeSMichael Walsh    """
1102ce1dbaeSMichael Walsh
1112ce1dbaeSMichael Walsh    if MASTER_PID != PROGRAM_PID:
1122ce1dbaeSMichael Walsh        stop_check_rc = stop_test_rc
1132ce1dbaeSMichael Walsh        save_plug_in_value(stop_check_rc)
1142ce1dbaeSMichael Walsh    exit(stop_test_rc)
1152ce1dbaeSMichael Walsh
1162ce1dbaeSMichael Walsh
1173ba8ecdcSMichael Walshdef rest_fail():
1183ba8ecdcSMichael Walsh    r"""
1193ba8ecdcSMichael Walsh    If STOP_REST_FAIL, then this function will determine whether REST commands
1203ba8ecdcSMichael Walsh    to the target are working.  If not, this function will stop the program by
1213ba8ecdcSMichael Walsh    returning stop_test_rc.
1223ba8ecdcSMichael Walsh    """
1233ba8ecdcSMichael Walsh
1243ba8ecdcSMichael Walsh    if STOP_REST_FAIL != '1':
1253ba8ecdcSMichael Walsh        return
1263ba8ecdcSMichael Walsh
1273ba8ecdcSMichael Walsh    print_timen("Checking to see whether REST commands are working.")
1288ab9aea6SMichael Walsh    init_robot_out_parms(get_plug_in_package_name() + "." + pgm_name + ".")
1298ab9aea6SMichael Walsh    lib_file_path = init_robot_file_path("lib/state.py") + ":"\
1308ab9aea6SMichael Walsh        + init_robot_file_path("lib/gen_robot_print.py")
1313ba8ecdcSMichael Walsh    set_mod_global(lib_file_path)
1323ba8ecdcSMichael Walsh    timeout = '0 seconds'
1333ba8ecdcSMichael Walsh    interval = '1 second'
1343ba8ecdcSMichael Walsh    keyword_string = "${match_state}=  Create Dictionary  rest=1 ;" +\
1353ba8ecdcSMichael Walsh        " ${state}=  Wait State  ${match_state}  " + timeout + "  " +\
1363ba8ecdcSMichael Walsh        interval + "  quiet=${1} ; Rpvar  state"
1373ba8ecdcSMichael Walsh    set_mod_global(keyword_string)
1383ba8ecdcSMichael Walsh
1393ba8ecdcSMichael Walsh    cmd_buf = create_robot_cmd_string("extended/run_keyword.robot",
1402ce1dbaeSMichael Walsh                                      OPENBMC_HOST, REST_USERNAME,
1412ce1dbaeSMichael Walsh                                      REST_PASSWORD, keyword_string,
1423ba8ecdcSMichael Walsh                                      lib_file_path, quiet, test_mode, debug,
1433ba8ecdcSMichael Walsh                                      outputdir, output, log, report, loglevel)
1443ba8ecdcSMichael Walsh    if not robot_cmd_fnc(cmd_buf):
1453ba8ecdcSMichael Walsh        print_timen("The caller wishes to stop test execution if REST" +
1463ba8ecdcSMichael Walsh                    " commands are failing.")
1472ce1dbaeSMichael Walsh        stop_check()
1483ba8ecdcSMichael Walsh    print_timen("REST commands are working so no reason as of yet to stop" +
1493ba8ecdcSMichael Walsh                " the test.")
1503ba8ecdcSMichael Walsh
1513ba8ecdcSMichael Walsh
1523ba8ecdcSMichael Walshdef esel_stop_check():
1533ba8ecdcSMichael Walsh    r"""
1543ba8ecdcSMichael Walsh    Run the esel_stop_check program to determine whether any eSEL entries
1557732c7e2SGunnar Mills    found warrant stopping the test run.  See esel_stop_check help text for
1563ba8ecdcSMichael Walsh    details.
1573ba8ecdcSMichael Walsh    """
1583ba8ecdcSMichael Walsh
1593ba8ecdcSMichael Walsh    if STOP_ESEL_STOP_FILE_PATH == "":
1603ba8ecdcSMichael Walsh        return
1613ba8ecdcSMichael Walsh
1623ba8ecdcSMichael Walsh    cmd_buf = "esel_stop_check --esel_stop_file_path=" +\
1633ba8ecdcSMichael Walsh        STOP_ESEL_STOP_FILE_PATH
1643ba8ecdcSMichael Walsh    shell_rc, out_buf = cmd_fnc_u(cmd_buf, show_err=0)
1653ba8ecdcSMichael Walsh    if shell_rc == stop_test_rc:
1663ba8ecdcSMichael Walsh        print_timen("The caller wishes to stop test execution based on the" +
1673ba8ecdcSMichael Walsh                    " presence of certain esel entries.")
1682ce1dbaeSMichael Walsh        stop_check()
1693ba8ecdcSMichael Walsh
1703ba8ecdcSMichael Walsh
1713ba8ecdcSMichael Walshdef main():
1723ba8ecdcSMichael Walsh
1733ba8ecdcSMichael Walsh    if not gen_get_options(parser, stock_list):
1743ba8ecdcSMichael Walsh        return False
1753ba8ecdcSMichael Walsh
1763ba8ecdcSMichael Walsh    if not validate_parms():
1773ba8ecdcSMichael Walsh        return False
1783ba8ecdcSMichael Walsh
1793ba8ecdcSMichael Walsh    qprint_pgm_header()
1803ba8ecdcSMichael Walsh
1813ba8ecdcSMichael Walsh    if not debug:
1823ba8ecdcSMichael Walsh        qprint_vars(STOP_REST_FAIL, STOP_COMMAND, AUTOBOOT_BOOT_SUCCESS)
1833ba8ecdcSMichael Walsh
18480caea09SMichael Walsh    print_plug_in_header()
1853ba8ecdcSMichael Walsh
1863ba8ecdcSMichael Walsh    if STOP_COMMAND.upper() == "FAIL":
1873ba8ecdcSMichael Walsh        if AUTOBOOT_BOOT_SUCCESS == "0":
1883ba8ecdcSMichael Walsh            print_timen("The caller wishes to stop after each boot failure.")
1892ce1dbaeSMichael Walsh            stop_check()
1903ba8ecdcSMichael Walsh    elif STOP_COMMAND.upper() == "ALL":
1913ba8ecdcSMichael Walsh        print_timen("The caller wishes to stop after each boot test.")
1922ce1dbaeSMichael Walsh        stop_check()
1933ba8ecdcSMichael Walsh    elif len(STOP_COMMAND) > 0:
1943ba8ecdcSMichael Walsh        shell_rc, out_buf = cmd_fnc_u(STOP_COMMAND, quiet=quiet, show_err=0)
1953ba8ecdcSMichael Walsh        if shell_rc != 0:
1963ba8ecdcSMichael Walsh            print_timen("The caller wishes to stop test execution.")
1972ce1dbaeSMichael Walsh            stop_check()
1983ba8ecdcSMichael Walsh
199*3c4869a2SMichael Walsh    rest_fail()
200*3c4869a2SMichael Walsh
201*3c4869a2SMichael Walsh    esel_stop_check()
202*3c4869a2SMichael Walsh
2033ba8ecdcSMichael Walsh    qprint_timen("The caller does not wish to stop the test run.")
2043ba8ecdcSMichael Walsh    return True
2053ba8ecdcSMichael Walsh
206004ad3c9SJoy Onyerikwu
2072ce1dbaeSMichael Walsh# Main
2087e96b132SMichael Walsh
2093ba8ecdcSMichael Walshif not main():
2103ba8ecdcSMichael Walsh    exit(1)
211