1#!/usr/bin/env python3
2
3r"""
4Check for stop conditions.  Return code of 2 if stop conditions are found.
5"""
6
7import sys
8import subprocess
9import os
10
11from gen_print import *
12from gen_valid import *
13from gen_arg import *
14from gen_misc import *
15from gen_cmd import *
16from gen_plug_in_utils import *
17from gen_call_robot import *
18
19# Set exit_on_error for gen_valid functions.
20set_exit_on_error(True)
21
22# Initialize default plug-in parms..
23STOP_REST_FAIL = 0
24STOP_COMMAND = ''
25stop_test_rc = 2
26STOP_VERIFY_HARDWARE_FAIL = 0
27
28
29# Create parser object to process command line parameters and args.
30parser = argparse.ArgumentParser(
31    usage='%(prog)s [OPTIONS]',
32    description="If the \"Stop\" plug-in is selected by the user, %(prog)s" +
33    " is called by OBMC Boot Test after each boot test.  If %(prog)s returns" +
34    " " + str(stop_test_rc) + ", then OBMC Boot Test will stop.  The user" +
35    " may set environment variable STOP_COMMAND to contain any valid bash" +
36    " command or program.  %(prog)s will run this stop command.  If the stop" +
37    " command returns non-zero, then %(prog)s will return " +
38    str(stop_test_rc) + ".  %(prog)s recognizes some special values for" +
39    " STOP_COMMAND: 1) \"FAIL\" means that OBMC Boot Test should stop" +
40    " whenever a boot test fails. 2) \"ALL\" means that OBMC Boot Test" +
41    " should stop after any boot test.  If environment variable" +
42    " STOP_REST_FAIL is set, OBMC Boot Test will stop if REST commands are" +
43    " no longer working.",
44    formatter_class=argparse.ArgumentDefaultsHelpFormatter,
45    prefix_chars='-+')
46
47# The stock_list will be passed to gen_get_options.  We populate it with the names of stock parm options we
48# want.  These stock parms are pre-defined by gen_get_options.
49stock_list = [("test_mode", get_plug_default("test_mode", 0)),
50              ("quiet", get_plug_default("quiet", 0)),
51              ("debug", get_plug_default("debug", 0))]
52
53
54def exit_function(signal_number=0,
55                  frame=None):
56    r"""
57    Execute whenever the program ends normally or with the signals that we catch (i.e. TERM, INT).
58
59    This function will be called by gen_exit_function().
60    """
61
62    process_robot_output_files()
63
64
65def validate_parms():
66    r"""
67    Validate program parameters, etc.
68
69    This function will be called by gen_setup().
70    """
71
72    get_plug_vars()
73
74
75def stop_check():
76    r"""
77    Stop this program with the stop check return code.
78    """
79
80    if MASTER_PID != PROGRAM_PID:
81        save_plug_in_value(stop_check_rc=stop_test_rc)
82    exit(stop_test_rc)
83
84
85def rest_fail():
86    r"""
87    If STOP_REST_FAIL, then this function will determine whether REST commands to the target are working.  If
88    not, this function will stop the program by returning stop_test_rc.
89    """
90
91    if not STOP_REST_FAIL:
92        return
93
94    REDFISH_SUPPORT_TRANS_STATE = int(os.environ.get('REDFISH_SUPPORT_TRANS_STATE', 0)) or \
95        int(os.environ.get('AUTOBOOT_REDFISH_SUPPORT_TRANS_STATE', 0))
96
97    if REDFISH_SUPPORT_TRANS_STATE:
98        interface = "redfish"
99    else:
100        interface = "rest"
101
102    print_timen("Checking to see whether %s commands are working." % interface)
103    init_robot_out_parms(get_plug_in_package_name() + "." + pgm_name + ".")
104    lib_file_path = init_robot_file_path("lib/utils.robot") + ":" \
105        + init_robot_file_path("lib/gen_robot_print.py")
106    set_mod_global(lib_file_path)
107    timeout = '0 seconds'
108    interval = '1 second'
109    keyword_string = "${match_state}=  Create Dictionary  %s=1 ;" % interface +\
110        " ${state}=  Wait State  ${match_state}  " + timeout + "  " +\
111        interval + "  quiet=${1} ; Rpvar  state"
112    set_mod_global(keyword_string)
113    cmd_buf = create_robot_cmd_string("extended/run_keyword.robot", OPENBMC_HOST, SSH_PORT, HTTPS_PORT,
114                                      REST_USERNAME, REST_PASSWORD, OPENBMC_USERNAME, OPENBMC_PASSWORD,
115                                      REDFISH_SUPPORT_TRANS_STATE, keyword_string, lib_file_path, quiet,
116                                      test_mode, debug, outputdir, output, log, report, loglevel)
117    if not robot_cmd_fnc(cmd_buf):
118        print_timen("The caller wishes to stop test execution if %s commands are failing." % interface)
119        stop_check()
120    print_timen("%s commands are working so no reason as of yet to stop the test." % interface)
121
122
123def esel_stop_check():
124    r"""
125    Run the esel_stop_check program to determine whether any eSEL entries found warrant stopping the test
126    run.  See esel_stop_check help text for details.
127    """
128
129    if STOP_ESEL_STOP_FILE_PATH == "":
130        return
131
132    cmd_buf = "esel_stop_check --esel_stop_file_path=" + STOP_ESEL_STOP_FILE_PATH
133    shell_rc, out_buf = shell_cmd(cmd_buf, show_err=0)
134    if shell_rc == stop_test_rc:
135        print_timen("The caller wishes to stop test execution based on the presence of certain esel entries.")
136        stop_check()
137
138
139def main():
140
141    gen_setup()
142
143    print_plug_in_header()
144
145    if STOP_COMMAND.upper() == "FAIL":
146        if AUTOBOOT_BOOT_SUCCESS == "0":
147            print_timen("The caller wishes to stop after each boot failure.")
148            stop_check()
149    elif STOP_COMMAND.upper() == "ALL":
150        print_timen("The caller wishes to stop after each boot test.")
151        stop_check()
152    elif len(STOP_COMMAND) > 0:
153        shell_rc, out_buf = shell_cmd(STOP_COMMAND, quiet=quiet, show_err=0)
154        if shell_rc != 0:
155            print_timen("The caller wishes to stop test execution.")
156            stop_check()
157
158    rest_fail()
159
160    esel_stop_check()
161
162    if STOP_VERIFY_HARDWARE_FAIL:
163        hardware_error_found = restore_plug_in_value(0, 'Verify_hardware')
164        if hardware_error_found:
165            print_timen("The caller wishes to stop test execution when the Verify_hardware plug-in detects a"
166                        + " hardware error.")
167            stop_check()
168
169    qprint_timen("The caller does not wish to stop the test run.")
170
171
172main()
173