blob: 713bd963b94e547cc75c283a8807989ad43fdf7e [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
52# The stock_list will be passed to gen_get_options. We populate it with the
53# names of stock parm options we want. These stock parms are pre-defined by
54# gen_get_options.
55stock_list = [("test_mode", 0),
56 ("quiet", get_plug_default("quiet", 0)),
57 ("debug", get_plug_default("debug", 0))]
58
59
60def exit_function(signal_number=0,
61 frame=None):
Michael Walsh3ba8ecd2018-04-24 11:33:25 -050062 r"""
63 Execute whenever the program ends normally or with the signals that we
64 catch (i.e. TERM, INT).
65 """
66
67 dprint_executing()
68 dprint_var(signal_number)
69
Michael Walsha0ce75a2018-07-31 13:54:29 -050070 process_robot_output_files()
71
Michael Walsh3ba8ecd2018-04-24 11:33:25 -050072 qprint_pgm_footer()
73
74
75def signal_handler(signal_number,
76 frame):
Michael Walsh3ba8ecd2018-04-24 11:33:25 -050077 r"""
78 Handle signals. Without a function to catch a SIGTERM or SIGINT, our
79 program would terminate immediately with return code 143 and without
80 calling our exit_function.
81 """
82
83 # Our convention is to set up exit_function with atexit.register() so
84 # there is no need to explicitly call exit_function from here.
85
86 dprint_executing()
87
88 # Calling exit prevents us from returning to the code that was running
89 # when we received the signal.
90 exit(0)
91
92
93def validate_parms():
Michael Walsh3ba8ecd2018-04-24 11:33:25 -050094 r"""
95 Validate program parameters, etc. Return True or False (i.e. pass/fail)
96 accordingly.
97 """
98
99 get_plug_vars()
100
Michael Walsh2ea965c2019-08-01 16:14:25 -0500101 valid_value(AUTOBOOT_OPENBMC_HOST)
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500102
103 gen_post_validation(exit_function, signal_handler)
104
105 return True
106
107
Michael Walsh2ce1dba2019-02-05 19:29:28 +0000108def stop_check():
109 r"""
110 Stop this program with the stop check return code.
111 """
112
113 if MASTER_PID != PROGRAM_PID:
114 stop_check_rc = stop_test_rc
115 save_plug_in_value(stop_check_rc)
116 exit(stop_test_rc)
117
118
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500119def rest_fail():
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500120 r"""
121 If STOP_REST_FAIL, then this function will determine whether REST commands
122 to the target are working. If not, this function will stop the program by
123 returning stop_test_rc.
124 """
125
126 if STOP_REST_FAIL != '1':
127 return
128
129 print_timen("Checking to see whether REST commands are working.")
Michael Walsh8ab9aea2018-11-01 16:30:45 -0500130 init_robot_out_parms(get_plug_in_package_name() + "." + pgm_name + ".")
131 lib_file_path = init_robot_file_path("lib/state.py") + ":"\
132 + init_robot_file_path("lib/gen_robot_print.py")
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500133 set_mod_global(lib_file_path)
134 timeout = '0 seconds'
135 interval = '1 second'
136 keyword_string = "${match_state}= Create Dictionary rest=1 ;" +\
137 " ${state}= Wait State ${match_state} " + timeout + " " +\
138 interval + " quiet=${1} ; Rpvar state"
139 set_mod_global(keyword_string)
140
141 cmd_buf = create_robot_cmd_string("extended/run_keyword.robot",
Michael Walsh2ce1dba2019-02-05 19:29:28 +0000142 OPENBMC_HOST, REST_USERNAME,
143 REST_PASSWORD, keyword_string,
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500144 lib_file_path, quiet, test_mode, debug,
145 outputdir, output, log, report, loglevel)
146 if not robot_cmd_fnc(cmd_buf):
147 print_timen("The caller wishes to stop test execution if REST" +
148 " commands are failing.")
Michael Walsh2ce1dba2019-02-05 19:29:28 +0000149 stop_check()
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500150 print_timen("REST commands are working so no reason as of yet to stop" +
151 " the test.")
152
153
154def esel_stop_check():
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500155 r"""
156 Run the esel_stop_check program to determine whether any eSEL entries
Gunnar Mills7732c7e2018-08-14 11:54:24 -0500157 found warrant stopping the test run. See esel_stop_check help text for
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500158 details.
159 """
160
161 if STOP_ESEL_STOP_FILE_PATH == "":
162 return
163
164 cmd_buf = "esel_stop_check --esel_stop_file_path=" +\
165 STOP_ESEL_STOP_FILE_PATH
166 shell_rc, out_buf = cmd_fnc_u(cmd_buf, show_err=0)
167 if shell_rc == stop_test_rc:
168 print_timen("The caller wishes to stop test execution based on the" +
169 " presence of certain esel entries.")
Michael Walsh2ce1dba2019-02-05 19:29:28 +0000170 stop_check()
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500171
172
173def main():
174
175 if not gen_get_options(parser, stock_list):
176 return False
177
178 if not validate_parms():
179 return False
180
181 qprint_pgm_header()
182
183 if not debug:
184 qprint_vars(STOP_REST_FAIL, STOP_COMMAND, AUTOBOOT_BOOT_SUCCESS)
185
Michael Walsh80caea02019-06-21 11:31:41 -0500186 print_plug_in_header()
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500187
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500188 if STOP_COMMAND.upper() == "FAIL":
189 if AUTOBOOT_BOOT_SUCCESS == "0":
190 print_timen("The caller wishes to stop after each boot failure.")
Michael Walsh2ce1dba2019-02-05 19:29:28 +0000191 stop_check()
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500192 elif STOP_COMMAND.upper() == "ALL":
193 print_timen("The caller wishes to stop after each boot test.")
Michael Walsh2ce1dba2019-02-05 19:29:28 +0000194 stop_check()
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500195 elif len(STOP_COMMAND) > 0:
196 shell_rc, out_buf = cmd_fnc_u(STOP_COMMAND, quiet=quiet, show_err=0)
197 if shell_rc != 0:
198 print_timen("The caller wishes to stop test execution.")
Michael Walsh2ce1dba2019-02-05 19:29:28 +0000199 stop_check()
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500200
Michael Walsh3c4869a2019-07-17 10:29:31 -0500201 rest_fail()
202
203 esel_stop_check()
204
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500205 qprint_timen("The caller does not wish to stop the test run.")
206 return True
207
Joy Onyerikwu004ad3c2018-06-11 16:29:56 -0500208
Michael Walsh2ce1dba2019-02-05 19:29:28 +0000209# Main
Michael Walsh7e96b132019-02-05 19:24:54 +0000210
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500211if not main():
212 exit(1)