10bbd860fSMichael Walsh#!/usr/bin/env python
20bbd860fSMichael Walsh
30bbd860fSMichael Walshr"""
40bbd860fSMichael WalshThis module is the python counterpart to obmc_boot_test.
50bbd860fSMichael Walsh"""
60bbd860fSMichael Walsh
70bbd860fSMichael Walshfrom tally_sheet import *
80bbd860fSMichael Walshimport gen_robot_print as grp
90bbd860fSMichael Walsh
100bbd860fSMichael Walshimport os
110bbd860fSMichael Walshimport time
120bbd860fSMichael Walshimport subprocess
130bbd860fSMichael Walsh
140bbd860fSMichael Walshfrom robot.utils import DotDict
150bbd860fSMichael Walshfrom robot.libraries.BuiltIn import BuiltIn
160bbd860fSMichael Walshfrom robot.libraries.OperatingSystem import OperatingSystem
170bbd860fSMichael Walsh
180bbd860fSMichael Walsh# Create boot_results_fields for use in creating boot_results.
190bbd860fSMichael Walshboot_results_fields = DotDict([('total', 0), ('pass', 0), ('fail', 0)])
200bbd860fSMichael Walsh# Create boot_results which is global to this module.
210bbd860fSMichael Walshboot_results = tally_sheet('boot type',
220bbd860fSMichael Walsh                           boot_results_fields,
230bbd860fSMichael Walsh                           'boot_test_results')
240bbd860fSMichael Walsh
250bbd860fSMichael Walshboot_results.set_sum_fields(['total', 'pass', 'fail'])
260bbd860fSMichael Walshboot_results.set_calc_fields(['total=pass+fail'])
270bbd860fSMichael Walsh
280bbd860fSMichael Walsh
290bbd860fSMichael Walsh###############################################################################
300bbd860fSMichael Walshdef add_trailing_slash(path):
310bbd860fSMichael Walsh
320bbd860fSMichael Walsh    r"""
330bbd860fSMichael Walsh    Add a trailing slash to path if it doesn't already have one and return it.
340bbd860fSMichael Walsh
350bbd860fSMichael Walsh    """
360bbd860fSMichael Walsh
370bbd860fSMichael Walsh    return os.path.normpath(path) + os.path.sep
380bbd860fSMichael Walsh
390bbd860fSMichael Walsh###############################################################################
400bbd860fSMichael Walsh
410bbd860fSMichael Walsh
420bbd860fSMichael Walsh###############################################################################
430bbd860fSMichael Walshdef plug_in_setup():
440bbd860fSMichael Walsh
450bbd860fSMichael Walsh    r"""
460bbd860fSMichael Walsh    Initialize all plug-in environment variables for use by the plug-in
470bbd860fSMichael Walsh    programs.
480bbd860fSMichael Walsh    """
490bbd860fSMichael Walsh
500bbd860fSMichael Walsh    boot_pass = int(BuiltIn().get_variable_value("${boot_pass}"))
510bbd860fSMichael Walsh    if boot_pass > 1:
520bbd860fSMichael Walsh        test_really_running = 1
530bbd860fSMichael Walsh    else:
540bbd860fSMichael Walsh        test_really_running = 0
550bbd860fSMichael Walsh
560bbd860fSMichael Walsh    BuiltIn().set_global_variable("${test_really_running}",
570bbd860fSMichael Walsh                                  test_really_running)
580bbd860fSMichael Walsh
590bbd860fSMichael Walsh    next_boot = BuiltIn().get_variable_value("${next_boot}")
600bbd860fSMichael Walsh    BuiltIn().set_global_variable("${boot_type_desc}", next_boot)
610bbd860fSMichael Walsh
620bbd860fSMichael Walsh    # Setting master_pid correctly influences the behavior of plug-ins like
630bbd860fSMichael Walsh    # DB_Logging
640bbd860fSMichael Walsh    program_pid = BuiltIn().get_variable_value("${program_pid}")
650bbd860fSMichael Walsh    try:
660bbd860fSMichael Walsh        master_pid = OperatingSystem().get_environment_variable(
670bbd860fSMichael Walsh            "AUTOBOOT_MASTER_PID")
680bbd860fSMichael Walsh    except RuntimeError:
690bbd860fSMichael Walsh        master_pid = program_pid
700bbd860fSMichael Walsh    if master_pid == "":
710bbd860fSMichael Walsh        master_pid = program_pid
720bbd860fSMichael Walsh
730bbd860fSMichael Walsh    BuiltIn().set_global_variable("${master_pid}", master_pid)
740bbd860fSMichael Walsh
750bbd860fSMichael Walsh    seconds = time.time()
760bbd860fSMichael Walsh    loc_time = time.localtime(seconds)
770bbd860fSMichael Walsh    time_string = time.strftime("%y%m%d.%H%M%S.", loc_time)
780bbd860fSMichael Walsh
790bbd860fSMichael Walsh    openbmc_nickname = BuiltIn().get_variable_value("${openbmc_nickname}")
800bbd860fSMichael Walsh    openbmc_host = BuiltIn().get_variable_value("${openbmc_host}")
81*769c2a1bSMichael Walsh    if openbmc_nickname == "":
820bbd860fSMichael Walsh        ffdc_prefix = openbmc_host
830bbd860fSMichael Walsh    else:
840bbd860fSMichael Walsh        ffdc_prefix = openbmc_nickname
850bbd860fSMichael Walsh
860bbd860fSMichael Walsh    ffdc_prefix += "." + time_string
870bbd860fSMichael Walsh
8886de0d2eSMichael Walsh    try:
8986de0d2eSMichael Walsh        ffdc_dir_path = os.environ['FFDC_DIR_PATH']
900bbd860fSMichael Walsh        # Add trailing slash.
910bbd860fSMichael Walsh        ffdc_dir_path = os.path.normpath(ffdc_dir_path) + os.sep
9286de0d2eSMichael Walsh    except KeyError:
9386de0d2eSMichael Walsh        ffdc_dir_path = ""
940bbd860fSMichael Walsh    BuiltIn().set_global_variable("${FFDC_DIR_PATH}", ffdc_dir_path)
950bbd860fSMichael Walsh
964c9a6453SMichael Walsh    try:
974c9a6453SMichael Walsh        base_tool_dir_path = os.environ['AUTOBOOT_BASE_TOOL_DIR_PATH']
984c9a6453SMichael Walsh    except KeyError:
994c9a6453SMichael Walsh        base_tool_dir_path = "/tmp/"
1004c9a6453SMichael Walsh    base_tool_dir_path = os.path.normpath(base_tool_dir_path) + os.sep
1014c9a6453SMichael Walsh    BuiltIn().set_global_variable("${BASE_TOOL_DIR_PATH}", base_tool_dir_path)
1024c9a6453SMichael Walsh
1034c9a6453SMichael Walsh    ffdc_list_file_path = base_tool_dir_path + openbmc_host + "/FFDC_FILE_LIST"
1044c9a6453SMichael Walsh
1054c9a6453SMichael Walsh    BuiltIn().set_global_variable("${FFDC_LIST_FILE_PATH}",
1064c9a6453SMichael Walsh                                  ffdc_list_file_path)
1074c9a6453SMichael Walsh
1080bbd860fSMichael Walsh    # For each program parameter, set the corresponding AUTOBOOT_ environment
1090bbd860fSMichael Walsh    # variable value.  Also, set an AUTOBOOT_ environment variable for every
1100bbd860fSMichael Walsh    # element in additional_values.
1110bbd860fSMichael Walsh    additional_values = ["boot_type_desc", "boot_success", "boot_pass",
1120bbd860fSMichael Walsh                         "boot_fail", "test_really_running", "program_pid",
1134c9a6453SMichael Walsh                         "master_pid", "ffdc_prefix", "ffdc_dir_path",
1144c9a6453SMichael Walsh                         "base_tool_dir_path", "ffdc_list_file_path"]
1150bbd860fSMichael Walsh    BuiltIn().set_global_variable("${ffdc_prefix}", ffdc_prefix)
1160bbd860fSMichael Walsh
1170bbd860fSMichael Walsh    parm_list = BuiltIn().get_variable_value("@{parm_list}")
1180bbd860fSMichael Walsh
1190bbd860fSMichael Walsh    plug_in_vars = parm_list + additional_values
1200bbd860fSMichael Walsh
1210bbd860fSMichael Walsh    for var_name in plug_in_vars:
1220bbd860fSMichael Walsh        var_value = BuiltIn().get_variable_value("${" + var_name + "}")
1230bbd860fSMichael Walsh        var_name = var_name.upper()
1240bbd860fSMichael Walsh        if var_value is None:
1250bbd860fSMichael Walsh            var_value = ""
1260bbd860fSMichael Walsh        OperatingSystem().set_environment_variable(
1270bbd860fSMichael Walsh            "AUTOBOOT_" + var_name, var_value)
1280bbd860fSMichael Walsh
1290bbd860fSMichael Walsh    debug = int(BuiltIn().get_variable_value("${debug}"))
1300bbd860fSMichael Walsh    if debug:
1310bbd860fSMichael Walsh        cmd_buf = "printenv | egrep AUTOBOOT_ | sort -u"
1320bbd860fSMichael Walsh        grp.rpissuing(cmd_buf)
1330bbd860fSMichael Walsh        sub_proc = subprocess.Popen(cmd_buf, shell=True,
1340bbd860fSMichael Walsh                                    stdout=subprocess.PIPE,
1350bbd860fSMichael Walsh                                    stderr=subprocess.STDOUT)
1360bbd860fSMichael Walsh        out_buf, err_buf = sub_proc.communicate()
1370bbd860fSMichael Walsh        shell_rc = sub_proc.returncode
1380bbd860fSMichael Walsh        grp.rprint(out_buf)
1390bbd860fSMichael Walsh
1400bbd860fSMichael Walsh###############################################################################
1410bbd860fSMichael Walsh
1420bbd860fSMichael Walsh
1430bbd860fSMichael Walsh###############################################################################
1440bbd860fSMichael Walshdef create_boot_results_table():
1450bbd860fSMichael Walsh
1460bbd860fSMichael Walsh    r"""
1470bbd860fSMichael Walsh    Create our boot_results_table.
1480bbd860fSMichael Walsh    """
1490bbd860fSMichael Walsh
1500bbd860fSMichael Walsh    # At some point we'll want to change to reading in our boot types from
1510bbd860fSMichael Walsh    # some external source (e.g. file).
1520bbd860fSMichael Walsh
1530bbd860fSMichael Walsh    boot_results.add_row('BMC Power On')
1540bbd860fSMichael Walsh    boot_results.add_row('BMC Power Off')
1550bbd860fSMichael Walsh
1560bbd860fSMichael Walsh###############################################################################
1570bbd860fSMichael Walsh
1580bbd860fSMichael Walsh
1590bbd860fSMichael Walsh###############################################################################
1600bbd860fSMichael Walshdef update_boot_results_table(boot_type,
1610bbd860fSMichael Walsh                              boot_status):
1620bbd860fSMichael Walsh
1630bbd860fSMichael Walsh    r"""
1640bbd860fSMichael Walsh    Update our boot_results_table.  This includes:
1650bbd860fSMichael Walsh    - Updating the record for the given boot_type by incrementing the pass or
1660bbd860fSMichael Walsh      fail field.
1670bbd860fSMichael Walsh    - Calling the calc method to have the totals, etc. calculated.
1680bbd860fSMichael Walsh    - Updating global variables boot_pass/boot_fail.
1690bbd860fSMichael Walsh
1700bbd860fSMichael Walsh    Description of arguments:
1710bbd860fSMichael Walsh    boot_type    The type of boot just done (e.g. "BMC Power On").
1720bbd860fSMichael Walsh    boot_status  The status of the boot just done.  This should be equal to
1730bbd860fSMichael Walsh                 either "pass" or "fail" (case-insensitive).
1740bbd860fSMichael Walsh    """
1750bbd860fSMichael Walsh
1760bbd860fSMichael Walsh    boot_results.inc_row_field(boot_type, boot_status.lower())
1770bbd860fSMichael Walsh    totals_line = boot_results.calc()
1780bbd860fSMichael Walsh
1790bbd860fSMichael Walsh    # The caller of obmc_boot_test can pass boot_pass/boot_fail values because
1800bbd860fSMichael Walsh    # the caller may have already done some testing (e.g. "BMC OOB").  For the
1810bbd860fSMichael Walsh    # sake of DB logging done by plug-ins, we want to include these in our
1820bbd860fSMichael Walsh    # overall totals.
1830bbd860fSMichael Walsh    initial_boot_pass = int(BuiltIn().get_variable_value(
1840bbd860fSMichael Walsh        "${initial_boot_pass}"))
1850bbd860fSMichael Walsh    initial_boot_fail = int(BuiltIn().get_variable_value(
1860bbd860fSMichael Walsh        "${initial_boot_fail}"))
1870bbd860fSMichael Walsh
1880bbd860fSMichael Walsh    BuiltIn().set_global_variable("${boot_pass}",
1890bbd860fSMichael Walsh                                  totals_line['pass'] + initial_boot_pass)
1900bbd860fSMichael Walsh    BuiltIn().set_global_variable("${boot_fail}",
1910bbd860fSMichael Walsh                                  totals_line['fail'] + initial_boot_fail)
1920bbd860fSMichael Walsh
1930bbd860fSMichael Walsh###############################################################################
1940bbd860fSMichael Walsh
1950bbd860fSMichael Walsh
1960bbd860fSMichael Walsh###############################################################################
1970bbd860fSMichael Walshdef print_boot_results_table(header_footer="\n"):
1980bbd860fSMichael Walsh
1990bbd860fSMichael Walsh    r"""
2000bbd860fSMichael Walsh    Print the formatted boot_resuls_table to the console.
2010bbd860fSMichael Walsh    """
2020bbd860fSMichael Walsh
2030bbd860fSMichael Walsh    grp.rprint(header_footer)
2040bbd860fSMichael Walsh    grp.rprint(boot_results.sprint_report())
2050bbd860fSMichael Walsh    grp.rprint(header_footer)
2060bbd860fSMichael Walsh
2070bbd860fSMichael Walsh###############################################################################
208