1#!/usr/bin/env python 2 3r""" 4This module is the python counterpart to obmc_boot_test. 5""" 6 7from tally_sheet import * 8import gen_robot_print as grp 9 10import os 11import time 12import subprocess 13 14from robot.utils import DotDict 15from robot.libraries.BuiltIn import BuiltIn 16from robot.libraries.OperatingSystem import OperatingSystem 17 18# Create boot_results_fields for use in creating boot_results. 19boot_results_fields = DotDict([('total', 0), ('pass', 0), ('fail', 0)]) 20# Create boot_results which is global to this module. 21boot_results = tally_sheet('boot type', 22 boot_results_fields, 23 'boot_test_results') 24 25boot_results.set_sum_fields(['total', 'pass', 'fail']) 26boot_results.set_calc_fields(['total=pass+fail']) 27 28 29############################################################################### 30def add_trailing_slash(path): 31 32 r""" 33 Add a trailing slash to path if it doesn't already have one and return it. 34 35 """ 36 37 return os.path.normpath(path) + os.path.sep 38 39############################################################################### 40 41 42############################################################################### 43def plug_in_setup(): 44 45 r""" 46 Initialize all plug-in environment variables for use by the plug-in 47 programs. 48 """ 49 50 boot_pass = int(BuiltIn().get_variable_value("${boot_pass}")) 51 if boot_pass > 1: 52 test_really_running = 1 53 else: 54 test_really_running = 0 55 56 BuiltIn().set_global_variable("${test_really_running}", 57 test_really_running) 58 59 next_boot = BuiltIn().get_variable_value("${next_boot}") 60 BuiltIn().set_global_variable("${boot_type_desc}", next_boot) 61 62 # Setting master_pid correctly influences the behavior of plug-ins like 63 # DB_Logging 64 program_pid = BuiltIn().get_variable_value("${program_pid}") 65 try: 66 master_pid = OperatingSystem().get_environment_variable( 67 "AUTOBOOT_MASTER_PID") 68 except RuntimeError: 69 master_pid = program_pid 70 if master_pid == "": 71 master_pid = program_pid 72 73 BuiltIn().set_global_variable("${master_pid}", master_pid) 74 75 seconds = time.time() 76 loc_time = time.localtime(seconds) 77 time_string = time.strftime("%y%m%d.%H%M%S.", loc_time) 78 79 openbmc_nickname = BuiltIn().get_variable_value("${openbmc_nickname}") 80 if openbmc_nickname == "": 81 openbmc_host = BuiltIn().get_variable_value("${openbmc_host}") 82 ffdc_prefix = openbmc_host 83 else: 84 ffdc_prefix = openbmc_nickname 85 86 ffdc_prefix += "." + time_string 87 88 ffdc_dir_path = OperatingSystem().get_environment_variable( 89 "FFDC_DIR_PATH") 90 # Add trailing slash. 91 ffdc_dir_path = os.path.normpath(ffdc_dir_path) + os.sep 92 BuiltIn().set_global_variable("${FFDC_DIR_PATH}", ffdc_dir_path) 93 94 # For each program parameter, set the corresponding AUTOBOOT_ environment 95 # variable value. Also, set an AUTOBOOT_ environment variable for every 96 # element in additional_values. 97 additional_values = ["boot_type_desc", "boot_success", "boot_pass", 98 "boot_fail", "test_really_running", "program_pid", 99 "master_pid", "ffdc_prefix", "ffdc_dir_path"] 100 BuiltIn().set_global_variable("${ffdc_prefix}", ffdc_prefix) 101 102 parm_list = BuiltIn().get_variable_value("@{parm_list}") 103 104 plug_in_vars = parm_list + additional_values 105 106 for var_name in plug_in_vars: 107 var_value = BuiltIn().get_variable_value("${" + var_name + "}") 108 var_name = var_name.upper() 109 if var_value is None: 110 var_value = "" 111 OperatingSystem().set_environment_variable( 112 "AUTOBOOT_" + var_name, var_value) 113 114 debug = int(BuiltIn().get_variable_value("${debug}")) 115 if debug: 116 cmd_buf = "printenv | egrep AUTOBOOT_ | sort -u" 117 grp.rpissuing(cmd_buf) 118 sub_proc = subprocess.Popen(cmd_buf, shell=True, 119 stdout=subprocess.PIPE, 120 stderr=subprocess.STDOUT) 121 out_buf, err_buf = sub_proc.communicate() 122 shell_rc = sub_proc.returncode 123 grp.rprint(out_buf) 124 125############################################################################### 126 127 128############################################################################### 129def create_boot_results_table(): 130 131 r""" 132 Create our boot_results_table. 133 """ 134 135 # At some point we'll want to change to reading in our boot types from 136 # some external source (e.g. file). 137 138 boot_results.add_row('BMC Power On') 139 boot_results.add_row('BMC Power Off') 140 141############################################################################### 142 143 144############################################################################### 145def update_boot_results_table(boot_type, 146 boot_status): 147 148 r""" 149 Update our boot_results_table. This includes: 150 - Updating the record for the given boot_type by incrementing the pass or 151 fail field. 152 - Calling the calc method to have the totals, etc. calculated. 153 - Updating global variables boot_pass/boot_fail. 154 155 Description of arguments: 156 boot_type The type of boot just done (e.g. "BMC Power On"). 157 boot_status The status of the boot just done. This should be equal to 158 either "pass" or "fail" (case-insensitive). 159 """ 160 161 boot_results.inc_row_field(boot_type, boot_status.lower()) 162 totals_line = boot_results.calc() 163 164 # The caller of obmc_boot_test can pass boot_pass/boot_fail values because 165 # the caller may have already done some testing (e.g. "BMC OOB"). For the 166 # sake of DB logging done by plug-ins, we want to include these in our 167 # overall totals. 168 initial_boot_pass = int(BuiltIn().get_variable_value( 169 "${initial_boot_pass}")) 170 initial_boot_fail = int(BuiltIn().get_variable_value( 171 "${initial_boot_fail}")) 172 173 BuiltIn().set_global_variable("${boot_pass}", 174 totals_line['pass'] + initial_boot_pass) 175 BuiltIn().set_global_variable("${boot_fail}", 176 totals_line['fail'] + initial_boot_fail) 177 178############################################################################### 179 180 181############################################################################### 182def print_boot_results_table(header_footer="\n"): 183 184 r""" 185 Print the formatted boot_resuls_table to the console. 186 """ 187 188 grp.rprint(header_footer) 189 grp.rprint(boot_results.sprint_report()) 190 grp.rprint(header_footer) 191 192############################################################################### 193