1#!/usr/bin/env python 2 3r""" 4This module is the python counterpart to obmc_boot_test. 5""" 6 7import os 8import imp 9import time 10import glob 11import random 12import cPickle as pickle 13 14from robot.utils import DotDict 15from robot.libraries.BuiltIn import BuiltIn 16 17from boot_data import * 18import gen_robot_print as grp 19import gen_robot_plug_in as grpi 20import gen_robot_valid as grv 21import gen_misc as gm 22import gen_cmd as gc 23import state as st 24 25base_path = os.path.dirname(os.path.dirname( 26 imp.find_module("gen_robot_print")[1])) +\ 27 os.sep 28sys.path.append(base_path + "extended/") 29import run_keyword as rk 30 31# Program parameter processing. 32# Assign all program parms to python variables which are global to this module. 33parm_list = BuiltIn().get_variable_value("${parm_list}") 34int_list = ['max_num_tests', 'boot_pass', 'boot_fail', 'quiet', 'test_mode', 35 'debug'] 36for parm in parm_list: 37 if parm in int_list: 38 sub_cmd = "int(BuiltIn().get_variable_value(\"${" + parm +\ 39 "}\", \"0\"))" 40 else: 41 sub_cmd = "BuiltIn().get_variable_value(\"${" + parm + "}\")" 42 cmd_buf = parm + " = " + sub_cmd 43 exec(cmd_buf) 44 45if ffdc_dir_path_style == "": 46 ffdc_dir_path_style = int(os.environ.get('FFDC_DIR_PATH_STYLE', '0')) 47 48# Set up boot data structures. 49boot_table = create_boot_table() 50valid_boot_types = create_valid_boot_list(boot_table) 51 52boot_results_file_path = "/tmp/" + openbmc_nickname + "_boot_results" 53if (boot_pass > 0 or boot_fail > 0) and \ 54 os.path.isfile(boot_results_file_path): 55 # We've been called before in this run so we'll load the saved 56 # boot_results object. 57 boot_results = pickle.load(open(boot_results_file_path, 'rb')) 58else: 59 boot_results = boot_results(boot_table, boot_pass, boot_fail) 60 61boot_lists = read_boot_lists() 62last_ten = [] 63# Convert these program parms to more useable lists. 64boot_list = filter(None, boot_list.split(":")) 65boot_stack = filter(None, boot_stack.split(":")) 66 67state = st.return_default_state() 68cp_setup_called = 0 69next_boot = "" 70base_tool_dir_path = os.path.normpath(os.environ.get( 71 'AUTOBOOT_BASE_TOOL_DIR_PATH', "/tmp")) + os.sep 72ffdc_dir_path = os.path.normpath(os.environ.get('FFDC_DIR_PATH', '')) + os.sep 73ffdc_list_file_path = base_tool_dir_path + openbmc_nickname + "/FFDC_FILE_LIST" 74boot_success = 0 75# Setting master_pid correctly influences the behavior of plug-ins like 76# DB_Logging 77program_pid = os.getpid() 78master_pid = os.environ.get('AUTOBOOT_MASTER_PID', program_pid) 79status_dir_path = os.environ.get('STATUS_DIR_PATH', "") 80if status_dir_path != "": 81 status_dir_path = os.path.normpath(status_dir_path) + os.sep 82default_power_on = "REST Power On" 83default_power_off = "REST Power Off" 84boot_count = 0 85 86 87############################################################################### 88def plug_in_setup(): 89 90 r""" 91 Initialize all plug-in environment variables for use by the plug-in 92 programs. 93 """ 94 95 boot_pass, boot_fail = boot_results.return_total_pass_fail() 96 if boot_pass > 1: 97 test_really_running = 1 98 else: 99 test_really_running = 0 100 101 seconds = time.time() 102 loc_time = time.localtime(seconds) 103 time_string = time.strftime("%y%m%d.%H%M%S.", loc_time) 104 105 ffdc_prefix = openbmc_nickname + "." + time_string 106 107 BuiltIn().set_global_variable("${test_really_running}", 108 test_really_running) 109 BuiltIn().set_global_variable("${boot_type_desc}", next_boot) 110 BuiltIn().set_global_variable("${master_pid}", master_pid) 111 BuiltIn().set_global_variable("${FFDC_DIR_PATH}", ffdc_dir_path) 112 BuiltIn().set_global_variable("${STATUS_DIR_PATH}", status_dir_path) 113 BuiltIn().set_global_variable("${BASE_TOOL_DIR_PATH}", base_tool_dir_path) 114 BuiltIn().set_global_variable("${FFDC_LIST_FILE_PATH}", 115 ffdc_list_file_path) 116 BuiltIn().set_global_variable("${FFDC_DIR_PATH_STYLE}", 117 ffdc_dir_path_style) 118 BuiltIn().set_global_variable("${FFDC_CHECK}", 119 ffdc_check) 120 BuiltIn().set_global_variable("${boot_pass}", boot_pass) 121 BuiltIn().set_global_variable("${boot_fail}", boot_fail) 122 BuiltIn().set_global_variable("${boot_success}", boot_success) 123 BuiltIn().set_global_variable("${ffdc_prefix}", ffdc_prefix) 124 125 # For each program parameter, set the corresponding AUTOBOOT_ environment 126 # variable value. Also, set an AUTOBOOT_ environment variable for every 127 # element in additional_values. 128 additional_values = ["boot_type_desc", "boot_success", "boot_pass", 129 "boot_fail", "test_really_running", "program_pid", 130 "master_pid", "ffdc_prefix", "ffdc_dir_path", 131 "status_dir_path", "base_tool_dir_path", 132 "ffdc_list_file_path"] 133 134 plug_in_vars = parm_list + additional_values 135 136 for var_name in plug_in_vars: 137 var_value = BuiltIn().get_variable_value("${" + var_name + "}") 138 var_name = var_name.upper() 139 if var_value is None: 140 var_value = "" 141 os.environ["AUTOBOOT_" + var_name] = str(var_value) 142 143 if debug: 144 shell_rc, out_buf = \ 145 gc.cmd_fnc_u("printenv | egrep AUTOBOOT_ | sort -u") 146 147############################################################################### 148 149 150############################################################################### 151def setup(): 152 153 r""" 154 Do general program setup tasks. 155 """ 156 157 global cp_setup_called 158 159 grp.rqprintn() 160 161 validate_parms() 162 163 grp.rqprint_pgm_header() 164 165 plug_in_setup() 166 rc, shell_rc, failed_plug_in_name = grpi.rprocess_plug_in_packages( 167 call_point='setup') 168 if rc != 0: 169 error_message = "Plug-in setup failed.\n" 170 grp.rprint_error_report(error_message) 171 BuiltIn().fail(error_message) 172 # Setting cp_setup_called lets our Teardown know that it needs to call 173 # the cleanup plug-in call point. 174 cp_setup_called = 1 175 176 # Keyword "FFDC" will fail if TEST_MESSAGE is not set. 177 BuiltIn().set_global_variable("${TEST_MESSAGE}", "${EMPTY}") 178 179 grp.rdprint_var(boot_table, 1) 180 grp.rdprint_var(boot_lists) 181 182############################################################################### 183 184 185############################################################################### 186def validate_parms(): 187 188 r""" 189 Validate all program parameters. 190 """ 191 192 grp.rqprintn() 193 194 grv.rvalid_value("openbmc_host") 195 grv.rvalid_value("openbmc_username") 196 grv.rvalid_value("openbmc_password") 197 if os_host != "": 198 grv.rvalid_value("os_username") 199 grv.rvalid_value("os_password") 200 201 if pdu_host != "": 202 grv.rvalid_value("pdu_username") 203 grv.rvalid_value("pdu_password") 204 grv.rvalid_integer("pdu_slot_no") 205 if openbmc_serial_host != "": 206 grv.rvalid_integer("openbmc_serial_port") 207 grv.rvalid_integer("max_num_tests") 208 grv.rvalid_value("openbmc_model") 209 grv.rvalid_integer("boot_pass") 210 grv.rvalid_integer("boot_fail") 211 212 plug_in_packages_list = grpi.rvalidate_plug_ins(plug_in_dir_paths) 213 BuiltIn().set_global_variable("${plug_in_packages_list}", 214 plug_in_packages_list) 215 216 if len(boot_list) == 0 and len(boot_stack) == 0: 217 error_message = "You must provide either a value for either the" +\ 218 " boot_list or the boot_stack parm.\n" 219 BuiltIn().fail(gp.sprint_error(error_message)) 220 221 valid_boot_list(boot_list, valid_boot_types) 222 valid_boot_list(boot_stack, valid_boot_types) 223 224 return 225 226############################################################################### 227 228 229############################################################################### 230def my_get_state(): 231 232 r""" 233 Get the system state plus a little bit of wrapping. 234 """ 235 236 global state 237 238 req_states = ['epoch_seconds'] + st.default_req_states 239 240 grp.rqprint_timen("Getting system state.") 241 if test_mode: 242 state['epoch_seconds'] = int(time.time()) 243 else: 244 state = st.get_state(req_states=req_states, quiet=0) 245 grp.rprint_var(state) 246 247############################################################################### 248 249 250############################################################################### 251def select_boot(): 252 253 r""" 254 Select a boot test to be run based on our current state and return the 255 chosen boot type. 256 257 Description of arguments: 258 state The state of the machine. 259 """ 260 261 global boot_stack 262 263 grp.rprint_timen("Selecting a boot test.") 264 265 my_get_state() 266 267 stack_popped = 0 268 if len(boot_stack) > 0: 269 stack_popped = 1 270 grp.rprint_dashes() 271 grp.rprint_var(boot_stack) 272 grp.rprint_dashes() 273 boot_candidate = boot_stack.pop() 274 if st.compare_states(state, boot_table[boot_candidate]['start']): 275 grp.rprint_timen("The machine state is valid for a '" + 276 boot_candidate + "' boot test.") 277 grp.rprint_dashes() 278 grp.rprint_var(boot_stack) 279 grp.rprint_dashes() 280 return boot_candidate 281 else: 282 grp.rprint_timen("The machine state is not valid for a '" + 283 boot_candidate + "' boot test.") 284 boot_stack.append(boot_candidate) 285 popped_boot = boot_candidate 286 287 # Loop through your list selecting a boot_candidates 288 boot_candidates = [] 289 for boot_candidate in boot_list: 290 if st.compare_states(state, boot_table[boot_candidate]['start']): 291 if stack_popped: 292 if st.compare_states(boot_table[boot_candidate]['end'], 293 boot_table[popped_boot]['start']): 294 boot_candidates.append(boot_candidate) 295 else: 296 boot_candidates.append(boot_candidate) 297 298 if len(boot_candidates) == 0: 299 grp.rprint_timen("The user's boot list contained no boot tests" + 300 " which are valid for the current machine state.") 301 boot_candidate = default_power_on 302 if not st.compare_states(state, boot_table[default_power_on]['start']): 303 boot_candidate = default_power_off 304 boot_candidates.append(boot_candidate) 305 grp.rprint_timen("Using default '" + boot_candidate + 306 "' boot type to transtion to valid state.") 307 308 grp.rdprint_var(boot_candidates) 309 310 # Randomly select a boot from the candidate list. 311 boot = random.choice(boot_candidates) 312 313 return boot 314 315############################################################################### 316 317 318############################################################################### 319def print_last_boots(): 320 321 r""" 322 Print the last ten boots done with their time stamps. 323 """ 324 325 # indent 0, 90 chars wide, linefeed, char is "=" 326 grp.rqprint_dashes(0, 90) 327 grp.rqprintn("Last 10 boots:\n") 328 329 for boot_entry in last_ten: 330 grp.rqprint(boot_entry) 331 grp.rqprint_dashes(0, 90) 332 333############################################################################### 334 335 336############################################################################### 337def print_defect_report(): 338 339 r""" 340 Print a defect report. 341 """ 342 343 grp.rqprintn() 344 # indent=0, width=90, linefeed=1, char="=" 345 grp.rqprint_dashes(0, 90, 1, "=") 346 grp.rqprintn("Copy this data to the defect:\n") 347 348 grp.rqpvars(*parm_list) 349 350 grp.rqprintn() 351 352 print_last_boots() 353 grp.rqprintn() 354 grp.rqpvar(state) 355 356 # At some point I'd like to have the 'Call FFDC Methods' return a list 357 # of files it has collected. In that case, the following "ls" command 358 # would no longer be needed. For now, however, glob shows the files 359 # named in FFDC_LIST_FILE_PATH so I will refrain from printing those 360 # out (so we don't see duplicates in the list). 361 362 LOG_PREFIX = BuiltIn().get_variable_value("${LOG_PREFIX}") 363 364 output = '\n'.join(glob.glob(LOG_PREFIX + '*')) 365 try: 366 ffdc_list = open(ffdc_list_file_path, 'r') 367 except IOError: 368 ffdc_list = "" 369 370 grp.rqprintn() 371 grp.rqprintn("FFDC data files:") 372 if status_file_path != "": 373 grp.rqprintn(status_file_path) 374 375 grp.rqprintn(output) 376 # grp.rqprintn(ffdc_list) 377 grp.rqprintn() 378 379 grp.rqprint_dashes(0, 90, 1, "=") 380 381############################################################################### 382 383 384############################################################################### 385def my_ffdc(): 386 387 r""" 388 Collect FFDC data. 389 """ 390 391 global state 392 393 plug_in_setup() 394 rc, shell_rc, failed_plug_in_name = grpi.rprocess_plug_in_packages( 395 call_point='ffdc', stop_on_plug_in_failure=1) 396 397 AUTOBOOT_FFDC_PREFIX = os.environ['AUTOBOOT_FFDC_PREFIX'] 398 399 # FFDC_LOG_PATH is used by "FFDC" keyword. 400 BuiltIn().set_global_variable("${FFDC_LOG_PATH}", ffdc_dir_path) 401 402 cmd_buf = ["FFDC", "ffdc_prefix=" + AUTOBOOT_FFDC_PREFIX] 403 grp.rpissuing_keyword(cmd_buf) 404 BuiltIn().run_keyword(*cmd_buf) 405 406 my_get_state() 407 408 print_defect_report() 409 410############################################################################### 411 412 413############################################################################### 414def print_test_start_message(boot_keyword): 415 416 r""" 417 Print a message indicating what boot test is about to run. 418 419 Description of arguments: 420 boot_keyword The name of the boot which is to be run 421 (e.g. "BMC Power On"). 422 """ 423 424 global last_ten 425 426 doing_msg = gp.sprint_timen("Doing \"" + boot_keyword + "\".") 427 grp.rqprint(doing_msg) 428 429 last_ten.append(doing_msg) 430 431 if len(last_ten) > 10: 432 del last_ten[0] 433 434############################################################################### 435 436 437############################################################################### 438def run_boot(boot): 439 440 r""" 441 Run the specified boot. 442 443 Description of arguments: 444 boot The name of the boot test to be performed. 445 """ 446 447 global state 448 449 print_test_start_message(boot) 450 451 plug_in_setup() 452 rc, shell_rc, failed_plug_in_name = \ 453 grpi.rprocess_plug_in_packages(call_point="pre_boot") 454 if rc != 0: 455 error_message = "Plug-in failed with non-zero return code.\n" +\ 456 gp.sprint_var(rc, 1) 457 BuiltIn().fail(gp.sprint_error(error_message)) 458 459 if test_mode: 460 # In test mode, we'll pretend the boot worked by assigning its 461 # required end state to the default state value. 462 state = st.strip_anchor_state(boot_table[boot]['end']) 463 else: 464 # Assertion: We trust that the state data was made fresh by the 465 # caller. 466 467 grp.rprintn() 468 469 if boot_table[boot]['method_type'] == "keyword": 470 rk.my_run_keywords(boot_table[boot].get('lib_file_path', ''), 471 boot_table[boot]['method']) 472 473 if boot_table[boot]['bmc_reboot']: 474 st.wait_for_comm_cycle(int(state['epoch_seconds'])) 475 plug_in_setup() 476 rc, shell_rc, failed_plug_in_name = \ 477 grpi.rprocess_plug_in_packages(call_point="post_reboot") 478 if rc != 0: 479 error_message = "Plug-in failed with non-zero return code.\n" 480 error_message += gp.sprint_var(rc, 1) 481 BuiltIn().fail(gp.sprint_error(error_message)) 482 else: 483 match_state = st.anchor_state(state) 484 del match_state['epoch_seconds'] 485 # Wait for the state to change in any way. 486 st.wait_state(match_state, wait_time=state_change_timeout, 487 interval="3 seconds", invert=1) 488 489 grp.rprintn() 490 if boot_table[boot]['end']['chassis'] == "Off": 491 boot_timeout = power_off_timeout 492 else: 493 boot_timeout = power_on_timeout 494 st.wait_state(boot_table[boot]['end'], wait_time=boot_timeout, 495 interval="3 seconds") 496 497 plug_in_setup() 498 rc, shell_rc, failed_plug_in_name = \ 499 grpi.rprocess_plug_in_packages(call_point="post_boot") 500 if rc != 0: 501 error_message = "Plug-in failed with non-zero return code.\n" +\ 502 gp.sprint_var(rc, 1) 503 BuiltIn().fail(gp.sprint_error(error_message)) 504 505############################################################################### 506 507 508############################################################################### 509def test_loop_body(): 510 511 r""" 512 The main loop body for the loop in main_py. 513 514 Description of arguments: 515 boot_count The iteration number (starts at 1). 516 """ 517 518 global boot_count 519 global state 520 global next_boot 521 global boot_success 522 523 grp.rqprintn() 524 525 boot_count += 1 526 527 next_boot = select_boot() 528 529 grp.rqprint_timen("Starting boot " + str(boot_count) + ".") 530 531 # Clear the ffdc_list_file_path file. Plug-ins may now write to it. 532 try: 533 os.remove(ffdc_list_file_path) 534 except OSError: 535 pass 536 537 cmd_buf = ["run_boot", next_boot] 538 boot_status, msg = BuiltIn().run_keyword_and_ignore_error(*cmd_buf) 539 if boot_status == "FAIL": 540 grp.rprint(msg) 541 542 grp.rqprintn() 543 if boot_status == "PASS": 544 boot_success = 1 545 grp.rqprint_timen("BOOT_SUCCESS: \"" + next_boot + "\" succeeded.") 546 else: 547 boot_success = 0 548 grp.rqprint_timen("BOOT_FAILED: \"" + next_boot + "\" failed.") 549 550 boot_results.update(next_boot, boot_status) 551 552 plug_in_setup() 553 # NOTE: A post_test_case call point failure is NOT counted as a boot 554 # failure. 555 rc, shell_rc, failed_plug_in_name = grpi.rprocess_plug_in_packages( 556 call_point='post_test_case', stop_on_plug_in_failure=1) 557 558 plug_in_setup() 559 rc, shell_rc, failed_plug_in_name = grpi.rprocess_plug_in_packages( 560 call_point='ffdc_check', shell_rc=0x00000200, 561 stop_on_plug_in_failure=1, stop_on_non_zero_rc=1) 562 if boot_status != "PASS" or ffdc_check == "All" or shell_rc == 0x00000200: 563 cmd_buf = ["my_ffdc"] 564 grp.rpissuing_keyword(cmd_buf) 565 BuiltIn().run_keyword_and_continue_on_failure(*cmd_buf) 566 567 plug_in_setup() 568 rc, shell_rc, failed_plug_in_name = grpi.rprocess_plug_in_packages( 569 call_point='stop_check') 570 if rc != 0: 571 error_message = "Stopping as requested by user.\n" 572 grp.rprint_error_report(error_message) 573 BuiltIn().fail(error_message) 574 575 boot_results.print_report() 576 grp.rqprint_timen("Finished boot " + str(boot_count) + ".") 577 578 return True 579 580############################################################################### 581 582 583############################################################################### 584def program_teardown(): 585 586 r""" 587 Clean up after this program. 588 """ 589 590 if cp_setup_called: 591 plug_in_setup() 592 rc, shell_rc, failed_plug_in_name = grpi.rprocess_plug_in_packages( 593 call_point='cleanup', stop_on_plug_in_failure=1) 594 595 # Save boot_results object to a file in case it is needed again. 596 grp.rprint_timen("Saving boot_results to the following path.") 597 grp.rprint_var(boot_results_file_path) 598 pickle.dump(boot_results, open(boot_results_file_path, 'wb'), 599 pickle.HIGHEST_PROTOCOL) 600 601############################################################################### 602 603 604############################################################################### 605def main_py(): 606 607 r""" 608 Do main program processing. 609 """ 610 611 setup() 612 613 # Process caller's boot_stack. 614 while (len(boot_stack) > 0): 615 test_loop_body() 616 617 grp.rprint_timen("Finished processing stack.") 618 619 # Process caller's boot_list. 620 if len(boot_list) > 0: 621 for ix in range(1, max_num_tests + 1): 622 test_loop_body() 623 624 grp.rqprint_timen("Completed all requested boot tests.") 625 626############################################################################### 627