| Michael Walsh | 3ba8ecd | 2018-04-24 11:33:25 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python | 
|  | 2 |  | 
|  | 3 | r""" | 
|  | 4 | Check for stop conditions.  Return code of 2 if stop conditions are found. | 
|  | 5 | """ | 
|  | 6 |  | 
|  | 7 | import sys | 
|  | 8 | import subprocess | 
|  | 9 |  | 
|  | 10 | save_path_0 = sys.path[0] | 
|  | 11 | del sys.path[0] | 
|  | 12 |  | 
|  | 13 | from gen_print import * | 
|  | 14 | from gen_valid import * | 
|  | 15 | from gen_arg import * | 
|  | 16 | from gen_misc import * | 
|  | 17 | from gen_cmd import * | 
|  | 18 | from gen_plug_in_utils import * | 
|  | 19 | from gen_call_robot import * | 
|  | 20 |  | 
|  | 21 | # Restore sys.path[0]. | 
|  | 22 | sys.path.insert(0, save_path_0) | 
|  | 23 |  | 
| Michael Walsh | 2ea965c | 2019-08-01 16:14:25 -0500 | [diff] [blame] | 24 | # Set exit_on_error for gen_valid functions. | 
|  | 25 | set_exit_on_error(True) | 
|  | 26 |  | 
| Michael Walsh | 3ba8ecd | 2018-04-24 11:33:25 -0500 | [diff] [blame] | 27 | # Initialize. | 
|  | 28 | STOP_REST_FAIL = '' | 
|  | 29 | STOP_COMMAND = '' | 
|  | 30 | stop_test_rc = 2 | 
|  | 31 |  | 
|  | 32 | # Create parser object to process command line parameters and args. | 
|  | 33 |  | 
|  | 34 | # Create parser object. | 
|  | 35 | parser = 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 Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 52 | # 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 Walsh | 3ba8ecd | 2018-04-24 11:33:25 -0500 | [diff] [blame] | 54 | stock_list = [("test_mode", 0), | 
|  | 55 | ("quiet", get_plug_default("quiet", 0)), | 
|  | 56 | ("debug", get_plug_default("debug", 0))] | 
|  | 57 |  | 
|  | 58 |  | 
|  | 59 | def exit_function(signal_number=0, | 
|  | 60 | frame=None): | 
| Michael Walsh | 3ba8ecd | 2018-04-24 11:33:25 -0500 | [diff] [blame] | 61 | r""" | 
| Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 62 | Execute whenever the program ends normally or with the signals that we catch (i.e. TERM, INT). | 
| Michael Walsh | 3ba8ecd | 2018-04-24 11:33:25 -0500 | [diff] [blame] | 63 | """ | 
|  | 64 |  | 
|  | 65 | dprint_executing() | 
|  | 66 | dprint_var(signal_number) | 
|  | 67 |  | 
| Michael Walsh | a0ce75a | 2018-07-31 13:54:29 -0500 | [diff] [blame] | 68 | process_robot_output_files() | 
|  | 69 |  | 
| Michael Walsh | 3ba8ecd | 2018-04-24 11:33:25 -0500 | [diff] [blame] | 70 | qprint_pgm_footer() | 
|  | 71 |  | 
|  | 72 |  | 
|  | 73 | def signal_handler(signal_number, | 
|  | 74 | frame): | 
| Michael Walsh | 3ba8ecd | 2018-04-24 11:33:25 -0500 | [diff] [blame] | 75 | r""" | 
| Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 76 | 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 Walsh | 3ba8ecd | 2018-04-24 11:33:25 -0500 | [diff] [blame] | 78 | """ | 
|  | 79 |  | 
| Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 80 | # 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 Walsh | 3ba8ecd | 2018-04-24 11:33:25 -0500 | [diff] [blame] | 82 |  | 
|  | 83 | dprint_executing() | 
|  | 84 |  | 
| Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 85 | # Calling exit prevents us from returning to the code that was running when we received the signal. | 
| Michael Walsh | 3ba8ecd | 2018-04-24 11:33:25 -0500 | [diff] [blame] | 86 | exit(0) | 
|  | 87 |  | 
|  | 88 |  | 
|  | 89 | def validate_parms(): | 
| Michael Walsh | 3ba8ecd | 2018-04-24 11:33:25 -0500 | [diff] [blame] | 90 | r""" | 
| Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 91 | Validate program parameters, etc.  Return True or False (i.e. pass/fail) accordingly. | 
| Michael Walsh | 3ba8ecd | 2018-04-24 11:33:25 -0500 | [diff] [blame] | 92 | """ | 
|  | 93 |  | 
|  | 94 | get_plug_vars() | 
|  | 95 |  | 
| Michael Walsh | 2ea965c | 2019-08-01 16:14:25 -0500 | [diff] [blame] | 96 | valid_value(AUTOBOOT_OPENBMC_HOST) | 
| Michael Walsh | 3ba8ecd | 2018-04-24 11:33:25 -0500 | [diff] [blame] | 97 |  | 
|  | 98 | gen_post_validation(exit_function, signal_handler) | 
|  | 99 |  | 
|  | 100 | return True | 
|  | 101 |  | 
|  | 102 |  | 
| Michael Walsh | 2ce1dba | 2019-02-05 19:29:28 +0000 | [diff] [blame] | 103 | def stop_check(): | 
|  | 104 | r""" | 
|  | 105 | Stop this program with the stop check return code. | 
|  | 106 | """ | 
|  | 107 |  | 
|  | 108 | if MASTER_PID != PROGRAM_PID: | 
|  | 109 | stop_check_rc = stop_test_rc | 
|  | 110 | save_plug_in_value(stop_check_rc) | 
|  | 111 | exit(stop_test_rc) | 
|  | 112 |  | 
|  | 113 |  | 
| Michael Walsh | 3ba8ecd | 2018-04-24 11:33:25 -0500 | [diff] [blame] | 114 | def rest_fail(): | 
| Michael Walsh | 3ba8ecd | 2018-04-24 11:33:25 -0500 | [diff] [blame] | 115 | r""" | 
| Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 116 | If STOP_REST_FAIL, then this function will determine whether REST commands to the target are working.  If | 
|  | 117 | not, this function will stop the program by returning stop_test_rc. | 
| Michael Walsh | 3ba8ecd | 2018-04-24 11:33:25 -0500 | [diff] [blame] | 118 | """ | 
|  | 119 |  | 
|  | 120 | if STOP_REST_FAIL != '1': | 
|  | 121 | return | 
|  | 122 |  | 
|  | 123 | print_timen("Checking to see whether REST commands are working.") | 
| Michael Walsh | 8ab9aea | 2018-11-01 16:30:45 -0500 | [diff] [blame] | 124 | init_robot_out_parms(get_plug_in_package_name() + "." + pgm_name + ".") | 
|  | 125 | lib_file_path = init_robot_file_path("lib/state.py") + ":"\ | 
|  | 126 | + init_robot_file_path("lib/gen_robot_print.py") | 
| Michael Walsh | 3ba8ecd | 2018-04-24 11:33:25 -0500 | [diff] [blame] | 127 | set_mod_global(lib_file_path) | 
|  | 128 | timeout = '0 seconds' | 
|  | 129 | interval = '1 second' | 
|  | 130 | keyword_string = "${match_state}=  Create Dictionary  rest=1 ;" +\ | 
|  | 131 | " ${state}=  Wait State  ${match_state}  " + timeout + "  " +\ | 
|  | 132 | interval + "  quiet=${1} ; Rpvar  state" | 
|  | 133 | set_mod_global(keyword_string) | 
|  | 134 |  | 
|  | 135 | cmd_buf = create_robot_cmd_string("extended/run_keyword.robot", | 
| Michael Walsh | 2ce1dba | 2019-02-05 19:29:28 +0000 | [diff] [blame] | 136 | OPENBMC_HOST, REST_USERNAME, | 
|  | 137 | REST_PASSWORD, keyword_string, | 
| Michael Walsh | 3ba8ecd | 2018-04-24 11:33:25 -0500 | [diff] [blame] | 138 | lib_file_path, quiet, test_mode, debug, | 
|  | 139 | outputdir, output, log, report, loglevel) | 
|  | 140 | if not robot_cmd_fnc(cmd_buf): | 
|  | 141 | print_timen("The caller wishes to stop test execution if REST" + | 
|  | 142 | " commands are failing.") | 
| Michael Walsh | 2ce1dba | 2019-02-05 19:29:28 +0000 | [diff] [blame] | 143 | stop_check() | 
| Michael Walsh | 3ba8ecd | 2018-04-24 11:33:25 -0500 | [diff] [blame] | 144 | print_timen("REST commands are working so no reason as of yet to stop" + | 
|  | 145 | " the test.") | 
|  | 146 |  | 
|  | 147 |  | 
|  | 148 | def esel_stop_check(): | 
| Michael Walsh | 3ba8ecd | 2018-04-24 11:33:25 -0500 | [diff] [blame] | 149 | r""" | 
| Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 150 | Run the esel_stop_check program to determine whether any eSEL entries found warrant stopping the test | 
|  | 151 | run.  See esel_stop_check help text for details. | 
| Michael Walsh | 3ba8ecd | 2018-04-24 11:33:25 -0500 | [diff] [blame] | 152 | """ | 
|  | 153 |  | 
|  | 154 | if STOP_ESEL_STOP_FILE_PATH == "": | 
|  | 155 | return | 
|  | 156 |  | 
|  | 157 | cmd_buf = "esel_stop_check --esel_stop_file_path=" +\ | 
|  | 158 | STOP_ESEL_STOP_FILE_PATH | 
|  | 159 | shell_rc, out_buf = cmd_fnc_u(cmd_buf, show_err=0) | 
|  | 160 | if shell_rc == stop_test_rc: | 
|  | 161 | print_timen("The caller wishes to stop test execution based on the" + | 
|  | 162 | " presence of certain esel entries.") | 
| Michael Walsh | 2ce1dba | 2019-02-05 19:29:28 +0000 | [diff] [blame] | 163 | stop_check() | 
| Michael Walsh | 3ba8ecd | 2018-04-24 11:33:25 -0500 | [diff] [blame] | 164 |  | 
|  | 165 |  | 
|  | 166 | def main(): | 
|  | 167 |  | 
|  | 168 | if not gen_get_options(parser, stock_list): | 
|  | 169 | return False | 
|  | 170 |  | 
|  | 171 | if not validate_parms(): | 
|  | 172 | return False | 
|  | 173 |  | 
|  | 174 | qprint_pgm_header() | 
|  | 175 |  | 
|  | 176 | if not debug: | 
|  | 177 | qprint_vars(STOP_REST_FAIL, STOP_COMMAND, AUTOBOOT_BOOT_SUCCESS) | 
|  | 178 |  | 
| Michael Walsh | 80caea0 | 2019-06-21 11:31:41 -0500 | [diff] [blame] | 179 | print_plug_in_header() | 
| Michael Walsh | 3ba8ecd | 2018-04-24 11:33:25 -0500 | [diff] [blame] | 180 |  | 
| Michael Walsh | 3ba8ecd | 2018-04-24 11:33:25 -0500 | [diff] [blame] | 181 | if STOP_COMMAND.upper() == "FAIL": | 
|  | 182 | if AUTOBOOT_BOOT_SUCCESS == "0": | 
|  | 183 | print_timen("The caller wishes to stop after each boot failure.") | 
| Michael Walsh | 2ce1dba | 2019-02-05 19:29:28 +0000 | [diff] [blame] | 184 | stop_check() | 
| Michael Walsh | 3ba8ecd | 2018-04-24 11:33:25 -0500 | [diff] [blame] | 185 | elif STOP_COMMAND.upper() == "ALL": | 
|  | 186 | print_timen("The caller wishes to stop after each boot test.") | 
| Michael Walsh | 2ce1dba | 2019-02-05 19:29:28 +0000 | [diff] [blame] | 187 | stop_check() | 
| Michael Walsh | 3ba8ecd | 2018-04-24 11:33:25 -0500 | [diff] [blame] | 188 | elif len(STOP_COMMAND) > 0: | 
|  | 189 | shell_rc, out_buf = cmd_fnc_u(STOP_COMMAND, quiet=quiet, show_err=0) | 
|  | 190 | if shell_rc != 0: | 
|  | 191 | print_timen("The caller wishes to stop test execution.") | 
| Michael Walsh | 2ce1dba | 2019-02-05 19:29:28 +0000 | [diff] [blame] | 192 | stop_check() | 
| Michael Walsh | 3ba8ecd | 2018-04-24 11:33:25 -0500 | [diff] [blame] | 193 |  | 
| Michael Walsh | 3c4869a | 2019-07-17 10:29:31 -0500 | [diff] [blame] | 194 | rest_fail() | 
|  | 195 |  | 
|  | 196 | esel_stop_check() | 
|  | 197 |  | 
| Michael Walsh | 3ba8ecd | 2018-04-24 11:33:25 -0500 | [diff] [blame] | 198 | qprint_timen("The caller does not wish to stop the test run.") | 
|  | 199 | return True | 
|  | 200 |  | 
| Joy Onyerikwu | 004ad3c | 2018-06-11 16:29:56 -0500 | [diff] [blame] | 201 |  | 
| Michael Walsh | 2ce1dba | 2019-02-05 19:29:28 +0000 | [diff] [blame] | 202 | # Main | 
| Michael Walsh | 7e96b13 | 2019-02-05 19:24:54 +0000 | [diff] [blame] | 203 |  | 
| Michael Walsh | 3ba8ecd | 2018-04-24 11:33:25 -0500 | [diff] [blame] | 204 | if not main(): | 
|  | 205 | exit(1) |