Michael Walsh | ac29d06 | 2017-02-20 16:13:10 -0600 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | r""" |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 4 | This module has functions to support various data structures such as the boot_table, valid_boot_list and |
| 5 | boot_results_table. |
Michael Walsh | ac29d06 | 2017-02-20 16:13:10 -0600 | [diff] [blame] | 6 | """ |
| 7 | |
| 8 | import os |
| 9 | import tempfile |
| 10 | import json |
Michael Walsh | b6e3aac | 2017-09-19 16:57:27 -0500 | [diff] [blame] | 11 | import glob |
Michael Walsh | ac29d06 | 2017-02-20 16:13:10 -0600 | [diff] [blame] | 12 | from tally_sheet import * |
| 13 | |
| 14 | from robot.libraries.BuiltIn import BuiltIn |
| 15 | try: |
| 16 | from robot.utils import DotDict |
| 17 | except ImportError: |
| 18 | import collections |
| 19 | |
| 20 | import gen_print as gp |
Michael Walsh | ac29d06 | 2017-02-20 16:13:10 -0600 | [diff] [blame] | 21 | import gen_valid as gv |
| 22 | import gen_misc as gm |
| 23 | import gen_cmd as gc |
Michael Walsh | b6e3aac | 2017-09-19 16:57:27 -0500 | [diff] [blame] | 24 | import var_funcs as vf |
Michael Walsh | ac29d06 | 2017-02-20 16:13:10 -0600 | [diff] [blame] | 25 | |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 26 | # The code base directory will be one level up from the directory containing this module. |
Michael Walsh | ac29d06 | 2017-02-20 16:13:10 -0600 | [diff] [blame] | 27 | code_base_dir_path = os.path.dirname(os.path.dirname(__file__)) + os.sep |
| 28 | |
Michael Shepos | da40c1d | 2020-12-01 22:30:00 -0600 | [diff] [blame] | 29 | redfish_support_trans_state = int(os.environ.get('REDFISH_SUPPORT_TRANS_STATE', 0)) or \ |
| 30 | int(BuiltIn().get_variable_value("${REDFISH_SUPPORT_TRANS_STATE}", default=0)) |
| 31 | |
George Keishing | 1e2fbee | 2021-03-19 11:19:29 -0500 | [diff] [blame] | 32 | platform_arch_type = BuiltIn().get_variable_value("${PLATFORM_ARCH_TYPE}", default="power") |
| 33 | |
Michael Walsh | ac29d06 | 2017-02-20 16:13:10 -0600 | [diff] [blame] | 34 | |
Michael Walsh | 6c4520c | 2019-07-16 16:40:00 -0500 | [diff] [blame] | 35 | def create_boot_table(file_path=None, |
| 36 | os_host=""): |
Michael Walsh | ac29d06 | 2017-02-20 16:13:10 -0600 | [diff] [blame] | 37 | r""" |
| 38 | Read the boot table JSON file, convert it to an object and return it. |
| 39 | |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 40 | Note that if the user is running without a global OS_HOST robot variable specified, this function will |
| 41 | remove all of the "os_" start and end state requirements from the JSON data. |
Michael Walsh | ac29d06 | 2017-02-20 16:13:10 -0600 | [diff] [blame] | 42 | |
Michael Walsh | 6c4520c | 2019-07-16 16:40:00 -0500 | [diff] [blame] | 43 | Description of argument(s): |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 44 | file_path The path to the boot_table file. If this value is not specified, it will |
| 45 | be obtained from the "BOOT_TABLE_PATH" environment variable, if set. |
| 46 | Otherwise, it will default to "data/boot_table.json". If this value is a |
| 47 | relative path, this function will use the code_base_dir_path as the base |
| 48 | directory (see definition above). |
| 49 | os_host The host name or IP address of the host associated with the machine being |
| 50 | tested. If the user is running without an OS_HOST (i.e. if this argument |
| 51 | is blank), we remove os starting and ending state requirements from the |
| 52 | boot entries. |
Michael Walsh | ac29d06 | 2017-02-20 16:13:10 -0600 | [diff] [blame] | 53 | """ |
| 54 | if file_path is None: |
Michael Shepos | da40c1d | 2020-12-01 22:30:00 -0600 | [diff] [blame] | 55 | if redfish_support_trans_state: |
| 56 | file_path = os.environ.get('BOOT_TABLE_PATH', 'data/boot_table_redfish.json') |
George Keishing | 1e2fbee | 2021-03-19 11:19:29 -0500 | [diff] [blame] | 57 | elif platform_arch_type == "x86": |
| 58 | file_path = os.environ.get('BOOT_TABLE_PATH', 'data/boot_table_x86.json') |
Michael Shepos | da40c1d | 2020-12-01 22:30:00 -0600 | [diff] [blame] | 59 | else: |
| 60 | file_path = os.environ.get('BOOT_TABLE_PATH', 'data/boot_table.json') |
Michael Walsh | ac29d06 | 2017-02-20 16:13:10 -0600 | [diff] [blame] | 61 | |
| 62 | if not file_path.startswith("/"): |
| 63 | file_path = code_base_dir_path + file_path |
| 64 | |
| 65 | # Pre-process the file by removing blank lines and comment lines. |
| 66 | temp = tempfile.NamedTemporaryFile() |
| 67 | temp_file_path = temp.name |
| 68 | |
| 69 | cmd_buf = "egrep -v '^[ ]*$|^[ ]*#' " + file_path + " > " + temp_file_path |
| 70 | gc.cmd_fnc_u(cmd_buf, quiet=1) |
| 71 | |
| 72 | boot_file = open(temp_file_path) |
| 73 | boot_table = json.load(boot_file, object_hook=DotDict) |
| 74 | |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 75 | # If the user is running without an OS_HOST, we remove os starting and ending state requirements from |
| 76 | # the boot entries. |
Michael Walsh | ac29d06 | 2017-02-20 16:13:10 -0600 | [diff] [blame] | 77 | if os_host == "": |
| 78 | for boot in boot_table: |
| 79 | state_keys = ['start', 'end'] |
| 80 | for state_key in state_keys: |
Michael Walsh | 37f833d | 2019-03-04 17:09:12 -0600 | [diff] [blame] | 81 | for sub_state in list(boot_table[boot][state_key]): |
Michael Walsh | ac29d06 | 2017-02-20 16:13:10 -0600 | [diff] [blame] | 82 | if sub_state.startswith("os_"): |
| 83 | boot_table[boot][state_key].pop(sub_state, None) |
| 84 | |
Michael Walsh | 07a01ef | 2017-02-27 14:20:22 -0600 | [diff] [blame] | 85 | # For every boot_type we should have a corresponding mfg mode boot type. |
| 86 | enhanced_boot_table = DotDict() |
George Keishing | 36efbc0 | 2018-12-12 10:18:23 -0600 | [diff] [blame] | 87 | for key, value in boot_table.items(): |
Michael Walsh | 07a01ef | 2017-02-27 14:20:22 -0600 | [diff] [blame] | 88 | enhanced_boot_table[key] = value |
| 89 | enhanced_boot_table[key + " (mfg)"] = value |
| 90 | |
| 91 | return enhanced_boot_table |
Michael Walsh | ac29d06 | 2017-02-20 16:13:10 -0600 | [diff] [blame] | 92 | |
Michael Walsh | ac29d06 | 2017-02-20 16:13:10 -0600 | [diff] [blame] | 93 | |
Michael Walsh | ac29d06 | 2017-02-20 16:13:10 -0600 | [diff] [blame] | 94 | def create_valid_boot_list(boot_table): |
Michael Walsh | ac29d06 | 2017-02-20 16:13:10 -0600 | [diff] [blame] | 95 | r""" |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 96 | Return a list of all of the valid boot types (e.g. ['REST Power On', 'REST Power Off', ...]). |
Michael Walsh | ac29d06 | 2017-02-20 16:13:10 -0600 | [diff] [blame] | 97 | |
Michael Walsh | 6c4520c | 2019-07-16 16:40:00 -0500 | [diff] [blame] | 98 | Description of argument(s): |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 99 | boot_table A boot table such as is returned by the create_boot_table function. |
Michael Walsh | ac29d06 | 2017-02-20 16:13:10 -0600 | [diff] [blame] | 100 | """ |
| 101 | |
| 102 | return list(boot_table.keys()) |
| 103 | |
Michael Walsh | ac29d06 | 2017-02-20 16:13:10 -0600 | [diff] [blame] | 104 | |
Michael Walsh | ac29d06 | 2017-02-20 16:13:10 -0600 | [diff] [blame] | 105 | def read_boot_lists(dir_path="data/boot_lists/"): |
Michael Walsh | ac29d06 | 2017-02-20 16:13:10 -0600 | [diff] [blame] | 106 | r""" |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 107 | Read the contents of all the boot lists files found in the given boot lists directory and return |
| 108 | dictionary of the lists. |
Michael Walsh | ac29d06 | 2017-02-20 16:13:10 -0600 | [diff] [blame] | 109 | |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 110 | Boot lists are simply files containing a boot test name on each line. These files are useful for |
| 111 | categorizing and organizing boot tests. For example, there may be a "Power_on" list, a "Power_off" list, |
| 112 | etc. |
Michael Walsh | ac29d06 | 2017-02-20 16:13:10 -0600 | [diff] [blame] | 113 | |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 114 | The names of the boot list files will be the keys to the top level dictionary. Each dictionary entry is |
| 115 | a list of all the boot tests found in the corresponding file. |
Michael Walsh | ac29d06 | 2017-02-20 16:13:10 -0600 | [diff] [blame] | 116 | |
| 117 | Here is an abbreviated look at the resulting boot_lists dictionary. |
| 118 | |
| 119 | boot_lists: |
| 120 | boot_lists[All]: |
Michael Walsh | 6c4520c | 2019-07-16 16:40:00 -0500 | [diff] [blame] | 121 | boot_lists[All][0]: REST Power On |
| 122 | boot_lists[All][1]: REST Power Off |
Michael Walsh | ac29d06 | 2017-02-20 16:13:10 -0600 | [diff] [blame] | 123 | ... |
| 124 | boot_lists[Code_update]: |
| 125 | boot_lists[Code_update][0]: BMC oob hpm |
| 126 | boot_lists[Code_update][1]: BMC ib hpm |
| 127 | ... |
| 128 | |
Michael Walsh | 6c4520c | 2019-07-16 16:40:00 -0500 | [diff] [blame] | 129 | Description of argument(s): |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 130 | dir_path The path to the directory containing the boot list files. If this value |
| 131 | is a relative path, this function will use the code_base_dir_path as the |
| 132 | base directory (see definition above). |
Michael Walsh | ac29d06 | 2017-02-20 16:13:10 -0600 | [diff] [blame] | 133 | """ |
| 134 | |
| 135 | if not dir_path.startswith("/"): |
| 136 | # Dir path is relative. |
| 137 | dir_path = code_base_dir_path + dir_path |
| 138 | |
| 139 | # Get a list of all file names in the directory. |
| 140 | boot_file_names = os.listdir(dir_path) |
| 141 | |
| 142 | boot_lists = DotDict() |
| 143 | for boot_category in boot_file_names: |
| 144 | file_path = gm.which(dir_path + boot_category) |
| 145 | boot_list = gm.file_to_list(file_path, newlines=0, comments=0, trim=1) |
| 146 | boot_lists[boot_category] = boot_list |
| 147 | |
| 148 | return boot_lists |
| 149 | |
Michael Walsh | ac29d06 | 2017-02-20 16:13:10 -0600 | [diff] [blame] | 150 | |
Michael Walsh | ac29d06 | 2017-02-20 16:13:10 -0600 | [diff] [blame] | 151 | def valid_boot_list(boot_list, |
| 152 | valid_boot_types): |
Michael Walsh | ac29d06 | 2017-02-20 16:13:10 -0600 | [diff] [blame] | 153 | r""" |
| 154 | Verify that each entry in boot_list is a supported boot test. |
| 155 | |
Michael Walsh | 6c4520c | 2019-07-16 16:40:00 -0500 | [diff] [blame] | 156 | Description of argument(s): |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 157 | boot_list An array (i.e. list) of boot test types (e.g. "REST Power On"). |
| 158 | valid_boot_types A list of valid boot types such as that returned by |
| 159 | create_valid_boot_list. |
Michael Walsh | ac29d06 | 2017-02-20 16:13:10 -0600 | [diff] [blame] | 160 | """ |
| 161 | |
| 162 | for boot_name in boot_list: |
| 163 | boot_name = boot_name.strip(" ") |
Michael Walsh | ec01a6f | 2019-08-01 12:43:20 -0500 | [diff] [blame] | 164 | error_message = gv.valid_value(boot_name, |
| 165 | valid_values=valid_boot_types, |
| 166 | var_name="boot_name") |
Michael Walsh | ac29d06 | 2017-02-20 16:13:10 -0600 | [diff] [blame] | 167 | if error_message != "": |
| 168 | BuiltIn().fail(gp.sprint_error(error_message)) |
| 169 | |
Michael Walsh | ac29d06 | 2017-02-20 16:13:10 -0600 | [diff] [blame] | 170 | |
Michael Walsh | ac29d06 | 2017-02-20 16:13:10 -0600 | [diff] [blame] | 171 | class boot_results: |
| 172 | |
| 173 | r""" |
| 174 | This class defines a boot_results table. |
| 175 | """ |
| 176 | |
| 177 | def __init__(self, |
| 178 | boot_table, |
| 179 | boot_pass=0, |
| 180 | boot_fail=0, |
| 181 | obj_name='boot_results'): |
Michael Walsh | ac29d06 | 2017-02-20 16:13:10 -0600 | [diff] [blame] | 182 | r""" |
| 183 | Initialize the boot results object. |
| 184 | |
Michael Walsh | 6c4520c | 2019-07-16 16:40:00 -0500 | [diff] [blame] | 185 | Description of argument(s): |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 186 | boot_table Boot table object (see definition above). The boot table contains all of |
| 187 | the valid boot test types. It can be created with the create_boot_table |
| 188 | function. |
| 189 | boot_pass An initial boot_pass value. This program may be called as part of a |
| 190 | larger test suite. As such there may already have been some successful |
| 191 | boot tests that we need to keep track of. |
| 192 | boot_fail An initial boot_fail value. This program may be called as part of a |
| 193 | larger test suite. As such there may already have been some unsuccessful |
| 194 | boot tests that we need to keep track of. |
Michael Walsh | 6c4520c | 2019-07-16 16:40:00 -0500 | [diff] [blame] | 195 | obj_name The name of this object. |
Michael Walsh | ac29d06 | 2017-02-20 16:13:10 -0600 | [diff] [blame] | 196 | """ |
| 197 | |
| 198 | # Store the method parms as class data. |
| 199 | self.__obj_name = obj_name |
| 200 | self.__initial_boot_pass = boot_pass |
| 201 | self.__initial_boot_fail = boot_fail |
| 202 | |
| 203 | # Create boot_results_fields for use in creating boot_results table. |
| 204 | boot_results_fields = DotDict([('total', 0), ('pass', 0), ('fail', 0)]) |
| 205 | # Create boot_results table. |
| 206 | self.__boot_results = tally_sheet('boot type', |
| 207 | boot_results_fields, |
| 208 | 'boot_test_results') |
| 209 | self.__boot_results.set_sum_fields(['total', 'pass', 'fail']) |
| 210 | self.__boot_results.set_calc_fields(['total=pass+fail']) |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 211 | # Create one row in the result table for each kind of boot test in the boot_table (i.e. for all |
| 212 | # supported boot tests). |
Michael Walsh | ac29d06 | 2017-02-20 16:13:10 -0600 | [diff] [blame] | 213 | for boot_name in list(boot_table.keys()): |
| 214 | self.__boot_results.add_row(boot_name) |
| 215 | |
Michael Walsh | 6c4520c | 2019-07-16 16:40:00 -0500 | [diff] [blame] | 216 | def add_row(self, *args, **kwargs): |
| 217 | r""" |
| 218 | Add row to tally_sheet class object. |
| 219 | |
| 220 | Description of argument(s): |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 221 | See add_row method in tally_sheet.py for a description of all arguments. |
Michael Walsh | 6c4520c | 2019-07-16 16:40:00 -0500 | [diff] [blame] | 222 | """ |
| 223 | self.__boot_results.add_row(*args, **kwargs) |
| 224 | |
Michael Walsh | ac29d06 | 2017-02-20 16:13:10 -0600 | [diff] [blame] | 225 | def return_total_pass_fail(self): |
Michael Walsh | ac29d06 | 2017-02-20 16:13:10 -0600 | [diff] [blame] | 226 | r""" |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 227 | Return the total boot_pass and boot_fail values. This information is comprised of the pass/fail |
| 228 | values from the table plus the initial pass/fail values. |
Michael Walsh | ac29d06 | 2017-02-20 16:13:10 -0600 | [diff] [blame] | 229 | """ |
| 230 | |
| 231 | totals_line = self.__boot_results.calc() |
| 232 | return totals_line['pass'] + self.__initial_boot_pass,\ |
| 233 | totals_line['fail'] + self.__initial_boot_fail |
| 234 | |
| 235 | def update(self, |
| 236 | boot_type, |
| 237 | boot_status): |
Michael Walsh | ac29d06 | 2017-02-20 16:13:10 -0600 | [diff] [blame] | 238 | r""" |
| 239 | Update our boot_results_table. This includes: |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 240 | - Updating the record for the given boot_type by incrementing the pass or fail field. |
Michael Walsh | ac29d06 | 2017-02-20 16:13:10 -0600 | [diff] [blame] | 241 | - Calling the calc method to have the totals calculated. |
| 242 | |
Michael Walsh | 6c4520c | 2019-07-16 16:40:00 -0500 | [diff] [blame] | 243 | Description of argument(s): |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 244 | boot_type The type of boot test just done (e.g. "REST Power On"). |
| 245 | boot_status The status of the boot just done. This should be equal to either "pass" |
| 246 | or "fail" (case-insensitive). |
Michael Walsh | ac29d06 | 2017-02-20 16:13:10 -0600 | [diff] [blame] | 247 | """ |
| 248 | |
| 249 | self.__boot_results.inc_row_field(boot_type, boot_status.lower()) |
Michael Walsh | 8f1ef9e | 2017-03-02 14:31:24 -0600 | [diff] [blame] | 250 | self.__boot_results.calc() |
Michael Walsh | ac29d06 | 2017-02-20 16:13:10 -0600 | [diff] [blame] | 251 | |
| 252 | def sprint_report(self, |
| 253 | header_footer="\n"): |
Michael Walsh | ac29d06 | 2017-02-20 16:13:10 -0600 | [diff] [blame] | 254 | r""" |
| 255 | String-print the formatted boot_resuls_table and return them. |
| 256 | |
Michael Walsh | 6c4520c | 2019-07-16 16:40:00 -0500 | [diff] [blame] | 257 | Description of argument(s): |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 258 | header_footer This indicates whether a header and footer are to be included in the |
| 259 | report. |
Michael Walsh | ac29d06 | 2017-02-20 16:13:10 -0600 | [diff] [blame] | 260 | """ |
| 261 | |
| 262 | buffer = "" |
| 263 | |
| 264 | buffer += gp.sprint(header_footer) |
| 265 | buffer += self.__boot_results.sprint_report() |
| 266 | buffer += gp.sprint(header_footer) |
| 267 | |
| 268 | return buffer |
| 269 | |
| 270 | def print_report(self, |
Michael Walsh | 6c4520c | 2019-07-16 16:40:00 -0500 | [diff] [blame] | 271 | header_footer="\n", |
| 272 | quiet=None): |
Michael Walsh | ac29d06 | 2017-02-20 16:13:10 -0600 | [diff] [blame] | 273 | r""" |
| 274 | Print the formatted boot_resuls_table to the console. |
| 275 | |
Michael Walsh | 6c4520c | 2019-07-16 16:40:00 -0500 | [diff] [blame] | 276 | Description of argument(s): |
Michael Walsh | ac29d06 | 2017-02-20 16:13:10 -0600 | [diff] [blame] | 277 | See sprint_report for details. |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 278 | quiet Only print if this value is 0. This function will search upward in the |
| 279 | stack to get the default value. |
Michael Walsh | ac29d06 | 2017-02-20 16:13:10 -0600 | [diff] [blame] | 280 | """ |
| 281 | |
Michael Walsh | 6c4520c | 2019-07-16 16:40:00 -0500 | [diff] [blame] | 282 | quiet = int(gm.dft(quiet, gp.get_stack_var('quiet', 0))) |
| 283 | |
Michael Walsh | c108e42 | 2019-03-28 12:27:18 -0500 | [diff] [blame] | 284 | gp.qprint(self.sprint_report(header_footer)) |
Michael Walsh | ac29d06 | 2017-02-20 16:13:10 -0600 | [diff] [blame] | 285 | |
| 286 | def sprint_obj(self): |
Michael Walsh | ac29d06 | 2017-02-20 16:13:10 -0600 | [diff] [blame] | 287 | r""" |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 288 | sprint the fields of this object. This would normally be for debug purposes only. |
Michael Walsh | ac29d06 | 2017-02-20 16:13:10 -0600 | [diff] [blame] | 289 | """ |
| 290 | |
| 291 | buffer = "" |
| 292 | |
| 293 | buffer += "class name: " + self.__class__.__name__ + "\n" |
| 294 | buffer += gp.sprint_var(self.__obj_name) |
| 295 | buffer += self.__boot_results.sprint_obj() |
| 296 | buffer += gp.sprint_var(self.__initial_boot_pass) |
| 297 | buffer += gp.sprint_var(self.__initial_boot_fail) |
| 298 | |
| 299 | return buffer |
| 300 | |
| 301 | def print_obj(self): |
Michael Walsh | ac29d06 | 2017-02-20 16:13:10 -0600 | [diff] [blame] | 302 | r""" |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 303 | Print the fields of this object to stdout. This would normally be for debug purposes. |
Michael Walsh | ac29d06 | 2017-02-20 16:13:10 -0600 | [diff] [blame] | 304 | """ |
| 305 | |
Michael Walsh | c108e42 | 2019-03-28 12:27:18 -0500 | [diff] [blame] | 306 | gp.gp_print(self.sprint_obj()) |
Michael Walsh | ac29d06 | 2017-02-20 16:13:10 -0600 | [diff] [blame] | 307 | |
Michael Walsh | b6e3aac | 2017-09-19 16:57:27 -0500 | [diff] [blame] | 308 | |
Michael Walsh | b6e3aac | 2017-09-19 16:57:27 -0500 | [diff] [blame] | 309 | def create_boot_results_file_path(pgm_name, |
| 310 | openbmc_nickname, |
| 311 | master_pid): |
Michael Walsh | b6e3aac | 2017-09-19 16:57:27 -0500 | [diff] [blame] | 312 | r""" |
| 313 | Create a file path to be used to store a boot_results object. |
| 314 | |
| 315 | Description of argument(s): |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 316 | pgm_name The name of the program. This will form part of the resulting file name. |
| 317 | openbmc_nickname The name of the system. This could be a nickname, a hostname, an IP, |
| 318 | etc. This will form part of the resulting file name. |
| 319 | master_pid The master process id which will form part of the file name. |
Michael Walsh | 6c4520c | 2019-07-16 16:40:00 -0500 | [diff] [blame] | 320 | """ |
Michael Walsh | b6e3aac | 2017-09-19 16:57:27 -0500 | [diff] [blame] | 321 | |
Michael Walsh | 8d7b738 | 2017-09-27 16:00:25 -0500 | [diff] [blame] | 322 | USER = os.environ.get("USER", "") |
| 323 | dir_path = "/tmp/" + USER + "/" |
| 324 | if not os.path.exists(dir_path): |
| 325 | os.makedirs(dir_path) |
| 326 | |
Michael Walsh | b6e3aac | 2017-09-19 16:57:27 -0500 | [diff] [blame] | 327 | file_name_dict = vf.create_var_dict(pgm_name, openbmc_nickname, master_pid) |
Michael Walsh | 8d7b738 | 2017-09-27 16:00:25 -0500 | [diff] [blame] | 328 | return vf.create_file_path(file_name_dict, dir_path=dir_path, |
| 329 | file_suffix=":boot_results") |
Michael Walsh | b6e3aac | 2017-09-19 16:57:27 -0500 | [diff] [blame] | 330 | |
Michael Walsh | b6e3aac | 2017-09-19 16:57:27 -0500 | [diff] [blame] | 331 | |
Michael Walsh | b6e3aac | 2017-09-19 16:57:27 -0500 | [diff] [blame] | 332 | def cleanup_boot_results_file(): |
Michael Walsh | b6e3aac | 2017-09-19 16:57:27 -0500 | [diff] [blame] | 333 | r""" |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 334 | Delete all boot results files whose corresponding pids are no longer active. |
Michael Walsh | b6e3aac | 2017-09-19 16:57:27 -0500 | [diff] [blame] | 335 | """ |
| 336 | |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 337 | # Use create_boot_results_file_path to create a globex to find all of the existing boot results files. |
Michael Walsh | b6e3aac | 2017-09-19 16:57:27 -0500 | [diff] [blame] | 338 | globex = create_boot_results_file_path("*", "*", "*") |
| 339 | file_list = sorted(glob.glob(globex)) |
| 340 | for file_path in file_list: |
| 341 | # Use parse_file_path to extract info from the file path. |
| 342 | file_dict = vf.parse_file_path(file_path) |
| 343 | if gm.pid_active(file_dict['master_pid']): |
| 344 | gp.qprint_timen("Preserving " + file_path + ".") |
| 345 | else: |
| 346 | gc.cmd_fnc("rm -f " + file_path) |
Michael Walsh | 6c4520c | 2019-07-16 16:40:00 -0500 | [diff] [blame] | 347 | |
| 348 | |
| 349 | def update_boot_history(boot_history, boot_start_message, max_boot_history=10): |
| 350 | r""" |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 351 | Update the boot_history list by appending the boot_start_message and by removing all but the last n |
| 352 | entries. |
Michael Walsh | 6c4520c | 2019-07-16 16:40:00 -0500 | [diff] [blame] | 353 | |
| 354 | Description of argument(s): |
| 355 | boot_history A list of boot start messages. |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 356 | boot_start_message This is typically a time-stamped line of text announcing the start of a |
| 357 | boot test. |
| 358 | max_boot_history The max number of entries to be kept in the boot_history list. The |
| 359 | oldest entries are deleted to achieve this list size. |
Michael Walsh | 6c4520c | 2019-07-16 16:40:00 -0500 | [diff] [blame] | 360 | """ |
| 361 | |
| 362 | boot_history.append(boot_start_message) |
| 363 | |
| 364 | # Trim list to max number of entries. |
| 365 | del boot_history[:max(0, len(boot_history) - max_boot_history)] |
| 366 | |
| 367 | |
| 368 | def print_boot_history(boot_history, quiet=None): |
| 369 | r""" |
| 370 | Print the last ten boots done with their time stamps. |
| 371 | |
| 372 | Description of argument(s): |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 373 | quiet Only print if this value is 0. This function will search upward in the |
| 374 | stack to get the default value. |
Michael Walsh | 6c4520c | 2019-07-16 16:40:00 -0500 | [diff] [blame] | 375 | """ |
| 376 | |
| 377 | quiet = int(gm.dft(quiet, gp.get_stack_var('quiet', 0))) |
| 378 | |
| 379 | # indent 0, 90 chars wide, linefeed, char is "=" |
| 380 | gp.qprint_dashes(0, 90) |
| 381 | gp.qprintn("Last 10 boots:\n") |
| 382 | |
| 383 | for boot_entry in boot_history: |
| 384 | gp.qprint(boot_entry) |
| 385 | gp.qprint_dashes(0, 90) |