blob: a19d8fe42135520d285548f0ce8d5d5493adc760 [file] [log] [blame]
Chris Austenb29d2e82016-06-07 12:25:35 -05001*** Settings ***
2Library Collections
3Library String
4Library RequestsLibrary.RequestsKeywords
5Library OperatingSystem
George Keishingeaa73b72018-07-30 09:30:16 -05006Resource resource.txt
7Library disable_warning_urllib.py
Michael Walshad66c952018-10-04 15:12:47 -05008Library utils.py
George Keishingeaa73b72018-07-30 09:30:16 -05009Resource rest_response_code.robot
Chris Austenb29d2e82016-06-07 12:25:35 -050010
11*** Variables ***
Michael Walsha6723f22016-11-22 11:12:01 -060012# Assign default value to QUIET for programs which may not define it.
13${QUIET} ${0}
Chris Austenb29d2e82016-06-07 12:25:35 -050014
15*** Keywords ***
16OpenBMC Get Request
manasarm604b8cd2018-01-29 12:14:20 +053017 [Documentation] Do REST GET request and return the result.
18 # Example result data:
19 # Response code:200, Content:{
20 # "data": [
21 # "/xyz/openbmc_project/state/host0",
22 # "/xyz/openbmc_project/state/chassis0",
23 # "/xyz/openbmc_project/state/bmc0"
24 # ],
25 # "message": "200 OK",
26 # "status": "ok"
27 # }
George Keishing41c44cf2017-11-15 08:02:59 -060028 [Arguments] ${uri} ${timeout}=30 ${quiet}=${QUIET} &{kwargs}
manasarm604b8cd2018-01-29 12:14:20 +053029 # Description of argument(s):
30 # uri The URI to establish connection with
31 # (e.g. '/xyz/openbmc_project/software/').
32 # timeout Timeout in seconds to establish connection with URI.
33 # quiet If enabled, turns off logging to console.
34 # kwargs Any additional arguments to be passed directly to the
35 # Get Request call. For example, the caller might
36 # set kwargs as follows:
37 # ${kwargs}= Create Dictionary allow_redirect=${True}.
Michael Walshf00edee2016-12-09 14:10:26 -060038
39 Initialize OpenBMC ${timeout} quiet=${quiet}
Chris Austenb29d2e82016-06-07 12:25:35 -050040 ${base_uri}= Catenate SEPARATOR= ${DBUS_PREFIX} ${uri}
Michael Walsha6723f22016-11-22 11:12:01 -060041 Run Keyword If '${quiet}' == '${0}' Log Request method=Get
42 ... base_uri=${base_uri} args=&{kwargs}
Michael Walsha6723f22016-11-22 11:12:01 -060043 ${ret}= Get Request openbmc ${base_uri} &{kwargs} timeout=${timeout}
44 Run Keyword If '${quiet}' == '${0}' Log Response ${ret}
George Keishing08540c02017-07-19 09:42:50 -050045 Delete All Sessions
Chris Austenb29d2e82016-06-07 12:25:35 -050046 [Return] ${ret}
47
48OpenBMC Post Request
manasarm604b8cd2018-01-29 12:14:20 +053049 [Documentation] Do REST POST request and return the result.
50 # Example result data:
51 # <Response [200]>
Michael Walsha6723f22016-11-22 11:12:01 -060052 [Arguments] ${uri} ${timeout}=10 ${quiet}=${QUIET} &{kwargs}
manasarm604b8cd2018-01-29 12:14:20 +053053 # Description of argument(s):
54 # uri The URI to establish connection with
55 # (e.g. '/xyz/openbmc_project/software/').
56 # timeout Timeout in seconds to establish connection with URI.
57 # quiet If enabled, turns off logging to console.
58 # kwargs Any additional arguments to be passed directly to the
59 # Post Request call. For example, the caller might
60 # set kwargs as follows:
61 # ${kwargs}= Create Dictionary allow_redirect=${True}.
Michael Walsha6723f22016-11-22 11:12:01 -060062
Michael Walshf00edee2016-12-09 14:10:26 -060063 Initialize OpenBMC ${timeout} quiet=${quiet}
Chris Austenb29d2e82016-06-07 12:25:35 -050064 ${base_uri}= Catenate SEPARATOR= ${DBUS_PREFIX} ${uri}
65 ${headers}= Create Dictionary Content-Type=application/json
66 set to dictionary ${kwargs} headers ${headers}
Michael Walsha6723f22016-11-22 11:12:01 -060067 Run Keyword If '${quiet}' == '${0}' Log Request method=Post
68 ... base_uri=${base_uri} args=&{kwargs}
Michael Walsha6723f22016-11-22 11:12:01 -060069 ${ret}= Post Request openbmc ${base_uri} &{kwargs} timeout=${timeout}
70 Run Keyword If '${quiet}' == '${0}' Log Response ${ret}
George Keishing08540c02017-07-19 09:42:50 -050071 Delete All Sessions
Chris Austenb29d2e82016-06-07 12:25:35 -050072 [Return] ${ret}
73
74OpenBMC Put Request
manasarm604b8cd2018-01-29 12:14:20 +053075 [Documentation] Do REST PUT request on the resource identified by the URI.
Rahul Maheshwari79c12942016-10-17 09:39:17 -050076 [Arguments] ${uri} ${timeout}=10 &{kwargs}
manasarm604b8cd2018-01-29 12:14:20 +053077 # Description of argument(s):
78 # uri The URI to establish connection with
79 # (e.g. '/xyz/openbmc_project/software/').
80 # timeout Timeout in seconds to establish connection with URI.
81 # kwargs Arguments passed to the REST call.
82 # kwargs Any additional arguments to be passed directly to the
83 # Put Request call. For example, the caller might
84 # set kwargs as follows:
85 # ${kwargs}= Create Dictionary allow_redirect=${True}.
Michael Walshf00edee2016-12-09 14:10:26 -060086
87 Initialize OpenBMC ${timeout}
Chris Austenb29d2e82016-06-07 12:25:35 -050088 ${base_uri}= Catenate SEPARATOR= ${DBUS_PREFIX} ${uri}
89 ${headers}= Create Dictionary Content-Type=application/json
90 set to dictionary ${kwargs} headers ${headers}
91 Log Request method=Put base_uri=${base_uri} args=&{kwargs}
Michael Walsha6723f22016-11-22 11:12:01 -060092 ${ret}= Put Request openbmc ${base_uri} &{kwargs} timeout=${timeout}
Chris Austenb29d2e82016-06-07 12:25:35 -050093 Log Response ${ret}
George Keishing08540c02017-07-19 09:42:50 -050094 Delete All Sessions
Chris Austenb29d2e82016-06-07 12:25:35 -050095 [Return] ${ret}
96
97OpenBMC Delete Request
manasarm604b8cd2018-01-29 12:14:20 +053098 [Documentation] Do REST request to delete the resource identified by the
99 ... URI.
Rahul Maheshwari79c12942016-10-17 09:39:17 -0500100 [Arguments] ${uri} ${timeout}=10 &{kwargs}
manasarm604b8cd2018-01-29 12:14:20 +0530101 # Description of argument(s):
102 # uri The URI to establish connection with
103 # (e.g. '/xyz/openbmc_project/software/').
104 # timeout Timeout in seconds to establish connection with URI.
105 # kwargs Any additional arguments to be passed directly to the
106 # Delete Request call. For example, the caller might
107 # set kwargs as follows:
108 # ${kwargs}= Create Dictionary allow_redirect=${True}.
Michael Walshf00edee2016-12-09 14:10:26 -0600109
110 Initialize OpenBMC ${timeout}
Chris Austenb29d2e82016-06-07 12:25:35 -0500111 ${base_uri}= Catenate SEPARATOR= ${DBUS_PREFIX} ${uri}
112 Log Request method=Delete base_uri=${base_uri} args=&{kwargs}
George Keishing0e7c3a02017-04-17 05:01:14 -0500113 ${ret}= Delete Request openbmc ${base_uri} &{kwargs} timeout=${timeout}
Chris Austenb29d2e82016-06-07 12:25:35 -0500114 Log Response ${ret}
George Keishing08540c02017-07-19 09:42:50 -0500115 Delete All Sessions
Chris Austenb29d2e82016-06-07 12:25:35 -0500116 [Return] ${ret}
117
118Initialize OpenBMC
manasarm604b8cd2018-01-29 12:14:20 +0530119 [Documentation] Do a REST login connection within specified time.
George Keishing1cdc6dd2017-04-24 15:31:03 -0500120 [Arguments] ${timeout}=20 ${quiet}=${1}
George Keishingaf4ab322018-10-17 12:42:52 -0500121 ... ${REST_USERNAME}=${REST_USERNAME}
122 ... ${REST_PASSWORD}=${REST_PASSWORD}
Michael Walshf00edee2016-12-09 14:10:26 -0600123
George Keishing1cdc6dd2017-04-24 15:31:03 -0500124 # Description of argument(s):
125 # timeout REST login attempt time out.
Gunnar Mills28e403b2017-10-25 16:16:38 -0500126 # quiet Suppress console log if set.
George Keishing1cdc6dd2017-04-24 15:31:03 -0500127
128 # TODO : Task to revert this changes openbmc/openbmc-test-automation#532
129 # This will retry at 20 second interval.
130 Wait Until Keyword Succeeds 40 sec 20 sec
131 ... Post Login Request ${timeout} ${quiet}
George Keishingaf4ab322018-10-17 12:42:52 -0500132 ... ${REST_USERNAME} ${REST_PASSWORD}
George Keishing1cdc6dd2017-04-24 15:31:03 -0500133
134Post Login Request
manasarm604b8cd2018-01-29 12:14:20 +0530135 [Documentation] Do REST login request.
George Keishing1cdc6dd2017-04-24 15:31:03 -0500136 [Arguments] ${timeout}=20 ${quiet}=${1}
George Keishingaf4ab322018-10-17 12:42:52 -0500137 ... ${REST_USERNAME}=${REST_USERNAME}
138 ... ${REST_PASSWORD}=${REST_PASSWORD}
George Keishing1cdc6dd2017-04-24 15:31:03 -0500139
140 # Description of argument(s):
141 # timeout REST login attempt time out.
Gunnar Mills28e403b2017-10-25 16:16:38 -0500142 # quiet Suppress console log if set.
George Keishing1cdc6dd2017-04-24 15:31:03 -0500143
144 Create Session openbmc ${AUTH_URI} timeout=${timeout} max_retries=3
Michael Walsha6723f22016-11-22 11:12:01 -0600145 ${headers}= Create Dictionary Content-Type=application/json
George Keishingaf4ab322018-10-17 12:42:52 -0500146 @{credentials}= Create List ${REST_USERNAME} ${REST_PASSWORD}
Michael Walsha6723f22016-11-22 11:12:01 -0600147 ${data}= create dictionary data=@{credentials}
Michael Walshf00edee2016-12-09 14:10:26 -0600148 ${status} ${resp}= Run Keyword And Ignore Error Post Request openbmc
149 ... /login data=${data} headers=${headers}
150
151 Should Be Equal ${status} PASS msg=${resp}
152 Should Be Equal As Strings ${resp.status_code} ${HTTP_OK}
Chris Austenb29d2e82016-06-07 12:25:35 -0500153
George Keishinge8225522017-03-31 09:21:13 -0500154Log Out OpenBMC
manasarm604b8cd2018-01-29 12:14:20 +0530155 [Documentation] Log out of the openbmc REST session.
George Keishinge8225522017-03-31 09:21:13 -0500156
157 ${headers}= Create Dictionary Content-Type=application/json
158 ${data}= Create dictionary data=@{EMPTY}
159
160 # If there is no active sesion it will throw the following exception
161 # "Non-existing index or alias 'openbmc'"
162 ${resp}= Post Request openbmc
163 ... /logout data=${data} headers=${headers}
164
165 Should Be Equal As Strings ${resp.status_code} ${HTTP_OK}
166 ... msg=${resp}
167
Chris Austenb29d2e82016-06-07 12:25:35 -0500168Log Request
manasarm604b8cd2018-01-29 12:14:20 +0530169 [Documentation] Log the specific REST URI, method name on the console.
Chris Austenb29d2e82016-06-07 12:25:35 -0500170 [Arguments] &{kwargs}
Michael Walsha6723f22016-11-22 11:12:01 -0600171 ${msg}= Catenate SEPARATOR= URI: ${AUTH_URI} ${kwargs["base_uri"]}
172 ... , method: ${kwargs["method"]} , args: ${kwargs["args"]}
Chris Austenb29d2e82016-06-07 12:25:35 -0500173 Logging ${msg} console=True
174
175Log Response
manasarm604b8cd2018-01-29 12:14:20 +0530176 [Documentation] Log the response code on the console.
Chris Austenb29d2e82016-06-07 12:25:35 -0500177 [Arguments] ${resp}
Michael Walsha6723f22016-11-22 11:12:01 -0600178 ${msg}= Catenate SEPARATOR= Response code: ${resp.status_code}
179 ... , Content: ${resp.content}
Chris Austenb29d2e82016-06-07 12:25:35 -0500180 Logging ${msg} console=True
181
182Logging
manasarm604b8cd2018-01-29 12:14:20 +0530183 [Documentation] Log the specified message on the console.
Chris Austenb29d2e82016-06-07 12:25:35 -0500184 [Arguments] ${msg} ${console}=default False
185 Log ${msg} console=True
186
187Read Attribute
manasarm604b8cd2018-01-29 12:14:20 +0530188 [Documentation] Retrieve attribute value from URI and return result.
189 # Example result data for the attribute 'FieldModeEnabled' in
190 # "/xyz/openbmc_project/software/attr/" :
191 # 0
Gunnar Mills38032802016-12-12 13:43:40 -0600192 [Arguments] ${uri} ${attr} ${timeout}=10 ${quiet}=${QUIET}
manasarmc505e382018-02-05 14:05:48 +0530193 ... ${expected_value}=${EMPTY}
manasarm604b8cd2018-01-29 12:14:20 +0530194 # Description of argument(s):
195 # uri URI of the object that the attribute lives on
196 # (e.g. '/xyz/openbmc_project/software/').
197 # attr Name of the attribute (e.g. 'FieldModeEnabled').
198 # timeout Timeout for the REST call.
199 # quiet If enabled, turns off logging to console.
manasarmc505e382018-02-05 14:05:48 +0530200 # expected_value If this argument is not empty, the retrieved value
201 # must match this value.
manasarm604b8cd2018-01-29 12:14:20 +0530202
Michael Walsha6723f22016-11-22 11:12:01 -0600203 ${resp}= OpenBMC Get Request ${uri}/attr/${attr} timeout=${timeout}
204 ... quiet=${quiet}
Michael Walsh9cd61932017-01-17 16:11:02 -0600205 Should Be Equal As Strings ${resp.status_code} ${HTTP_OK}
Chris Austenb29d2e82016-06-07 12:25:35 -0500206 ${content}= To Json ${resp.content}
manasarmc505e382018-02-05 14:05:48 +0530207 Run Keyword If '${expected_value}' != '${EMPTY}'
208 ... Should Be Equal As Strings ${expected_value} ${content["data"]}
Gunnar Millsc9ea9362016-12-13 16:21:13 -0600209 [Return] ${content["data"]}
Chris Austenb29d2e82016-06-07 12:25:35 -0500210
Charles Paul Hoferf45e5ee2017-10-04 12:15:48 -0500211
Chris Austenb29d2e82016-06-07 12:25:35 -0500212Write Attribute
Charles Paul Hoferf45e5ee2017-10-04 12:15:48 -0500213 [Documentation] Write a D-Bus attribute with REST.
214 [Arguments] ${uri} ${attr} ${timeout}=10 ${verify}=${FALSE}
215 ... ${expected_value}=${EMPTY} &{kwargs}
216
217 # Description of argument(s):
218 # uri URI of the object that the attribute lives on
219 # (e.g. '/xyz/openbmc_project/software/').
220 # attr Name of the attribute (e.g. 'FieldModeEnabled').
221 # timeout Timeout for the REST call.
222 # verify If set to ${TRUE}, the attribute will be read back to
223 # ensure that its value is set to ${verify_attr}.
224 # expected_value Only used if verify is set to ${TRUE}. The value that
225 # ${attr} should be set to. This defaults to
226 # ${kwargs['data']. There are cases where the caller
227 # expects some other value in which case this value can
228 # be explicitly specified.
229 # kwargs Arguments passed to the REST call. This should always
230 # contain the value to set the property to at the 'data'
231 # key (e.g. data={"data": 1}).
232
233 ${base_uri}= Catenate SEPARATOR= ${DBUS_PREFIX} ${uri}
234 ${resp}= Openbmc Put Request ${base_uri}/attr/${attr}
Michael Walsha6723f22016-11-22 11:12:01 -0600235 ... timeout=${timeout} &{kwargs}
Charles Paul Hoferf45e5ee2017-10-04 12:15:48 -0500236 Should Be Equal As Strings ${resp.status_code} ${HTTP_OK}
237
238 # Verify the attribute was set correctly if the caller requested it.
239 Return From Keyword If ${verify} == ${FALSE}
240
241 ${expected_value}= Set Variable If '${expected_value}' == '${EMPTY}'
Charles Paul Hofer00401e72017-10-31 11:42:30 -0500242 ... ${kwargs['data']['data']} ${expected_value}
Charles Paul Hoferf45e5ee2017-10-04 12:15:48 -0500243 ${value}= Read Attribute ${uri} ${attr}
244 Should Be Equal ${value} ${expected_value}
245
Chris Austenb29d2e82016-06-07 12:25:35 -0500246Read Properties
manasarm604b8cd2018-01-29 12:14:20 +0530247 [Documentation] Read data part of the URI object and return result.
248 # Example result data:
249 # [u'/xyz/openbmc_project/software/cf7bf9d5',
250 # u'/xyz/openbmc_project/software/5ecb8b2c',
251 # u'/xyz/openbmc_project/software/active',
252 # u'/xyz/openbmc_project/software/functional']
George Keishing335f5362017-06-30 15:11:20 -0500253 [Arguments] ${uri} ${timeout}=10 ${quiet}=${QUIET}
manasarm604b8cd2018-01-29 12:14:20 +0530254 # Description of argument(s):
255 # uri URI of the object
256 # (e.g. '/xyz/openbmc_project/software/').
257 # timeout Timeout for the REST call.
258 # quiet If enabled, turns off logging to console.
259
George Keishing335f5362017-06-30 15:11:20 -0500260 ${resp}= OpenBMC Get Request ${uri} timeout=${timeout} quiet=${quiet}
261 Should Be Equal As Strings ${resp.status_code} ${HTTP_OK}
Michael Walshad66c952018-10-04 15:12:47 -0500262 ${content}= To Json Ordered ${resp.content}
263
George Keishing335f5362017-06-30 15:11:20 -0500264 [Return] ${content["data"]}
Chris Austenb29d2e82016-06-07 12:25:35 -0500265
266Call Method
manasarm604b8cd2018-01-29 12:14:20 +0530267 [Documentation] Invoke the specific REST service method.
Gunnar Mills38032802016-12-12 13:43:40 -0600268 [Arguments] ${uri} ${method} ${timeout}=10 ${quiet}=${QUIET} &{kwargs}
manasarm604b8cd2018-01-29 12:14:20 +0530269 # Description of arguments:
270 # uri The URI to establish connection with
271 # (e.g. '/xyz/openbmc_project/software/').
272 # timeout Timeout in seconds to establish connection with URI.
273 # quiet If enabled, turns off logging to console.
274 # kwargs Arguments passed to the REST call.
Michael Walsha6723f22016-11-22 11:12:01 -0600275
Chris Austenb29d2e82016-06-07 12:25:35 -0500276 ${base_uri}= Catenate SEPARATOR= ${DBUS_PREFIX} ${uri}
Michael Walsha6723f22016-11-22 11:12:01 -0600277 ${resp}= OpenBmc Post Request ${base_uri}/action/${method}
278 ... timeout=${timeout} quiet=${quiet} &{kwargs}
Gunnar Millsc9ea9362016-12-13 16:21:13 -0600279 [Return] ${resp}
Saqib Khanbb8b63f2017-05-24 10:58:01 -0500280
281Upload Image To BMC
manasarm604b8cd2018-01-29 12:14:20 +0530282 [Documentation] Upload image to BMC device using REST POST operation.
George Keishingad9a8802017-08-08 12:41:48 -0500283 [Arguments] ${uri} ${timeout}=10 ${quiet}=${1} &{kwargs}
Saqib Khanbb8b63f2017-05-24 10:58:01 -0500284
285 # Description of argument(s):
286 # uri URI for uploading image via REST e.g. "/upload/image".
287 # timeout Time allocated for the REST command to return status
288 # (specified in Robot Framework Time Format e.g. "3 mins").
manasarm604b8cd2018-01-29 12:14:20 +0530289 # quiet If enabled, turns off logging to console.
Saqib Khanbb8b63f2017-05-24 10:58:01 -0500290 # kwargs A dictionary keys/values to be passed directly to
291 # Post Request.
292
293 Initialize OpenBMC ${timeout} quiet=${quiet}
294 ${base_uri}= Catenate SEPARATOR= ${DBUS_PREFIX} ${uri}
295 ${headers}= Create Dictionary Content-Type=application/octet-stream
296 ... Accept=application/octet-stream
297 Set To Dictionary ${kwargs} headers ${headers}
298 Run Keyword If '${quiet}' == '${0}' Log Request method=Post
299 ... base_uri=${base_uri} args=&{kwargs}
300 ${ret}= Post Request openbmc ${base_uri} &{kwargs} timeout=${timeout}
301 Run Keyword If '${quiet}' == '${0}' Log Response ${ret}
302 Should Be Equal As Strings ${ret.status_code} ${HTTP_OK}
George Keishing08540c02017-07-19 09:42:50 -0500303 Delete All Sessions