13ba8ecdcSMichael Walsh#!/usr/bin/env python 23ba8ecdcSMichael Walsh 33ba8ecdcSMichael Walshr""" 43ba8ecdcSMichael WalshCheck for stop conditions. Return code of 2 if stop conditions are found. 53ba8ecdcSMichael Walsh""" 63ba8ecdcSMichael Walsh 73ba8ecdcSMichael Walshimport sys 83ba8ecdcSMichael Walshimport subprocess 93ba8ecdcSMichael Walsh 103ba8ecdcSMichael Walshsave_path_0 = sys.path[0] 113ba8ecdcSMichael Walshdel sys.path[0] 123ba8ecdcSMichael Walsh 133ba8ecdcSMichael Walshfrom gen_print import * 143ba8ecdcSMichael Walshfrom gen_valid import * 153ba8ecdcSMichael Walshfrom gen_arg import * 163ba8ecdcSMichael Walshfrom gen_misc import * 173ba8ecdcSMichael Walshfrom gen_cmd import * 183ba8ecdcSMichael Walshfrom gen_plug_in_utils import * 193ba8ecdcSMichael Walshfrom gen_call_robot import * 203ba8ecdcSMichael Walsh 213ba8ecdcSMichael Walsh# Restore sys.path[0]. 223ba8ecdcSMichael Walshsys.path.insert(0, save_path_0) 233ba8ecdcSMichael Walsh 24*2ea965ceSMichael Walsh# Set exit_on_error for gen_valid functions. 25*2ea965ceSMichael Walshset_exit_on_error(True) 26*2ea965ceSMichael Walsh 273ba8ecdcSMichael Walsh# Initialize. 283ba8ecdcSMichael WalshSTOP_REST_FAIL = '' 293ba8ecdcSMichael WalshSTOP_COMMAND = '' 303ba8ecdcSMichael Walshstop_test_rc = 2 313ba8ecdcSMichael Walsh 323ba8ecdcSMichael Walsh# Create parser object to process command line parameters and args. 333ba8ecdcSMichael Walsh 343ba8ecdcSMichael Walsh# Create parser object. 353ba8ecdcSMichael Walshparser = argparse.ArgumentParser( 363ba8ecdcSMichael Walsh usage='%(prog)s [OPTIONS]', 373ba8ecdcSMichael Walsh description="If the \"Stop\" plug-in is selected by the user, %(prog)s" + 383ba8ecdcSMichael Walsh " is called by OBMC Boot Test after each boot test. If %(prog)s returns" + 393ba8ecdcSMichael Walsh " " + str(stop_test_rc) + ", then OBMC Boot Test will stop. The user" + 403ba8ecdcSMichael Walsh " may set environment variable STOP_COMMAND to contain any valid bash" + 413ba8ecdcSMichael Walsh " command or program. %(prog)s will run this stop command. If the stop" + 423ba8ecdcSMichael Walsh " command returns non-zero, then %(prog)s will return " + 433ba8ecdcSMichael Walsh str(stop_test_rc) + ". %(prog)s recognizes some special values for" + 443ba8ecdcSMichael Walsh " STOP_COMMAND: 1) \"FAIL\" means that OBMC Boot Test should stop" + 453ba8ecdcSMichael Walsh " whenever a boot test fails. 2) \"ALL\" means that OBMC Boot Test" + 463ba8ecdcSMichael Walsh " should stop after any boot test. If environment variable" + 473ba8ecdcSMichael Walsh " STOP_REST_FAIL is set, OBMC Boot Test will stop if REST commands are" + 483ba8ecdcSMichael Walsh " no longer working.", 493ba8ecdcSMichael Walsh formatter_class=argparse.RawTextHelpFormatter, 503ba8ecdcSMichael Walsh prefix_chars='-+') 513ba8ecdcSMichael Walsh 523ba8ecdcSMichael Walsh# The stock_list will be passed to gen_get_options. We populate it with the 533ba8ecdcSMichael Walsh# names of stock parm options we want. These stock parms are pre-defined by 543ba8ecdcSMichael Walsh# gen_get_options. 553ba8ecdcSMichael Walshstock_list = [("test_mode", 0), 563ba8ecdcSMichael Walsh ("quiet", get_plug_default("quiet", 0)), 573ba8ecdcSMichael Walsh ("debug", get_plug_default("debug", 0))] 583ba8ecdcSMichael Walsh 593ba8ecdcSMichael Walsh 603ba8ecdcSMichael Walshdef exit_function(signal_number=0, 613ba8ecdcSMichael Walsh frame=None): 623ba8ecdcSMichael Walsh r""" 633ba8ecdcSMichael Walsh Execute whenever the program ends normally or with the signals that we 643ba8ecdcSMichael Walsh catch (i.e. TERM, INT). 653ba8ecdcSMichael Walsh """ 663ba8ecdcSMichael Walsh 673ba8ecdcSMichael Walsh dprint_executing() 683ba8ecdcSMichael Walsh dprint_var(signal_number) 693ba8ecdcSMichael Walsh 70a0ce75a7SMichael Walsh process_robot_output_files() 71a0ce75a7SMichael Walsh 723ba8ecdcSMichael Walsh qprint_pgm_footer() 733ba8ecdcSMichael Walsh 743ba8ecdcSMichael Walsh 753ba8ecdcSMichael Walshdef signal_handler(signal_number, 763ba8ecdcSMichael Walsh frame): 773ba8ecdcSMichael Walsh r""" 783ba8ecdcSMichael Walsh Handle signals. Without a function to catch a SIGTERM or SIGINT, our 793ba8ecdcSMichael Walsh program would terminate immediately with return code 143 and without 803ba8ecdcSMichael Walsh calling our exit_function. 813ba8ecdcSMichael Walsh """ 823ba8ecdcSMichael Walsh 833ba8ecdcSMichael Walsh # Our convention is to set up exit_function with atexit.register() so 843ba8ecdcSMichael Walsh # there is no need to explicitly call exit_function from here. 853ba8ecdcSMichael Walsh 863ba8ecdcSMichael Walsh dprint_executing() 873ba8ecdcSMichael Walsh 883ba8ecdcSMichael Walsh # Calling exit prevents us from returning to the code that was running 893ba8ecdcSMichael Walsh # when we received the signal. 903ba8ecdcSMichael Walsh exit(0) 913ba8ecdcSMichael Walsh 923ba8ecdcSMichael Walsh 933ba8ecdcSMichael Walshdef validate_parms(): 943ba8ecdcSMichael Walsh r""" 953ba8ecdcSMichael Walsh Validate program parameters, etc. Return True or False (i.e. pass/fail) 963ba8ecdcSMichael Walsh accordingly. 973ba8ecdcSMichael Walsh """ 983ba8ecdcSMichael Walsh 993ba8ecdcSMichael Walsh get_plug_vars() 1003ba8ecdcSMichael Walsh 101*2ea965ceSMichael Walsh valid_value(AUTOBOOT_OPENBMC_HOST) 1023ba8ecdcSMichael Walsh 1033ba8ecdcSMichael Walsh gen_post_validation(exit_function, signal_handler) 1043ba8ecdcSMichael Walsh 1053ba8ecdcSMichael Walsh return True 1063ba8ecdcSMichael Walsh 1073ba8ecdcSMichael Walsh 1082ce1dbaeSMichael Walshdef stop_check(): 1092ce1dbaeSMichael Walsh r""" 1102ce1dbaeSMichael Walsh Stop this program with the stop check return code. 1112ce1dbaeSMichael Walsh """ 1122ce1dbaeSMichael Walsh 1132ce1dbaeSMichael Walsh if MASTER_PID != PROGRAM_PID: 1142ce1dbaeSMichael Walsh stop_check_rc = stop_test_rc 1152ce1dbaeSMichael Walsh save_plug_in_value(stop_check_rc) 1162ce1dbaeSMichael Walsh exit(stop_test_rc) 1172ce1dbaeSMichael Walsh 1182ce1dbaeSMichael Walsh 1193ba8ecdcSMichael Walshdef rest_fail(): 1203ba8ecdcSMichael Walsh r""" 1213ba8ecdcSMichael Walsh If STOP_REST_FAIL, then this function will determine whether REST commands 1223ba8ecdcSMichael Walsh to the target are working. If not, this function will stop the program by 1233ba8ecdcSMichael Walsh returning stop_test_rc. 1243ba8ecdcSMichael Walsh """ 1253ba8ecdcSMichael Walsh 1263ba8ecdcSMichael Walsh if STOP_REST_FAIL != '1': 1273ba8ecdcSMichael Walsh return 1283ba8ecdcSMichael Walsh 1293ba8ecdcSMichael Walsh print_timen("Checking to see whether REST commands are working.") 1308ab9aea6SMichael Walsh init_robot_out_parms(get_plug_in_package_name() + "." + pgm_name + ".") 1318ab9aea6SMichael Walsh lib_file_path = init_robot_file_path("lib/state.py") + ":"\ 1328ab9aea6SMichael Walsh + init_robot_file_path("lib/gen_robot_print.py") 1333ba8ecdcSMichael Walsh set_mod_global(lib_file_path) 1343ba8ecdcSMichael Walsh timeout = '0 seconds' 1353ba8ecdcSMichael Walsh interval = '1 second' 1363ba8ecdcSMichael Walsh keyword_string = "${match_state}= Create Dictionary rest=1 ;" +\ 1373ba8ecdcSMichael Walsh " ${state}= Wait State ${match_state} " + timeout + " " +\ 1383ba8ecdcSMichael Walsh interval + " quiet=${1} ; Rpvar state" 1393ba8ecdcSMichael Walsh set_mod_global(keyword_string) 1403ba8ecdcSMichael Walsh 1413ba8ecdcSMichael Walsh cmd_buf = create_robot_cmd_string("extended/run_keyword.robot", 1422ce1dbaeSMichael Walsh OPENBMC_HOST, REST_USERNAME, 1432ce1dbaeSMichael Walsh REST_PASSWORD, keyword_string, 1443ba8ecdcSMichael Walsh lib_file_path, quiet, test_mode, debug, 1453ba8ecdcSMichael Walsh outputdir, output, log, report, loglevel) 1463ba8ecdcSMichael Walsh if not robot_cmd_fnc(cmd_buf): 1473ba8ecdcSMichael Walsh print_timen("The caller wishes to stop test execution if REST" + 1483ba8ecdcSMichael Walsh " commands are failing.") 1492ce1dbaeSMichael Walsh stop_check() 1503ba8ecdcSMichael Walsh print_timen("REST commands are working so no reason as of yet to stop" + 1513ba8ecdcSMichael Walsh " the test.") 1523ba8ecdcSMichael Walsh 1533ba8ecdcSMichael Walsh 1543ba8ecdcSMichael Walshdef esel_stop_check(): 1553ba8ecdcSMichael Walsh r""" 1563ba8ecdcSMichael Walsh Run the esel_stop_check program to determine whether any eSEL entries 1577732c7e2SGunnar Mills found warrant stopping the test run. See esel_stop_check help text for 1583ba8ecdcSMichael Walsh details. 1593ba8ecdcSMichael Walsh """ 1603ba8ecdcSMichael Walsh 1613ba8ecdcSMichael Walsh if STOP_ESEL_STOP_FILE_PATH == "": 1623ba8ecdcSMichael Walsh return 1633ba8ecdcSMichael Walsh 1643ba8ecdcSMichael Walsh cmd_buf = "esel_stop_check --esel_stop_file_path=" +\ 1653ba8ecdcSMichael Walsh STOP_ESEL_STOP_FILE_PATH 1663ba8ecdcSMichael Walsh shell_rc, out_buf = cmd_fnc_u(cmd_buf, show_err=0) 1673ba8ecdcSMichael Walsh if shell_rc == stop_test_rc: 1683ba8ecdcSMichael Walsh print_timen("The caller wishes to stop test execution based on the" + 1693ba8ecdcSMichael Walsh " presence of certain esel entries.") 1702ce1dbaeSMichael Walsh stop_check() 1713ba8ecdcSMichael Walsh 1723ba8ecdcSMichael Walsh 1733ba8ecdcSMichael Walshdef main(): 1743ba8ecdcSMichael Walsh 1753ba8ecdcSMichael Walsh if not gen_get_options(parser, stock_list): 1763ba8ecdcSMichael Walsh return False 1773ba8ecdcSMichael Walsh 1783ba8ecdcSMichael Walsh if not validate_parms(): 1793ba8ecdcSMichael Walsh return False 1803ba8ecdcSMichael Walsh 1813ba8ecdcSMichael Walsh qprint_pgm_header() 1823ba8ecdcSMichael Walsh 1833ba8ecdcSMichael Walsh if not debug: 1843ba8ecdcSMichael Walsh qprint_vars(STOP_REST_FAIL, STOP_COMMAND, AUTOBOOT_BOOT_SUCCESS) 1853ba8ecdcSMichael Walsh 18680caea09SMichael Walsh print_plug_in_header() 1873ba8ecdcSMichael Walsh 1883ba8ecdcSMichael Walsh if STOP_COMMAND.upper() == "FAIL": 1893ba8ecdcSMichael Walsh if AUTOBOOT_BOOT_SUCCESS == "0": 1903ba8ecdcSMichael Walsh print_timen("The caller wishes to stop after each boot failure.") 1912ce1dbaeSMichael Walsh stop_check() 1923ba8ecdcSMichael Walsh elif STOP_COMMAND.upper() == "ALL": 1933ba8ecdcSMichael Walsh print_timen("The caller wishes to stop after each boot test.") 1942ce1dbaeSMichael Walsh stop_check() 1953ba8ecdcSMichael Walsh elif len(STOP_COMMAND) > 0: 1963ba8ecdcSMichael Walsh shell_rc, out_buf = cmd_fnc_u(STOP_COMMAND, quiet=quiet, show_err=0) 1973ba8ecdcSMichael Walsh if shell_rc != 0: 1983ba8ecdcSMichael Walsh print_timen("The caller wishes to stop test execution.") 1992ce1dbaeSMichael Walsh stop_check() 2003ba8ecdcSMichael Walsh 2013c4869a2SMichael Walsh rest_fail() 2023c4869a2SMichael Walsh 2033c4869a2SMichael Walsh esel_stop_check() 2043c4869a2SMichael Walsh 2053ba8ecdcSMichael Walsh qprint_timen("The caller does not wish to stop the test run.") 2063ba8ecdcSMichael Walsh return True 2073ba8ecdcSMichael Walsh 208004ad3c9SJoy Onyerikwu 2092ce1dbaeSMichael Walsh# Main 2107e96b132SMichael Walsh 2113ba8ecdcSMichael Walshif not main(): 2123ba8ecdcSMichael Walsh exit(1) 213