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