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