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