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    if openbmc_nickname == "":
81        openbmc_host = BuiltIn().get_variable_value("${openbmc_host}")
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    # For each program parameter, set the corresponding AUTOBOOT_ environment
97    # variable value.  Also, set an AUTOBOOT_ environment variable for every
98    # element in additional_values.
99    additional_values = ["boot_type_desc", "boot_success", "boot_pass",
100                         "boot_fail", "test_really_running", "program_pid",
101                         "master_pid", "ffdc_prefix", "ffdc_dir_path"]
102    BuiltIn().set_global_variable("${ffdc_prefix}", ffdc_prefix)
103
104    parm_list = BuiltIn().get_variable_value("@{parm_list}")
105
106    plug_in_vars = parm_list + additional_values
107
108    for var_name in plug_in_vars:
109        var_value = BuiltIn().get_variable_value("${" + var_name + "}")
110        var_name = var_name.upper()
111        if var_value is None:
112            var_value = ""
113        OperatingSystem().set_environment_variable(
114            "AUTOBOOT_" + var_name, var_value)
115
116    debug = int(BuiltIn().get_variable_value("${debug}"))
117    if debug:
118        cmd_buf = "printenv | egrep AUTOBOOT_ | sort -u"
119        grp.rpissuing(cmd_buf)
120        sub_proc = subprocess.Popen(cmd_buf, shell=True,
121                                    stdout=subprocess.PIPE,
122                                    stderr=subprocess.STDOUT)
123        out_buf, err_buf = sub_proc.communicate()
124        shell_rc = sub_proc.returncode
125        grp.rprint(out_buf)
126
127###############################################################################
128
129
130###############################################################################
131def create_boot_results_table():
132
133    r"""
134    Create our boot_results_table.
135    """
136
137    # At some point we'll want to change to reading in our boot types from
138    # some external source (e.g. file).
139
140    boot_results.add_row('BMC Power On')
141    boot_results.add_row('BMC Power Off')
142
143###############################################################################
144
145
146###############################################################################
147def update_boot_results_table(boot_type,
148                              boot_status):
149
150    r"""
151    Update our boot_results_table.  This includes:
152    - Updating the record for the given boot_type by incrementing the pass or
153      fail field.
154    - Calling the calc method to have the totals, etc. calculated.
155    - Updating global variables boot_pass/boot_fail.
156
157    Description of arguments:
158    boot_type    The type of boot just done (e.g. "BMC Power On").
159    boot_status  The status of the boot just done.  This should be equal to
160                 either "pass" or "fail" (case-insensitive).
161    """
162
163    boot_results.inc_row_field(boot_type, boot_status.lower())
164    totals_line = boot_results.calc()
165
166    # The caller of obmc_boot_test can pass boot_pass/boot_fail values because
167    # the caller may have already done some testing (e.g. "BMC OOB").  For the
168    # sake of DB logging done by plug-ins, we want to include these in our
169    # overall totals.
170    initial_boot_pass = int(BuiltIn().get_variable_value(
171        "${initial_boot_pass}"))
172    initial_boot_fail = int(BuiltIn().get_variable_value(
173        "${initial_boot_fail}"))
174
175    BuiltIn().set_global_variable("${boot_pass}",
176                                  totals_line['pass'] + initial_boot_pass)
177    BuiltIn().set_global_variable("${boot_fail}",
178                                  totals_line['fail'] + initial_boot_fail)
179
180###############################################################################
181
182
183###############################################################################
184def print_boot_results_table(header_footer="\n"):
185
186    r"""
187    Print the formatted boot_resuls_table to the console.
188    """
189
190    grp.rprint(header_footer)
191    grp.rprint(boot_results.sprint_report())
192    grp.rprint(header_footer)
193
194###############################################################################
195