xref: /openbmc/openbmc-test-automation/bin/plug_ins/Stop/cp_stop_check (revision 1ea9b7a64279f952bcb00dff006e6b2e7477e8aa)
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
242ea965ceSMichael Walsh# Set exit_on_error for gen_valid functions.
252ea965ceSMichael Walshset_exit_on_error(True)
262ea965ceSMichael Walsh
273ba8ecdcSMichael Walsh# Initialize.
283ba8ecdcSMichael WalshSTOP_REST_FAIL = ''
293ba8ecdcSMichael WalshSTOP_COMMAND = ''
303ba8ecdcSMichael Walshstop_test_rc = 2
313ba8ecdcSMichael Walsh
323ba8ecdcSMichael Walsh# Create parser object to process command line parameters and args.
333ba8ecdcSMichael Walsh
343ba8ecdcSMichael Walsh# Create parser object.
353ba8ecdcSMichael Walshparser = argparse.ArgumentParser(
363ba8ecdcSMichael Walsh    usage='%(prog)s [OPTIONS]',
373ba8ecdcSMichael Walsh    description="If the \"Stop\" plug-in is selected by the user, %(prog)s" +
383ba8ecdcSMichael Walsh    " is called by OBMC Boot Test after each boot test.  If %(prog)s returns" +
393ba8ecdcSMichael Walsh    " " + str(stop_test_rc) + ", then OBMC Boot Test will stop.  The user" +
403ba8ecdcSMichael Walsh    " may set environment variable STOP_COMMAND to contain any valid bash" +
413ba8ecdcSMichael Walsh    " command or program.  %(prog)s will run this stop command.  If the stop" +
423ba8ecdcSMichael Walsh    " command returns non-zero, then %(prog)s will return " +
433ba8ecdcSMichael Walsh    str(stop_test_rc) + ".  %(prog)s recognizes some special values for" +
443ba8ecdcSMichael Walsh    " STOP_COMMAND: 1) \"FAIL\" means that OBMC Boot Test should stop" +
453ba8ecdcSMichael Walsh    " whenever a boot test fails. 2) \"ALL\" means that OBMC Boot Test" +
463ba8ecdcSMichael Walsh    " should stop after any boot test.  If environment variable" +
473ba8ecdcSMichael Walsh    " STOP_REST_FAIL is set, OBMC Boot Test will stop if REST commands are" +
483ba8ecdcSMichael Walsh    " no longer working.",
493ba8ecdcSMichael Walsh    formatter_class=argparse.RawTextHelpFormatter,
503ba8ecdcSMichael Walsh    prefix_chars='-+')
513ba8ecdcSMichael Walsh
52410b1787SMichael Walsh# The stock_list will be passed to gen_get_options.  We populate it with the names of stock parm options we
53410b1787SMichael Walsh# want.  These stock parms are pre-defined by gen_get_options.
54*1ea9b7a6SMichael Walshstock_list = [("test_mode", get_plug_default("test_mode", 0)),
553ba8ecdcSMichael Walsh              ("quiet", get_plug_default("quiet", 0)),
563ba8ecdcSMichael Walsh              ("debug", get_plug_default("debug", 0))]
573ba8ecdcSMichael Walsh
583ba8ecdcSMichael Walsh
593ba8ecdcSMichael Walshdef exit_function(signal_number=0,
603ba8ecdcSMichael Walsh                  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    """
643ba8ecdcSMichael Walsh
653ba8ecdcSMichael Walsh    dprint_executing()
663ba8ecdcSMichael Walsh    dprint_var(signal_number)
673ba8ecdcSMichael Walsh
68a0ce75a7SMichael Walsh    process_robot_output_files()
69a0ce75a7SMichael Walsh
703ba8ecdcSMichael Walsh    qprint_pgm_footer()
713ba8ecdcSMichael Walsh
723ba8ecdcSMichael Walsh
733ba8ecdcSMichael Walshdef signal_handler(signal_number,
743ba8ecdcSMichael Walsh                   frame):
753ba8ecdcSMichael Walsh    r"""
76410b1787SMichael Walsh    Handle signals.  Without a function to catch a SIGTERM or SIGINT, our program would terminate immediately
77410b1787SMichael Walsh    with return code 143 and without calling our exit_function.
783ba8ecdcSMichael Walsh    """
793ba8ecdcSMichael Walsh
80410b1787SMichael Walsh    # Our convention is to set up exit_function with atexit.register() so there is no need to explicitly
81410b1787SMichael Walsh    # call exit_function from here.
823ba8ecdcSMichael Walsh
833ba8ecdcSMichael Walsh    dprint_executing()
843ba8ecdcSMichael Walsh
85410b1787SMichael Walsh    # Calling exit prevents us from returning to the code that was running when we received the signal.
863ba8ecdcSMichael Walsh    exit(0)
873ba8ecdcSMichael Walsh
883ba8ecdcSMichael Walsh
893ba8ecdcSMichael Walshdef validate_parms():
903ba8ecdcSMichael Walsh    r"""
91410b1787SMichael Walsh    Validate program parameters, etc.  Return True or False (i.e. pass/fail) accordingly.
923ba8ecdcSMichael Walsh    """
933ba8ecdcSMichael Walsh
943ba8ecdcSMichael Walsh    get_plug_vars()
953ba8ecdcSMichael Walsh
962ea965ceSMichael Walsh    valid_value(AUTOBOOT_OPENBMC_HOST)
973ba8ecdcSMichael Walsh
983ba8ecdcSMichael Walsh    gen_post_validation(exit_function, signal_handler)
993ba8ecdcSMichael Walsh
1003ba8ecdcSMichael Walsh    return True
1013ba8ecdcSMichael Walsh
1023ba8ecdcSMichael Walsh
1032ce1dbaeSMichael Walshdef stop_check():
1042ce1dbaeSMichael Walsh    r"""
1052ce1dbaeSMichael Walsh    Stop this program with the stop check return code.
1062ce1dbaeSMichael Walsh    """
1072ce1dbaeSMichael Walsh
1082ce1dbaeSMichael Walsh    if MASTER_PID != PROGRAM_PID:
109c213d499SMichael Walsh        save_plug_in_value(stop_check_rc=stop_test_rc)
1102ce1dbaeSMichael Walsh    exit(stop_test_rc)
1112ce1dbaeSMichael Walsh
1122ce1dbaeSMichael Walsh
1133ba8ecdcSMichael Walshdef rest_fail():
1143ba8ecdcSMichael Walsh    r"""
115410b1787SMichael Walsh    If STOP_REST_FAIL, then this function will determine whether REST commands to the target are working.  If
116410b1787SMichael Walsh    not, this function will stop the program by returning stop_test_rc.
1173ba8ecdcSMichael Walsh    """
1183ba8ecdcSMichael Walsh
1193ba8ecdcSMichael Walsh    if STOP_REST_FAIL != '1':
1203ba8ecdcSMichael Walsh        return
1213ba8ecdcSMichael Walsh
1223ba8ecdcSMichael Walsh    print_timen("Checking to see whether REST commands are working.")
1238ab9aea6SMichael Walsh    init_robot_out_parms(get_plug_in_package_name() + "." + pgm_name + ".")
1248ab9aea6SMichael Walsh    lib_file_path = init_robot_file_path("lib/state.py") + ":"\
1258ab9aea6SMichael Walsh        + init_robot_file_path("lib/gen_robot_print.py")
1263ba8ecdcSMichael Walsh    set_mod_global(lib_file_path)
1273ba8ecdcSMichael Walsh    timeout = '0 seconds'
1283ba8ecdcSMichael Walsh    interval = '1 second'
1293ba8ecdcSMichael Walsh    keyword_string = "${match_state}=  Create Dictionary  rest=1 ;" +\
1303ba8ecdcSMichael Walsh        " ${state}=  Wait State  ${match_state}  " + timeout + "  " +\
1313ba8ecdcSMichael Walsh        interval + "  quiet=${1} ; Rpvar  state"
1323ba8ecdcSMichael Walsh    set_mod_global(keyword_string)
1333ba8ecdcSMichael Walsh
134046fe223SMichael Walsh    cmd_buf = create_robot_cmd_string("extended/run_keyword.robot", OPENBMC_HOST, SSH_PORT, HTTPS_PORT,
135046fe223SMichael Walsh                                      REST_USERNAME, REST_PASSWORD, keyword_string, lib_file_path, quiet,
136046fe223SMichael Walsh                                      test_mode, debug, outputdir, output, log, report, loglevel)
1373ba8ecdcSMichael Walsh    if not robot_cmd_fnc(cmd_buf):
1383ba8ecdcSMichael Walsh        print_timen("The caller wishes to stop test execution if REST" +
1393ba8ecdcSMichael Walsh                    " commands are failing.")
1402ce1dbaeSMichael Walsh        stop_check()
1413ba8ecdcSMichael Walsh    print_timen("REST commands are working so no reason as of yet to stop" +
1423ba8ecdcSMichael Walsh                " the test.")
1433ba8ecdcSMichael Walsh
1443ba8ecdcSMichael Walsh
1453ba8ecdcSMichael Walshdef esel_stop_check():
1463ba8ecdcSMichael Walsh    r"""
147410b1787SMichael Walsh    Run the esel_stop_check program to determine whether any eSEL entries found warrant stopping the test
148410b1787SMichael Walsh    run.  See esel_stop_check help text for details.
1493ba8ecdcSMichael Walsh    """
1503ba8ecdcSMichael Walsh
1513ba8ecdcSMichael Walsh    if STOP_ESEL_STOP_FILE_PATH == "":
1523ba8ecdcSMichael Walsh        return
1533ba8ecdcSMichael Walsh
1543ba8ecdcSMichael Walsh    cmd_buf = "esel_stop_check --esel_stop_file_path=" +\
1553ba8ecdcSMichael Walsh        STOP_ESEL_STOP_FILE_PATH
1563ba8ecdcSMichael Walsh    shell_rc, out_buf = cmd_fnc_u(cmd_buf, show_err=0)
1573ba8ecdcSMichael Walsh    if shell_rc == stop_test_rc:
1583ba8ecdcSMichael Walsh        print_timen("The caller wishes to stop test execution based on the" +
1593ba8ecdcSMichael Walsh                    " presence of certain esel entries.")
1602ce1dbaeSMichael Walsh        stop_check()
1613ba8ecdcSMichael Walsh
1623ba8ecdcSMichael Walsh
1633ba8ecdcSMichael Walshdef main():
1643ba8ecdcSMichael Walsh
1653ba8ecdcSMichael Walsh    if not gen_get_options(parser, stock_list):
1663ba8ecdcSMichael Walsh        return False
1673ba8ecdcSMichael Walsh
1683ba8ecdcSMichael Walsh    if not validate_parms():
1693ba8ecdcSMichael Walsh        return False
1703ba8ecdcSMichael Walsh
1713ba8ecdcSMichael Walsh    qprint_pgm_header()
1723ba8ecdcSMichael Walsh
1733ba8ecdcSMichael Walsh    if not debug:
1743ba8ecdcSMichael Walsh        qprint_vars(STOP_REST_FAIL, STOP_COMMAND, AUTOBOOT_BOOT_SUCCESS)
1753ba8ecdcSMichael Walsh
17680caea09SMichael Walsh    print_plug_in_header()
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.")
1812ce1dbaeSMichael Walsh            stop_check()
1823ba8ecdcSMichael Walsh    elif STOP_COMMAND.upper() == "ALL":
1833ba8ecdcSMichael Walsh        print_timen("The caller wishes to stop after each boot test.")
1842ce1dbaeSMichael Walsh        stop_check()
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.")
1892ce1dbaeSMichael Walsh            stop_check()
1903ba8ecdcSMichael Walsh
1913c4869a2SMichael Walsh    rest_fail()
1923c4869a2SMichael Walsh
1933c4869a2SMichael Walsh    esel_stop_check()
1943c4869a2SMichael Walsh
1953ba8ecdcSMichael Walsh    qprint_timen("The caller does not wish to stop the test run.")
1963ba8ecdcSMichael Walsh    return True
1973ba8ecdcSMichael Walsh
198004ad3c9SJoy Onyerikwu
1992ce1dbaeSMichael Walsh# Main
2007e96b132SMichael Walsh
2013ba8ecdcSMichael Walshif not main():
2023ba8ecdcSMichael Walsh    exit(1)
203