xref: /openbmc/openbmc-test-automation/lib/utils.robot (revision d29c5366d4be9bbdad26752ff057c760e3347112)
1*** Settings ***
2
3Documentation  Utilities for Robot keywords that use REST.
4
5Resource                ../lib/resource.robot
6Resource                ../lib/rest_client.robot
7Resource                ../lib/connection_client.robot
8Resource                ../lib/boot_utils.robot
9Resource                ../lib/common_utils.robot
10Resource                ../lib/bmc_redfish_utils.robot
11Library                 String
12Library                 DateTime
13Library                 Process
14Library                 OperatingSystem
15Library                 gen_print.py
16Library                 gen_misc.py
17Library                 gen_robot_print.py
18Library                 gen_cmd.py
19Library                 gen_robot_keyword.py
20Library                 bmc_ssh_utils.py
21Library                 utils.py
22Library                 var_funcs.py
23Library                 SCPLibrary  AS  scp
24Library                 gen_robot_valid.py
25Library                 pldm_utils.py
26
27
28*** Variables ***
29
30${SYSTEM_SHUTDOWN_TIME}   ${5}
31
32# Assign default value to QUIET for programs which may not define it.
33${QUIET}  ${0}
34
35${HOST_SETTING}    ${SETTINGS_URI}host0
36
37${boot_prog_method}               ${EMPTY}
38${power_policy_setup}             ${0}
39${bmc_power_policy_method}        ${EMPTY}
40@{BOOT_PROGRESS_STATES}           SystemHardwareInitializationComplete  OSBootStarted  OSRunning
41
42${REDFISH_SYS_STATE_WAIT_TIMEOUT}    120 Seconds
43
44*** Keywords ***
45
46
47Verify Ping and REST Authentication
48    [Documentation]  Verify ping and rest authentication.
49    ${l_ping}=   Run Keyword And Return Status
50    ...    Ping Host  ${OPENBMC_HOST}
51    Run Keyword If  '${l_ping}' == '${False}'
52    ...    Fail   msg=Ping Failed
53
54    ${l_rest}=   Run Keyword And Return Status
55    ...    Initialize OpenBMC
56    Run Keyword If  '${l_rest}' == '${False}'
57    ...    Fail   msg=REST Authentication Failed
58
59    # Just to make sure the SSH is working for SCP
60    Open Connection And Log In
61    ${system}   ${stderr}=    Execute Command   hostname   return_stderr=True
62    Should Be Empty     ${stderr}
63
64
65Verify Ping SSH And Redfish Authentication
66    [Documentation]  Verify ping, SSH and redfish authentication.
67
68    ${l_ping}=   Run Keyword And Return Status  Ping Host  ${OPENBMC_HOST}
69    Run Keyword If  '${l_ping}' == '${False}'  Fail   msg=Ping Failed
70
71    ${l_rest}=   Run Keyword And Return Status   Redfish.Login
72    Run Keyword If  '${l_rest}' == '${False}'  Fail   msg=REST Authentication Failed
73
74    # Just to make sure the SSH is working.
75    Open Connection And Log In
76    ${system}   ${stderr}=    Execute Command   hostname   return_stderr=True
77    Should Be Empty     ${stderr}
78
79
80Check If BMC is Up
81    [Documentation]  Wait for Host to be online. Checks every X seconds
82    ...              interval for Y minutes and fails if timed out.
83    ...              Default MAX timedout is 10 min, interval 10 seconds.
84    [Arguments]      ${max_timeout}=${OPENBMC_REBOOT_TIMEOUT} min
85    ...              ${interval}=10 sec
86
87    # Description of argument(s):
88    # max_timeout   Maximum time to wait.
89    #               This should be expressed in Robot Framework's time format
90    #               (e.g. "10 minutes").
91    # interval      Interval to wait between status checks.
92    #               This should be expressed in Robot Framework's time format
93    #               (e.g. "5 seconds").
94
95    Wait Until Keyword Succeeds
96    ...   ${max_timeout}  ${interval}   Verify Ping and REST Authentication
97
98
99Flush REST Sessions
100    [Documentation]   Removes all the active session objects
101    Delete All Sessions
102
103
104Trigger Host Watchdog Error
105    [Documentation]  Inject host watchdog timeout error via REST.
106    [Arguments]  ${milliseconds}=1000  ${sleep_time}=5s
107
108    # Description of argument(s):
109    # milliseconds  The time watchdog timer value in milliseconds (e.g. 1000 =
110    #               1 second).
111    # sleep_time    Time delay for host watchdog error to get injected.
112    #               Default is 5 seconds.
113
114    ${data}=  Create Dictionary
115    ...  data=xyz.openbmc_project.State.Watchdog.Action.PowerCycle
116    ${status}  ${result}=  Run Keyword And Ignore Error
117    ...  Read Attribute  ${HOST_WATCHDOG_URI}  ExpireAction
118    Run Keyword If  '${status}' == 'PASS'
119    ...  Write Attribute  ${HOST_WATCHDOG_URI}  ExpireAction  data=${data}
120
121    ${int_milliseconds}=  Convert To Integer  ${milliseconds}
122    ${data}=  Create Dictionary  data=${int_milliseconds}
123    Write Attribute  ${HOST_WATCHDOG_URI}  Interval  data=${data}
124
125    ${data}=  Create Dictionary  data=${True}
126    Write Attribute  ${HOST_WATCHDOG_URI}  Enabled  data=${data}
127
128    Sleep  ${sleep_time}
129
130
131Login To OS Host
132    [Documentation]  Login to OS Host and return the Login response code.
133    [Arguments]  ${os_host}=${OS_HOST}  ${os_username}=${OS_USERNAME}
134    ...          ${os_password}=${OS_PASSWORD}
135
136    # Description of arguments:
137    # ${os_host} IP address of the OS Host.
138    # ${os_username}  OS Host Login user name.
139    # ${os_password}  OS Host Login passwrd.
140
141    Redfish Power On  stack_mode=skip  quiet=1
142
143    SSHLibrary.Open Connection  ${os_host}
144    ${resp}=  SSHLibrary.Login  ${os_username}  ${os_password}
145    RETURN  ${resp}
146
147
148Initiate Auto Reboot
149    [Documentation]  Initiate an auto reboot.
150    [Arguments]  ${milliseconds}=5000
151
152    # Description of argument(s):
153    # milliseconds  The number of milliseconds for the watchdog timer.
154
155    # Set the auto reboot policy.
156    Set Auto Reboot  ${1}
157    # Set the watchdog timer.
158    Trigger Host Watchdog Error  ${milliseconds}
159
160
161Initiate OS Host Reboot
162    [Documentation]  Initiate an OS reboot.
163    [Arguments]  ${os_host}=${OS_HOST}  ${os_username}=${OS_USERNAME}
164    ...          ${os_password}=${OS_PASSWORD}
165
166    # Description of argument(s):
167    # os_host      The host name or IP address of the OS.
168    # os_username  The username to be used to sign in to the OS.
169    # os_password  The password to be used to sign in to the OS.
170
171    ${cmd_buf}=  Run Keyword If  '${os_username}' == 'root'
172    ...      Set Variable  reboot
173    ...  ELSE
174    ...      Set Variable  echo ${os_password} | sudo -S reboot
175
176    ${output}  ${stderr}  ${rc}=  OS Execute Command
177    ...  ${cmd_buf}  fork=${1}
178
179
180Initiate OS Host Power Off
181    [Documentation]  Initiate an OS reboot.
182    [Arguments]  ${os_host}=${OS_HOST}  ${os_username}=${OS_USERNAME}
183    ...          ${os_password}=${OS_PASSWORD}  ${hard}=${0}
184
185    # Description of argument(s):
186    # os_host      The DNS name or IP of the OS.
187    # os_username  The username to be used to sign in to the OS.
188    # os_password  The password to be used to sign in to the OS.
189    # hard         Indicates whether to do a hard vs. soft power off.
190
191    ${time_string}=  Run Keyword If  ${hard}  Set Variable  ${SPACE}now
192    ...  ELSE  Set Variable  ${EMPTY}
193
194    ${cmd_buf}=  Run Keyword If  '${os_username}' == 'root'
195    ...      Set Variable  shutdown${time_string}
196    ...  ELSE
197    ...      Set Variable  echo ${os_password} | sudo -S shutdown${time_string}
198
199    ${output}  ${stderr}  ${rc}=  OS Execute Command
200    ...  ${cmd_buf}  fork=${1}
201
202
203Set System LED State
204    [Documentation]  Set given system LED via REST.
205    [Arguments]  ${led_name}  ${led_state}
206    # Description of argument(s):
207    # led_name     System LED name (e.g. heartbeat, identify, beep).
208    # led_state    LED state to be set (e.g. On, Off).
209
210    ${args}=  Create Dictionary
211    ...  data=xyz.openbmc_project.Led.Physical.Action.${led_state}
212    Write Attribute  ${LED_PHYSICAL_URI}${led_name}  State  data=${args}
213
214    Verify LED State  ${led_name}  ${led_state}
215
216
217Old Get Boot Progress
218    [Documentation]  Get the boot progress the old way (via org location).
219    [Arguments]  ${quiet}=${QUIET}
220
221    # Description of argument(s):
222    # quiet   Indicates whether this keyword should run without any output to
223    #         the console.
224
225    ${state}=  Read Attribute  ${OPENBMC_BASE_URI}sensors/host/BootProgress
226    ...  value  quiet=${quiet}
227
228    RETURN  ${state}
229
230
231Set Boot Progress Method
232    [Documentation]  Set the boot_prog_method to either 'Old' or 'New'.
233
234    # The boot progress data has moved from an 'org' location to an 'xyz'
235    # location.  This keyword will determine whether the new method of getting
236    # the boot progress is valid and will set the global boot_prog_method
237    # variable accordingly.  If boot_prog_method is already set (either by a
238    # prior call to this function or via a -v parm), this keyword will simply
239    # return.
240
241    # Note:  There are interim builds that contain boot_progress in both the
242    # old and the new location values.  It is nearly impossible for this
243    # keyword to determine whether the old boot_progress or the new one is
244    # active.  When using such builds where the old boot_progress is active,
245    # the only recourse users will have is that they may specify
246    # -v boot_prog_method:Old to force old behavior on such builds.
247
248    Run Keyword If  '${boot_prog_method}' != '${EMPTY}'  Return From Keyword
249
250    ${new_status}  ${new_value}=  Run Keyword And Ignore Error
251    ...  New Get Boot Progress
252    # If the new style read fails, the method must necessarily be "Old".
253    Run Keyword If  '${new_status}' == 'PASS'
254    ...  Run Keywords
255    ...  Set Global Variable  ${boot_prog_method}  New  AND
256    ...  Rqpvars  boot_prog_method  AND
257    ...  Return From Keyword
258
259    # Default method is "Old".
260    Set Global Variable  ${boot_prog_method}  Old
261    Rqpvars  boot_prog_method
262
263
264Initiate Power On
265    [Documentation]  Initiates the power on and waits until the Is Power On
266    ...  keyword returns that the power state has switched to on.
267    [Arguments]  ${wait}=${1}
268
269    # Description of argument(s):
270    # wait   Indicates whether to wait for a powered on state after issuing
271    #        the power on command.
272
273    @{arglist}=   Create List
274    ${args}=     Create Dictionary    data=@{arglist}
275    ${resp}=  Call Method  ${OPENBMC_BASE_URI}control/chassis0/  powerOn
276    ...  data=${args}
277    should be equal as strings      ${resp.status_code}     ${HTTP_OK}
278
279    # Does caller want to wait for power on status?
280    Run Keyword If  '${wait}' == '${0}'  Return From Keyword
281    Wait Until Keyword Succeeds  3 min  10 sec  Is Power On
282
283
284Initiate Power Off
285    [Documentation]  Initiates the power off and waits until the Is Power Off
286    ...  keyword returns that the power state has switched to off.
287
288    @{arglist}=   Create List
289    ${args}=     Create Dictionary    data=@{arglist}
290    ${resp}=  Call Method  ${OPENBMC_BASE_URI}control/chassis0/  powerOff
291    ...  data=${args}
292    should be equal as strings      ${resp.status_code}     ${HTTP_OK}
293    Wait Until Keyword Succeeds  1 min  10 sec  Is Power Off
294
295
296Get Boot Progress
297    [Documentation]  Get the boot progress and return it.
298    [Arguments]  ${quiet}=${QUIET}
299
300    # Description of argument(s):
301    # quiet   Indicates whether this keyword should run without any output to
302    #         the console.
303
304    Set Boot Progress Method
305    ${state}=  Run Keyword If  '${boot_prog_method}' == 'New'
306    ...      New Get Boot Progress  quiet=${quiet}
307    ...  ELSE
308    ...      Old Get Boot Progress  quiet=${quiet}
309
310    RETURN  ${state}
311
312
313New Get Boot Progress
314    [Documentation]  Get the boot progress the new way (via xyz location).
315    [Arguments]  ${quiet}=${QUIET}
316
317    # Description of argument(s):
318    # quiet   Indicates whether this keyword should run without any output to
319    #         the console.
320
321    ${state}=  Read Attribute  ${HOST_STATE_URI}  BootProgress  quiet=${quiet}
322
323    RETURN  ${state.rsplit('.', 1)[1]}
324
325
326New Get Power Policy
327    [Documentation]  Returns the BMC power policy (new method).
328    ${currentPolicy}=  Read Attribute  ${POWER_RESTORE_URI}  PowerRestorePolicy
329
330    RETURN  ${currentPolicy}
331
332
333Old Get Power Policy
334    [Documentation]  Returns the BMC power policy (old method).
335    ${currentPolicy}=  Read Attribute  ${HOST_SETTING}  power_policy
336
337    RETURN  ${currentPolicy}
338
339
340Redfish Get Power Restore Policy
341    [Documentation]  Returns the BMC power restore policy.
342
343    ${power_restore_policy}=  Redfish.Get Attribute  /redfish/v1/Systems/${SYSTEM_ID}  PowerRestorePolicy
344    RETURN  ${power_restore_policy}
345
346
347Get Auto Reboot
348    [Documentation]  Returns auto reboot setting.
349    ${setting}=  Read Attribute  ${CONTROL_HOST_URI}/auto_reboot  AutoReboot
350
351    RETURN  ${setting}
352
353
354Redfish Get Auto Reboot
355    [Documentation]  Returns auto reboot setting.
356
357    ${resp}=  Wait Until Keyword Succeeds  1 min  20 sec
358    ...  Redfish.Get Attribute  /redfish/v1/Systems/${SYSTEM_ID}  Boot
359    RETURN  ${resp["AutomaticRetryConfig"]}
360
361
362Trigger Warm Reset
363    [Documentation]  Initiate a warm reset.
364
365    log to console    "Triggering warm reset"
366    ${data}=   create dictionary   data=@{EMPTY}
367    ${resp}=  Openbmc Post Request
368    ...  ${OPENBMC_BASE_URI}control/bmc0/action/warmReset  data=${data}
369    Should Be Equal As Strings      ${resp.status_code}     ${HTTP_OK}
370    ${session_active}=   Check If warmReset is Initiated
371    Run Keyword If   '${session_active}' == '${True}'
372    ...    Fail   msg=warm reset didn't occur
373
374    Sleep   ${SYSTEM_SHUTDOWN_TIME}min
375    Check If BMC Is Up
376
377
378Get Power State
379    [Documentation]  Returns the power state as an integer. Either 0 or 1.
380    [Arguments]  ${quiet}=${QUIET}
381
382    # Description of argument(s):
383    # quiet   Indicates whether this keyword should run without any output to
384    #         the console.
385
386    @{arglist}=  Create List
387    ${args}=  Create Dictionary  data=@{arglist}
388
389    ${resp}=  Call Method  ${OPENBMC_BASE_URI}control/chassis0/  getPowerState
390    ...        data=${args}  quiet=${quiet}
391    Should be equal as strings  ${resp.status_code}  ${HTTP_OK}
392
393    RETURN  ${resp.json()["data"]}
394
395
396Clear BMC Gard Record
397    [Documentation]  Clear gard records from the system.
398
399    @{arglist}=  Create List
400    ${args}=  Create Dictionary  data=@{arglist}
401    ${resp}=  Call Method
402    ...  ${OPENPOWER_CONTROL}gard  Reset  data=${args}
403    Should Be Equal As Strings  ${resp.status_code}  ${HTTP_OK}
404
405
406Flash PNOR
407    [Documentation]    Calls flash bios update method to flash PNOR image
408    [Arguments]    ${pnor_image}
409
410    # Description of argument(s):
411    # pnor_image  The filename and path of the PNOR image
412    #             (e.g. "/home/image/zaius.pnor").
413
414    @{arglist}=   Create List    ${pnor_image}
415    ${args}=     Create Dictionary    data=@{arglist}
416    ${resp}=  Call Method  /org/openbmc/control/flash/bios/  update
417    ...  data=${args}
418    should be equal as strings      ${resp.status_code}     ${HTTP_OK}
419    Wait Until Keyword Succeeds    2 min   10 sec    Is PNOR Flashing
420
421
422Get Flash BIOS Status
423    [Documentation]  Returns the status of the flash BIOS API as a string. For
424    ...              example 'Flashing', 'Flash Done', etc
425    ${data}=  Read Properties  /org/openbmc/control/flash/bios
426    RETURN    ${data['status']}
427
428
429Is PNOR Flashing
430    [Documentation]  Get BIOS 'Flashing' status. This indicates that PNOR
431    ...              flashing has started.
432    ${status}=    Get Flash BIOS Status
433    Should Contain  ${status}  Flashing
434
435
436Is PNOR Flash Done
437    [Documentation]  Get BIOS 'Flash Done' status.  This indicates that the
438    ...              PNOR flashing has completed.
439    ${status}=    Get Flash BIOS Status
440    should be equal as strings     ${status}     Flash Done
441
442
443Create OS Console File Path
444    [Documentation]  Create OS console file path name and return it.
445    [Arguments]  ${log_file_path}=${EMPTY}
446
447    # Description of arguments:
448    # file_path  The caller's candidate value.  If this value is ${EMPTY}, this
449    #            keyword will compose a file path name.  Otherwise, this
450    #            keyword will use the caller's file_path value.  In either
451    #            case, the value will be returned.
452
453    ${status}=  Run Keyword And Return Status  Variable Should Exist
454    ...  ${TEST_NAME}
455
456    ${default_file_path}=  Set Variable If  ${status} == ${TRUE}
457    ...  ${EXECDIR}${/}tmp${/}${OPENBMC_HOST}_${TEST_NAME.replace(' ', '')}_os_console.txt
458    ...  ${EXECDIR}${/}tmp${/}${OPENBMC_HOST}_os_console.txt
459
460    ${log_file_path}=  Set Variable If  '${log_file_path}' == '${EMPTY}'
461    ...  ${default_file_path}  ${log_file_path}
462
463    RETURN  ${log_file_path}
464
465
466Get Endpoint Paths
467    [Documentation]   Returns all url paths ending with given endpoint
468    ...               Example:
469    ...               Given the following endpoint: cpu
470    ...               This keyword will return: list of all urls ending with
471    ...               cpu -
472    ...               /org/openbmc/inventory/system/chassis/motherboard/cpu0,
473    ...               /org/openbmc/inventory/system/chassis/motherboard/cpu1
474    [Arguments]   ${path}   ${endpoint}
475
476    # Description of arguments:
477    # path       URL path for enumeration.
478    # endpoint   Endpoint string (url path ending).
479
480    # Make sure path ends with slash.
481    ${path}=  Add Trailing Slash  ${path}
482
483    ${resp}=  Read Properties  ${path}enumerate  timeout=30
484    Log Dictionary  ${resp}
485
486    ${list}=  Get Dictionary Keys  ${resp}
487    # For a given string, look for prefix and suffix for matching expression.
488    # Start of string followed by zero or more of any character followed by
489    # any digit or lower case character.
490    ${resp}=  Get Matches  ${list}  regexp=^.*[0-9a-z_].${endpoint}\[_0-9a-z]*$  case_insensitive=${True}
491
492    RETURN  ${resp}
493
494
495Set BMC Power Policy
496    [Documentation]   Set the given BMC power policy.
497    [Arguments]   ${policy}
498
499    # Note that this function will translate the old style "RESTORE_LAST_STATE"
500    # policy to the new style "xyz.openbmc_project.Control.Power.RestorePolicy.
501    # Policy.Restore" for you.
502
503    # Description of argument(s):
504    # policy    Power restore policy (e.g "RESTORE_LAST_STATE",
505    #           ${RESTORE_LAST_STATE}).
506
507    # Set the bmc_power_policy_method to either 'Old' or 'New'.
508    Set Power Policy Method
509    # This translation helps bridge between old and new method for calling.
510    ${policy}=  Translate Power Policy Value  ${policy}
511    # Run the appropriate keyword.
512    Run Key  ${bmc_power_policy_method} Set Power Policy \ ${policy}
513    ${currentPolicy}=  Get System Power Policy
514    Should Be Equal    ${currentPolicy}   ${policy}
515
516
517Delete Error Logs
518    [Documentation]  Delete error logs.
519    [Arguments]  ${quiet}=${0}
520    # Description of argument(s):
521    # quiet    If enabled, turns off logging to console.
522
523    # Check if error logs entries exist, if not return.
524    ${resp}=  OpenBMC Get Request  ${BMC_LOGGING_ENTRY}list  quiet=${1}
525    Return From Keyword If  ${resp.status_code} == ${HTTP_NOT_FOUND}
526
527    # Get the list of error logs entries and delete them all.
528    ${elog_entries}=  Get URL List  ${BMC_LOGGING_ENTRY}
529    FOR  ${entry}  IN  @{elog_entries}
530        Delete Error Log Entry  ${entry}  quiet=${quiet}
531    END
532
533
534Delete All Error Logs
535    [Documentation]  Delete all error log entries using "DeleteAll" interface.
536
537    ${args}=  Set Variable   {"data": []}
538    ${resp}=  Openbmc Post Request  ${BMC_LOGGING_URI}action/DeleteAll
539    ...  data=${args}
540    Should Be Equal As Strings  ${resp.status_code}  ${HTTP_OK}
541
542
543Get Elog URL List
544    [Documentation]  Return error log entry list of URLs.
545
546    ${url_list}=  Read Properties  /xyz/openbmc_project/logging/entry/
547    Sort List  ${url_list}
548    RETURN  ${url_list}
549
550
551Get BMC Flash Chip Boot Side
552    [Documentation]  Return the BMC flash chip boot side.
553
554    # Example:
555    # 0  - indicates chip select is current side.
556    # 32 - indicates chip select is alternate side.
557
558    ${boot_side}  ${stderr}  ${rc}=  BMC Execute Command
559    ...  cat /sys/class/watchdog/watchdog1/bootstatus
560
561    RETURN  ${boot_side}
562
563
564Watchdog Object Should Exist
565    [Documentation]  Check that watchdog object exists.
566
567    ${resp}=  OpenBMC Get Request  ${WATCHDOG_URI}host0
568    Should Be Equal As Strings  ${resp.status_code}  ${HTTP_OK}
569    ...  msg=Expected watchdog object does not exist.
570
571
572Get System LED State
573    [Documentation]  Return the state of given system LED.
574    [Arguments]  ${led_name}
575
576    # Description of argument(s):
577    # led_name     System LED name (e.g. heartbeat, identify, beep).
578
579    ${state}=  Read Attribute  ${LED_PHYSICAL_URI}${led_name}  State
580    RETURN  ${state.rsplit('.', 1)[1]}
581
582
583Verify LED State
584    [Documentation]  Checks if LED is in given state.
585    [Arguments]  ${led_name}  ${led_state}
586    # Description of argument(s):
587    # led_name     System LED name (e.g. heartbeat, identify, beep).
588    # led_state    LED state to be verified (e.g. On, Off).
589
590    ${state}=  Get System LED State  ${led_name}
591    Should Be Equal  ${state}  ${led_state}
592
593
594Get LED State XYZ
595    [Documentation]  Returns state of given LED.
596    [Arguments]  ${led_name}
597
598    # Description of argument(s):
599    # led_name  Name of LED.
600
601    ${state}=  Read Attribute  ${LED_GROUPS_URI}${led_name}  Asserted
602    # Returns the state of the LED, either On or Off.
603    RETURN  ${state}
604
605
606Verify Identify LED State
607    [Documentation]  Verify that the identify state of the LED group matches caller's expectations.
608    [Arguments]  ${expected_state}
609
610    # Description of argument(s):
611    # expected_state  The expected LED asserted state (1 = asserted, 0 = not asserted).
612
613    ${led_state}=  Get LED State XYZ  enclosure_identify
614    Should Be Equal  ${led_state}  ${expected_state}  msg=Unexpected LED state.
615
616Verify The Attribute
617    [Documentation]  Verify the given attribute.
618    [Arguments]  ${uri}  ${attribute_name}  ${attribute_value}
619
620    # Description of argument(s):
621    # uri              URI path
622    #                  (e.g. "/xyz/openbmc_project/control/host0/TPMEnable").
623    # attribute_name   Name of attribute to be verified (e.g. "TPMEnable").
624    # attribute_value  The expected value of attribute (e.g. "1", "0", etc.)
625
626    ${output}=  Read Attribute  ${uri}  ${attribute_name}
627    Should Be Equal  ${attribute_value}  ${output}
628    ...  msg=Attribute "${attribute_name} does not have the expected value.
629
630
631New Set Power Policy
632    [Documentation]   Set the given BMC power policy (new method).
633    [Arguments]   ${policy}
634
635    # Description of argument(s):
636    # policy    Power restore policy (e.g. ${ALWAYS_POWER_OFF}).
637
638    ${valueDict}=  Create Dictionary  data=${policy}
639    Write Attribute
640    ...  ${POWER_RESTORE_URI}  PowerRestorePolicy  data=${valueDict}
641
642
643Old Set Power Policy
644    [Documentation]   Set the given BMC power policy (old method).
645    [Arguments]   ${policy}
646
647    # Description of argument(s):
648    # policy    Power restore policy (e.g. "ALWAYS_POWER_OFF").
649
650    ${valueDict}=     create dictionary  data=${policy}
651    Write Attribute    ${HOST_SETTING}    power_policy   data=${valueDict}
652
653
654Redfish Set Power Restore Policy
655    [Documentation]   Set the BMC power restore policy.
656    [Arguments]   ${power_restore_policy}
657
658    # Description of argument(s):
659    # power_restore_policy    Power restore policy (e.g. "AlwaysOff", "AlwaysOn", "LastState").
660
661    Redfish.Patch  /redfish/v1/Systems/${SYSTEM_ID}  body={"PowerRestorePolicy": "${power_restore_policy}"}
662    ...  valid_status_codes=[${HTTP_OK}, ${HTTP_NO_CONTENT}]
663
664
665IPMI Set Power Restore Policy
666    [Documentation]   Set the BMC power restore policy using IPMI.
667    [Arguments]   ${power_restore_policy}=always-off
668
669    # Description of argument(s):
670    # power_restore_policy    Power restore policies
671    #                         always-on   : turn on when power is restored
672    #                         previous    : return to previous state when power is restored
673    #                         always-off  : stay off after power is restored
674
675    ${resp}=  Run IPMI Standard Command  chassis policy ${power_restore_policy}
676    # Example:  Set chassis power restore policy to always-off
677    Should Contain  ${resp}  ${power_restore_policy}
678
679
680Set Auto Reboot Setting
681    [Documentation]  Set the given auto reboot setting (REST or Redfish).
682    [Arguments]  ${value}
683
684    # Description of argument(s):
685    # value    The reboot setting, 1 for enabling and 0 for disabling.
686
687    # This is to cater to boot call points and plugin script which will always
688    # send using value 0 or 1. This dictionary maps to redfish string values.
689    ${rest_redfish_dict}=  Create Dictionary
690    ...                    1=RetryAttempts
691    ...                    0=Disabled
692
693    Run Keyword If  ${REDFISH_SUPPORT_TRANS_STATE} == ${1}
694    ...    Redfish Set Auto Reboot  ${rest_redfish_dict["${value}"]}
695    ...  ELSE
696    ...    Set Auto Reboot  ${value}
697
698Set Auto Reboot
699    [Documentation]  Set the given auto reboot setting.
700    [Arguments]  ${setting}
701
702    # Description of argument(s):
703    # setting    The reboot setting, 1 for enabling and 0 for disabling.
704
705    ${valueDict}=  Convert To Integer  ${setting}
706    ${data}=  Create Dictionary  data=${valueDict}
707    Write Attribute  ${CONTROL_HOST_URI}/auto_reboot  AutoReboot   data=${data}
708    ${current_setting}=  Get Auto Reboot
709    Should Be Equal As Integers  ${current_setting}  ${setting}
710
711
712Redfish Set Auto Reboot
713    [Documentation]  Set the given auto reboot setting.
714    [Arguments]  ${setting}
715
716    # Description of argument(s):
717    # setting    The reboot setting, "RetryAttempts" and "Disabled".
718
719    Redfish.Patch  /redfish/v1/Systems/${SYSTEM_ID}  body={"Boot": {"AutomaticRetryConfig": "${setting}"}}
720    ...  valid_status_codes=[${HTTP_OK}, ${HTTP_NO_CONTENT}]
721
722    ${current_setting}=  Redfish Get Auto Reboot
723    Should Be Equal As Strings  ${current_setting}  ${setting}
724
725
726Set Control Boot Mode
727    [Documentation]  Set given boot mode on the boot object path attribute.
728    [Arguments]  ${boot_path}  ${boot_mode}
729
730    # Description of argument(s):
731    # boot_path  Boot object path.
732    #            Example:
733    #            /xyz/openbmc_project/control/host0/boot
734    #            /xyz/openbmc_project/control/host0/boot/one_time
735    # boot_mode  Boot mode which need to be set.
736    #            Example:
737    #            "xyz.openbmc_project.Control.Boot.Mode.Modes.Regular"
738
739    ${valueDict}=  Create Dictionary  data=${boot_mode}
740    Write Attribute  ${boot_path}  BootMode  data=${valueDict}
741
742
743Is Power On
744    [Documentation]  Verify that the BMC chassis state is on.
745    ${state}=  Get Power State
746    Should be equal  ${state}  ${1}
747
748
749Is Power Off
750    [Documentation]  Verify that the BMC chassis state is off.
751    ${state}=  Get Power State
752    Should be equal  ${state}  ${0}
753
754
755CLI Get BMC DateTime
756    [Documentation]  Returns BMC date time from date command.
757
758    ${bmc_time_via_date}  ${stderr}  ${rc}=  BMC Execute Command  date +"%Y-%m-%dT%H:%M:%S"  print_err=1
759    RETURN  ${bmc_time_via_date}
760
761
762Update Root Password
763    [Documentation]  Update system "root" user password.
764    [Arguments]  ${openbmc_password}=${OPENBMC_PASSWORD}
765
766    # Description of argument(s):
767    # openbmc_password   The root password for the open BMC system.
768
769    @{password}=  Create List  ${openbmc_password}
770    ${data}=  Create Dictionary  data=@{password}
771
772    ${headers}=  Create Dictionary  Content-Type=application/json  X-Auth-Token=${XAUTH_TOKEN}
773    ${resp}=  POST On Session  openbmc  ${BMC_USER_URI}root/action/SetPassword
774    ...  data=${data}  headers=${headers}
775    Valid Value  resp.status_code  [${HTTP_OK}]
776
777
778Get Post Boot Action
779    [Documentation]  Get post boot action.
780
781    # Post code update action dictionary.
782    #
783    # {
784    #    BMC image: {
785    #        OnReset: Redfish OBMC Reboot (off),
786    #        Immediate: Wait For Reboot  start_boot_seconds=${state['epoch_seconds']}
787    #    },
788    #    Host image: {
789    #        OnReset: RF SYS GracefulRestart,
790    #        Immediate: Wait State  os_running_match_state  10 mins
791    #    }
792    # }
793
794    ${code_base_dir_path}=  Get Code Base Dir Path
795    ${post_code_update_actions}=  Evaluate
796    ...  json.load(open('${code_base_dir_path}data/applytime_table.json'))  modules=json
797    Rprint Vars  post_code_update_actions
798
799    RETURN  ${post_code_update_actions}
800
801
802Get Task State From File
803    [Documentation]  Get task states from pre-define data/task_state.json file.
804
805    # Example:  Task state JSON format.
806    #
807    # {
808    #   "TaskRunning": {
809    #           "TaskState": "Running",
810    #           "TaskStatus": "OK"
811    #   },
812    #   "TaskCompleted": {
813    #           "TaskState": "Completed",
814    #           "TaskStatus": "OK"
815    #   },
816    #   "TaskException": {
817    #           "TaskState": "Exception",
818    #           "TaskStatus": "Warning"
819    #   }
820    # }
821
822    # Python module: get_code_base_dir_path()
823    ${code_base_dir_path}=  Get Code Base Dir Path
824    ${task_state}=  Evaluate
825    ...  json.load(open('${code_base_dir_path}data/task_state.json'))  modules=json
826    Rprint Vars  task_state
827
828    RETURN  ${task_state}
829
830
831Redfish Set Boot Default
832    [Documentation]  Set and Verify Boot source override
833    [Arguments]      ${override_enabled}  ${override_target}  ${override_mode}=UEFI
834
835    # Description of argument(s):
836    # override_enabled    Boot source override enable type.
837    #                     ('Once', 'Continuous', 'Disabled').
838    # override_target     Boot source override target.
839    #                     ('Pxe', 'Cd', 'Hdd', 'Diags', 'BiosSetup', 'None').
840    # override_mode       Boot source override mode (relevant only for x86 arch).
841    #                     ('Legacy', 'UEFI').
842
843    ${data}=  Create Dictionary  BootSourceOverrideEnabled=${override_enabled}
844    ...  BootSourceOverrideTarget=${override_target}
845
846    Run Keyword If  '${PLATFORM_ARCH_TYPE}' == 'x86'
847    ...  Set To Dictionary  ${data}  BootSourceOverrideMode  ${override_mode}
848
849    ${payload}=  Create Dictionary  Boot=${data}
850
851    Redfish.Patch  /redfish/v1/Systems/${SYSTEM_ID}  body=&{payload}
852    ...  valid_status_codes=[${HTTP_OK},${HTTP_NO_CONTENT}]
853
854    ${resp}=  Redfish.Get Attribute  /redfish/v1/Systems/${SYSTEM_ID}  Boot
855    Should Be Equal As Strings  ${resp["BootSourceOverrideEnabled"]}  ${override_enabled}
856    Should Be Equal As Strings  ${resp["BootSourceOverrideTarget"]}  ${override_target}
857    Run Keyword If  '${PLATFORM_ARCH_TYPE}' == 'x86'
858    ...  Should Be Equal As Strings  ${resp["BootSourceOverrideMode"]}  ${override_mode}
859
860
861# Redfish state keywords.
862
863Redfish Get BMC State
864    [Documentation]  Return BMC health state.
865
866    # "Enabled" ->  BMC Ready, "Starting" -> BMC NotReady
867
868    # Example:
869    # "Status": {
870    #    "Health": "OK",
871    #    "HealthRollup": "OK",
872    #    "State": "Enabled"
873    # },
874
875    ${status}=  Wait Until Keyword Succeeds  1 min  20 sec
876    ...  Redfish.Get Attribute  /redfish/v1/Managers/${MANAGER_ID}  Status
877    RETURN  ${status["State"]}
878
879
880Redfish Verify BMC State
881    [Documentation]  Verify BMC state is enabled.
882    [Arguments]  ${match_state}=Enabled
883
884    # Description of argument(s):
885    # match_state    Expected match state (e.g. Enabled, Starting, Error)
886
887    ${Status}=  Wait Until Keyword Succeeds  1 min  20 sec
888    ...  Redfish.Get Attribute  /redfish/v1/Managers/${MANAGER_ID}  Status
889
890    Should Be Equal As Strings  ${match_state}  ${Status['State']}
891
892
893Redfish Get Host State
894    [Documentation]  Return host power and health state.
895
896    # Refer: http://redfish.dmtf.org/schemas/v1/Resource.json#/definitions/Status
897
898    # Example:
899    # "PowerState": "Off",
900    # "Status": {
901    #    "Health": "OK",
902    #    "HealthRollup": "OK",
903    #    "State": "StandbyOffline"
904    # },
905
906    ${chassis}=  Wait Until Keyword Succeeds  1 min  20 sec
907    ...  Redfish.Get Properties  /redfish/v1/Chassis/${CHASSIS_ID}
908    RETURN  ${chassis["PowerState"]}  ${chassis["Status"]["State"]}
909
910
911Redfish Get Boot Progress
912    [Documentation]  Return boot progress state.
913
914    # Example: /redfish/v1/Systems/system/
915    # "BootProgress": {
916    #    "LastState": "OSRunning"
917    # },
918
919    ${boot_progress}=  Wait Until Keyword Succeeds  1 min  20 sec
920    ...  Redfish.Get Properties  /redfish/v1/Systems/${SYSTEM_ID}/
921
922    Return From Keyword If  "${PLATFORM_ARCH_TYPE}" == "x86"
923    ...  NA  ${boot_progress["Status"]["State"]}
924
925    RETURN  ${boot_progress["BootProgress"]["LastState"]}  ${boot_progress["Status"]["State"]}
926
927
928Redfish Get States
929    [Documentation]  Return all the BMC and host states in dictionary.
930    [Timeout]  ${REDFISH_SYS_STATE_WAIT_TIMEOUT}
931
932    # Refer: openbmc/docs/designs/boot-progress.md
933
934    Redfish.Login
935
936    ${bmc_state}=  Redfish Get BMC State
937    ${chassis_state}  ${chassis_status}=  Redfish Get Host State
938    ${boot_progress}  ${host_state}=  Redfish Get Boot Progress
939
940    ${states}=  Create Dictionary
941    ...  bmc=${bmc_state}
942    ...  chassis=${chassis_state}
943    ...  host=${host_state}
944    ...  boot_progress=${boot_progress}
945
946    # Disable loggoing state to prevent huge log.html record when boot
947    # test is run in loops.
948    #Log  ${states}
949
950    RETURN  ${states}
951
952
953Is BMC Not Quiesced
954    [Documentation]  Verify BMC state is not quiesced.
955
956    ${bmc_state}=   Redfish Get States
957
958    Log To Console  BMC State : ${bmc_state}
959
960    Should Not Be Equal As Strings  Quiesced  ${bmc_state['bmc']}
961
962
963Is BMC Standby
964    [Documentation]  Check if BMC is ready and host at standby.
965
966    ${standby_states}=  Create Dictionary
967    ...  bmc=Enabled
968    ...  chassis=Off
969    ...  host=Disabled
970    ...  boot_progress=None
971
972    Run Keyword If  '${PLATFORM_ARCH_TYPE}' == 'x86'
973    ...  Set To Dictionary  ${standby_states}  boot_progress=NA
974
975    Wait Until Keyword Succeeds  3 min  10 sec  Redfish Get States
976
977    Wait Until Keyword Succeeds  5 min  10 sec  Match State  ${standby_states}
978
979
980Match State
981    [Documentation]  Check if the expected and current states are matched.
982    [Arguments]  ${match_state}
983
984    # Description of argument(s):
985    # match_state      Expected states in dictionary.
986
987    ${current_state}=  Redfish Get States
988    Dictionaries Should Be Equal  ${match_state}  ${current_state}
989
990
991Wait For Host Boot Progress To Reach Required State
992    [Documentation]  Wait till host boot progress reaches required state.
993    [Arguments]      ${expected_boot_state}=OSRunning
994
995    # Description of argument(s):
996    # expected_boot_state    Expected boot state. E.g. OSRunning, SystemInitComplete etc.
997
998    Wait Until Keyword Succeeds  ${power_on_timeout}  20 sec
999    ...  Is Boot Progress At Required State  ${expected_boot_state}
1000
1001
1002Redfish Initiate Auto Reboot
1003    [Documentation]  Initiate an auto reboot.
1004    [Arguments]  ${interval}=2000
1005
1006    # Description of argument(s):
1007    # interval  Value in milliseconds to set Watchdog interval
1008
1009    # Set auto reboot policy
1010    Redfish Set Auto Reboot  RetryAttempts
1011
1012    Redfish Power Operation  On
1013
1014    Wait Until Keyword Succeeds  2 min  5 sec  Is Boot Progress Changed
1015
1016    # Set watchdog timer
1017    Set Watchdog Interval Using Busctl  ${interval}
1018
1019
1020Is Boot Progress Changed
1021    [Documentation]  Get BootProgress state and expect boot state mismatch.
1022    [Arguments]  ${boot_state}=None
1023
1024    # Description of argument(s):
1025    # boot_state   Value of the BootProgress state to match against.
1026
1027    ${boot_progress}  ${host_state}=  Redfish Get Boot Progress
1028
1029    Should Not Be Equal  ${boot_progress}   ${boot_state}
1030
1031
1032Is Boot Progress At Required State
1033    [Documentation]  Get BootProgress state and expect boot state to match.
1034    [Arguments]  ${boot_state}=None
1035
1036    # Description of argument(s):
1037    # boot_state   Value of the BootProgress state to match.
1038
1039    ${boot_progress}  ${host_state}=  Redfish Get Boot Progress
1040
1041    Should Be Equal  ${boot_progress}   ${boot_state}
1042
1043
1044Is Boot Progress At Any State
1045    [Documentation]  Get BootProgress state and expect boot state to match
1046    ...              with any of the states mentioned in the list.
1047    [Arguments]  ${boot_states}=@{BOOT_PROGRESS_STATES}
1048
1049    # Description of argument(s):
1050    # boot_states   List of the BootProgress states to match.
1051
1052    ${boot_progress}  ${host_state}=  Redfish Get Boot Progress
1053    Should Contain Any  ${boot_progress}  @{boot_states}
1054
1055
1056Is Host At State
1057    [Documentation]  Get Host state and check if it matches
1058    ...   user input expected state.
1059    [Arguments]  ${expected_host_state}
1060
1061    # Description of argument(s):
1062    # expected_host_state  Expected Host State to check.(e.g. Quiesced).
1063
1064    ${boot_progress}  ${host_state}=  Redfish Get Boot Progress
1065
1066    Should Be Equal  ${host_state}  ${expected_host_state}
1067
1068
1069Set Watchdog Interval Using Busctl
1070    [Documentation]  Set Watchdog time interval.
1071    [Arguments]  ${milliseconds}=1000
1072
1073    # Description of argument(s):
1074    # milliseconds     Time interval for watchdog timer
1075
1076    ${cmd}=  Catenate  busctl set-property xyz.openbmc_project.Watchdog
1077    ...                /xyz/openbmc_project/watchdog/host0
1078    ...                xyz.openbmc_project.State.Watchdog Interval t ${milliseconds}
1079    BMC Execute Command  ${cmd}
1080
1081
1082Stop PLDM Service And Wait
1083    [Documentation]  Stop PLDM service and wait for Host to initiate reset.
1084
1085    BMC Execute Command  systemctl stop pldmd.service
1086
1087
1088Get BIOS Attribute
1089    [Documentation]  Get the BIOS attribute for /redfish/v1/Systems/system/Bios.
1090
1091    # Python module:  get_member_list(resource_path)
1092    ${systems}=  Redfish_Utils.Get Member List  /redfish/v1/Systems
1093    ${bios_attr_dict}=  Redfish.Get Attribute  ${systems[0]}/Bios  Attributes
1094
1095    RETURN  ${bios_attr_dict}
1096
1097
1098Set BIOS Attribute
1099    [Documentation]  PATCH the BIOS attribute for /redfish/v1/Systems/system/Bios.
1100    [Arguments]  ${attribute_name}  ${attribute_value}
1101
1102    # Description of argument(s):
1103    # attribute_name     Any valid BIOS attribute.
1104    # attribute_value    Valid allowed attribute values.
1105
1106    # Python module:  get_member_list(resource_path)
1107    ${systems}=  Redfish_Utils.Get Member List  /redfish/v1/Systems
1108    Redfish.Patch  ${systems[0]}/Bios/Settings  body={"Attributes":{"${attribute_name}":"${attribute_value}"}}
1109
1110
1111Is BMC Operational
1112    [Documentation]  Check if BMC is enabled.
1113    [Teardown]  Redfish.Logout
1114
1115    Wait Until Keyword Succeeds  5 min  5 sec  Ping Host  ${OPENBMC_HOST}
1116    # In some of bmc stack, network services will gets loaded before redfish/ipmi services gets loaded.
1117    # Hence, 3mins sleep time is added to allow other service gets loaded.
1118    Sleep  180s
1119    Redfish.login
1120    ${bmc_status}=  Redfish Get BMC State
1121    Should Be Equal  ${bmc_status}  Enabled
1122
1123
1124PLDM Set BIOS Attribute
1125    [Documentation]  Set the BIOS attribute via pldmtool and verify the attribute is set.
1126    ...              Defaulted for fw_boot_side for boot test usage caller.
1127    [Arguments]  ${attribute_name}=fw_boot_side  ${attribute_value}=Temp
1128
1129    # Description of argument(s):
1130    # attribute_name      Valid BIOS attribute name e.g ("fw_boot_side")
1131    # attribute_value     Valid BIOS attribute value for fw_boot_side.
1132
1133    # PLDM response output example:
1134    # {
1135    #    "Response": "SUCCESS"
1136    # }
1137
1138    ${resp}=  pldmtool  bios SetBIOSAttributeCurrentValue -a ${attribute_name} -d ${attribute_value}
1139    Should Be Equal As Strings  ${resp["Response"]}  SUCCESS
1140
1141    # PLDM GET output example:
1142    # {
1143    #    "CurrentValue": "Temp"
1144    # }
1145
1146    ${pldm_output}=  PLDM Get BIOS Attribute  ${attribute_name}
1147    Should Be Equal As Strings  ${pldm_output["CurrentValue"]}  ${attribute_value}
1148    ...  msg=Expecting ${attribute_value} but got ${pldm_output["CurrentValue"]}
1149
1150
1151PLDM Get BIOS Attribute
1152    [Documentation]  Get the BIOS attribute via pldmtool for a given attribute and return value.
1153    [Arguments]  ${attribute_name}
1154
1155    # Description of argument(s):
1156    # attribute_name     Valid BIOS attribute name e.g ("fw_boot_side")
1157
1158    ${pldm_output}=  pldmtool  bios GetBIOSAttributeCurrentValueByHandle -a ${attribute_name}
1159    RETURN  ${pldm_output}
1160
1161
1162Verify Host Power State
1163    [Documentation]  Get the Host Power state and compare it with the expected state.
1164    [Arguments]  ${expected_power_state}
1165
1166    # Description of argument(s):
1167    # expected_power_state   State of Host e.g. Off or On.
1168
1169    ${power_state}  ${health_status}=  Redfish Get Host State
1170    Should Be Equal  ${power_state}  ${expected_power_state}
1171
1172
1173Verify Host Is Up
1174    [Documentation]  Verify Host is Up.
1175
1176    Wait Until Keyword Succeeds  3 min  30 sec  Verify Host Power State  On
1177    # Python module:  os_execute(cmd)
1178    Wait Until Keyword Succeeds  10 min  30 sec  OS Execute Command  uptime
1179