xref: /openbmc/openbmc-test-automation/lib/openbmc_ffdc.py (revision 4c1b99f1a44c4fe3dd126a8bcf5dddc1622fc113)
1#!/usr/bin/env python
2
3r"""
4This module is the python counterpart to openbmc_ffdc.robot..
5"""
6
7import os
8
9import gen_robot_print as grp
10import gen_valid as gv
11
12from robot.libraries.BuiltIn import BuiltIn
13
14
15###############################################################################
16def ffdc(ffdc_dir_path=None,
17         ffdc_prefix=None):
18
19    r"""
20    Gather First Failure Data Capture (FFDC).
21
22    This includes:
23    - Set global FFDC_TIME.
24    - Create FFDC work space directory.
25    - Write test info details.
26    - Call BMC methods to write/collect FFDC data.
27
28    Description of arguments:
29    ffdc_dir_path  The dir path where FFDC data should be put.
30    ffdc_prefix    The prefix to be given to each FFDC file name generated.
31    """
32
33    grp.rprint_timen("Collecting FFDC.")
34
35    # Note: Several subordinate functions like 'Get Test Dir and Name' and
36    # 'Header Message' expect global variable FFDC_TIME to be set.
37    cmd_buf = ["Get Current Time Stamp"]
38    grp.rpissuing_keyword(cmd_buf)
39    FFDC_TIME = BuiltIn().run_keyword(*cmd_buf)
40    BuiltIn().set_global_variable("${FFDC_TIME}", FFDC_TIME)
41
42    # Get default values for arguments.
43    ffdc_dir_path, ffdc_prefix = set_ffdc_defaults(ffdc_dir_path, ffdc_prefix)
44    grp.rprint_var(ffdc_dir_path)
45    grp.rprint_var(ffdc_prefix)
46
47    # LOG_PREFIX is used by subordinate functions.
48    LOG_PREFIX = ffdc_dir_path + ffdc_prefix
49    BuiltIn().set_global_variable("${LOG_PREFIX}", LOG_PREFIX)
50
51    cmd_buf = ["Create Directory", ffdc_dir_path]
52    grp.rpissuing_keyword(cmd_buf)
53    status, output = BuiltIn().run_keyword_and_ignore_error(*cmd_buf)
54    if status != "PASS":
55        error_message = grp.sprint_error_report("Create Directory failed with the following error:\n" + output)
56        BuiltIn().fail(error_message)
57
58    # FFDC_FILE_PATH is used by Header Message.
59    FFDC_FILE_PATH = ffdc_dir_path + ffdc_prefix + "BMC_general.txt"
60    BuiltIn().set_global_variable("${FFDC_FILE_PATH}", FFDC_FILE_PATH)
61
62    cmd_buf = ["Header Message"]
63    grp.rpissuing_keyword(cmd_buf)
64    BuiltIn().run_keyword(*cmd_buf)
65
66    cmd_buf = ["Call FFDC Methods"]
67    grp.rpissuing_keyword(cmd_buf)
68    BuiltIn().run_keyword(*cmd_buf)
69
70    grp.rprint_timen("Finished collecting FFDC.")
71
72###############################################################################
73
74
75###############################################################################
76def set_ffdc_defaults(ffdc_dir_path=None,
77                      ffdc_prefix=None):
78
79    r"""
80    Set a default value for ffdc_dir_path and ffdc_prefix if they don't
81    already have values.  Return both values.
82
83    Description of arguments:
84    ffdc_dir_path  The dir path where FFDC data should be put.
85    ffdc_prefix    The prefix to be given to each FFDC file name generated.
86
87    NOTE: If global variable ffdc_dir_path_style is set to ${1}, this function
88    will create default values in a newer way.  Otherwise, its behavior
89    will remain unchanged.
90    """
91
92    ffdc_dir_path_style = BuiltIn().get_variable_value("${ffdc_dir_path_style}")
93
94    if ffdc_dir_path is None:
95        if ffdc_dir_path_style:
96            try:
97                ffdc_dir_path = os.environ['FFDC_DIR_PATH']
98            except KeyError:
99                ffdc_dir_path = os.path.dirname(
100                    BuiltIn().get_variable_value("${LOG_FILE}")) + "/"
101        else:
102            FFDC_LOG_PATH = BuiltIn().get_variable_value("${FFDC_LOG_PATH}")
103            if FFDC_LOG_PATH is None:
104                FFDC_LOG_PATH = ""
105            if FFDC_LOG_PATH == "":
106                FFDC_LOG_PATH = os.path.dirname(
107                    BuiltIn().get_variable_value("${LOG_FILE}")) + "/"
108            error_message = gv.svalid_value(FFDC_LOG_PATH,
109                                            var_name="FFDC_LOG_PATH")
110            if error_message != "":
111                error_message = grp.sprint_error_report(error_message)
112                BuiltIn().fail(error_message)
113            FFDC_LOG_PATH = os.path.normpath(FFDC_LOG_PATH) + os.sep
114
115            cmd_buf = ["Get Test Dir and Name"]
116            grp.rpissuing_keyword(cmd_buf)
117            suitename, testname = BuiltIn().run_keyword(*cmd_buf)
118
119            ffdc_dir_path = FFDC_LOG_PATH + suitename + "/" + testname + "/"
120
121    # Add trailing slash.
122    ffdc_dir_path = os.path.normpath(ffdc_dir_path) + os.sep
123
124    if ffdc_prefix is None:
125        FFDC_TIME = BuiltIn().get_variable_value("${FFDC_TIME}")
126        if ffdc_prefix is None:
127            if ffdc_dir_path_style:
128                OPENBMC_HOST = BuiltIn().get_variable_value("${OPENBMC_HOST}")
129                ffdc_prefix = OPENBMC_HOST + "." + FFDC_TIME[2:8] + "." +\
130                    FFDC_TIME[8:14] + "."
131            else:
132                ffdc_prefix = FFDC_TIME + "_"
133
134    return ffdc_dir_path, ffdc_prefix
135
136###############################################################################
137