blob: daee3fbdb603b3cf7db837a667fc52f188cebc2f [file] [log] [blame]
Michael Walsh0bbd8602016-11-22 11:31:49 -06001#!/usr/bin/env python
2
3r"""
4This module is the python counterpart to obmc_boot_test.
5"""
6
Michael Walsh0b93fbf2017-03-02 14:42:41 -06007import os
8import imp
9import time
10import glob
11import random
Michael Walsh0ad0f7f2017-05-04 14:39:58 -050012import re
Michael Walsh0b93fbf2017-03-02 14:42:41 -060013import cPickle as pickle
Michael Walshdc80d672017-05-09 12:58:32 -050014import socket
Michael Walsh0b93fbf2017-03-02 14:42:41 -060015
16from robot.utils import DotDict
17from robot.libraries.BuiltIn import BuiltIn
18
Michael Walsh6741f742017-02-20 16:16:38 -060019from boot_data import *
Michael Walshc9116812017-03-10 14:23:06 -060020import gen_print as gp
Michael Walsh0bbd8602016-11-22 11:31:49 -060021import gen_robot_print as grp
Michael Walsh55302292017-01-10 11:43:02 -060022import gen_robot_plug_in as grpi
Michael Walsh6741f742017-02-20 16:16:38 -060023import gen_robot_valid as grv
24import gen_misc as gm
25import gen_cmd as gc
Michael Walshb5839d02017-04-12 16:11:20 -050026import gen_robot_keyword as grk
Michael Walsh55302292017-01-10 11:43:02 -060027import state as st
Michael Walsh0bbd8602016-11-22 11:31:49 -060028
Michael Walsh0b93fbf2017-03-02 14:42:41 -060029base_path = os.path.dirname(os.path.dirname(
30 imp.find_module("gen_robot_print")[1])) +\
Michael Walshc9116812017-03-10 14:23:06 -060031 os.sep
Michael Walsh0b93fbf2017-03-02 14:42:41 -060032sys.path.append(base_path + "extended/")
33import run_keyword as rk
Michael Walsh0bbd8602016-11-22 11:31:49 -060034
Michael Walshe1e26442017-03-06 17:50:07 -060035# Setting master_pid correctly influences the behavior of plug-ins like
36# DB_Logging
37program_pid = os.getpid()
38master_pid = os.environ.get('AUTOBOOT_MASTER_PID', program_pid)
39
Michael Walshb5839d02017-04-12 16:11:20 -050040# Set up boot data structures.
41boot_table = create_boot_table()
42valid_boot_types = create_valid_boot_list(boot_table)
Michael Walsh0b93fbf2017-03-02 14:42:41 -060043
Michael Walsh6741f742017-02-20 16:16:38 -060044boot_lists = read_boot_lists()
45last_ten = []
Michael Walsh6741f742017-02-20 16:16:38 -060046
47state = st.return_default_state()
48cp_setup_called = 0
49next_boot = ""
50base_tool_dir_path = os.path.normpath(os.environ.get(
51 'AUTOBOOT_BASE_TOOL_DIR_PATH', "/tmp")) + os.sep
Michael Walshb5839d02017-04-12 16:11:20 -050052
Michael Walsh6741f742017-02-20 16:16:38 -060053ffdc_dir_path = os.path.normpath(os.environ.get('FFDC_DIR_PATH', '')) + os.sep
Michael Walsh6741f742017-02-20 16:16:38 -060054boot_success = 0
Michael Walsh6741f742017-02-20 16:16:38 -060055status_dir_path = os.environ.get('STATUS_DIR_PATH', "")
56if status_dir_path != "":
57 status_dir_path = os.path.normpath(status_dir_path) + os.sep
Michael Walsh0b93fbf2017-03-02 14:42:41 -060058default_power_on = "REST Power On"
59default_power_off = "REST Power Off"
Michael Walsh6741f742017-02-20 16:16:38 -060060boot_count = 0
Michael Walsh0bbd8602016-11-22 11:31:49 -060061
Michael Walsh85678942017-03-27 14:34:22 -050062LOG_LEVEL = BuiltIn().get_variable_value("${LOG_LEVEL}")
63
64
65###############################################################################
Michael Walsh0ad0f7f2017-05-04 14:39:58 -050066def process_host(host,
67 host_var_name=""):
68
69 r"""
70 Process a host by getting the associated host name and IP address and
71 setting them in global variables.
72
73 If the caller does not pass the host_var_name, this function will try to
74 figure out the name of the variable used by the caller for the host parm.
75 Callers are advised to explicitly specify the host_var_name when calling
76 with an exec command. In such cases, the get_arg_name cannot figure out
77 the host variable name.
78
79 This function will then create similar global variable names by
80 removing "_host" and appending "_host_name" or "_ip" to the host variable
81 name.
82
83 Example:
84
85 If a call is made like this:
86 process_host(openbmc_host)
87
88 Global variables openbmc_host_name and openbmc_ip will be set.
89
90 Description of argument(s):
91 host A host name or IP. The name of the variable used should
92 have a suffix of "_host".
93 host_var_name The name of the variable being used as the host parm.
94 """
95
96 if host_var_name == "":
97 host_var_name = gp.get_arg_name(0, 1, stack_frame_ix=2)
98
99 host_name_var_name = re.sub("host", "host_name", host_var_name)
100 ip_var_name = re.sub("host", "ip", host_var_name)
101 cmd_buf = "global " + host_name_var_name + ", " + ip_var_name + " ; " +\
102 host_name_var_name + ", " + ip_var_name + " = gm.get_host_name_ip('" +\
103 host + "')"
104 exec(cmd_buf)
105
106###############################################################################
107
108
109###############################################################################
Michael Walshb5839d02017-04-12 16:11:20 -0500110def process_pgm_parms():
111
112 r"""
113 Process the program parameters by assigning them all to corresponding
114 globals. Also, set some global values that depend on program parameters.
115 """
116
117 # Program parameter processing.
118 # Assign all program parms to python variables which are global to this
119 # module.
120
121 global parm_list
122 parm_list = BuiltIn().get_variable_value("${parm_list}")
123 # The following subset of parms should be processed as integers.
124 int_list = ['max_num_tests', 'boot_pass', 'boot_fail', 'ffdc_only',
125 'boot_fail_threshold', 'quiet', 'test_mode', 'debug']
126 for parm in parm_list:
127 if parm in int_list:
128 sub_cmd = "int(BuiltIn().get_variable_value(\"${" + parm +\
129 "}\", \"0\"))"
130 else:
131 sub_cmd = "BuiltIn().get_variable_value(\"${" + parm + "}\")"
132 cmd_buf = "global " + parm + " ; " + parm + " = " + sub_cmd
133 exec(cmd_buf)
Michael Walsh0ad0f7f2017-05-04 14:39:58 -0500134 if re.match(r".*_host$", parm):
135 cmd_buf = "process_host(" + parm + ", '" + parm + "')"
136 exec(cmd_buf)
137 if re.match(r".*_password$", parm):
138 # Register the value of any parm whose name ends in _password.
139 # This will cause the print functions to replace passwords with
140 # asterisks in the output.
141 cmd_buf = "gp.register_passwords(" + parm + ")"
142 exec(cmd_buf)
Michael Walshb5839d02017-04-12 16:11:20 -0500143
144 global ffdc_dir_path_style
145 global boot_list
146 global boot_stack
147 global boot_results_file_path
148 global boot_results
149 global ffdc_list_file_path
150
151 if ffdc_dir_path_style == "":
152 ffdc_dir_path_style = int(os.environ.get('FFDC_DIR_PATH_STYLE', '0'))
153
154 # Convert these program parms to lists for easier processing..
155 boot_list = filter(None, boot_list.split(":"))
156 boot_stack = filter(None, boot_stack.split(":"))
157
158 boot_results_file_path = "/tmp/" + openbmc_nickname + ":pid_" +\
159 str(master_pid) + ":boot_results"
160
161 if os.path.isfile(boot_results_file_path):
162 # We've been called before in this run so we'll load the saved
163 # boot_results object.
164 boot_results = pickle.load(open(boot_results_file_path, 'rb'))
165 else:
166 boot_results = boot_results(boot_table, boot_pass, boot_fail)
167
168 ffdc_list_file_path = base_tool_dir_path + openbmc_nickname +\
169 "/FFDC_FILE_LIST"
170
171###############################################################################
172
173
174###############################################################################
Michael Walsh85678942017-03-27 14:34:22 -0500175def initial_plug_in_setup():
176
177 r"""
178 Initialize all plug-in environment variables which do not change for the
179 duration of the program.
180
181 """
182
183 global LOG_LEVEL
184 BuiltIn().set_log_level("NONE")
185
186 BuiltIn().set_global_variable("${master_pid}", master_pid)
187 BuiltIn().set_global_variable("${FFDC_DIR_PATH}", ffdc_dir_path)
188 BuiltIn().set_global_variable("${STATUS_DIR_PATH}", status_dir_path)
189 BuiltIn().set_global_variable("${BASE_TOOL_DIR_PATH}", base_tool_dir_path)
190 BuiltIn().set_global_variable("${FFDC_LIST_FILE_PATH}",
191 ffdc_list_file_path)
192
193 BuiltIn().set_global_variable("${FFDC_DIR_PATH_STYLE}",
194 ffdc_dir_path_style)
195 BuiltIn().set_global_variable("${FFDC_CHECK}",
196 ffdc_check)
197
198 # For each program parameter, set the corresponding AUTOBOOT_ environment
199 # variable value. Also, set an AUTOBOOT_ environment variable for every
200 # element in additional_values.
201 additional_values = ["program_pid", "master_pid", "ffdc_dir_path",
202 "status_dir_path", "base_tool_dir_path",
203 "ffdc_list_file_path"]
204
205 plug_in_vars = parm_list + additional_values
206
207 for var_name in plug_in_vars:
208 var_value = BuiltIn().get_variable_value("${" + var_name + "}")
209 var_name = var_name.upper()
210 if var_value is None:
211 var_value = ""
212 os.environ["AUTOBOOT_" + var_name] = str(var_value)
213
214 BuiltIn().set_log_level(LOG_LEVEL)
215
Michael Walsh68a61162017-04-25 11:54:06 -0500216 # Make sure the ffdc list directory exists.
217 ffdc_list_dir_path = os.path.dirname(ffdc_list_file_path) + os.sep
218 if not os.path.exists(ffdc_list_dir_path):
219 os.makedirs(ffdc_list_dir_path)
Michael Walsh85678942017-03-27 14:34:22 -0500220
221###############################################################################
222
Michael Walsh0bbd8602016-11-22 11:31:49 -0600223
224###############################################################################
Michael Walsh0bbd8602016-11-22 11:31:49 -0600225def plug_in_setup():
226
227 r"""
Michael Walsh85678942017-03-27 14:34:22 -0500228 Initialize all changing plug-in environment variables for use by the
229 plug-in programs.
Michael Walsh0bbd8602016-11-22 11:31:49 -0600230 """
231
Michael Walsh85678942017-03-27 14:34:22 -0500232 global LOG_LEVEL
233 global test_really_running
234
235 BuiltIn().set_log_level("NONE")
236
Michael Walsh6741f742017-02-20 16:16:38 -0600237 boot_pass, boot_fail = boot_results.return_total_pass_fail()
Michael Walsh0bbd8602016-11-22 11:31:49 -0600238 if boot_pass > 1:
239 test_really_running = 1
240 else:
241 test_really_running = 0
242
Michael Walsh0bbd8602016-11-22 11:31:49 -0600243 seconds = time.time()
244 loc_time = time.localtime(seconds)
245 time_string = time.strftime("%y%m%d.%H%M%S.", loc_time)
246
Michael Walsh6741f742017-02-20 16:16:38 -0600247 ffdc_prefix = openbmc_nickname + "." + time_string
Michael Walsh0bbd8602016-11-22 11:31:49 -0600248
Michael Walsh6741f742017-02-20 16:16:38 -0600249 BuiltIn().set_global_variable("${test_really_running}",
250 test_really_running)
251 BuiltIn().set_global_variable("${boot_type_desc}", next_boot)
Michael Walsh6741f742017-02-20 16:16:38 -0600252 BuiltIn().set_global_variable("${boot_pass}", boot_pass)
253 BuiltIn().set_global_variable("${boot_fail}", boot_fail)
254 BuiltIn().set_global_variable("${boot_success}", boot_success)
255 BuiltIn().set_global_variable("${ffdc_prefix}", ffdc_prefix)
Michael Walsh4c9a6452016-12-13 16:03:11 -0600256
Michael Walsh0bbd8602016-11-22 11:31:49 -0600257 # For each program parameter, set the corresponding AUTOBOOT_ environment
258 # variable value. Also, set an AUTOBOOT_ environment variable for every
259 # element in additional_values.
260 additional_values = ["boot_type_desc", "boot_success", "boot_pass",
Michael Walsh85678942017-03-27 14:34:22 -0500261 "boot_fail", "test_really_running", "ffdc_prefix"]
Michael Walsh0bbd8602016-11-22 11:31:49 -0600262
Michael Walsh85678942017-03-27 14:34:22 -0500263 plug_in_vars = additional_values
Michael Walsh0bbd8602016-11-22 11:31:49 -0600264
265 for var_name in plug_in_vars:
266 var_value = BuiltIn().get_variable_value("${" + var_name + "}")
267 var_name = var_name.upper()
268 if var_value is None:
269 var_value = ""
Michael Walsh6741f742017-02-20 16:16:38 -0600270 os.environ["AUTOBOOT_" + var_name] = str(var_value)
Michael Walsh0bbd8602016-11-22 11:31:49 -0600271
Michael Walsh0bbd8602016-11-22 11:31:49 -0600272 if debug:
Michael Walsh6741f742017-02-20 16:16:38 -0600273 shell_rc, out_buf = \
274 gc.cmd_fnc_u("printenv | egrep AUTOBOOT_ | sort -u")
Michael Walsh0bbd8602016-11-22 11:31:49 -0600275
Michael Walsh85678942017-03-27 14:34:22 -0500276 BuiltIn().set_log_level(LOG_LEVEL)
277
Michael Walsh0bbd8602016-11-22 11:31:49 -0600278###############################################################################
279
280
281###############################################################################
Michael Walsh6741f742017-02-20 16:16:38 -0600282def setup():
Michael Walsh0bbd8602016-11-22 11:31:49 -0600283
284 r"""
Michael Walsh6741f742017-02-20 16:16:38 -0600285 Do general program setup tasks.
Michael Walsh0bbd8602016-11-22 11:31:49 -0600286 """
287
Michael Walsh6741f742017-02-20 16:16:38 -0600288 global cp_setup_called
Michael Walsh0bbd8602016-11-22 11:31:49 -0600289
Michael Walshb5839d02017-04-12 16:11:20 -0500290 gp.qprintn()
291
Michael Walsh83f4bc72017-04-20 16:49:43 -0500292 robot_pgm_dir_path = os.path.dirname(__file__) + os.sep
293 repo_bin_path = robot_pgm_dir_path.replace("/lib/", "/bin/")
294 # If we can't find ssh_pw, then we don't have our repo bin in PATH.
Michael Walshb5839d02017-04-12 16:11:20 -0500295 shell_rc, out_buf = gc.cmd_fnc_u("which ssh_pw", quiet=1, print_output=0,
296 show_err=0)
297 if shell_rc != 0:
Michael Walsh83f4bc72017-04-20 16:49:43 -0500298 os.environ['PATH'] = repo_bin_path + ":" + os.environ.get('PATH', "")
299 # Likewise, our repo lib subdir needs to be in sys.path and PYTHONPATH.
300 if robot_pgm_dir_path not in sys.path:
301 sys.path.append(robot_pgm_dir_path)
302 PYTHONPATH = os.environ.get("PYTHONPATH", "")
303 if PYTHONPATH == "":
304 os.environ['PYTHONPATH'] = robot_pgm_dir_path
305 else:
306 os.environ['PYTHONPATH'] = robot_pgm_dir_path + ":" + PYTHONPATH
Michael Walsh6741f742017-02-20 16:16:38 -0600307
308 validate_parms()
309
310 grp.rqprint_pgm_header()
311
Michael Walshb5839d02017-04-12 16:11:20 -0500312 grk.run_key("Set BMC Power Policy RESTORE_LAST_STATE")
Michael Walsh11cfc8c2017-03-31 09:40:55 -0500313
Michael Walsh85678942017-03-27 14:34:22 -0500314 initial_plug_in_setup()
315
Michael Walsh6741f742017-02-20 16:16:38 -0600316 plug_in_setup()
317 rc, shell_rc, failed_plug_in_name = grpi.rprocess_plug_in_packages(
318 call_point='setup')
319 if rc != 0:
320 error_message = "Plug-in setup failed.\n"
321 grp.rprint_error_report(error_message)
322 BuiltIn().fail(error_message)
323 # Setting cp_setup_called lets our Teardown know that it needs to call
324 # the cleanup plug-in call point.
325 cp_setup_called = 1
326
327 # Keyword "FFDC" will fail if TEST_MESSAGE is not set.
328 BuiltIn().set_global_variable("${TEST_MESSAGE}", "${EMPTY}")
Michael Walsh85678942017-03-27 14:34:22 -0500329 # FFDC_LOG_PATH is used by "FFDC" keyword.
330 BuiltIn().set_global_variable("${FFDC_LOG_PATH}", ffdc_dir_path)
Michael Walsh6741f742017-02-20 16:16:38 -0600331
Michael Walshdc80d672017-05-09 12:58:32 -0500332 # Also printed by FFDC.
333 global host_name
334 global host_ip
335 host = socket.gethostname()
336 host_name, host_ip = gm.get_host_name_ip(host)
337
Michael Walshb5839d02017-04-12 16:11:20 -0500338 gp.dprint_var(boot_table, 1)
339 gp.dprint_var(boot_lists)
Michael Walsh0bbd8602016-11-22 11:31:49 -0600340
341###############################################################################
342
343
344###############################################################################
Michael Walsh6741f742017-02-20 16:16:38 -0600345def validate_parms():
Michael Walsh0bbd8602016-11-22 11:31:49 -0600346
347 r"""
Michael Walsh6741f742017-02-20 16:16:38 -0600348 Validate all program parameters.
Michael Walsh0bbd8602016-11-22 11:31:49 -0600349 """
350
Michael Walshb5839d02017-04-12 16:11:20 -0500351 process_pgm_parms()
Michael Walsh0bbd8602016-11-22 11:31:49 -0600352
Michael Walshb5839d02017-04-12 16:11:20 -0500353 gp.qprintn()
354
355 global openbmc_model
Michael Walsh6741f742017-02-20 16:16:38 -0600356 grv.rvalid_value("openbmc_host")
357 grv.rvalid_value("openbmc_username")
358 grv.rvalid_value("openbmc_password")
359 if os_host != "":
360 grv.rvalid_value("os_username")
361 grv.rvalid_value("os_password")
Michael Walsh0bbd8602016-11-22 11:31:49 -0600362
Michael Walsh6741f742017-02-20 16:16:38 -0600363 if pdu_host != "":
364 grv.rvalid_value("pdu_username")
365 grv.rvalid_value("pdu_password")
Michael Walsh85678942017-03-27 14:34:22 -0500366 grv.rvalid_integer("pdu_slot_no")
Michael Walsh6741f742017-02-20 16:16:38 -0600367 if openbmc_serial_host != "":
368 grv.rvalid_integer("openbmc_serial_port")
Michael Walshb5839d02017-04-12 16:11:20 -0500369 if openbmc_model == "":
370 status, ret_values =\
371 grk.run_key_u("Get BMC System Model")
372 openbmc_model = ret_values
373 BuiltIn().set_global_variable("${openbmc_model}", openbmc_model)
Michael Walsh6741f742017-02-20 16:16:38 -0600374 grv.rvalid_value("openbmc_model")
Michael Walshb5839d02017-04-12 16:11:20 -0500375 grv.rvalid_integer("max_num_tests")
Michael Walsh6741f742017-02-20 16:16:38 -0600376 grv.rvalid_integer("boot_pass")
377 grv.rvalid_integer("boot_fail")
378
379 plug_in_packages_list = grpi.rvalidate_plug_ins(plug_in_dir_paths)
380 BuiltIn().set_global_variable("${plug_in_packages_list}",
381 plug_in_packages_list)
382
Michael Walshb5839d02017-04-12 16:11:20 -0500383 grv.rvalid_value("stack_mode", valid_values=['normal', 'skip'])
Michael Walsha20da402017-03-31 16:27:45 -0500384 if len(boot_list) == 0 and len(boot_stack) == 0 and not ffdc_only:
Michael Walsh6741f742017-02-20 16:16:38 -0600385 error_message = "You must provide either a value for either the" +\
386 " boot_list or the boot_stack parm.\n"
387 BuiltIn().fail(gp.sprint_error(error_message))
388
389 valid_boot_list(boot_list, valid_boot_types)
390 valid_boot_list(boot_stack, valid_boot_types)
391
Michael Walsh11cfc8c2017-03-31 09:40:55 -0500392 selected_PDU_boots = list(set(boot_list + boot_stack) &
393 set(boot_lists['PDU_reboot']))
394
395 if len(selected_PDU_boots) > 0 and pdu_host == "":
396 error_message = "You have selected the following boots which" +\
397 " require a PDU host but no value for pdu_host:\n"
398 error_message += gp.sprint_var(selected_PDU_boots)
399 error_message += gp.sprint_var(pdu_host, 2)
400 BuiltIn().fail(gp.sprint_error(error_message))
401
Michael Walsh6741f742017-02-20 16:16:38 -0600402 return
Michael Walsh0bbd8602016-11-22 11:31:49 -0600403
404###############################################################################
405
406
407###############################################################################
Michael Walsh6741f742017-02-20 16:16:38 -0600408def my_get_state():
Michael Walsh0bbd8602016-11-22 11:31:49 -0600409
410 r"""
Michael Walsh6741f742017-02-20 16:16:38 -0600411 Get the system state plus a little bit of wrapping.
Michael Walsh0bbd8602016-11-22 11:31:49 -0600412 """
413
Michael Walsh6741f742017-02-20 16:16:38 -0600414 global state
415
416 req_states = ['epoch_seconds'] + st.default_req_states
417
Michael Walshb5839d02017-04-12 16:11:20 -0500418 gp.qprint_timen("Getting system state.")
Michael Walsh6741f742017-02-20 16:16:38 -0600419 if test_mode:
420 state['epoch_seconds'] = int(time.time())
421 else:
Michael Walshb5839d02017-04-12 16:11:20 -0500422 state = st.get_state(req_states=req_states, quiet=quiet)
423 gp.qprint_var(state)
Michael Walsh341c21e2017-01-17 16:25:20 -0600424
425###############################################################################
426
427
428###############################################################################
Michael Walsh6741f742017-02-20 16:16:38 -0600429def select_boot():
Michael Walsh341c21e2017-01-17 16:25:20 -0600430
431 r"""
432 Select a boot test to be run based on our current state and return the
433 chosen boot type.
434
435 Description of arguments:
Michael Walsh6741f742017-02-20 16:16:38 -0600436 state The state of the machine.
Michael Walsh341c21e2017-01-17 16:25:20 -0600437 """
438
Michael Walsh30dadae2017-02-27 14:25:52 -0600439 global boot_stack
440
Michael Walshb5839d02017-04-12 16:11:20 -0500441 gp.qprint_timen("Selecting a boot test.")
Michael Walsh6741f742017-02-20 16:16:38 -0600442
443 my_get_state()
444
445 stack_popped = 0
446 if len(boot_stack) > 0:
447 stack_popped = 1
Michael Walshb5839d02017-04-12 16:11:20 -0500448 gp.qprint_dashes()
449 gp.qprint_var(boot_stack)
450 gp.qprint_dashes()
451 skip_boot_printed = 0
452 while len(boot_stack) > 0:
453 boot_candidate = boot_stack.pop()
454 if stack_mode == 'normal':
455 break
456 else:
457 if st.compare_states(state, boot_table[boot_candidate]['end']):
458 if not skip_boot_printed:
459 gp.print_var(stack_mode)
460 gp.printn()
461 gp.print_timen("Skipping the following boot tests" +
462 " which are unnecessary since their" +
463 " required end states match the" +
464 " current machine state:")
465 skip_boot_printed = 1
466 gp.print_var(boot_candidate)
467 boot_candidate = ""
468 if boot_candidate == "":
469 gp.qprint_dashes()
470 gp.qprint_var(boot_stack)
471 gp.qprint_dashes()
472 return boot_candidate
Michael Walsh6741f742017-02-20 16:16:38 -0600473 if st.compare_states(state, boot_table[boot_candidate]['start']):
Michael Walshb5839d02017-04-12 16:11:20 -0500474 gp.qprint_timen("The machine state is valid for a '" +
475 boot_candidate + "' boot test.")
476 gp.qprint_dashes()
477 gp.qprint_var(boot_stack)
478 gp.qprint_dashes()
Michael Walsh6741f742017-02-20 16:16:38 -0600479 return boot_candidate
Michael Walsh341c21e2017-01-17 16:25:20 -0600480 else:
Michael Walshb5839d02017-04-12 16:11:20 -0500481 gp.qprint_timen("The machine state does not match the required" +
482 " starting state for a '" + boot_candidate +
483 "' boot test:")
484 gp.print_varx("boot_table[" + boot_candidate + "][start]",
485 boot_table[boot_candidate]['start'], 1)
Michael Walsh6741f742017-02-20 16:16:38 -0600486 boot_stack.append(boot_candidate)
487 popped_boot = boot_candidate
488
489 # Loop through your list selecting a boot_candidates
490 boot_candidates = []
491 for boot_candidate in boot_list:
492 if st.compare_states(state, boot_table[boot_candidate]['start']):
493 if stack_popped:
494 if st.compare_states(boot_table[boot_candidate]['end'],
495 boot_table[popped_boot]['start']):
496 boot_candidates.append(boot_candidate)
497 else:
498 boot_candidates.append(boot_candidate)
499
500 if len(boot_candidates) == 0:
Michael Walshb5839d02017-04-12 16:11:20 -0500501 gp.qprint_timen("The user's boot list contained no boot tests" +
502 " which are valid for the current machine state.")
Michael Walsh6741f742017-02-20 16:16:38 -0600503 boot_candidate = default_power_on
504 if not st.compare_states(state, boot_table[default_power_on]['start']):
505 boot_candidate = default_power_off
506 boot_candidates.append(boot_candidate)
Michael Walshb5839d02017-04-12 16:11:20 -0500507 gp.qprint_timen("Using default '" + boot_candidate +
508 "' boot type to transition to valid state.")
Michael Walsh6741f742017-02-20 16:16:38 -0600509
Michael Walshb5839d02017-04-12 16:11:20 -0500510 gp.dprint_var(boot_candidates)
Michael Walsh6741f742017-02-20 16:16:38 -0600511
512 # Randomly select a boot from the candidate list.
513 boot = random.choice(boot_candidates)
Michael Walsh341c21e2017-01-17 16:25:20 -0600514
515 return boot
Michael Walsh0bbd8602016-11-22 11:31:49 -0600516
517###############################################################################
Michael Walsh55302292017-01-10 11:43:02 -0600518
519
520###############################################################################
Michael Walsh341c21e2017-01-17 16:25:20 -0600521def print_last_boots():
522
523 r"""
524 Print the last ten boots done with their time stamps.
525 """
526
527 # indent 0, 90 chars wide, linefeed, char is "="
Michael Walshb5839d02017-04-12 16:11:20 -0500528 gp.qprint_dashes(0, 90)
529 gp.qprintn("Last 10 boots:\n")
Michael Walsh341c21e2017-01-17 16:25:20 -0600530
531 for boot_entry in last_ten:
532 grp.rqprint(boot_entry)
Michael Walshb5839d02017-04-12 16:11:20 -0500533 gp.qprint_dashes(0, 90)
Michael Walsh341c21e2017-01-17 16:25:20 -0600534
535###############################################################################
536
537
538###############################################################################
Michael Walsh341c21e2017-01-17 16:25:20 -0600539def print_defect_report():
540
541 r"""
542 Print a defect report.
543 """
544
Michael Walsh341c21e2017-01-17 16:25:20 -0600545 # At some point I'd like to have the 'Call FFDC Methods' return a list
546 # of files it has collected. In that case, the following "ls" command
547 # would no longer be needed. For now, however, glob shows the files
548 # named in FFDC_LIST_FILE_PATH so I will refrain from printing those
549 # out (so we don't see duplicates in the list).
550
551 LOG_PREFIX = BuiltIn().get_variable_value("${LOG_PREFIX}")
552
553 output = '\n'.join(glob.glob(LOG_PREFIX + '*'))
Michael Walsh341c21e2017-01-17 16:25:20 -0600554 try:
Michael Walsh6741f742017-02-20 16:16:38 -0600555 ffdc_list = open(ffdc_list_file_path, 'r')
Michael Walsh341c21e2017-01-17 16:25:20 -0600556 except IOError:
557 ffdc_list = ""
558
Michael Walsh68a61162017-04-25 11:54:06 -0500559 # Open ffdc_file_list for writing. We will write a complete list of
560 # FFDC files to it for possible use by plug-ins like cp_stop_check.
561 ffdc_list_file = open(ffdc_list_file_path, 'w')
562
563 gp.qprintn()
564 # indent=0, width=90, linefeed=1, char="="
565 gp.qprint_dashes(0, 90, 1, "=")
566 gp.qprintn("Copy this data to the defect:\n")
567
Michael Walshdc80d672017-05-09 12:58:32 -0500568 gp.qpvars(host_name, host_ip, openbmc_nickname, openbmc_host,
569 openbmc_host_name, openbmc_ip, openbmc_username,
570 openbmc_password, os_host, os_host_name, os_ip, os_username,
571 os_password, pdu_host, pdu_host_name, pdu_ip, pdu_username,
572 pdu_password, pdu_slot_no, openbmc_serial_host,
573 openbmc_serial_host_name, openbmc_serial_ip, openbmc_serial_port)
Michael Walsh68a61162017-04-25 11:54:06 -0500574
575 gp.qprintn()
576
577 print_last_boots()
578 gp.qprintn()
579 gp.qprint_var(state)
580
Michael Walshb5839d02017-04-12 16:11:20 -0500581 gp.qprintn()
582 gp.qprintn("FFDC data files:")
Michael Walsh341c21e2017-01-17 16:25:20 -0600583 if status_file_path != "":
Michael Walshb5839d02017-04-12 16:11:20 -0500584 gp.qprintn(status_file_path)
Michael Walsh68a61162017-04-25 11:54:06 -0500585 ffdc_list_file.write(status_file_path + "\n")
Michael Walsh341c21e2017-01-17 16:25:20 -0600586
Michael Walshb5839d02017-04-12 16:11:20 -0500587 gp.qprintn(output)
588 # gp.qprintn(ffdc_list)
589 gp.qprintn()
Michael Walsh341c21e2017-01-17 16:25:20 -0600590
Michael Walshb5839d02017-04-12 16:11:20 -0500591 gp.qprint_dashes(0, 90, 1, "=")
Michael Walsh341c21e2017-01-17 16:25:20 -0600592
Michael Walsh68a61162017-04-25 11:54:06 -0500593 ffdc_list_file.write(output + "\n")
594 ffdc_list_file.close()
595
Michael Walsh341c21e2017-01-17 16:25:20 -0600596###############################################################################
Michael Walsh6741f742017-02-20 16:16:38 -0600597
598
599###############################################################################
600def my_ffdc():
601
602 r"""
603 Collect FFDC data.
604 """
605
606 global state
607
608 plug_in_setup()
609 rc, shell_rc, failed_plug_in_name = grpi.rprocess_plug_in_packages(
610 call_point='ffdc', stop_on_plug_in_failure=1)
611
612 AUTOBOOT_FFDC_PREFIX = os.environ['AUTOBOOT_FFDC_PREFIX']
Michael Walsh83f4bc72017-04-20 16:49:43 -0500613 status, ret_values = grk.run_key_u("FFDC ffdc_prefix=" +
614 AUTOBOOT_FFDC_PREFIX +
615 " ffdc_function_list=" +
616 ffdc_function_list, ignore=1)
617 if status != 'PASS':
Michael Walsh3328caf2017-03-21 17:04:08 -0500618 gp.print_error("Call to ffdc failed.\n")
Michael Walsh6741f742017-02-20 16:16:38 -0600619
620 my_get_state()
621
622 print_defect_report()
623
624###############################################################################
625
626
627###############################################################################
628def print_test_start_message(boot_keyword):
629
630 r"""
631 Print a message indicating what boot test is about to run.
632
633 Description of arguments:
634 boot_keyword The name of the boot which is to be run
635 (e.g. "BMC Power On").
636 """
637
638 global last_ten
639
640 doing_msg = gp.sprint_timen("Doing \"" + boot_keyword + "\".")
Michael Walshb5839d02017-04-12 16:11:20 -0500641 gp.qprint(doing_msg)
Michael Walsh6741f742017-02-20 16:16:38 -0600642
643 last_ten.append(doing_msg)
644
645 if len(last_ten) > 10:
646 del last_ten[0]
647
648###############################################################################
649
650
651###############################################################################
652def run_boot(boot):
653
654 r"""
655 Run the specified boot.
656
657 Description of arguments:
658 boot The name of the boot test to be performed.
659 """
660
661 global state
662
663 print_test_start_message(boot)
664
665 plug_in_setup()
666 rc, shell_rc, failed_plug_in_name = \
667 grpi.rprocess_plug_in_packages(call_point="pre_boot")
668 if rc != 0:
669 error_message = "Plug-in failed with non-zero return code.\n" +\
670 gp.sprint_var(rc, 1)
671 BuiltIn().fail(gp.sprint_error(error_message))
672
673 if test_mode:
674 # In test mode, we'll pretend the boot worked by assigning its
675 # required end state to the default state value.
Michael Walsh30dadae2017-02-27 14:25:52 -0600676 state = st.strip_anchor_state(boot_table[boot]['end'])
Michael Walsh6741f742017-02-20 16:16:38 -0600677 else:
678 # Assertion: We trust that the state data was made fresh by the
679 # caller.
680
Michael Walshb5839d02017-04-12 16:11:20 -0500681 gp.qprintn()
Michael Walsh6741f742017-02-20 16:16:38 -0600682
683 if boot_table[boot]['method_type'] == "keyword":
Michael Walsh0b93fbf2017-03-02 14:42:41 -0600684 rk.my_run_keywords(boot_table[boot].get('lib_file_path', ''),
Michael Walshb5839d02017-04-12 16:11:20 -0500685 boot_table[boot]['method'],
686 quiet=quiet)
Michael Walsh6741f742017-02-20 16:16:38 -0600687
688 if boot_table[boot]['bmc_reboot']:
689 st.wait_for_comm_cycle(int(state['epoch_seconds']))
Michael Walsh30dadae2017-02-27 14:25:52 -0600690 plug_in_setup()
691 rc, shell_rc, failed_plug_in_name = \
692 grpi.rprocess_plug_in_packages(call_point="post_reboot")
693 if rc != 0:
Michael Walsh0b93fbf2017-03-02 14:42:41 -0600694 error_message = "Plug-in failed with non-zero return code.\n"
695 error_message += gp.sprint_var(rc, 1)
Michael Walsh30dadae2017-02-27 14:25:52 -0600696 BuiltIn().fail(gp.sprint_error(error_message))
Michael Walsh6741f742017-02-20 16:16:38 -0600697 else:
698 match_state = st.anchor_state(state)
699 del match_state['epoch_seconds']
700 # Wait for the state to change in any way.
701 st.wait_state(match_state, wait_time=state_change_timeout,
702 interval="3 seconds", invert=1)
703
Michael Walshb5839d02017-04-12 16:11:20 -0500704 gp.qprintn()
Michael Walsh6741f742017-02-20 16:16:38 -0600705 if boot_table[boot]['end']['chassis'] == "Off":
706 boot_timeout = power_off_timeout
707 else:
708 boot_timeout = power_on_timeout
709 st.wait_state(boot_table[boot]['end'], wait_time=boot_timeout,
710 interval="3 seconds")
711
712 plug_in_setup()
713 rc, shell_rc, failed_plug_in_name = \
714 grpi.rprocess_plug_in_packages(call_point="post_boot")
715 if rc != 0:
716 error_message = "Plug-in failed with non-zero return code.\n" +\
717 gp.sprint_var(rc, 1)
718 BuiltIn().fail(gp.sprint_error(error_message))
719
720###############################################################################
721
722
723###############################################################################
724def test_loop_body():
725
726 r"""
727 The main loop body for the loop in main_py.
728
729 Description of arguments:
730 boot_count The iteration number (starts at 1).
731 """
732
733 global boot_count
734 global state
735 global next_boot
736 global boot_success
737
Michael Walshb5839d02017-04-12 16:11:20 -0500738 gp.qprintn()
Michael Walsh6741f742017-02-20 16:16:38 -0600739
740 next_boot = select_boot()
Michael Walshb5839d02017-04-12 16:11:20 -0500741 if next_boot == "":
742 return True
Michael Walsh6741f742017-02-20 16:16:38 -0600743
Michael Walshb5839d02017-04-12 16:11:20 -0500744 boot_count += 1
745 gp.qprint_timen("Starting boot " + str(boot_count) + ".")
Michael Walsh6741f742017-02-20 16:16:38 -0600746
747 # Clear the ffdc_list_file_path file. Plug-ins may now write to it.
748 try:
749 os.remove(ffdc_list_file_path)
750 except OSError:
751 pass
752
753 cmd_buf = ["run_boot", next_boot]
754 boot_status, msg = BuiltIn().run_keyword_and_ignore_error(*cmd_buf)
755 if boot_status == "FAIL":
Michael Walshb5839d02017-04-12 16:11:20 -0500756 gp.qprint(msg)
Michael Walsh6741f742017-02-20 16:16:38 -0600757
Michael Walshb5839d02017-04-12 16:11:20 -0500758 gp.qprintn()
Michael Walsh6741f742017-02-20 16:16:38 -0600759 if boot_status == "PASS":
760 boot_success = 1
Michael Walshb5839d02017-04-12 16:11:20 -0500761 gp.qprint_timen("BOOT_SUCCESS: \"" + next_boot + "\" succeeded.")
Michael Walsh6741f742017-02-20 16:16:38 -0600762 else:
763 boot_success = 0
Michael Walshb5839d02017-04-12 16:11:20 -0500764 gp.qprint_timen("BOOT_FAILED: \"" + next_boot + "\" failed.")
Michael Walsh6741f742017-02-20 16:16:38 -0600765
766 boot_results.update(next_boot, boot_status)
767
768 plug_in_setup()
769 # NOTE: A post_test_case call point failure is NOT counted as a boot
770 # failure.
771 rc, shell_rc, failed_plug_in_name = grpi.rprocess_plug_in_packages(
772 call_point='post_test_case', stop_on_plug_in_failure=1)
773
774 plug_in_setup()
775 rc, shell_rc, failed_plug_in_name = grpi.rprocess_plug_in_packages(
776 call_point='ffdc_check', shell_rc=0x00000200,
777 stop_on_plug_in_failure=1, stop_on_non_zero_rc=1)
778 if boot_status != "PASS" or ffdc_check == "All" or shell_rc == 0x00000200:
Michael Walsh83f4bc72017-04-20 16:49:43 -0500779 status, ret_values = grk.run_key_u("my_ffdc", ignore=1)
780 if status != 'PASS':
Michael Walsh3328caf2017-03-21 17:04:08 -0500781 gp.print_error("Call to my_ffdc failed.\n")
Michael Walsh6741f742017-02-20 16:16:38 -0600782
Michael Walshd139f282017-04-04 18:00:23 -0500783 # We need to purge error logs between boots or they build up.
Michael Walshb5839d02017-04-12 16:11:20 -0500784 grk.run_key("Delete Error logs", ignore=1)
Michael Walshd139f282017-04-04 18:00:23 -0500785
Michael Walsh952f9b02017-03-09 13:11:14 -0600786 boot_results.print_report()
Michael Walshb5839d02017-04-12 16:11:20 -0500787 gp.qprint_timen("Finished boot " + str(boot_count) + ".")
Michael Walsh952f9b02017-03-09 13:11:14 -0600788
Michael Walsh6741f742017-02-20 16:16:38 -0600789 plug_in_setup()
790 rc, shell_rc, failed_plug_in_name = grpi.rprocess_plug_in_packages(
791 call_point='stop_check')
792 if rc != 0:
793 error_message = "Stopping as requested by user.\n"
794 grp.rprint_error_report(error_message)
795 BuiltIn().fail(error_message)
796
Michael Walshd139f282017-04-04 18:00:23 -0500797 # This should help prevent ConnectionErrors.
Michael Walshb5839d02017-04-12 16:11:20 -0500798 grk.run_key_u("Delete All Sessions")
Michael Walshd139f282017-04-04 18:00:23 -0500799
Michael Walsh6741f742017-02-20 16:16:38 -0600800 return True
801
802###############################################################################
803
804
805###############################################################################
Michael Walsh83f4bc72017-04-20 16:49:43 -0500806def obmc_boot_test_teardown():
Michael Walsh6741f742017-02-20 16:16:38 -0600807
808 r"""
Michael Walshc9116812017-03-10 14:23:06 -0600809 Clean up after the Main keyword.
Michael Walsh6741f742017-02-20 16:16:38 -0600810 """
811
812 if cp_setup_called:
813 plug_in_setup()
814 rc, shell_rc, failed_plug_in_name = grpi.rprocess_plug_in_packages(
815 call_point='cleanup', stop_on_plug_in_failure=1)
816
Michael Walsh0b93fbf2017-03-02 14:42:41 -0600817 # Save boot_results object to a file in case it is needed again.
Michael Walshb5839d02017-04-12 16:11:20 -0500818 gp.qprint_timen("Saving boot_results to the following path.")
819 gp.qprint_var(boot_results_file_path)
Michael Walsh0b93fbf2017-03-02 14:42:41 -0600820 pickle.dump(boot_results, open(boot_results_file_path, 'wb'),
821 pickle.HIGHEST_PROTOCOL)
822
Michael Walsh6741f742017-02-20 16:16:38 -0600823###############################################################################
824
825
826###############################################################################
Michael Walshc9116812017-03-10 14:23:06 -0600827def test_teardown():
828
829 r"""
830 Clean up after this test case.
831 """
832
833 gp.qprintn()
834 cmd_buf = ["Print Error",
835 "A keyword timeout occurred ending this program.\n"]
836 BuiltIn().run_keyword_if_timeout_occurred(*cmd_buf)
837
Michael Walshb5839d02017-04-12 16:11:20 -0500838 grp.rqprint_pgm_footer()
839
Michael Walshc9116812017-03-10 14:23:06 -0600840###############################################################################
841
842
843###############################################################################
Michael Walsh83f4bc72017-04-20 16:49:43 -0500844def obmc_boot_test_py(alt_boot_stack=None):
Michael Walsh6741f742017-02-20 16:16:38 -0600845
846 r"""
847 Do main program processing.
848 """
849
Michael Walshb5839d02017-04-12 16:11:20 -0500850 if alt_boot_stack is not None:
851 BuiltIn().set_global_variable("${boot_stack}", alt_boot_stack)
852
Michael Walsh6741f742017-02-20 16:16:38 -0600853 setup()
854
Michael Walsha20da402017-03-31 16:27:45 -0500855 if ffdc_only:
856 gp.qprint_timen("Caller requested ffdc_only.")
Michael Walsh83f4bc72017-04-20 16:49:43 -0500857 grk.run_key_u("my_ffdc")
Michael Walsh764d2f82017-04-27 16:01:08 -0500858 return
Michael Walsha20da402017-03-31 16:27:45 -0500859
Michael Walsh6741f742017-02-20 16:16:38 -0600860 # Process caller's boot_stack.
861 while (len(boot_stack) > 0):
862 test_loop_body()
863
Michael Walshb5839d02017-04-12 16:11:20 -0500864 gp.qprint_timen("Finished processing stack.")
Michael Walsh30dadae2017-02-27 14:25:52 -0600865
Michael Walsh6741f742017-02-20 16:16:38 -0600866 # Process caller's boot_list.
867 if len(boot_list) > 0:
868 for ix in range(1, max_num_tests + 1):
869 test_loop_body()
870
Michael Walshb5839d02017-04-12 16:11:20 -0500871 gp.qprint_timen("Completed all requested boot tests.")
872
873 boot_pass, boot_fail = boot_results.return_total_pass_fail()
874 if boot_fail > boot_fail_threshold:
875 error_message = "Boot failures exceed the boot failure" +\
876 " threshold:\n" +\
877 gp.sprint_var(boot_fail) +\
878 gp.sprint_var(boot_fail_threshold)
879 BuiltIn().fail(gp.sprint_error(error_message))
Michael Walsh6741f742017-02-20 16:16:38 -0600880
881###############################################################################