xref: /openbmc/openbmc-test-automation/lib/code_update_utils.robot (revision b7842a5947488476ad689c16262e1fcee1e40530)
1*** Settings ***
2Documentation  BMC and PNOR update utilities keywords.
3
4Library     code_update_utils.py
5Library     OperatingSystem
6Library     String
7Variables   ../data/variables.py
8Resource    rest_client.robot
9Resource    openbmc_ffdc.robot
10
11*** Keywords ***
12
13Get Software Objects
14    [Documentation]  Get the host software objects and return as a list.
15    [Arguments]  ${version_type}=${VERSION_PURPOSE_HOST}
16
17    # Description of argument(s):
18    # version_type  Either BMC or host version purpose.
19    #               By default host version purpose string.
20    #  (e.g. "xyz.openbmc_project.Software.Version.VersionPurpose.BMC"
21    #        "xyz.openbmc_project.Software.Version.VersionPurpose.Host").
22
23    # Example:
24    # "data": [
25    #      "/xyz/openbmc_project/software/f3b29aa8",
26    #      "/xyz/openbmc_project/software/e49bc78e",
27    # ],
28    # Iterate the list and return the host object name path list.
29
30    ${host_list}=  Create List
31    ${sw_list}=  Read Properties  ${SOFTWARE_VERSION_URI}
32
33    :FOR  ${index}  IN  @{sw_list}
34    \  ${attr_purpose}=  Read Software Attribute  ${index}  Purpose
35    \  Continue For Loop If  '${attr_purpose}' != '${version_type}'
36    \  Append To List  ${host_list}  ${index}
37
38    [Return]  ${host_list}
39
40
41Read Software Attribute
42    [Documentation]  Return software attribute data.
43    [Arguments]  ${software_object}  ${attribute_name}
44
45    # Description of argument(s):
46    # software_object   Software object path.
47    #                   (e.g. "/xyz/openbmc_project/software/f3b29aa8").
48    # attribute_name    Software object attribute name.
49
50    ${resp}=  OpenBMC Get Request  ${software_object}/attr/${attribute_name}
51    ...  quiet=${1}
52    Return From Keyword If  ${resp.status_code} != ${HTTP_OK}
53    ${content}=  To JSON  ${resp.content}
54    [Return]  ${content["data"]}
55
56
57Get Host Software Property
58    [Documentation]  Return a dictionary of host software properties.
59    [Arguments]  ${host_object}
60
61    # Description of argument(s):
62    # host_object  Host software object path.
63    #             (e.g. "/xyz/openbmc_project/software/f3b29aa8").
64
65    ${sw_attributes}=  Read Properties  ${host_object}
66    [return]  ${sw_attributes}
67
68Get Host Software Objects Details
69    [Documentation]  Return software object details as a list of dictionaries.
70    [Arguments]  ${quiet}=${QUIET}
71
72    ${software}=  Create List
73
74    ${pnor_details}=  Get Software Objects  ${VERSION_PURPOSE_HOST}
75    :FOR  ${pnor}  IN  @{pnor_details}
76    \  ${resp}=  OpenBMC Get Request  ${pnor}  quiet=${1}
77    \  ${json}=  To JSON  ${resp.content}
78    \  Append To List  ${software}  ${json["data"]}
79
80    [Return]  ${software}
81
82Set Host Software Property
83    [Documentation]  Set the host software properties of a given object.
84    [Arguments]  ${host_object}  ${sw_attribute}  ${data}
85
86    # Description of argument(s):
87    # host_object   Host software object name.
88    # sw_attribute  Host software attribute name.
89    #               (e.g. "Activation", "Priority", "RequestedActivation" etc).
90    # data          Value to be written.
91
92    ${args}=  Create Dictionary  data=${data}
93    Write Attribute  ${host_object}  ${sw_attribute}  data=${args}
94
95
96Set Property To Invalid Value And Verify No Change
97    [Documentation]  Attempt to set a property and check that the value didn't
98    ...              change.
99    [Arguments]  ${property}  ${version_type}
100
101    # Description of argument(s):
102    # property      The property to attempt to set.
103    # version_type  Either BMC or host version purpose.
104    #               By default host version purpose string.
105    #  (e.g. "xyz.openbmc_project.Software.Version.VersionPurpose.BMC"
106    #        "xyz.openbmc_project.Software.Version.VersionPurpose.Host").
107
108    ${software_objects}=  Get Software Objects  version_type=${version_type}
109    ${prev_properties}=  Get Host Software Property  @{software_objects}[0]
110    Run Keyword And Expect Error  500 != 200
111    ...  Set Host Software Property  @{software_objects}[0]  ${property}  foo
112    ${cur_properties}=  Get Host Software Property  @{software_objects}[0]
113    Should Be Equal As Strings  &{prev_properties}[${property}]
114    ...  &{cur_properties}[${property}]
115
116
117Set Priority To Invalid Value And Expect Error
118    [Documentation]  Set the priority of an image to an invalid value and
119    ...              check that an error was returned.
120    [Arguments]  ${version_type}  ${priority}
121
122    # Description of argument(s):
123    # version_type  Either BMC or host version purpose.
124    #               (e.g. "xyz.openbmc_project.Software.Version.VersionPurpose.BMC"
125    #                     "xyz.openbmc_project.Software.Version.VersionPurpose.Host").
126    # priority      The priority value to set. Should be an integer outside of
127    #               the range of 0 through 255.
128
129    ${images}=  Get Software Objects  version_type=${version_type}
130    ${num_images}=  Get Length  ${images}
131    Should Be True  0 < ${num_images}
132
133    Run Keyword And Expect Error  403 != 200
134    ...  Set Host Software Property  @{images}[0]  Priority  ${priority}
135
136
137Upload And Activate Image
138    [Documentation]  Upload an image to the BMC and activate it with REST.
139    [Arguments]  ${image_file_path}  ${wait}=${1}  ${skip_if_active}=false
140
141    # Description of argument(s):
142    # image_file_path     The path to the image tarball to upload and activate.
143    # wait                Indicates that this keyword should wait for host or
144    #                     BMC activation is completed.
145    # skip_if_active      If set to true, will skip the code update if this
146    #                     image is already on the BMC.
147
148    OperatingSystem.File Should Exist  ${image_file_path}
149    ${image_version}=  Get Version Tar  ${image_file_path}
150
151    ${image_data}=  OperatingSystem.Get Binary File  ${image_file_path}
152    Upload Image To BMC  /upload/image  data=${image_data}
153    ${ret}  ${version_id}=  Verify Image Upload  ${image_version}
154    Should Be True  ${ret}
155
156    # Verify the image is 'READY' to be activated or if it's already active,
157    # set priority to 0 and reboot the BMC.
158    ${software_state}=  Read Properties  ${SOFTWARE_VERSION_URI}${version_id}
159    ${activation}=  Set Variable  &{software_state}[Activation]
160    Run Keyword If
161    ...  '${skip_if_active}' == 'true' and '${activation}' == '${ACTIVE}'
162    ...  Switch To Active Image And Pass  ${SOFTWARE_VERSION_URI}${version_id}
163    Should Be Equal As Strings  &{software_state}[Activation]  ${READY}
164
165    # Request the image to be activated.
166    ${args}=  Create Dictionary  data=${REQUESTED_ACTIVE}
167    Write Attribute  ${SOFTWARE_VERSION_URI}${version_id}
168    ...  RequestedActivation  data=${args}
169    ${software_state}=  Read Properties  ${SOFTWARE_VERSION_URI}${version_id}
170    Should Be Equal As Strings  &{software_state}[RequestedActivation]
171    ...  ${REQUESTED_ACTIVE}
172
173    # Does caller want to wait for activation to complete?
174    Run Keyword If  '${wait}' == '${0}'  Return From Keyword
175
176    # Verify code update was successful and Activation state is Active.
177    Wait For Activation State Change  ${version_id}  ${ACTIVATING}
178    ${software_state}=  Read Properties  ${SOFTWARE_VERSION_URI}${version_id}
179    Should Be Equal As Strings  &{software_state}[Activation]  ${ACTIVE}
180
181
182Switch To Active Image And Pass
183    [Documentation]  Make the given active image the image running on the BMC
184    ...              and pass the test.
185    [Arguments]  ${software_object}
186
187    # Description of argument(s):
188    # software_object  Software object path.
189    #                  (e.g. "/xyz/openbmc_project/software/f3b29aa8").
190
191    Set Host Software Property  ${software_object}  Priority  ${0}
192    OBMC Reboot (off)
193    Pass Execution  ${software_object} was already on the BMC.
194
195
196Activate Image And Verify No Duplicate Priorities
197    [Documentation]  Upload an image, and then check that no images have the
198    ...              same priority.
199    [Arguments]  ${image_file_path}  ${image_purpose}
200
201    # Description of argument(s):
202    # image_file_path  The path to the image to upload.
203    # image_purpose    The purpose in the image's MANIFEST file.
204
205    Upload And Activate Image  ${image_file_path}
206    Verify No Duplicate Image Priorities  ${image_purpose}
207
208
209Set Same Priority For Multiple Images
210    [Documentation]  Find two images, set the priorities to be the same, and
211    ...              verify that the priorities are not the same.
212    [Arguments]  ${version_purpose}
213
214    # Description of argument(s):
215    # version_purpose  Either BMC or host version purpose.
216    #                  (e.g. "xyz.openbmc_project.Software.Version.VersionPurpose.BMC"
217    #                        "xyz.openbmc_project.Software.Version.VersionPurpose.Host").
218
219    # Make sure we have more than two images.
220    ${software_objects}=  Get Software Objects  version_type=${version_purpose}
221    ${num_images}=  Get Length  ${software_objects}
222    Should Be True  1 < ${num_images}
223    ...  msg=Only found one image on the BMC with purpose ${version_purpose}.
224
225    # Set the priority of the second image to the priority of the first.
226    ${properties}=  Get Host Software Property  @{software_objects}[0]
227    Set Host Software Property  @{software_objects}[1]  Priority
228    ...  &{properties}[Priority]
229    Verify No Duplicate Image Priorities  ${version_purpose}
230
231    # Set the priority of the first image back to what it was before
232    Set Host Software Property  @{software_objects}[0]  Priority
233    ...  &{properties}[Priority]
234
235
236Delete Software Object
237    [Documentation]  Deletes an image from the BMC.
238    [Arguments]  ${software_object}
239
240    # Description of argument(s):
241    # software_object  The URI to the software image to delete.
242
243    ${arglist}=  Create List
244    ${args}=  Create Dictionary  data=${arglist}
245    ${resp}=  OpenBMC Post Request  ${software_object}/action/delete
246    ...  data=${args}
247    Should Be Equal As Strings  ${resp.status_code}  ${HTTP_OK}
248
249
250Delete Image And Verify
251    [Documentation]  Delete an image from the BMC and verify that it was
252    ...              removed from software and the /tmp/images directory.
253    [Arguments]  ${software_object}  ${version_type}
254
255    # Description of argument(s):
256    # software_object        The URI of the software object to delete.
257    # version_type  The type of the software object, e.g.
258    #               xyz.openbmc_project.Software.Version.VersionPurpose.Host
259    #               or xyz.openbmc_project.Software.Version.VersionPurpose.BMC.
260
261    Log To Console  Deleteing ${software_object}
262
263    # Delete the image.
264    Delete Software Object  ${software_object}
265    # TODO: If/when we don't have to delete twice anymore, take this out
266    Run Keyword And Ignore Error  Delete Software Object  ${software_object}
267
268    # Verify that it's gone from software.
269    ${software_objects}=  Get Software Objects  version_type=${version_type}
270    Should Not Contain  ${software_objects}  ${software_object}
271
272    # Check that there is no file in the /tmp/images directory.
273    ${image_id}=  Fetch From Right  ${software_object}  /
274    BMC Execute Command
275    ...  [ ! -d "/tmp/images/${image_id}" ]
276
277
278Check Error And Collect FFDC
279    [Documentation]  Collect FFDC if error log exists.
280
281    ${status}=  Run Keyword And Return Status  Error Logs Should Not Exist
282    Run Keyword If  '${status}' == 'False'  FFDC
283    Delete Error Logs
284