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