1*** Settings ***
2Documentation      Methods to execute commands on BMC and collect
3...                data to a list of FFDC files
4
5Resource           openbmc_ffdc_utils.robot
6Resource           rest_client.robot
7Resource           utils.robot
8Library            SSHLibrary
9
10*** Keywords ***
11
12################################################################
13# Method : Call FFDC Methods                                   #
14#          Execute the user define keywords from the FFDC List #
15#          Unlike any other keywords this will call into the   #
16#          list of keywords defined in the FFDC list at one go #
17################################################################
18
19Call FFDC Methods
20    [Documentation]   Calls into FFDC Keyword index list
21
22    @{entries}=     Get ffdc method index
23    :FOR  ${index}  IN   @{entries}
24    \     Method Call Keyword List   ${index}
25    SSHLibrary.Close All Connections
26
27
28Method Call Keyword List
29    [Documentation]   Iterate the list through keyword index
30    [Arguments]       ${index}
31
32    @{method_list}=      Get ffdc method call   ${index}
33    :FOR  ${method}  IN  @{method_list}
34    \    Execute Keyword Method   ${method[1]}
35
36
37Execute Keyword Method
38    [Documentation]   Calls into BMC method keywords. Don't let one
39    ...               failure skips the remaining. Get whatever data
40    ...               it could gather at worse case scenario.
41    [Arguments]   ${keyword_name}
42
43    Run Keyword And Continue On Failure   ${keyword_name}
44
45
46################################################################
47# Method : BMC FFDC Manifest                                   #
48#          Execute command on BMC and write to ffdc_report.txt #
49################################################################
50
51BMC FFDC Manifest
52    [Documentation]    Get the commands index for the FFDC_BMC_CMD,
53    ...                login to BMC and execute commands.
54    Open Connection And Log In
55
56    @{entries}=     Get ffdc cmd index
57    :FOR  ${index}  IN   @{entries}
58    \     Iterate BMC Command List Pairs   ${index}
59
60
61Iterate BMC Command List Pairs
62    [Documentation]    Feed in key pair list from dictionary to execute
63    [Arguments]        ${key_index}
64
65    @{cmd_list}=      Get ffdc bmc cmd    ${key_index}
66    Set Suite Variable   ${ENTRY_INDEX}   ${key_index}
67    :FOR  ${cmd}  IN  @{cmd_list}
68    \    Execute Command and Write FFDC    ${cmd[0]}  ${cmd[1]}
69
70
71Execute Command and Write FFDC
72    [Documentation]    Execute command on BMC or OS and write to ffdc
73    ...                By default to ffdc_report.txt file else to
74    ...                specified file path.
75    [Arguments]        ${key_index}
76    ...                ${cmd}
77    ...                ${logpath}=${FFDC_FILE_PATH}
78
79    Run Keyword If   '${logpath}' == '${FFDC_FILE_PATH}'
80    ...    Write Cmd Output to FFDC File   ${key_index}  ${cmd}
81
82    ${stdout}  ${stderr}=
83    ...   Execute Command    ${cmd}   return_stderr=True
84
85    # Write stdout on success and stderr/stdout to the file on failure.
86    Run Keyword If  $stderr == '${EMPTY}'
87    ...    Write Data to File  ${stdout}${\n}  ${logpath}
88    ...  ELSE  Write Data to File
89    ...    ERROR output:${\n}${stderr}${\n}Output:${\n}${stdout}${\n}
90    ...    ${logpath}
91
92
93################################################################
94# Method : BMC FFDC Files                                      #
95#          Execute command on BMC and write to individual file #
96#          based on the file name pre-defined in the list      #
97################################################################
98
99BMC FFDC Files
100    [Documentation]    Get the command list and iterate
101    Open Connection And Log In
102    @{entries}=     Get ffdc file index
103    :FOR  ${index}  IN   @{entries}
104    \     Create File and Write Data   ${index}
105
106
107Create File and Write Data
108    [Documentation]    Create files to current FFDC log directory,
109    ...                executes command and write to corresponding
110    ...                file name in the current FFDC directory.
111    [Arguments]        ${key_index}
112
113    @{cmd_list}=      Get ffdc bmc file   ${key_index}
114    :FOR  ${cmd}  IN  @{cmd_list}
115    \   ${logpath}=  Catenate  SEPARATOR=   ${LOG_PREFIX}   ${cmd[0]}.txt
116    \   Execute Command and Write FFDC  ${cmd[0]}  ${cmd[1]}   ${logpath}
117
118
119################################################################
120# Method : Log Test Case Status                                #
121#          Creates test result history footprint for reference #
122################################################################
123
124Log Test Case Status
125    [Documentation]  Test case execution result history.
126    ...  Create once and append to this file
127    ...  logs/test_history.txt
128    ...  Format   Date:Test suite:Test case:Status
129    ...  20160909214053719992:Test Warmreset:Test WarmReset via REST:FAIL
130    Create Directory   ${FFDC_LOG_PATH}
131
132    ${exist}=   Run Keyword and Return Status
133    ...   OperatingSystem.File Should Exist   ${TEST_HISTORY}
134
135    Run Keyword If  '${exist}' == '${False}'
136    ...   Create File  ${TEST_HISTORY}
137
138    ${cur_time}=      Get Current Time Stamp
139
140    Append To File    ${TEST_HISTORY}
141    ...   ${cur_time}:${SUITE_NAME}:${TEST_NAME}:${TEST_STATUS}${\n}
142
143
144Log FFDC Get Requests
145    [Documentation]    Create file in current FFDC log directory.
146    ...                Do openbmc get request and write to
147    ...                corresponding file name.
148    ...                JSON pretty print for logging to file.
149    [Arguments]        ${key_index}
150
151    @{cmd_list}=  Get ffdc get request  ${key_index}
152    :FOR  ${cmd}  IN  @{cmd_list}
153    \   ${logpath}=  Catenate  SEPARATOR=  ${LOG_PREFIX}  ${cmd[0]}.txt
154    \   ${resp}=  OpenBMC Get Request  ${cmd[1]}  quiet=${1}
155    \   ${status}=    Run Keyword and Return Status
156    ...   Should Be Equal As Strings    ${resp.status_code}    ${HTTP_OK}
157    \   Run Keyword If   '${status}' == '${False}'  Continue For Loop
158    \   ${jsondata}=  to json  ${resp.content}    pretty_print=True
159    \   Write Data to File  ${\n}${jsondata}${\n}  ${logpath}
160
161
162BMC FFDC Get Requests
163    [Documentation]    Get the command list and iterate
164    Open Connection And Log In
165    @{entries}=  Get ffdc get request index
166    :FOR  ${index}  IN  @{entries}
167    \   Log FFDC Get Requests   ${index}
168
169
170Log OS ALL DISTROS FFDC
171    [Documentation]    Create file in current FFDC log directory.
172    ...                Executes OS command and write to
173    ...                corresponding file name.
174    [Arguments]        ${key_index}
175
176    @{cmd_list}=  get ffdc os all distros call  ${key_index}
177    :FOR  ${cmd}  IN  @{cmd_list}
178    \   ${logpath}=  Catenate  SEPARATOR=  ${LOG_PREFIX}  ${cmd[0]}.txt
179    \   Execute Command and Write FFDC  ${cmd[0]}  ${cmd[1]}   ${logpath}
180
181
182Log OS SPECIFIC DISTRO FFDC
183    [Documentation]    Create file in current FFDC log directory.
184    ...                Executes OS command and write to
185    ...                corresponding file name.
186    [Arguments]        ${key_index}  ${linux_distro}
187
188    @{cmd_list}=  get ffdc os distro call  ${key_index}  ${linux_distro}
189    :FOR  ${cmd}  IN  @{cmd_list}
190    \   ${logpath}=  Catenate  SEPARATOR=  ${LOG_PREFIX}  ${cmd[0]}.txt
191    \   Execute Command and Write FFDC  ${cmd[0]}  ${cmd[1]}   ${logpath}
192
193
194
195OS FFDC Files
196    [Documentation]    Get the command list and iterate
197    [Arguments]  ${OS_HOST}=${OS_HOST}  ${OS_USERNAME}=${OS_USERNAME}
198    ...   ${OS_PASSWORD}=${OS_PASSWORD}
199
200    Return From Keyword If  '${OS_HOST}' == '${EMPTY}'
201    ...   No OS Host Provided
202
203    # If can't ping, return
204    ${rc}=  Run Keyword and Return Status  Ping Host  ${OS_HOST}
205    Return From Keyword If  '${rc}' == '${False}'
206    ...   Could not ping OS
207
208    Open Connection And Log In  host=${OS_HOST}  username=${OS_USERNAME}
209    ...   password=${OS_PASSWORD}
210
211    ${output}  ${stderr}  ${rc}=  Execute Command  uptime  return_stderr=True
212    ...   return_rc=True
213
214    # If the return code returned by "Execute Command" is non-zero, return
215    Return From Keyword If  '${rc}' != '${0}'
216    ...   Could not connect to OS
217
218    @{entries}=  Get ffdc os all distros index
219    :FOR  ${index}  IN  @{entries}
220    \   Log OS ALL DISTROS FFDC  ${index}
221
222    ${linux_distro}=  Execute Command
223    ...   . /etc/os-release; echo $ID
224    ...   return_stdout=True  return_stderr=False  return_rc=False
225
226    Return From Keyword If
227    ...  '${linux_distro}' == '${EMPTY}' or '${linux_distro}' == 'None'
228    ...  Could not determine Linux Distribution
229
230    @{entries}=  Get ffdc os distro index  ${linux_distro}
231    :FOR  ${index}  IN  @{entries}
232    \   Log OS SPECIFIC DISTRO FFDC  ${index}  ${linux_distro}
233
234
235##############################################################################
236SCP Coredump Files
237    [Documentation]  Copy core dump file from BMC to local system.
238    # Check if core dump exist in the /tmp
239    Open Connection And Log In
240    ${core_files}=  Execute Command  ls /tmp/core_*
241    @{core_list} =  Split String    ${core_files}
242    # Copy the core files
243    Open Connection for SCP
244    :FOR  ${index}  IN  @{core_list}
245    \  scp.Get File  ${index}  ${LOG_PREFIX}${index.lstrip("/tmp/")}
246    # Remove the file from remote to avoid re-copying on next FFDC call
247    \  Execute Command On BMC  rm ${index}
248
249