1*** Settings ***
2Documentation  This module provides general keywords for dump.
3
4Library         bmc_ssh_utils.py
5Variables       ../data/variables.py
6
7*** Variables ***
8
9*** Keywords ***
10
11Create User Initiated Dump
12    [Documentation]  Generate user initiated dump and return
13    ...  the dump id number (e.g., "5").  Optionally return EMPTY
14    ...  if out of dump space.
15    [Arguments]   ${check_out_of_space}=${False}
16
17    # Description of Argument(s):
18    # check_out_of_space   If ${False}, a dump will be created and
19    #                      its dump_id will be returned.
20    #                      If ${True}, either the dump_id will be
21    #                      returned, or the value ${EMPTY} will be
22    #                      returned if out of dump space was
23    #                      detected when creating the dump.
24
25    ${resp}=  OpenBMC Get Request  ${REST_DUMP_URI}
26    Run Keyword If  '${resp.status_code}' == '${HTTP_NOT_FOUND}'
27    ...  Set Global Variable  ${REST_DUMP_URI}  /xyz/openbmc_project/dump/
28
29    ${data}=  Create Dictionary  data=@{EMPTY}
30    ${resp}=  OpenBMC Post Request
31    ...  ${REST_DUMP_URI}action/CreateDump  data=${data}  quiet=${1}
32
33    Run Keyword If  '${check_out_of_space}' == '${False}'
34    ...      Run Keyword And Return  Get The Dump Id  ${resp}
35    ...  ELSE
36    ...      Run Keyword And Return  Check For Too Many Dumps  ${resp}
37
38
39Get The Dump Id
40    [Documentation]  Wait for the dump to be created. Return the
41    ...  dump id number (e.g., "5").
42    [Arguments]  ${resp}
43
44    # Description of Argument(s):
45    # resp   Response object from action/Create Dump attempt.
46    #        Example object:
47    #        {
48    #           "data": 5,
49    #           "message": "200 OK",
50    #           "status": "ok"
51    #        },
52    #        The "data" field conveys the id number of the created dump.
53
54    Should Be Equal As Strings  ${resp.status_code}  ${HTTP_OK}
55
56    Run Keyword If  ${resp.json()["data"]} == ${None}
57    ...  Fail  Dump id returned null.
58
59    ${dump_id}=  Set Variable  ${json["data"]}
60
61    Wait Until Keyword Succeeds  3 min  15 sec  Check Dump Existence
62    ...  ${dump_id}
63
64    [Return]  ${dump_id}
65
66
67Check For Too Many Dumps
68    [Documentation]  Return the dump_id number, or return ${EMPTY} if dump
69    ...  creation failed due to too many dumps.
70    [Arguments]  ${resp}
71
72    # Description of Argument(s):
73    # resp   Response object from action/Create Dump attempt.
74    #        Example object if there are too many dumps:
75    #       {
76    #           "data": {
77    #               "description": "xyz.openbmc_project.Dump.Create.Error.QuotaExceeded"
78    #           },
79    #           "message": "Dump not captured due to a cap.",
80    #           "status": "error"
81    #       }
82
83    # If dump was created normally, return the dump_id number.
84    Run Keyword If  '${resp.status_code}' == '${HTTP_OK}'
85    ...  Run Keyword And Return  Get The Dump Id  ${resp}
86
87    ${exception}=  Set Variable  ${resp.json()["message"]}
88    ${at_capacity}=  Set Variable  Dump not captured due to a cap
89    ${too_many_dumps}=  Evaluate  $at_capacity in $exception
90    Printn
91    Rprint Vars   exception  too_many_dumps
92    # If there are too many dumps, return ${EMPTY}, otherwise Fail.
93    ${status}=  Run Keyword If  ${too_many_dumps}  Set Variable  ${EMPTY}
94    ...  ELSE  Fail  msg=${exception}.
95
96    [Return]  ${status}
97
98
99Verify No Dump In Progress
100    [Documentation]  Verify no dump in progress.
101
102    ${dump_progress}  ${stderr}  ${rc}=  BMC Execute Command  ls /tmp
103    Should Not Contain  ${dump_progress}  obmcdump
104
105
106Check Dump Existence
107    [Documentation]  Verify if given dump exist.
108    [Arguments]  ${dump_id}
109
110    # Description of Argument(s):
111    # dump_id  An integer value that identifies a particular dump
112    #          object(e.g. 1, 3, 5).
113
114    ${resp}=  OpenBMC Get Request  ${REST_DUMP_URI}
115    Run Keyword If  '${resp.status_code}' == '${HTTP_NOT_FOUND}'
116    ...  Set Global Variable  ${DUMP_ENTRY_URI}  /xyz/openbmc_project/dump/entry/
117
118    ${resp}=  OpenBMC Get Request  ${DUMP_ENTRY_URI}${dump_id}
119    Should Be Equal As Strings  ${resp.status_code}  ${HTTP_OK}
120
121
122Delete BMC Dump
123    [Documentation]  Deletes a given bmc dump.
124    [Arguments]  ${dump_id}
125
126    # Description of Argument(s):
127    # dump_id  An integer value that identifies a particular dump (e.g. 1, 3).
128
129    ${resp}=  OpenBMC Get Request  ${REST_DUMP_URI}
130    Run Keyword If  '${resp.status_code}' == '${HTTP_NOT_FOUND}'
131    ...  Set Global Variable  ${DUMP_ENTRY_URI}  /xyz/openbmc_project/dump/entry/
132
133    ${args}=  Set Variable   {"data": []}
134    ${resp}=  OpenBMC Post Request
135    ...  ${DUMP_ENTRY_URI}${dump_id}/action/Delete  data=${args}
136
137    Should Be Equal As Strings  ${resp.status_code}  ${HTTP_OK}
138
139Delete All Dumps
140    [Documentation]  Delete all dumps.
141
142    ${resp}=  OpenBMC Get Request  ${REST_DUMP_URI}
143    Run Keyword If  '${resp.status_code}' == '${HTTP_NOT_FOUND}'
144    ...  Set Global Variable  ${DUMP_ENTRY_URI}  /xyz/openbmc_project/dump/entry/
145
146    # Check if dump entries exist, if not return.
147    ${resp}=  OpenBMC Get Request  ${DUMP_ENTRY_URI}list  quiet=${1}
148    Return From Keyword If  ${resp.status_code} == ${HTTP_NOT_FOUND}
149
150    # Get the list of dump entries and delete them all.
151    ${dump_entries}=  Get URL List  ${DUMP_ENTRY_URI}
152    FOR  ${entry}  IN  @{dump_entries}
153        ${dump_id}=  Fetch From Right  ${entry}  /
154        Delete BMC Dump  ${dump_id}
155    END
156
157
158Redfish Delete BMC Dump
159    [Documentation]  Deletes a given BMC dump via Redfish.
160    [Arguments]  ${dump_id}
161
162    # Description of Argument(s):
163    # dump_id  An integer value that identifies a particular dump (e.g. 1, 3).
164
165    Redfish.Delete  /redfish/v1/Managers/${MANAGER_ID}/LogServices/Dump/Entries/${dump_id}
166
167
168Redfish Delete All BMC Dumps
169    [Documentation]  Delete all BMC dumps via Redfish.
170
171    # Check if dump entries exist, if not return.
172    ${resp}=  Redfish.Get  /redfish/v1/Managers/${MANAGER_ID}/LogServices/Dump/Entries
173    Return From Keyword If  ${resp.dict["Members@odata.count"]} == ${0}
174
175    Redfish.Post  /redfish/v1/Managers/${MANAGER_ID}/LogServices/Dump/Actions/LogService.ClearLog
176
177
178Redfish Get All System Dumps
179     [Documentation]  Get the system dump log entries.
180
181     ${resp}=  Redfish.Get  ${REDFISH_SYSTEM_DUMP}
182
183     [Return]  ${resp.dict}
184
185
186Get Redfish BMC Dump Log Entries
187     [Documentation]  Get the BMC dump log entries.
188
189     ${resp}=  Redfish.Get  ${REDFISH_DUMP_URI}
190
191     [Return]  ${resp.dict}
192
193
194Redfish Delete All System Dumps
195    [Documentation]  Delete all system  dumps via Redfish.
196
197    Redfish.Post  /redfish/v1/Systems/system/LogServices/Dump/Actions/LogService.ClearLog
198
199
200Redfish BMC Dump Should Not Exist
201     [Documentation]  Verify that there is no BMC dump at dump URI.
202
203     # Verify no dump exists.
204     ${dump_entries}=  Get Redfish BMC Dump Log Entries
205     Should Be Equal As Integers  0  ${dump_entries['Members@odata.count']}
206
207
208Delete All BMC Dump
209    [Documentation]  Delete all BMC dump entries using "DeleteAll" interface.
210
211    ${resp}=  OpenBMC Get Request  ${REST_DUMP_URI}
212    Run Keyword If  '${resp.status_code}' == '${HTTP_NOT_FOUND}'
213    ...  Set Global Variable  ${REST_DUMP_URI}  /xyz/openbmc_project/dump/
214
215    ${args}=  Set Variable   {"data": []}
216    ${resp}=  Openbmc Post Request  ${REST_DUMP_URI}action/DeleteAll  data=${args}
217    Should Be Equal As Strings  ${resp.status_code}  ${HTTP_OK}
218
219Dump Should Not Exist
220    [Documentation]  Verify that BMC dumps do not exist.
221
222    ${resp}=  OpenBMC Get Request  ${REST_DUMP_URI}
223    Run Keyword If  '${resp.status_code}' == '${HTTP_NOT_FOUND}'
224    ...  Set Global Variable  ${DUMP_ENTRY_URI}  /xyz/openbmc_project/dump/entry/
225
226    ${resp}=  OpenBMC Get Request  ${DUMP_ENTRY_URI}list  quiet=${1}
227    Should Be Equal As Strings  ${resp.status_code}  ${HTTP_NOT_FOUND}
228
229Check Existence Of BMC Dump File
230    [Documentation]  Verify existence of BMC dump file.
231    [Arguments]  ${dump_id}
232
233    # Description of argument(s):
234    # dump_id  BMC dump identifier
235
236    ${dump_check_cmd}=  Set Variable
237    ...  ls /var/lib/phosphor-debug-collector/dumps
238
239    # Output of sample BMC Execute command with '2' as dump id is as follows
240    # ls /var/lib/phosphor-debug-collector/dumps/2
241    # obmcdump_2_XXXXXXXXXX.tar.xz
242    ${file_there}  ${stderr}  ${rc}=  BMC Execute Command
243    ...  ${dump_check_cmd}/${dump_id}
244    Should End With  ${file_there}  tar.xz  msg=BMC dump file not found.
245
246Get Dump Entries
247    [Documentation]  Return dump entries list.
248
249    ${resp}=  OpenBMC Get Request  ${REST_DUMP_URI}
250    Run Keyword If  '${resp.status_code}' == '${HTTP_NOT_FOUND}'
251    ...  Set Global Variable  ${DUMP_ENTRY_URI}  /xyz/openbmc_project/dump/entry/
252
253    ${dump_entries}=  Get URL List  ${DUMP_ENTRY_URI}
254    [Return]  ${dump_entries}
255
256Trigger Core Dump
257    [Documentation]  Trigger core dump.
258
259    # Find the pid of the active ipmid and kill it.
260    ${cmd_buf}=  Catenate  kill -s SEGV $(ps | egrep ' ipmid$' |
261    ...  egrep -v grep | \ cut -c1-6)
262
263    ${cmd_output}  ${stderr}  ${rc}=  BMC Execute Command  ${cmd_buf}
264    Should Be Empty  ${stderr}  msg=BMC execute command error.
265    Should Be Equal As Integers  ${rc}  ${0}
266    ...  msg=BMC execute command return code is not zero.
267
268Initiate BMC Dump Using Redfish And Return Task Id
269     [Documentation]  Initiate BMC dump via Redfish and return its task ID.
270
271     ${payload}=  Create Dictionary  DiagnosticDataType=Manager
272     ${resp}=  Redfish.Post
273     ...  /redfish/v1/Managers/${MANAGER_ID}/LogServices/Dump/Actions/LogService.CollectDiagnosticData
274     ...  body=${payload}  valid_status_codes=[${HTTP_ACCEPTED}]
275
276     # Example of response from above Redfish POST request.
277     # "@odata.id": "/redfish/v1/TaskService/Tasks/0",
278     # "@odata.type": "#Task.v1_4_3.Task",
279     # "Id": "0",
280     # "TaskState": "Running",
281     # "TaskStatus": "OK"
282
283     [Return]  ${resp.dict['Id']}
284
285Create User Initiated BMC Dump Via Redfish
286    [Documentation]  Generate user initiated BMC dump via Redfish and return the dump id number (e.g., "5").
287    [Arguments]  ${skip_dump_completion}=0
288
289    # Description of Argument(s):
290    # skip_dump_completion          If skip_dump_completion is set to 0, this
291    #                               keyword will waiting for BMC dump to
292    #                               complete and returns the dump id.
293    #                               Otherwise, the keyword is skipped after
294    #                               initiating BMC dump and returns dump task id.
295
296    ${payload}=  Create Dictionary  DiagnosticDataType=Manager
297    ${resp}=  Redfish.Post  /redfish/v1/Managers/${MANAGER_ID}/LogServices/Dump/Actions/LogService.CollectDiagnosticData
298    ...  body=${payload}  valid_status_codes=[${HTTP_ACCEPTED}]
299
300    # Example of response from above Redfish POST request.
301    # "@odata.id": "/redfish/v1/TaskService/Tasks/0",
302    # "@odata.type": "#Task.v1_4_3.Task",
303    # "Id": "0",
304    # "TaskState": "Running",
305    # "TaskStatus": "OK"
306
307    Run Keyword If  ${skip_dump_completion} != 0  Return From Keyword  ${resp.dict['Id']}
308    Wait Until Keyword Succeeds  5 min  15 sec  Check Task Completion  ${resp.dict['Id']}
309    ${task_id}=  Set Variable  ${resp.dict['Id']}
310
311    ${task_dict}=  Redfish.Get Properties  /redfish/v1/TaskService/Tasks/${task_id}
312
313    # Example of HttpHeaders field of task details.
314    # "Payload": {
315    #   "HttpHeaders": [
316    #     "Host: <BMC_IP>",
317    #      "Accept-Encoding: identity",
318    #      "Connection: Keep-Alive",
319    #      "Accept: */*",
320    #      "Content-Length: 33",
321    #      "Location: /redfish/v1/Managers/${MANAGER_ID}/LogServices/Dump/Entries/2"]
322    #    ],
323    #    "HttpOperation": "POST",
324    #    "JsonBody": "{\"DiagnosticDataType\":\"Manager\"}",
325    #     "TargetUri": "/redfish/v1/Managers/${MANAGER_ID}/LogServices/Dump/Actions/LogService.CollectDiagnosticData"
326    # }
327
328    [Return]  ${task_dict["Payload"]["HttpHeaders"][-1].split("/")[-1]}
329
330Auto Generate BMC Dump
331    [Documentation]  Auto generate BMC dump.
332
333    ${cmd}=  Catenate  busctl --verbose call xyz.openbmc_project.Dump.Manager
334    ...  /xyz/openbmc_project/dump/bmc xyz.openbmc_project.Dump.Create CreateDump a{sv} 0
335    ${stdout}  ${stderr}  ${rc}=
336    ...  BMC Execute Command  ${cmd}
337    [Return]  ${stdout}  ${stderr}  ${rc}
338
339Get Dump Size
340    [Documentation]  Get dump size.
341    [Arguments]  ${dump_uri}
342
343    # Description of argument(s):
344    # dump_uri        Dump URI
345    #                 (Eg. 	/xyz/openbmc_project/dump/bmc/entry/1).
346
347    # Example of Dump entry.
348    # "data": {
349    #   "CompletedTime": 1616760931,
350    #   "Elapsed": 1616760931,
351    #   "OffloadUri": "",
352    #   "Offloaded": false,
353    #   "Password": "",
354    #   "Size": 3056,
355    #   "SourceDumpId": 117440513,
356    #   "StartTime": 1616760931,
357    #   "Status": "xyz.openbmc_project.Common.Progress.OperationStatus.Completed",
358    #   "VSPString": ""
359    #  },
360
361    Log  ${dump_uri}
362    ${dump_data}=  Redfish.Get Properties  ${dump_uri}
363    [Return]  ${dump_data["data"]["Size"]}
364
365Get Dump ID
366    [Documentation]  Return dump ID.
367    [Arguments]   ${task_id}
368
369    # Description of argument(s):
370    # task_id        Task ID.
371
372    # Example of HttpHeaders field of task details.
373    # "Payload": {
374    #   "HttpHeaders": [
375    #     "Host: <BMC_IP>",
376    #      "Accept-Encoding: identity",
377    #      "Connection: Keep-Alive",
378    #      "Accept: */*",
379    #      "Content-Length: 33",
380    #      "Location: /redfish/v1/Managers/${MANAGER_ID}/LogServices/Dump/Entries/2"]
381    #    ],
382    #    "HttpOperation": "POST",
383    #    "JsonBody": "{\"DiagnosticDataType\":\"Manager\"}",
384    #     "TargetUri":
385    # "/redfish/v1/Managers/${MANAGER_ID}/LogServices/Dump/Actions/LogService.CollectDiagnosticData"
386    # }
387
388    ${task_dict}=  Redfish.Get Properties  /redfish/v1/TaskService/Tasks/${task_id}
389    ${key}  ${value}=  Set Variable  ${task_dict["Payload"]["HttpHeaders"][-1].split(":")}
390    Run Keyword If  '${key}' != 'Location'  Fail
391    [Return]  ${value.strip('/').split('/')[-1]}
392
393Get Task Status
394    [Documentation]  Return task status.
395    [Arguments]   ${task_id}
396
397    # Description of argument(s):
398    # task_id        Task ID.
399
400    ${resp}=  Redfish.Get Properties  /redfish/v1/TaskService/Tasks/${task_id}
401    [Return]  ${resp['TaskState']}
402
403Check Task Completion
404    [Documentation]  Check if the task is complete.
405    [Arguments]   ${task_id}
406
407    # Description of argument(s):
408    # task_id        Task ID.
409
410    ${task_dict}=  Redfish.Get Properties  /redfish/v1/TaskService/Tasks/${task_id}
411    Should Be Equal As Strings  ${task_dict['TaskState']}  Completed
412
413Get Dump ID And Status
414    [Documentation]  Return dump ID and status.
415    [Arguments]   ${task_id}
416
417    # Description of argument(s):
418    # task_id        Task ID.
419
420    Wait Until Keyword Succeeds  10 min  15 sec  Check Task Completion  ${task_id}
421    ${dump_id}=  Get Dump ID  ${task_id}
422    [Return]  ${dump_id}  Completed
423
424
425Create BMC User Dump
426    [Documentation]  Generate user initiated BMC dump via Redfish and return
427    ...  the task instance Id and response object (e.g., "5").
428
429    ${payload}=  Create Dictionary  DiagnosticDataType=Manager
430    ${resp}=  Redfish.Post  /redfish/v1/Managers/${MANAGER_ID}/LogServices/Dump/Actions/LogService.CollectDiagnosticData
431    ...  body=${payload}  valid_status_codes=[${HTTP_ACCEPTED}]
432
433    ${ip_resp}=  Evaluate  json.loads(r'''${resp.text}''')  json
434
435    Return From Keyword  ${ip_resp["Id"]}  ${resp}
436
437
438Wait For Task Completion
439    [Documentation]  Check whether the state of task instance matches any of the
440    ...  expected completion states before maximum number of retries exceeds and
441    ...  exit loop in case completion state is met.
442    [Arguments]  ${task_id}  ${expected_status}  ${retry_max_count}=300
443    ...  ${check_state}=${FALSE}
444
445    # Description of argument(s):
446    # task_id                     the task id for which completion is
447    #                             to be monitored.
448    # expected_status             the task state which is to be considered as the
449    #                             end of task life cycle.
450    # retry_max_count             the maximum number of retry count to wait for
451    #                             task to reach its completion state. Default
452    #                             value of retry_max_count is 300.
453    # check_state                 if set as TRUE, the task state will be
454    #                             monitored whether the task state value is
455    #                             valid throughout task life cycle until
456    #                             expected completion state is reached.
457    #                             Default value of check_state is FALSE.
458
459    FOR  ${retry}  IN RANGE  ${retry_max_count}
460        ${resp}=  Redfish.Get Properties  /redfish/v1/TaskService/Tasks/${task_id}
461        ${current_task_state}=  Set Variable  ${resp["TaskState"]}
462        Rprint Vars  current_task_state
463
464        Run Keyword If  ${check_state} == ${TRUE}  Should Be True
465        ...  '${resp["TaskState"]}' in ${allowed_task_state}
466        ...  msg=Verify task state is valid
467
468        Exit For Loop If
469        ...  '${resp["TaskState"]}' in ${expected_status}
470
471        Sleep  5s
472    END
473
474Get Dump Status In BMC
475    [Documentation]  Get dump status from BMC using busctl method.
476    [Arguments]  ${dump_uri}
477
478    # Description of argument(s):
479    # dump_uri   Dump URI E.g: /xyz/openbmc_project/dump/bmc/entry/7.
480
481    ${cmd}=  Catenate  busctl get-property xyz.openbmc_project.Dump.Manager
482    ...  ${dump_uri} xyz.openbmc_project.Common.Progress Status
483
484    ${stdout}  ${stderr}  ${rc}=  BMC Execute Command  ${cmd}
485    Log  ${stdout}
486    # Example output:
487    # s "xyz.openbmc_project.Common.Progress.OperationStatus.Completed".
488
489    ${status}=  Set Variable  ${stdout.split('.')[-1].strip('"')}
490    [Return]  ${status}
491
492Verify Dump Status In BMC
493    [Documentation]  Verify Dump Status in BMC.
494    [Arguments]  ${dump_uri}  ${expected_dump_status}
495
496    # Description of argument(s):
497    # dump_uri              Dump URI E.g: /xyz/openbmc_project/dump/bmc/entry/7.
498    # expected_dump_status  Expected Dump Status (Completed or Failed etc).
499
500    ${dump_status}=  Get Dump Status In BMC  ${dump_uri}
501    Should Be Equal  ${dump_status}  ${expected_dump_status}
502