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