blob: 2bc8da801c2fb0c5658e75dfeb84885934d3d24d [file] [log] [blame]
Michael Walsh3ba8ecd2018-04-24 11:33:25 -05001#!/usr/bin/env python
2
3r"""
4Check for stop conditions. Return code of 2 if stop conditions are found.
5"""
6
7import sys
8import subprocess
9
10save_path_0 = sys.path[0]
11del sys.path[0]
12
13from gen_print import *
14from gen_valid import *
15from gen_arg import *
16from gen_misc import *
17from gen_cmd import *
18from gen_plug_in_utils import *
19from gen_call_robot import *
20
21# Restore sys.path[0].
22sys.path.insert(0, save_path_0)
23
Michael Walsh2ea965c2019-08-01 16:14:25 -050024# Set exit_on_error for gen_valid functions.
25set_exit_on_error(True)
26
Michael Walsh3ba8ecd2018-04-24 11:33:25 -050027# Initialize.
28STOP_REST_FAIL = ''
29STOP_COMMAND = ''
30stop_test_rc = 2
31
32# Create parser object to process command line parameters and args.
33
34# Create parser object.
35parser = argparse.ArgumentParser(
36 usage='%(prog)s [OPTIONS]',
37 description="If the \"Stop\" plug-in is selected by the user, %(prog)s" +
38 " is called by OBMC Boot Test after each boot test. If %(prog)s returns" +
39 " " + str(stop_test_rc) + ", then OBMC Boot Test will stop. The user" +
40 " may set environment variable STOP_COMMAND to contain any valid bash" +
41 " command or program. %(prog)s will run this stop command. If the stop" +
42 " command returns non-zero, then %(prog)s will return " +
43 str(stop_test_rc) + ". %(prog)s recognizes some special values for" +
44 " STOP_COMMAND: 1) \"FAIL\" means that OBMC Boot Test should stop" +
45 " whenever a boot test fails. 2) \"ALL\" means that OBMC Boot Test" +
46 " should stop after any boot test. If environment variable" +
47 " STOP_REST_FAIL is set, OBMC Boot Test will stop if REST commands are" +
48 " no longer working.",
49 formatter_class=argparse.RawTextHelpFormatter,
50 prefix_chars='-+')
51
Michael Walsh410b1782019-10-22 15:56:18 -050052# The stock_list will be passed to gen_get_options. We populate it with the names of stock parm options we
53# want. These stock parms are pre-defined by gen_get_options.
Michael Walsh3ba8ecd2018-04-24 11:33:25 -050054stock_list = [("test_mode", 0),
55 ("quiet", get_plug_default("quiet", 0)),
56 ("debug", get_plug_default("debug", 0))]
57
58
59def exit_function(signal_number=0,
60 frame=None):
Michael Walsh3ba8ecd2018-04-24 11:33:25 -050061 r"""
Michael Walsh410b1782019-10-22 15:56:18 -050062 Execute whenever the program ends normally or with the signals that we catch (i.e. TERM, INT).
Michael Walsh3ba8ecd2018-04-24 11:33:25 -050063 """
64
65 dprint_executing()
66 dprint_var(signal_number)
67
Michael Walsha0ce75a2018-07-31 13:54:29 -050068 process_robot_output_files()
69
Michael Walsh3ba8ecd2018-04-24 11:33:25 -050070 qprint_pgm_footer()
71
72
73def signal_handler(signal_number,
74 frame):
Michael Walsh3ba8ecd2018-04-24 11:33:25 -050075 r"""
Michael Walsh410b1782019-10-22 15:56:18 -050076 Handle signals. Without a function to catch a SIGTERM or SIGINT, our program would terminate immediately
77 with return code 143 and without calling our exit_function.
Michael Walsh3ba8ecd2018-04-24 11:33:25 -050078 """
79
Michael Walsh410b1782019-10-22 15:56:18 -050080 # Our convention is to set up exit_function with atexit.register() so there is no need to explicitly
81 # call exit_function from here.
Michael Walsh3ba8ecd2018-04-24 11:33:25 -050082
83 dprint_executing()
84
Michael Walsh410b1782019-10-22 15:56:18 -050085 # Calling exit prevents us from returning to the code that was running when we received the signal.
Michael Walsh3ba8ecd2018-04-24 11:33:25 -050086 exit(0)
87
88
89def validate_parms():
Michael Walsh3ba8ecd2018-04-24 11:33:25 -050090 r"""
Michael Walsh410b1782019-10-22 15:56:18 -050091 Validate program parameters, etc. Return True or False (i.e. pass/fail) accordingly.
Michael Walsh3ba8ecd2018-04-24 11:33:25 -050092 """
93
94 get_plug_vars()
95
Michael Walsh2ea965c2019-08-01 16:14:25 -050096 valid_value(AUTOBOOT_OPENBMC_HOST)
Michael Walsh3ba8ecd2018-04-24 11:33:25 -050097
98 gen_post_validation(exit_function, signal_handler)
99
100 return True
101
102
Michael Walsh2ce1dba2019-02-05 19:29:28 +0000103def stop_check():
104 r"""
105 Stop this program with the stop check return code.
106 """
107
108 if MASTER_PID != PROGRAM_PID:
Michael Walshc213d492019-11-20 14:26:05 -0600109 save_plug_in_value(stop_check_rc=stop_test_rc)
Michael Walsh2ce1dba2019-02-05 19:29:28 +0000110 exit(stop_test_rc)
111
112
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500113def rest_fail():
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500114 r"""
Michael Walsh410b1782019-10-22 15:56:18 -0500115 If STOP_REST_FAIL, then this function will determine whether REST commands to the target are working. If
116 not, this function will stop the program by returning stop_test_rc.
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500117 """
118
119 if STOP_REST_FAIL != '1':
120 return
121
122 print_timen("Checking to see whether REST commands are working.")
Michael Walsh8ab9aea2018-11-01 16:30:45 -0500123 init_robot_out_parms(get_plug_in_package_name() + "." + pgm_name + ".")
124 lib_file_path = init_robot_file_path("lib/state.py") + ":"\
125 + init_robot_file_path("lib/gen_robot_print.py")
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500126 set_mod_global(lib_file_path)
127 timeout = '0 seconds'
128 interval = '1 second'
129 keyword_string = "${match_state}= Create Dictionary rest=1 ;" +\
130 " ${state}= Wait State ${match_state} " + timeout + " " +\
131 interval + " quiet=${1} ; Rpvar state"
132 set_mod_global(keyword_string)
133
Michael Walsh046fe222019-11-08 14:33:53 -0600134 cmd_buf = create_robot_cmd_string("extended/run_keyword.robot", OPENBMC_HOST, SSH_PORT, HTTPS_PORT,
135 REST_USERNAME, REST_PASSWORD, keyword_string, lib_file_path, quiet,
136 test_mode, debug, outputdir, output, log, report, loglevel)
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500137 if not robot_cmd_fnc(cmd_buf):
138 print_timen("The caller wishes to stop test execution if REST" +
139 " commands are failing.")
Michael Walsh2ce1dba2019-02-05 19:29:28 +0000140 stop_check()
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500141 print_timen("REST commands are working so no reason as of yet to stop" +
142 " the test.")
143
144
145def esel_stop_check():
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500146 r"""
Michael Walsh410b1782019-10-22 15:56:18 -0500147 Run the esel_stop_check program to determine whether any eSEL entries found warrant stopping the test
148 run. See esel_stop_check help text for details.
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500149 """
150
151 if STOP_ESEL_STOP_FILE_PATH == "":
152 return
153
154 cmd_buf = "esel_stop_check --esel_stop_file_path=" +\
155 STOP_ESEL_STOP_FILE_PATH
156 shell_rc, out_buf = cmd_fnc_u(cmd_buf, show_err=0)
157 if shell_rc == stop_test_rc:
158 print_timen("The caller wishes to stop test execution based on the" +
159 " presence of certain esel entries.")
Michael Walsh2ce1dba2019-02-05 19:29:28 +0000160 stop_check()
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500161
162
163def main():
164
165 if not gen_get_options(parser, stock_list):
166 return False
167
168 if not validate_parms():
169 return False
170
171 qprint_pgm_header()
172
173 if not debug:
174 qprint_vars(STOP_REST_FAIL, STOP_COMMAND, AUTOBOOT_BOOT_SUCCESS)
175
Michael Walsh80caea02019-06-21 11:31:41 -0500176 print_plug_in_header()
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500177
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500178 if STOP_COMMAND.upper() == "FAIL":
179 if AUTOBOOT_BOOT_SUCCESS == "0":
180 print_timen("The caller wishes to stop after each boot failure.")
Michael Walsh2ce1dba2019-02-05 19:29:28 +0000181 stop_check()
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500182 elif STOP_COMMAND.upper() == "ALL":
183 print_timen("The caller wishes to stop after each boot test.")
Michael Walsh2ce1dba2019-02-05 19:29:28 +0000184 stop_check()
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500185 elif len(STOP_COMMAND) > 0:
186 shell_rc, out_buf = cmd_fnc_u(STOP_COMMAND, quiet=quiet, show_err=0)
187 if shell_rc != 0:
188 print_timen("The caller wishes to stop test execution.")
Michael Walsh2ce1dba2019-02-05 19:29:28 +0000189 stop_check()
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500190
Michael Walsh3c4869a2019-07-17 10:29:31 -0500191 rest_fail()
192
193 esel_stop_check()
194
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500195 qprint_timen("The caller does not wish to stop the test run.")
196 return True
197
Joy Onyerikwu004ad3c2018-06-11 16:29:56 -0500198
Michael Walsh2ce1dba2019-02-05 19:29:28 +0000199# Main
Michael Walsh7e96b132019-02-05 19:24:54 +0000200
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500201if not main():
202 exit(1)