1#!/usr/bin/env python
2
3r"""
4This module is the python counterpart to obmc_boot_test.
5"""
6
7from tally_sheet import *
8import gen_robot_print as grp
9
10import os
11import time
12import subprocess
13
14from robot.utils import DotDict
15from robot.libraries.BuiltIn import BuiltIn
16from robot.libraries.OperatingSystem import OperatingSystem
17
18# Create boot_results_fields for use in creating boot_results.
19boot_results_fields = DotDict([('total', 0), ('pass', 0), ('fail', 0)])
20# Create boot_results which is global to this module.
21boot_results = tally_sheet('boot type',
22                           boot_results_fields,
23                           'boot_test_results')
24
25boot_results.set_sum_fields(['total', 'pass', 'fail'])
26boot_results.set_calc_fields(['total=pass+fail'])
27
28
29###############################################################################
30def add_trailing_slash(path):
31
32    r"""
33    Add a trailing slash to path if it doesn't already have one and return it.
34
35    """
36
37    return os.path.normpath(path) + os.path.sep
38
39###############################################################################
40
41
42###############################################################################
43def plug_in_setup():
44
45    r"""
46    Initialize all plug-in environment variables for use by the plug-in
47    programs.
48    """
49
50    boot_pass = int(BuiltIn().get_variable_value("${boot_pass}"))
51    if boot_pass > 1:
52        test_really_running = 1
53    else:
54        test_really_running = 0
55
56    BuiltIn().set_global_variable("${test_really_running}",
57                                  test_really_running)
58
59    next_boot = BuiltIn().get_variable_value("${next_boot}")
60    BuiltIn().set_global_variable("${boot_type_desc}", next_boot)
61
62    # Setting master_pid correctly influences the behavior of plug-ins like
63    # DB_Logging
64    program_pid = BuiltIn().get_variable_value("${program_pid}")
65    try:
66        master_pid = OperatingSystem().get_environment_variable(
67            "AUTOBOOT_MASTER_PID")
68    except RuntimeError:
69        master_pid = program_pid
70    if master_pid == "":
71        master_pid = program_pid
72
73    BuiltIn().set_global_variable("${master_pid}", master_pid)
74
75    seconds = time.time()
76    loc_time = time.localtime(seconds)
77    time_string = time.strftime("%y%m%d.%H%M%S.", loc_time)
78
79    openbmc_nickname = BuiltIn().get_variable_value("${openbmc_nickname}")
80    openbmc_host = BuiltIn().get_variable_value("${openbmc_host}")
81    if openbmc_nickname == "":
82        ffdc_prefix = openbmc_host
83    else:
84        ffdc_prefix = openbmc_nickname
85
86    ffdc_prefix += "." + time_string
87
88    try:
89        ffdc_dir_path = os.environ['FFDC_DIR_PATH']
90        # Add trailing slash.
91        ffdc_dir_path = os.path.normpath(ffdc_dir_path) + os.sep
92    except KeyError:
93        ffdc_dir_path = ""
94    BuiltIn().set_global_variable("${FFDC_DIR_PATH}", ffdc_dir_path)
95
96    try:
97        base_tool_dir_path = os.environ['AUTOBOOT_BASE_TOOL_DIR_PATH']
98    except KeyError:
99        base_tool_dir_path = "/tmp/"
100    base_tool_dir_path = os.path.normpath(base_tool_dir_path) + os.sep
101    BuiltIn().set_global_variable("${BASE_TOOL_DIR_PATH}", base_tool_dir_path)
102
103    ffdc_list_file_path = base_tool_dir_path + openbmc_host + "/FFDC_FILE_LIST"
104
105    BuiltIn().set_global_variable("${FFDC_LIST_FILE_PATH}",
106                                  ffdc_list_file_path)
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                         "base_tool_dir_path", "ffdc_list_file_path"]
115    BuiltIn().set_global_variable("${ffdc_prefix}", ffdc_prefix)
116
117    parm_list = BuiltIn().get_variable_value("@{parm_list}")
118
119    plug_in_vars = parm_list + additional_values
120
121    for var_name in plug_in_vars:
122        var_value = BuiltIn().get_variable_value("${" + var_name + "}")
123        var_name = var_name.upper()
124        if var_value is None:
125            var_value = ""
126        OperatingSystem().set_environment_variable(
127            "AUTOBOOT_" + var_name, var_value)
128
129    debug = int(BuiltIn().get_variable_value("${debug}"))
130    if debug:
131        cmd_buf = "printenv | egrep AUTOBOOT_ | sort -u"
132        grp.rpissuing(cmd_buf)
133        sub_proc = subprocess.Popen(cmd_buf, shell=True,
134                                    stdout=subprocess.PIPE,
135                                    stderr=subprocess.STDOUT)
136        out_buf, err_buf = sub_proc.communicate()
137        shell_rc = sub_proc.returncode
138        grp.rprint(out_buf)
139
140###############################################################################
141
142
143###############################################################################
144def create_boot_results_table():
145
146    r"""
147    Create our boot_results_table.
148    """
149
150    # At some point we'll want to change to reading in our boot types from
151    # some external source (e.g. file).
152
153    boot_results.add_row('BMC Power On')
154    boot_results.add_row('BMC Power Off')
155
156###############################################################################
157
158
159###############################################################################
160def update_boot_results_table(boot_type,
161                              boot_status):
162
163    r"""
164    Update our boot_results_table.  This includes:
165    - Updating the record for the given boot_type by incrementing the pass or
166      fail field.
167    - Calling the calc method to have the totals, etc. calculated.
168    - Updating global variables boot_pass/boot_fail.
169
170    Description of arguments:
171    boot_type    The type of boot just done (e.g. "BMC Power On").
172    boot_status  The status of the boot just done.  This should be equal to
173                 either "pass" or "fail" (case-insensitive).
174    """
175
176    boot_results.inc_row_field(boot_type, boot_status.lower())
177    totals_line = boot_results.calc()
178
179    # The caller of obmc_boot_test can pass boot_pass/boot_fail values because
180    # the caller may have already done some testing (e.g. "BMC OOB").  For the
181    # sake of DB logging done by plug-ins, we want to include these in our
182    # overall totals.
183    initial_boot_pass = int(BuiltIn().get_variable_value(
184        "${initial_boot_pass}"))
185    initial_boot_fail = int(BuiltIn().get_variable_value(
186        "${initial_boot_fail}"))
187
188    BuiltIn().set_global_variable("${boot_pass}",
189                                  totals_line['pass'] + initial_boot_pass)
190    BuiltIn().set_global_variable("${boot_fail}",
191                                  totals_line['fail'] + initial_boot_fail)
192
193###############################################################################
194
195
196###############################################################################
197def print_boot_results_table(header_footer="\n"):
198
199    r"""
200    Print the formatted boot_resuls_table to the console.
201    """
202
203    grp.rprint(header_footer)
204    grp.rprint(boot_results.sprint_report())
205    grp.rprint(header_footer)
206
207###############################################################################
208