1*** Settings ***
2Documentation  This module provides general keywords for dump.
3
4Library         bmc_ssh_utils.py
5
6*** Variables ***
7
8*** Keywords ***
9
10Create User Initiated Dump
11    [Documentation]  Generate user initiated dump and return
12    ...  the dump id number (e.g., "5").  Optionally return EMPTY
13    ...  if out of dump space.
14    [Arguments]   ${check_out_of_space}=${False}
15
16    # Description of Argument(s):
17    # check_out_of_space   If ${False}, a dump will be created and
18    #                      its dump_id will be returned.
19    #                      If ${True}, either the dump_id will be
20    #                      returned, or the value ${EMPTY} will be
21    #                      returned if out of dump space was
22    #                      detected when creating the dump.
23
24    ${data}=  Create Dictionary  data=@{EMPTY}
25    ${resp}=  OpenBMC Post Request
26    ...  ${DUMP_URI}action/CreateDump  data=${data}  quiet=${1}
27
28    Run Keyword If  '${check_out_of_space}' == '${False}'
29    ...  Run Keyword And Return  Get The Dump Id  ${resp}
30    ...  ELSE   Run Keyword And Return  Check For Too Many Dumps  ${resp}
31
32
33Get The Dump Id
34    [Documentation]  Wait for the dump to be created. Return the
35    ...  dump id number (e.g., "5").
36    [Arguments]  ${resp}
37
38    # Description of Argument(s):
39    # resp   Response object from action/Create Dump attempt.
40    #        Example object:
41    #        {
42    #           "data": 5,
43    #           "message": "200 OK",
44    #           "status": "ok"
45    #        },
46    #        The "data" field conveys the id number of the created dump.
47
48    Should Be Equal As Strings  ${resp.status_code}  ${HTTP_OK}
49    ${json}=  To JSON  ${resp.content}
50
51    Run Keyword If  ${json["data"]} == ${None}
52    ...  Fail  Dump id returned null.
53
54    ${dump_id}=  Set Variable  ${json["data"]}
55
56    Wait Until Keyword Succeeds  3 min  15 sec  Check Dump Existence
57    ...  ${dump_id}
58
59    [Return]  ${dump_id}
60
61
62Check For Too Many Dumps
63    [Documentation]  Return the dump_id number, or return ${EMPTY} if dump
64    ...  creation failed due to too many dumps.
65    [Arguments]  ${resp}
66
67    # Description of Argument(s):
68    # resp   Response object from action/Create Dump attempt.
69    #        Example object if there are too many dumps:
70    #        {
71    #           "data": {
72    #              "description": "Internal Server Error",
73    #              "exception": "'Dump not captured due to a cap.'",
74    #              "traceback": [
75    #              "Traceback (most recent call last):",
76    #                ...
77    #              "DBusException: Create.Error.QuotaExceeded"
78    #                           ]
79    #              },
80    #           "message": "500 Internal Server Error",
81    #           "status": "error"
82    #        }
83
84    # If dump was created normally, return the dump_id number.
85    Run Keyword If  '${resp.status_code}' == '${HTTP_OK}'
86    ...  Run Keyword And Return  Get The Dump Id  ${resp}
87
88    ${exception}=  Set Variable  ${resp.json()['data']['exception']}
89    ${at_capacity}=  Set Variable  Dump not captured due to a cap
90    ${too_many_dumps}=  Evaluate  $at_capacity in $exception
91    Printn
92    Rprint Vars   exception  too_many_dumps
93    # If there are too many dumps, return ${EMPTY}, otherwise Fail.
94    ${status}=  Run Keyword If  ${too_many_dumps}  Set Variable  ${EMPTY}
95    ...  ELSE  Fail  msg=${exception}.
96
97    [Return]  ${status}
98
99
100Verify No Dump In Progress
101    [Documentation]  Verify no dump in progress.
102
103    ${dump_progress}  ${stderr}  ${rc}=  BMC Execute Command  ls /tmp
104    Should Not Contain  ${dump_progress}  obmcdump
105
106
107Check Dump Existence
108    [Documentation]  Verify if given dump exist.
109    [Arguments]  ${dump_id}
110
111    # Description of Argument(s):
112    # dump_id  An integer value that identifies a particular dump
113    #          object(e.g. 1, 3, 5).
114
115    ${resp}=  OpenBMC Get Request  ${DUMP_ENTRY_URI}${dump_id}
116    Should Be Equal As Strings  ${resp.status_code}  ${HTTP_OK}
117
118
119Delete BMC Dump
120    [Documentation]  Deletes a given bmc dump.
121    [Arguments]  ${dump_id}
122
123    # Description of Argument(s):
124    # dump_id  An integer value that identifies a particular dump (e.g. 1, 3).
125
126    ${data}=  Create Dictionary  data=@{EMPTY}
127    ${resp}=  OpenBMC Post Request
128    ...  ${DUMP_ENTRY_URI}${dump_id}/action/Delete  data=${data}
129
130    Should Be Equal As Strings  ${resp.status_code}  ${HTTP_OK}
131
132Delete All Dumps
133    [Documentation]  Delete all dumps.
134
135    # Check if dump entries exist, if not return.
136    ${resp}=  OpenBMC Get Request  ${DUMP_ENTRY_URI}list  quiet=${1}
137    Return From Keyword If  ${resp.status_code} == ${HTTP_NOT_FOUND}
138
139    # Get the list of dump entries and delete them all.
140    ${dump_entries}=  Get URL List  ${DUMP_ENTRY_URI}
141    :FOR  ${entry}  IN  @{dump_entries}
142    \  ${dump_id}=  Fetch From Right  ${entry}  /
143    \  Delete BMC Dump  ${dump_id}
144
145
146Delete All BMC Dump
147    [Documentation]  Delete all BMC dump entries using "DeleteAll" interface.
148
149    ${data}=  Create Dictionary  data=@{EMPTY}
150    ${resp}=  Openbmc Post Request  ${DUMP_URI}action/DeleteAll  data=${data}
151    Should Be Equal As Strings  ${resp.status_code}  ${HTTP_OK}
152
153Dump Should Not Exist
154    [Documentation]  Verify that BMC dumps do not exist.
155
156    ${resp}=  OpenBMC Get Request  ${DUMP_ENTRY_URI}list  quiet=${1}
157    Should Be Equal As Strings  ${resp.status_code}  ${HTTP_NOT_FOUND}
158
159Check Existence of BMC Dump file
160    [Documentation]  Verify existence of BMC dump file.
161    [Arguments]  ${dump_id}
162
163    # Description of argument(s):
164    # dump_id  BMC dump identifier
165
166    ${dump_check_cmd}=  Set Variable
167    ...  ls /var/lib/phosphor-debug-collector/dumps
168
169    # Output of sample BMC Execute command with '2' as dump id is as follows
170    # ls /var/lib/phosphor-debug-collector/dumps/2
171    # obmcdump_2_XXXXXXXXXX.tar.xz
172    ${file_there}  ${stderr}  ${rc}=  BMC Execute Command
173    ...  ${dump_check_cmd}/${dump_id}
174    Should End With  ${file_there}  tar.xz  msg=BMC dump file not found.
175
176Get Dump Entries
177    [Documentation]  Return dump entries list.
178
179    ${dump_entries}=  Get URL List  ${DUMP_ENTRY_URI}
180    [Return]  ${dump_entries}
181
182
183Trigger Core Dump
184    [Documentation]  Trigger core dump.
185
186    # Find the pid of the active ipmid and kill it.
187    ${cmd_buf}=  Catenate  kill -s SEGV $(ps | egrep ' ipmid$' |
188    ...  egrep -v grep | \ cut -c1-6)
189
190    ${cmd_output}  ${stderr}  ${rc}=  BMC Execute Command  ${cmd_buf}
191    Should Be Empty  ${stderr}  msg=BMC execute command error.
192    Should Be Equal As Integers  ${rc}  ${0}
193    ...  msg=BMC execute command return code is not zero.
194