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 | |
| 24 | # Initialize. |
| 25 | STOP_REST_FAIL = '' |
| 26 | STOP_COMMAND = '' |
| 27 | stop_test_rc = 2 |
| 28 | |
| 29 | # Create parser object to process command line parameters and args. |
| 30 | |
| 31 | # Create parser object. |
| 32 | parser = argparse.ArgumentParser( |
| 33 | usage='%(prog)s [OPTIONS]', |
| 34 | description="If the \"Stop\" plug-in is selected by the user, %(prog)s" + |
| 35 | " is called by OBMC Boot Test after each boot test. If %(prog)s returns" + |
| 36 | " " + str(stop_test_rc) + ", then OBMC Boot Test will stop. The user" + |
| 37 | " 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 " + |
| 40 | str(stop_test_rc) + ". %(prog)s recognizes some special values for" + |
| 41 | " STOP_COMMAND: 1) \"FAIL\" means that OBMC Boot Test should stop" + |
| 42 | " whenever a boot test fails. 2) \"ALL\" means that OBMC Boot Test" + |
| 43 | " should stop after any boot test. If environment variable" + |
| 44 | " STOP_REST_FAIL is set, OBMC Boot Test will stop if REST commands are" + |
| 45 | " no longer working.", |
| 46 | formatter_class=argparse.RawTextHelpFormatter, |
| 47 | prefix_chars='-+') |
| 48 | |
| 49 | # The stock_list will be passed to gen_get_options. We populate it with the |
| 50 | # names of stock parm options we want. These stock parms are pre-defined by |
| 51 | # gen_get_options. |
| 52 | stock_list = [("test_mode", 0), |
| 53 | ("quiet", get_plug_default("quiet", 0)), |
| 54 | ("debug", get_plug_default("debug", 0))] |
| 55 | |
| 56 | |
| 57 | def exit_function(signal_number=0, |
| 58 | frame=None): |
Michael Walsh | 3ba8ecd | 2018-04-24 11:33:25 -0500 | [diff] [blame] | 59 | r""" |
| 60 | Execute whenever the program ends normally or with the signals that we |
| 61 | catch (i.e. TERM, INT). |
| 62 | """ |
| 63 | |
| 64 | dprint_executing() |
| 65 | dprint_var(signal_number) |
| 66 | |
Michael Walsh | a0ce75a | 2018-07-31 13:54:29 -0500 | [diff] [blame] | 67 | process_robot_output_files() |
| 68 | |
Michael Walsh | 3ba8ecd | 2018-04-24 11:33:25 -0500 | [diff] [blame] | 69 | qprint_pgm_footer() |
| 70 | |
| 71 | |
| 72 | def signal_handler(signal_number, |
| 73 | frame): |
Michael Walsh | 3ba8ecd | 2018-04-24 11:33:25 -0500 | [diff] [blame] | 74 | r""" |
| 75 | Handle signals. Without a function to catch a SIGTERM or SIGINT, our |
| 76 | program would terminate immediately with return code 143 and without |
| 77 | calling our exit_function. |
| 78 | """ |
| 79 | |
| 80 | # Our convention is to set up exit_function with atexit.register() so |
| 81 | # there is no need to explicitly call exit_function from here. |
| 82 | |
| 83 | dprint_executing() |
| 84 | |
| 85 | # Calling exit prevents us from returning to the code that was running |
| 86 | # when we received the signal. |
| 87 | exit(0) |
| 88 | |
| 89 | |
| 90 | def validate_parms(): |
Michael Walsh | 3ba8ecd | 2018-04-24 11:33:25 -0500 | [diff] [blame] | 91 | r""" |
| 92 | Validate program parameters, etc. Return True or False (i.e. pass/fail) |
| 93 | accordingly. |
| 94 | """ |
| 95 | |
| 96 | get_plug_vars() |
| 97 | |
| 98 | if not valid_value(AUTOBOOT_OPENBMC_HOST, ["", None]): |
| 99 | return False |
| 100 | |
| 101 | gen_post_validation(exit_function, signal_handler) |
| 102 | |
| 103 | return True |
| 104 | |
| 105 | |
Michael Walsh | 2ce1dba | 2019-02-05 19:29:28 +0000 | [diff] [blame] | 106 | def stop_check(): |
| 107 | r""" |
| 108 | Stop this program with the stop check return code. |
| 109 | """ |
| 110 | |
| 111 | if MASTER_PID != PROGRAM_PID: |
| 112 | stop_check_rc = stop_test_rc |
| 113 | save_plug_in_value(stop_check_rc) |
| 114 | exit(stop_test_rc) |
| 115 | |
| 116 | |
Michael Walsh | 3ba8ecd | 2018-04-24 11:33:25 -0500 | [diff] [blame] | 117 | def rest_fail(): |
Michael Walsh | 3ba8ecd | 2018-04-24 11:33:25 -0500 | [diff] [blame] | 118 | r""" |
| 119 | If STOP_REST_FAIL, then this function will determine whether REST commands |
| 120 | to the target are working. If not, this function will stop the program by |
| 121 | returning stop_test_rc. |
| 122 | """ |
| 123 | |
| 124 | if STOP_REST_FAIL != '1': |
| 125 | return |
| 126 | |
| 127 | print_timen("Checking to see whether REST commands are working.") |
Michael Walsh | 8ab9aea | 2018-11-01 16:30:45 -0500 | [diff] [blame] | 128 | init_robot_out_parms(get_plug_in_package_name() + "." + pgm_name + ".") |
| 129 | lib_file_path = init_robot_file_path("lib/state.py") + ":"\ |
| 130 | + init_robot_file_path("lib/gen_robot_print.py") |
Michael Walsh | 3ba8ecd | 2018-04-24 11:33:25 -0500 | [diff] [blame] | 131 | set_mod_global(lib_file_path) |
| 132 | timeout = '0 seconds' |
| 133 | interval = '1 second' |
| 134 | keyword_string = "${match_state}= Create Dictionary rest=1 ;" +\ |
| 135 | " ${state}= Wait State ${match_state} " + timeout + " " +\ |
| 136 | interval + " quiet=${1} ; Rpvar state" |
| 137 | set_mod_global(keyword_string) |
| 138 | |
| 139 | cmd_buf = create_robot_cmd_string("extended/run_keyword.robot", |
Michael Walsh | 2ce1dba | 2019-02-05 19:29:28 +0000 | [diff] [blame] | 140 | OPENBMC_HOST, REST_USERNAME, |
| 141 | REST_PASSWORD, keyword_string, |
Michael Walsh | 3ba8ecd | 2018-04-24 11:33:25 -0500 | [diff] [blame] | 142 | lib_file_path, quiet, test_mode, debug, |
| 143 | outputdir, output, log, report, loglevel) |
| 144 | if not robot_cmd_fnc(cmd_buf): |
| 145 | print_timen("The caller wishes to stop test execution if REST" + |
| 146 | " commands are failing.") |
Michael Walsh | 2ce1dba | 2019-02-05 19:29:28 +0000 | [diff] [blame] | 147 | stop_check() |
Michael Walsh | 3ba8ecd | 2018-04-24 11:33:25 -0500 | [diff] [blame] | 148 | print_timen("REST commands are working so no reason as of yet to stop" + |
| 149 | " the test.") |
| 150 | |
| 151 | |
| 152 | def esel_stop_check(): |
Michael Walsh | 3ba8ecd | 2018-04-24 11:33:25 -0500 | [diff] [blame] | 153 | r""" |
| 154 | Run the esel_stop_check program to determine whether any eSEL entries |
Gunnar Mills | 7732c7e | 2018-08-14 11:54:24 -0500 | [diff] [blame] | 155 | found warrant stopping the test run. See esel_stop_check help text for |
Michael Walsh | 3ba8ecd | 2018-04-24 11:33:25 -0500 | [diff] [blame] | 156 | details. |
| 157 | """ |
| 158 | |
| 159 | if STOP_ESEL_STOP_FILE_PATH == "": |
| 160 | return |
| 161 | |
| 162 | cmd_buf = "esel_stop_check --esel_stop_file_path=" +\ |
| 163 | STOP_ESEL_STOP_FILE_PATH |
| 164 | shell_rc, out_buf = cmd_fnc_u(cmd_buf, show_err=0) |
| 165 | if shell_rc == stop_test_rc: |
| 166 | print_timen("The caller wishes to stop test execution based on the" + |
| 167 | " presence of certain esel entries.") |
Michael Walsh | 2ce1dba | 2019-02-05 19:29:28 +0000 | [diff] [blame] | 168 | stop_check() |
Michael Walsh | 3ba8ecd | 2018-04-24 11:33:25 -0500 | [diff] [blame] | 169 | |
| 170 | |
| 171 | def main(): |
| 172 | |
| 173 | if not gen_get_options(parser, stock_list): |
| 174 | return False |
| 175 | |
| 176 | if not validate_parms(): |
| 177 | return False |
| 178 | |
| 179 | qprint_pgm_header() |
| 180 | |
| 181 | if not debug: |
| 182 | qprint_vars(STOP_REST_FAIL, STOP_COMMAND, AUTOBOOT_BOOT_SUCCESS) |
| 183 | |
Michael Walsh | 80caea0 | 2019-06-21 11:31:41 -0500 | [diff] [blame] | 184 | print_plug_in_header() |
Michael Walsh | 3ba8ecd | 2018-04-24 11:33:25 -0500 | [diff] [blame] | 185 | |
| 186 | rest_fail() |
| 187 | |
| 188 | esel_stop_check() |
| 189 | |
| 190 | if STOP_COMMAND.upper() == "FAIL": |
| 191 | if AUTOBOOT_BOOT_SUCCESS == "0": |
| 192 | print_timen("The caller wishes to stop after each boot failure.") |
Michael Walsh | 2ce1dba | 2019-02-05 19:29:28 +0000 | [diff] [blame] | 193 | stop_check() |
Michael Walsh | 3ba8ecd | 2018-04-24 11:33:25 -0500 | [diff] [blame] | 194 | elif STOP_COMMAND.upper() == "ALL": |
| 195 | print_timen("The caller wishes to stop after each boot test.") |
Michael Walsh | 2ce1dba | 2019-02-05 19:29:28 +0000 | [diff] [blame] | 196 | stop_check() |
Michael Walsh | 3ba8ecd | 2018-04-24 11:33:25 -0500 | [diff] [blame] | 197 | elif len(STOP_COMMAND) > 0: |
| 198 | shell_rc, out_buf = cmd_fnc_u(STOP_COMMAND, quiet=quiet, show_err=0) |
| 199 | if shell_rc != 0: |
| 200 | print_timen("The caller wishes to stop test execution.") |
Michael Walsh | 2ce1dba | 2019-02-05 19:29:28 +0000 | [diff] [blame] | 201 | stop_check() |
Michael Walsh | 3ba8ecd | 2018-04-24 11:33:25 -0500 | [diff] [blame] | 202 | |
| 203 | qprint_timen("The caller does not wish to stop the test run.") |
| 204 | return True |
| 205 | |
Joy Onyerikwu | 004ad3c | 2018-06-11 16:29:56 -0500 | [diff] [blame] | 206 | |
Michael Walsh | 2ce1dba | 2019-02-05 19:29:28 +0000 | [diff] [blame] | 207 | # Main |
Michael Walsh | 7e96b13 | 2019-02-05 19:24:54 +0000 | [diff] [blame] | 208 | |
Michael Walsh | 3ba8ecd | 2018-04-24 11:33:25 -0500 | [diff] [blame] | 209 | if not main(): |
| 210 | exit(1) |