blob: 1afb3f58df2e07bf24764e6e5871ded9e76517b0 [file] [log] [blame]
George Keishinge7e91712021-09-03 11:28:44 -05001#!/usr/bin/env python3
Michael Walsh3ba8ecd2018-04-24 11:33:25 -05002
3r"""
4Check for stop conditions. Return code of 2 if stop conditions are found.
5"""
6
Patrick Williamsa57fef42022-12-03 07:00:14 -06007import argparse
Michael Sheposb3dffe82022-01-06 07:17:16 -06008import os
Michael Sheposf7129672022-01-13 11:00:37 -06009import re
Michael Walsh3ba8ecd2018-04-24 11:33:25 -050010
George Keishing7899a452023-02-15 02:46:54 -060011from gen_arg import * # NOQA
12from gen_call_robot import * # NOQA
13from gen_cmd import * # NOQA
14from gen_misc import * # NOQA
15from gen_plug_in_utils import * # NOQA
16from gen_print import * # NOQA
17from gen_valid import * # NOQA
Michael Walsh3ba8ecd2018-04-24 11:33:25 -050018
Michael Walsh2ea965c2019-08-01 16:14:25 -050019# Set exit_on_error for gen_valid functions.
20set_exit_on_error(True)
21
Michael Walsh71fec432020-04-09 17:25:31 -050022# Initialize default plug-in parms..
23STOP_REST_FAIL = 0
Patrick Williams20f38712022-12-08 06:18:26 -060024STOP_COMMAND = ""
Michael Walsh3ba8ecd2018-04-24 11:33:25 -050025stop_test_rc = 2
Michael Walsh71fec432020-04-09 17:25:31 -050026STOP_VERIFY_HARDWARE_FAIL = 0
27
Michael Walsh3ba8ecd2018-04-24 11:33:25 -050028
29# Create parser object to process command line parameters and args.
Michael Walsh3ba8ecd2018-04-24 11:33:25 -050030parser = argparse.ArgumentParser(
Patrick Williams20f38712022-12-08 06:18:26 -060031 usage="%(prog)s [OPTIONS]",
32 description='If the "Stop" plug-in is selected by the user, %(prog)s'
Patrick Williamsa57fef42022-12-03 07:00:14 -060033 + " is called by OBMC Boot Test after each boot test. If %(prog)s returns"
Patrick Williams20f38712022-12-08 06:18:26 -060034 + " "
35 + str(stop_test_rc)
36 + ", then OBMC Boot Test will stop. The user"
Patrick Williamsa57fef42022-12-03 07:00:14 -060037 + " may set environment variable STOP_COMMAND to contain any valid bash"
38 + " command or program. %(prog)s will run this stop command. If the stop"
39 + " command returns non-zero, then %(prog)s will return "
Patrick Williams20f38712022-12-08 06:18:26 -060040 + str(stop_test_rc)
41 + ". %(prog)s recognizes some special values for"
42 + ' STOP_COMMAND: 1) "FAIL" means that OBMC Boot Test should stop'
43 + ' whenever a boot test fails. 2) "ALL" means that OBMC Boot Test'
Patrick Williamsa57fef42022-12-03 07:00:14 -060044 + " should stop after any boot test. If environment variable"
45 + " STOP_REST_FAIL is set, OBMC Boot Test will stop if REST commands are"
46 + " no longer working.",
Michael Walsh8d6bf602020-05-05 13:56:50 -050047 formatter_class=argparse.ArgumentDefaultsHelpFormatter,
Patrick Williams20f38712022-12-08 06:18:26 -060048 prefix_chars="-+",
49)
Michael Walsh3ba8ecd2018-04-24 11:33:25 -050050
Michael Walsh410b1782019-10-22 15:56:18 -050051# The stock_list will be passed to gen_get_options. We populate it with the names of stock parm options we
52# want. These stock parms are pre-defined by gen_get_options.
Patrick Williams20f38712022-12-08 06:18:26 -060053stock_list = [
54 ("test_mode", get_plug_default("test_mode", 0)),
55 ("quiet", get_plug_default("quiet", 0)),
56 ("debug", get_plug_default("debug", 0)),
57]
Michael Walsh3ba8ecd2018-04-24 11:33:25 -050058
59
Patrick Williams20f38712022-12-08 06:18:26 -060060def exit_function(signal_number=0, 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
Michael Walsh71fec432020-04-09 17:25:31 -050064 This function will be called by gen_exit_function().
65 """
Michael Walsh3ba8ecd2018-04-24 11:33:25 -050066
Michael Walsha0ce75a2018-07-31 13:54:29 -050067 process_robot_output_files()
68
Michael Walsh3ba8ecd2018-04-24 11:33:25 -050069
70def validate_parms():
Michael Walsh3ba8ecd2018-04-24 11:33:25 -050071 r"""
Michael Walsh71fec432020-04-09 17:25:31 -050072 Validate program parameters, etc.
73
74 This function will be called by gen_setup().
Michael Walsh3ba8ecd2018-04-24 11:33:25 -050075 """
76
77 get_plug_vars()
78
Michael Walsh3ba8ecd2018-04-24 11:33:25 -050079
Michael Walsh2ce1dba2019-02-05 19:29:28 +000080def stop_check():
81 r"""
82 Stop this program with the stop check return code.
83 """
84
85 if MASTER_PID != PROGRAM_PID:
Michael Walshc213d492019-11-20 14:26:05 -060086 save_plug_in_value(stop_check_rc=stop_test_rc)
Michael Walsh2ce1dba2019-02-05 19:29:28 +000087 exit(stop_test_rc)
88
89
Michael Walsh3ba8ecd2018-04-24 11:33:25 -050090def rest_fail():
Michael Walsh3ba8ecd2018-04-24 11:33:25 -050091 r"""
Michael Walsh410b1782019-10-22 15:56:18 -050092 If STOP_REST_FAIL, then this function will determine whether REST commands to the target are working. If
93 not, this function will stop the program by returning stop_test_rc.
Michael Walsh3ba8ecd2018-04-24 11:33:25 -050094 """
95
Michael Walsh71fec432020-04-09 17:25:31 -050096 if not STOP_REST_FAIL:
Michael Walsh3ba8ecd2018-04-24 11:33:25 -050097 return
98
Patrick Williams20f38712022-12-08 06:18:26 -060099 REDFISH_SUPPORT_TRANS_STATE = int(
100 os.environ.get("REDFISH_SUPPORT_TRANS_STATE", 0)
101 ) or int(os.environ.get("AUTOBOOT_REDFISH_SUPPORT_TRANS_STATE", 0))
Michael Sheposb3dffe82022-01-06 07:17:16 -0600102
103 if REDFISH_SUPPORT_TRANS_STATE:
104 interface = "redfish"
105 else:
106 interface = "rest"
107
108 print_timen("Checking to see whether %s commands are working." % interface)
Michael Walsh8ab9aea2018-11-01 16:30:45 -0500109 init_robot_out_parms(get_plug_in_package_name() + "." + pgm_name + ".")
Patrick Williams20f38712022-12-08 06:18:26 -0600110 lib_file_path = (
111 init_robot_file_path("lib/utils.robot")
112 + ":"
Michael Walsh8ab9aea2018-11-01 16:30:45 -0500113 + init_robot_file_path("lib/gen_robot_print.py")
Patrick Williams20f38712022-12-08 06:18:26 -0600114 )
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500115 set_mod_global(lib_file_path)
Patrick Williams20f38712022-12-08 06:18:26 -0600116 timeout = "0 seconds"
117 interval = "1 second"
118 keyword_string = (
119 "${match_state}= Create Dictionary %s=1 ;" % interface
120 + " ${state}= Wait State ${match_state} "
121 + timeout
122 + " "
123 + interval
124 + " quiet=${1} ; Rpvar state"
125 )
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500126 set_mod_global(keyword_string)
Patrick Williams20f38712022-12-08 06:18:26 -0600127 cmd_buf = create_robot_cmd_string(
128 "extended/run_keyword.robot",
129 OPENBMC_HOST,
130 SSH_PORT,
131 HTTPS_PORT,
132 REST_USERNAME,
133 REST_PASSWORD,
134 OPENBMC_USERNAME,
135 OPENBMC_PASSWORD,
136 REDFISH_SUPPORT_TRANS_STATE,
137 keyword_string,
138 lib_file_path,
139 quiet,
140 test_mode,
141 debug,
142 outputdir,
143 output,
144 log,
145 report,
146 loglevel,
147 )
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500148 if not robot_cmd_fnc(cmd_buf):
Patrick Williams20f38712022-12-08 06:18:26 -0600149 print_timen(
150 "The caller wishes to stop test execution if %s commands are"
151 " failing." % interface
152 )
Michael Walsh2ce1dba2019-02-05 19:29:28 +0000153 stop_check()
Patrick Williams20f38712022-12-08 06:18:26 -0600154 print_timen(
155 "%s commands are working so no reason as of yet to stop the test."
156 % interface
157 )
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500158
159
160def esel_stop_check():
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500161 r"""
Michael Walsh410b1782019-10-22 15:56:18 -0500162 Run the esel_stop_check program to determine whether any eSEL entries found warrant stopping the test
163 run. See esel_stop_check help text for details.
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500164 """
165
166 if STOP_ESEL_STOP_FILE_PATH == "":
167 return
168
Patrick Williams20f38712022-12-08 06:18:26 -0600169 cmd_buf = (
170 "esel_stop_check --esel_stop_file_path=" + STOP_ESEL_STOP_FILE_PATH
171 )
Michael Walsh71fec432020-04-09 17:25:31 -0500172 shell_rc, out_buf = shell_cmd(cmd_buf, show_err=0)
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500173 if shell_rc == stop_test_rc:
Patrick Williams20f38712022-12-08 06:18:26 -0600174 print_timen(
175 "The caller wishes to stop test execution based on the presence of"
176 " certain esel entries."
177 )
Michael Walsh2ce1dba2019-02-05 19:29:28 +0000178 stop_check()
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500179
180
Michael Sheposf7129672022-01-13 11:00:37 -0600181def pel_stop_check():
182 r"""
183 Determine whether any PEL entries found warrant stopping the test
184 run.
185 """
186
187 if STOP_PEL_STOP_FILE_PATH == "":
188 return
189
Patrick Williams20f38712022-12-08 06:18:26 -0600190 pel_txt_file_path = (
191 os.environ.get("AUTOBOOT_FFDC_DIR_PATH", "")
192 + os.environ.get("AUTOBOOT_FFDC_PREFIX", "")
193 + "PEL_logs_list.json"
194 )
Michael Sheposf7129672022-01-13 11:00:37 -0600195
196 if not os.path.isfile(pel_txt_file_path):
Patrick Williams20f38712022-12-08 06:18:26 -0600197 qprint_timen(
198 "The following file was not present so no further"
199 + " action will be taken."
200 )
Michael Sheposf7129672022-01-13 11:00:37 -0600201 qprint_var(pel_txt_file_path)
202 return
203
George Keishing8e6bdae2023-02-17 10:42:25 -0600204 default_stop_dir_path = ""
Michael Sheposf7129672022-01-13 11:00:37 -0600205
206 # If pel_stop_file_path is unqualified and cannot be found, pre-pend
207 # default_stop_dir_path for the user.
208 pel_stop_file_path = os.environ.get("STOP_PEL_STOP_FILE_PATH", "")
Patrick Williams20f38712022-12-08 06:18:26 -0600209 if not os.path.isfile(pel_stop_file_path) and os.path.isfile(
210 default_stop_dir_path + pel_stop_file_path
211 ):
Michael Sheposf7129672022-01-13 11:00:37 -0600212 pel_stop_file_path = default_stop_dir_path + pel_stop_file_path
213 qprint_timen("Using default stop file path.")
214 qprint_var(pel_stop_file_path)
215
216 # First, read the file in and convert it to a list.
217 pel_stop_list = file_to_list(pel_stop_file_path, newlines=0, comments=0)
218
219 if len(pel_stop_list) == 0:
Patrick Williams20f38712022-12-08 06:18:26 -0600220 print_timen(
221 "There are no records to process in " + pel_stop_file_path + "."
222 )
Michael Sheposf7129672022-01-13 11:00:37 -0600223 return
224
225 pel_all_list = file_to_list(pel_txt_file_path, newlines=0, comments=0)
226
227 if len(pel_all_list) == 0:
Patrick Williams20f38712022-12-08 06:18:26 -0600228 print_timen(
229 "There are no records to process in " + pel_txt_file_path + "."
230 )
Michael Sheposf7129672022-01-13 11:00:37 -0600231 return
232
233 for stop_pel in pel_stop_list:
234 for pel_all in pel_all_list:
235 pel_match = re.search(".*SRC.*" + stop_pel + ".*", pel_all)
236 if pel_match:
Patrick Williams20f38712022-12-08 06:18:26 -0600237 print_timen(
238 "The caller wishes to stop test execution based on "
239 + "the presence of certain PEL entries."
240 )
Michael Sheposf7129672022-01-13 11:00:37 -0600241 stop_check()
242
243
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500244def main():
Michael Walsh71fec432020-04-09 17:25:31 -0500245 gen_setup()
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500246
Michael Walsh80caea02019-06-21 11:31:41 -0500247 print_plug_in_header()
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500248
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500249 if STOP_COMMAND.upper() == "FAIL":
250 if AUTOBOOT_BOOT_SUCCESS == "0":
251 print_timen("The caller wishes to stop after each boot failure.")
Michael Walsh2ce1dba2019-02-05 19:29:28 +0000252 stop_check()
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500253 elif STOP_COMMAND.upper() == "ALL":
254 print_timen("The caller wishes to stop after each boot test.")
Michael Walsh2ce1dba2019-02-05 19:29:28 +0000255 stop_check()
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500256 elif len(STOP_COMMAND) > 0:
Michael Walsh71fec432020-04-09 17:25:31 -0500257 shell_rc, out_buf = shell_cmd(STOP_COMMAND, quiet=quiet, show_err=0)
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500258 if shell_rc != 0:
259 print_timen("The caller wishes to stop test execution.")
Michael Walsh2ce1dba2019-02-05 19:29:28 +0000260 stop_check()
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500261
Michael Walsh3c4869a2019-07-17 10:29:31 -0500262 rest_fail()
263
264 esel_stop_check()
265
Michael Sheposf7129672022-01-13 11:00:37 -0600266 pel_stop_check()
267
Michael Walsh71fec432020-04-09 17:25:31 -0500268 if STOP_VERIFY_HARDWARE_FAIL:
Patrick Williams20f38712022-12-08 06:18:26 -0600269 hardware_error_found = restore_plug_in_value(0, "Verify_hardware")
Michael Walsh71fec432020-04-09 17:25:31 -0500270 if hardware_error_found:
Patrick Williams20f38712022-12-08 06:18:26 -0600271 print_timen(
272 "The caller wishes to stop test execution when the"
George Keishing7899a452023-02-15 02:46:54 -0600273 " Verify_hardware plug-in detects a hardware error."
Patrick Williams20f38712022-12-08 06:18:26 -0600274 )
Michael Walsh71fec432020-04-09 17:25:31 -0500275 stop_check()
276
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500277 qprint_timen("The caller does not wish to stop the test run.")
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500278
Joy Onyerikwu004ad3c2018-06-11 16:29:56 -0500279
Michael Walsh71fec432020-04-09 17:25:31 -0500280main()