xref: /openbmc/openbmc-test-automation/lib/code_update_utils.robot (revision 95686eff54a2d9341e5c1e52e15f949b2ed7e5bc)
1*** Settings ***
2Documentation  BMC and PNOR update utilities keywords.
3
4Library     code_update_utils.py
5Library     OperatingSystem
6Library     String
7Library     utilities.py
8Variables   ../data/variables.py
9Resource    boot_utils.robot
10Resource    rest_client.robot
11Resource    openbmc_ffdc.robot
12Resource    oem/ibm/serial_console_client.robot
13
14*** Keywords ***
15
16Get Software Objects
17    [Documentation]  Get the host software objects and return as a list.
18    [Arguments]  ${version_type}=${VERSION_PURPOSE_HOST}
19
20    # Description of argument(s):
21    # version_type  Either BMC or host version purpose.
22    #               By default host version purpose string.
23    #  (e.g. "xyz.openbmc_project.Software.Version.VersionPurpose.BMC"
24    #        "xyz.openbmc_project.Software.Version.VersionPurpose.Host").
25
26    # Example:
27    # "data": [
28    #      "/xyz/openbmc_project/software/f3b29aa8",
29    #      "/xyz/openbmc_project/software/e49bc78e",
30    # ],
31    # Iterate the list and return the host object name path list.
32
33    ${host_list}=  Create List
34    ${sw_list}=  Read Properties  ${SOFTWARE_VERSION_URI}
35
36    :FOR  ${index}  IN  @{sw_list}
37    \  ${attr_purpose}=  Read Software Attribute  ${index}  Purpose
38    \  Continue For Loop If  '${attr_purpose}' != '${version_type}'
39    \  Append To List  ${host_list}  ${index}
40
41    [Return]  ${host_list}
42
43
44Read Software Attribute
45    [Documentation]  Return software attribute data.
46    [Arguments]  ${software_object}  ${attribute_name}
47
48    # Description of argument(s):
49    # software_object   Software object path.
50    #                   (e.g. "/xyz/openbmc_project/software/f3b29aa8").
51    # attribute_name    Software object attribute name.
52
53    ${resp}=  OpenBMC Get Request  ${software_object}/attr/${attribute_name}
54    ...  quiet=${1}
55    Return From Keyword If  ${resp.status_code} != ${HTTP_OK}
56    ${content}=  To JSON  ${resp.content}
57    [Return]  ${content["data"]}
58
59
60Get Software Objects Id
61    [Documentation]  Get the software objects id and return as a list.
62    [Arguments]  ${version_type}=${VERSION_PURPOSE_HOST}
63
64    # Description of argument(s):
65    # version_type  Either BMC or host version purpose.
66    #               By default host version purpose string.
67    #              (e.g. "xyz.openbmc_project.Software.Version.VersionPurpose.BMC"
68    #               "xyz.openbmc_project.Software.Version.VersionPurpose.Host").
69
70    ${sw_id_list}=  Create List
71    ${sw_list}=  Get Software Objects  ${version_type}
72
73    :FOR  ${index}  IN  @{sw_list}
74    \  Append To List  ${sw_id_list}  ${index.rsplit('/', 1)[1]}
75
76    [Return]  ${sw_id_list}
77
78
79Get Host Software Property
80    [Documentation]  Return a dictionary of host software properties.
81    [Arguments]  ${host_object}
82
83    # Description of argument(s):
84    # host_object  Host software object path.
85    #             (e.g. "/xyz/openbmc_project/software/f3b29aa8").
86
87    ${sw_attributes}=  Read Properties  ${host_object}
88    [return]  ${sw_attributes}
89
90Get Host Software Objects Details
91    [Documentation]  Return software object details as a list of dictionaries.
92    [Arguments]  ${quiet}=${QUIET}
93
94    ${software}=  Create List
95
96    ${pnor_details}=  Get Software Objects  ${VERSION_PURPOSE_HOST}
97    :FOR  ${pnor}  IN  @{pnor_details}
98    \  ${resp}=  OpenBMC Get Request  ${pnor}  quiet=${1}
99    \  ${json}=  To JSON  ${resp.content}
100    \  Append To List  ${software}  ${json["data"]}
101
102    [Return]  ${software}
103
104Set Host Software Property
105    [Documentation]  Set the host software properties of a given object.
106    [Arguments]  ${host_object}  ${sw_attribute}  ${data}
107
108    # Description of argument(s):
109    # host_object   Host software object name.
110    # sw_attribute  Host software attribute name.
111    #               (e.g. "Activation", "Priority", "RequestedActivation" etc).
112    # data          Value to be written.
113
114    ${args}=  Create Dictionary  data=${data}
115    Write Attribute  ${host_object}  ${sw_attribute}  data=${args}
116
117
118Set Property To Invalid Value And Verify No Change
119    [Documentation]  Attempt to set a property and check that the value didn't
120    ...              change.
121    [Arguments]  ${property}  ${version_type}
122
123    # Description of argument(s):
124    # property      The property to attempt to set.
125    # version_type  Either BMC or host version purpose.
126    #               By default host version purpose string.
127    #  (e.g. "xyz.openbmc_project.Software.Version.VersionPurpose.BMC"
128    #        "xyz.openbmc_project.Software.Version.VersionPurpose.Host").
129
130    ${software_objects}=  Get Software Objects  version_type=${version_type}
131    ${prev_properties}=  Get Host Software Property  @{software_objects}[0]
132    Run Keyword And Expect Error  500 != 200
133    ...  Set Host Software Property  @{software_objects}[0]  ${property}  foo
134    ${cur_properties}=  Get Host Software Property  @{software_objects}[0]
135    Should Be Equal As Strings  &{prev_properties}[${property}]
136    ...  &{cur_properties}[${property}]
137
138
139Set Priority To Invalid Value And Expect Error
140    [Documentation]  Set the priority of an image to an invalid value and
141    ...              check that an error was returned.
142    [Arguments]  ${version_type}  ${priority}
143
144    # Description of argument(s):
145    # version_type  Either BMC or host version purpose.
146    #               (e.g. "xyz.openbmc_project.Software.Version.VersionPurpose.BMC"
147    #                     "xyz.openbmc_project.Software.Version.VersionPurpose.Host").
148    # priority      The priority value to set. Should be an integer outside of
149    #               the range of 0 through 255.
150
151    ${images}=  Get Software Objects  version_type=${version_type}
152    ${num_images}=  Get Length  ${images}
153    Should Be True  0 < ${num_images}
154
155    Run Keyword And Expect Error  403 != 200
156    ...  Set Host Software Property  @{images}[0]  Priority  ${priority}
157
158
159Upload And Activate Image
160    [Documentation]  Upload an image to the BMC and activate it with REST.
161    [Arguments]  ${image_file_path}  ${wait}=${1}  ${skip_if_active}=false
162
163    # Description of argument(s):
164    # image_file_path     The path to the image tarball to upload and activate.
165    # wait                Indicates that this keyword should wait for host or
166    #                     BMC activation is completed.
167    # skip_if_active      If set to true, will skip the code update if this
168    #                     image is already on the BMC.
169
170    OperatingSystem.File Should Exist  ${image_file_path}
171    ${image_version}=  Get Version Tar  ${image_file_path}
172
173    ${image_data}=  OperatingSystem.Get Binary File  ${image_file_path}
174    Upload Image To BMC  /upload/image  data=${image_data}
175    ${ret}  ${version_id}=  Verify Image Upload  ${image_version}
176    Should Be True  ${ret}
177
178    # Verify the image is 'READY' to be activated or if it's already active,
179    # set priority to 0 and reboot the BMC.
180    ${software_state}=  Read Properties  ${SOFTWARE_VERSION_URI}${version_id}
181    ${activation}=  Set Variable  &{software_state}[Activation]
182    Run Keyword If
183    ...  '${skip_if_active}' == 'true' and '${activation}' == '${ACTIVE}'
184    ...  Switch To Active Image And Pass  ${SOFTWARE_VERSION_URI}${version_id}
185    Should Be Equal As Strings  &{software_state}[Activation]  ${READY}
186
187    # Request the image to be activated.
188    ${args}=  Create Dictionary  data=${REQUESTED_ACTIVE}
189    Write Attribute  ${SOFTWARE_VERSION_URI}${version_id}
190    ...  RequestedActivation  data=${args}
191    ${software_state}=  Read Properties  ${SOFTWARE_VERSION_URI}${version_id}
192    Should Be Equal As Strings  &{software_state}[RequestedActivation]
193    ...  ${REQUESTED_ACTIVE}
194
195    # Does caller want to wait for activation to complete?
196    Return From Keyword If  '${wait}' == '${0}'  ${version_id}
197
198    # Verify code update was successful and Activation state is Active.
199    Wait For Activation State Change  ${version_id}  ${ACTIVATING}
200    ${software_state}=  Read Properties  ${SOFTWARE_VERSION_URI}${version_id}
201    Should Be Equal As Strings  &{software_state}[Activation]  ${ACTIVE}
202
203    [Return]  ${version_id}
204
205
206Attempt To Reboot BMC During Image Activation
207    [Documentation]  Attempt to reboot the BMC while an image is activating and
208    ...              check that the BMC ignores the reboot command and finishes
209    ...              activation.
210    [Arguments]  ${image_file_path}
211
212    # Description of argument(s):
213    # image_file_path  Path to the image to update to.
214
215    # Attempt to reboot during activation.
216    ${version_id}=  Upload And Activate Image  ${image_file_path}
217    ...  wait=${0}
218    BMC Execute Command  /sbin/reboot
219
220    # Wait for activation to finish.
221    Wait For Activation State Change  ${version_id}  ${ACTIVATING}
222    ${software_state}=  Read Properties  ${SOFTWARE_VERSION_URI}${version_id}
223    Should Be Equal As Strings  &{software_state}[Activation]  ${ACTIVE}
224
225    # Verify the image priority is 0.
226    ${priority}=  Read Software Attribute  ${SOFTWARE_VERSION_URI}${version_id}
227    ...  Priority
228    Should Be Equal  ${priority}  ${0}
229
230
231Switch To Active Image And Pass
232    [Documentation]  Make the given active image the image running on the BMC
233    ...              and pass the test.
234    [Arguments]  ${software_object}
235
236    # Description of argument(s):
237    # software_object  Software object path.
238    #                  (e.g. "/xyz/openbmc_project/software/f3b29aa8").
239
240    Set Host Software Property  ${software_object}  Priority  ${0}
241    OBMC Reboot (off)
242    Pass Execution  ${software_object} was already on the BMC.
243
244
245Activate Image And Verify No Duplicate Priorities
246    [Documentation]  Upload an image, and then check that no images have the
247    ...              same priority.
248    [Arguments]  ${image_file_path}  ${image_purpose}
249
250    # Description of argument(s):
251    # image_file_path  The path to the image to upload.
252    # image_purpose    The purpose in the image's MANIFEST file.
253
254    Upload And Activate Image  ${image_file_path}
255    Verify No Duplicate Image Priorities  ${image_purpose}
256
257
258Set Same Priority For Multiple Images
259    [Documentation]  Find two images, set the priorities to be the same, and
260    ...              verify that the priorities are not the same.
261    [Arguments]  ${version_purpose}
262
263    # Description of argument(s):
264    # version_purpose  Either BMC or host version purpose.
265    #                  (e.g. "xyz.openbmc_project.Software.Version.VersionPurpose.BMC"
266    #                        "xyz.openbmc_project.Software.Version.VersionPurpose.Host").
267
268    # Make sure we have more than two images.
269    ${software_objects}=  Get Software Objects  version_type=${version_purpose}
270    ${num_images}=  Get Length  ${software_objects}
271    Should Be True  1 < ${num_images}
272    ...  msg=Only found one image on the BMC with purpose ${version_purpose}.
273
274    # Set the priority of the second image to the priority of the first.
275    ${properties}=  Get Host Software Property  @{software_objects}[0]
276    Set Host Software Property  @{software_objects}[1]  Priority
277    ...  &{properties}[Priority]
278    Verify No Duplicate Image Priorities  ${version_purpose}
279
280    # Set the priority of the first image back to what it was before
281    Set Host Software Property  @{software_objects}[0]  Priority
282    ...  &{properties}[Priority]
283
284
285Delete Software Object
286    [Documentation]  Deletes an image from the BMC.
287    [Arguments]  ${software_object}
288
289    # Description of argument(s):
290    # software_object  The URI to the software image to delete.
291
292    ${arglist}=  Create List
293    ${args}=  Create Dictionary  data=${arglist}
294    ${resp}=  OpenBMC Post Request  ${software_object}/action/delete
295    ...  data=${args}
296    Should Be Equal As Strings  ${resp.status_code}  ${HTTP_OK}
297
298
299Delete Image And Verify
300    [Documentation]  Delete an image from the BMC and verify that it was
301    ...              removed from software and the /tmp/images directory.
302    [Arguments]  ${software_object}  ${version_type}
303
304    # Description of argument(s):
305    # software_object        The URI of the software object to delete.
306    # version_type  The type of the software object, e.g.
307    #               xyz.openbmc_project.Software.Version.VersionPurpose.Host
308    #               or xyz.openbmc_project.Software.Version.VersionPurpose.BMC.
309
310    Log To Console  Deleteing ${software_object}
311
312    # Delete the image.
313    Delete Software Object  ${software_object}
314    # TODO: If/when we don't have to delete twice anymore, take this out
315    Run Keyword And Ignore Error  Delete Software Object  ${software_object}
316
317    # Verify that it's gone from software.
318    ${software_objects}=  Get Software Objects  version_type=${version_type}
319    Should Not Contain  ${software_objects}  ${software_object}
320
321    # Check that there is no file in the /tmp/images directory.
322    ${image_id}=  Fetch From Right  ${software_object}  /
323    BMC Execute Command
324    ...  [ ! -d "/tmp/images/${image_id}" ]
325
326
327Delete All Non Running BMC Images
328    [Documentation]  Delete all BMC images that are not running on the BMC.
329
330    @{datalist}=  Create List
331    ${data}=  Create Dictionary  data=@{datalist}
332    Call Method  ${SOFTWARE_VERSION_URI}  DeleteAll  data=${data}
333
334
335Check Error And Collect FFDC
336    [Documentation]  Collect FFDC if error log exists.
337
338    ${status}=  Run Keyword And Return Status  Error Logs Should Not Exist
339    Run Keyword If  '${status}' == 'False'  FFDC
340    Delete Error Logs
341
342
343Verify Running BMC Image
344    [Documentation]  Verify that the version on the BMC is the same as the
345    ...              version in the given image.
346    [Arguments]  ${image_file_path}
347
348    # Description of argument(s):
349    # image_file_path   Path to the BMC image tarball.
350
351    ${tar_version}=  Get Version Tar  ${image_file_path}
352    ${bmc_version}=  Get BMC Version
353    ${bmc_version}=  Remove String  ${bmc_version}  "
354    Should Be Equal  ${tar_version}  ${bmc_version}
355
356
357Verify Running Host Image
358    [Documentation]  Verify that the version of the PNOR image that is on the
359    ...              BMC is the same as the one in the given image.
360    [Arguments]  ${image_file_path}
361
362    # Description of argument(s):
363    # image_file_path   Path to the PNOR image tarball.
364
365    ${tar_version}=  Get Version Tar  ${image_file_path}
366    ${pnor_version}=  Get PNOR Version
367    Should Be Equal  ${tar_version}  ${pnor_version}
368
369Reset Network Interface During Code Update
370    [Documentation]  Disable and re-enable the network while doing code update.
371    [Arguments]  ${image_file_path}  ${reboot}
372
373    # Resetting the network will be done via the serial console.
374    #
375    # Description of argument(s):
376    # image_file_path   Path to the image file to update to.
377    # reboot            If set to true, will reboot the BMC after the code
378    #                   update is finished.
379
380    ${version_id}=  Upload And Activate Image  ${image_file_path}  wait=${0}
381    Reset Network Interface
382
383    # Verify code update was successful and 'Activation' state is 'Active'.
384    Wait For Activation State Change  ${version_id}  ${ACTIVATING}
385    ${software_state}=  Read Properties  ${SOFTWARE_VERSION_URI}${version_id}
386    Should Be Equal As Strings  &{software_state}[Activation]  ${ACTIVE}
387
388    Run Keyword If  '${reboot}'  OBMC Reboot (off)  stack_mode=normal
389
390
391Reset Network Interface
392    [Documentation]  Turn the ethernet network interface off and then on again
393    ...              through the serial console.
394
395    Import Resource  ${CURDIR}/oem/ibm/serial_console_client.robot
396    Set Library Search Order  SSHLibrary  Telnet
397    Execute Command On Serial Console  ifconfig eth0 down
398    Sleep  30s
399    Execute Command On Serial Console  ifconfig eth0 up
400
401
402Get Least Value Priority Image
403    [Documentation]  Find the least value in "Priority" attribute and return.
404    [Arguments]  ${version_type}
405
406    # Description of argument(s):
407    # version_type  Either BMC or host version purpose.
408
409    ${priority_value_list}=  Create List
410    ${sw_list}=  Get Software Objects  version_type=${version_type}
411
412    :FOR  ${index}  IN  @{sw_list}
413    \  ${priority_value}=
414    ...  Read Software Attribute  ${index}  Priority
415    \  Append To List  ${priority_value_list}  ${priority_value}
416
417    ${min_value}=  Min List Value  ${priority_value_list}
418
419    [Return]  ${min_value}
420