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