xref: /openbmc/openbmc-test-automation/lib/utils.robot (revision 0fd3b249bfcdf993f88e38828da2e8bd66844ecf)
1*** Settings ***
2Resource                ../lib/resource.txt
3Resource                ../lib/rest_client.robot
4Resource                ../lib/connection_client.robot
5Library                 DateTime
6Library                 Process
7Library                 OperatingSystem
8Library                 gen_print.py
9Library                 gen_robot_print.py
10
11*** Variables ***
12${SYSTEM_SHUTDOWN_TIME}       ${5}
13${dbuscmdBase}
14...  dbus-send --system --print-reply --dest=org.openbmc.settings.Host
15${dbuscmdGet}
16...  /org/openbmc/settings/host0  org.freedesktop.DBus.Properties.Get
17${dbuscmdString}=   string:"org.openbmc.settings.Host" string:
18
19# Assign default value to QUIET for programs which may not define it.
20${QUIET}  ${0}
21
22*** Keywords ***
23Wait For Host To Ping
24    [Arguments]  ${host}  ${timeout}=${OPENBMC_REBOOT_TIMEOUT}min
25    ...          ${interval}=5 sec
26
27    # host      The DNS name or IP of the host to ping.
28    # timeout   The amount of time after which attempts to ping cease.
29    # interval  The amount of time in between attempts to ping.
30
31    Wait Until Keyword Succeeds  ${timeout}  ${interval}  Ping Host  ${host}
32
33Ping Host
34    [Arguments]     ${host}
35    Should Not Be Empty    ${host}   msg=No host provided
36    ${RC}   ${output}=     Run and return RC and Output    ping -c 4 ${host}
37    Log     RC: ${RC}\nOutput:\n${output}
38    Should be equal     ${RC}   ${0}
39
40Get Boot Progress
41    [Arguments]  ${quiet}=${QUIET}
42
43    ${state}=  Read Attribute  /org/openbmc/sensors/host/BootProgress
44    ...  value  quiet=${quiet}
45    [return]  ${state}
46
47Is Power On
48    ${state}=  Get Power State
49    Should be equal  ${state}  ${1}
50
51Is Power Off
52    ${state}=  Get Power State
53    Should be equal  ${state}  ${0}
54
55Initiate Power On
56    [Documentation]  Initiates the power on and waits until the Is Power On
57    ...  keyword returns that the power state has switched to on.
58    [Arguments]  ${wait}=${1}
59
60    @{arglist}=   Create List
61    ${args}=     Create Dictionary    data=@{arglist}
62    ${resp}=  Call Method  /org/openbmc/control/chassis0/  powerOn
63    ...  data=${args}
64    should be equal as strings      ${resp.status_code}     ${HTTP_OK}
65
66    # Does caller want to wait for power on status?
67    Run Keyword If  '${wait}' == '${0}'  Return From Keyword
68    Wait Until Keyword Succeeds  3 min  10 sec  Is Power On
69
70Initiate Power Off
71    [Documentation]  Initiates the power off and waits until the Is Power Off
72    ...  keyword returns that the power state has switched to off.
73    @{arglist}=   Create List
74    ${args}=     Create Dictionary    data=@{arglist}
75    ${resp}=  Call Method  /org/openbmc/control/chassis0/  powerOff
76    ...  data=${args}
77    should be equal as strings      ${resp.status_code}     ${HTTP_OK}
78    Wait Until Keyword Succeeds  1 min  10 sec  Is Power Off
79
80Trigger Warm Reset
81    log to console    "Triggering warm reset"
82    ${data}=   create dictionary   data=@{EMPTY}
83    ${resp}=  openbmc post request  /org/openbmc/control/bmc0/action/warmReset
84    ...  data=${data}
85    Should Be Equal As Strings      ${resp.status_code}     ${HTTP_OK}
86    ${session_active}=   Check If warmReset is Initiated
87    Run Keyword If   '${session_active}' == '${True}'
88    ...    Fail   msg=warm reset didn't occur
89
90    Sleep   ${SYSTEM_SHUTDOWN_TIME}min
91    Wait For Host To Ping   ${OPENBMC_HOST}
92
93Check OS
94    [Documentation]  Attempts to ping the host OS and then checks that the host
95    ...              OS is up by running an SSH command.
96
97    [Arguments]  ${os_host}=${OS_HOST}  ${os_username}=${OS_USERNAME}
98    ...          ${os_password}=${OS_PASSWORD}  ${quiet}=${QUIET}
99    ...          ${print_string}=${EMPTY}
100    [Teardown]  Close Connection
101
102    # os_host           The DNS name/IP of the OS host associated with our BMC.
103    # os_username       The username to be used to sign on to the OS host.
104    # os_password       The password to be used to sign on to the OS host.
105    # quiet             Indicates whether this keyword should write to console.
106    # print_string      A string to be printed before checking the OS.
107
108    rprint  ${print_string}
109
110    # Attempt to ping the OS. Store the return code to check later.
111    ${ping_rc}=  Run Keyword and Return Status  Ping Host  ${os_host}
112
113    Open connection  ${os_host}
114
115    ${status}  ${msg}=  Run Keyword And Ignore Error  Login  ${os_username}
116    ...  ${os_password}
117    ${err_msg1}=  Sprint Error  ${msg}
118    ${err_msg}=  Catenate  SEPARATOR=  \n  ${err_msg1}
119    Run Keyword If  '${status}' == 'FAIL'  Fail  msg=${err_msg}
120    ${output}  ${stderr}  ${rc}=  Execute Command  uptime  return_stderr=True
121    ...        return_rc=True
122
123    ${temp_msg}=  Catenate  Could not execute a command on the operating
124    ...  system.\n
125    ${err_msg1}=  Sprint Error  ${temp_msg}
126    ${err_msg}=  Catenate  SEPARATOR=  \n  ${err_msg1}
127
128    # If the return code returned by "Execute Command" is non-zero, this
129    # keyword will fail.
130    Should Be Equal  ${rc}  ${0}  msg=${err_msg}
131    # We will likewise fail if there is any stderr data.
132    Should Be Empty  ${stderr}
133
134    ${temp_msg}=  Set Variable  Could not ping the operating system.\n
135    ${err_msg1}=  Sprint Error  ${temp_msg}
136    ${err_msg}=  Catenate  SEPARATOR=  \n  ${err_msg1}
137    # We will likewise fail if the OS did not ping, as we could SSH but not
138    # ping
139    Should Be Equal As Strings  ${ping_rc}  ${TRUE}  msg=${err_msg}
140
141Wait for OS
142    [Documentation]  Waits for the host OS to come up via calls to "Check OS".
143    [Arguments]  ${os_host}=${OS_HOST}  ${os_username}=${OS_USERNAME}
144    ...          ${os_password}=${OS_PASSWORD}  ${timeout}=${OS_WAIT_TIMEOUT}
145    ...          ${quiet}=${0}
146    [Teardown]  rprintn
147
148    # os_host           The DNS name or IP of the OS host associated with our
149    #                   BMC.
150    # os_username       The username to be used to sign on to the OS host.
151    # os_password       The password to be used to sign on to the OS host.
152    # timeout           The timeout in seconds indicating how long you're
153    #                   willing to wait for the OS to respond.
154    # quiet             Indicates whether this keyword should write to console.
155
156    # The interval to be used between calls to "Check OS".
157    ${interval}=  Set Variable  5
158
159    ${message}=  Catenate  Checking every ${interval} seconds for up to
160    ...  ${timeout} seconds for the operating system to communicate.
161    rqprint_timen  ${message}
162
163    Wait Until Keyword Succeeds  ${timeout} sec  ${interval}  Check OS
164    ...                          ${os_host}  ${os_username}  ${os_password}
165    ...                          print_string=\#
166
167    rqprintn
168
169    rqprint_timen  The operating system is now communicating.
170
171Get BMC State
172    [Documentation]  Returns the state of the BMC as a string. (i.e: BMC_READY)
173    [Arguments]  ${quiet}=${QUIET}
174
175    @{arglist}=  Create List
176    ${args}=  Create Dictionary  data=@{arglist}
177    ${resp}=  Call Method  /org/openbmc/managers/System/  getSystemState
178    ...        data=${args}  quiet=${quiet}
179    Should be equal as strings  ${resp.status_code}  ${HTTP_OK}
180    ${content}=  to json  ${resp.content}
181    [return]  ${content["data"]}
182
183Get Power State
184    [Documentation]  Returns the power state as an integer. Either 0 or 1.
185    [Arguments]  ${quiet}=${QUIET}
186
187    @{arglist}=  Create List
188    ${args}=  Create Dictionary  data=@{arglist}
189
190    ${resp}=  Call Method  /org/openbmc/control/chassis0/  getPowerState
191    ...        data=${args}  quiet=${quiet}
192    Should be equal as strings  ${resp.status_code}  ${HTTP_OK}
193    ${content}=  to json  ${resp.content}
194    [return]  ${content["data"]}
195
196Clear BMC Record Log
197    [Documentation]  Clears all the event logs on the BMC. This would be
198    ...              equivalent to ipmitool sel clear.
199    @{arglist}=   Create List
200    ${args}=     Create Dictionary    data=@{arglist}
201    ${resp}=  Call Method  /org/openbmc/records/events/  clear  data=${args}
202    should be equal as strings      ${resp.status_code}     ${HTTP_OK}
203
204Copy PNOR to BMC
205    Import Library      SCPLibrary      WITH NAME       scp
206    Open Connection for SCP
207    Log    Copying ${PNOR_IMAGE_PATH} to /tmp
208    scp.Put File    ${PNOR_IMAGE_PATH}   /tmp
209
210Flash PNOR
211    [Documentation]    Calls flash bios update method to flash PNOR image
212    [arguments]    ${pnor_image}
213    @{arglist}=   Create List    ${pnor_image}
214    ${args}=     Create Dictionary    data=@{arglist}
215    ${resp}=  Call Method  /org/openbmc/control/flash/bios/  update
216    ...  data=${args}
217    should be equal as strings      ${resp.status_code}     ${HTTP_OK}
218    Wait Until Keyword Succeeds    2 min   10 sec    Is PNOR Flashing
219
220Get Flash BIOS Status
221    [Documentation]  Returns the status of the flash BIOS API as a string. For
222    ...              example 'Flashing', 'Flash Done', etc
223    ${data}=      Read Properties     /org/openbmc/control/flash/bios
224    [return]    ${data['status']}
225
226Is PNOR Flashing
227    [Documentation]  Get BIOS 'Flashing' status. This indicates that PNOR
228    ...              flashing has started.
229    ${status}=    Get Flash BIOS Status
230    should be equal as strings     ${status}     Flashing
231
232Is PNOR Flash Done
233    [Documentation]  Get BIOS 'Flash Done' status.  This indicates that the
234    ...              PNOR flashing has completed.
235    ${status}=    Get Flash BIOS Status
236    should be equal as strings     ${status}     Flash Done
237
238Is System State Host Booted
239    [Documentation]  Checks whether system state is HOST_BOOTED.
240    ${state}=    Get BMC State
241    should be equal as strings     ${state}     HOST_BOOTED
242
243Verify Ping and REST Authentication
244    ${l_ping}=   Run Keyword And Return Status
245    ...    Ping Host  ${OPENBMC_HOST}
246    Run Keyword If  '${l_ping}' == '${False}'
247    ...    Fail   msg=Ping Failed
248
249    ${l_rest}=   Run Keyword And Return Status
250    ...    Initialize OpenBMC
251    Run Keyword If  '${l_rest}' == '${False}'
252    ...    Fail   msg=REST Authentication Failed
253
254    # Just to make sure the SSH is working for SCP
255    Open Connection And Log In
256    ${system}   ${stderr}=    Execute Command   hostname   return_stderr=True
257    Should Be Empty     ${stderr}
258
259Check If BMC is Up
260    [Documentation]  Wait for Host to be online. Checks every X seconds
261    ...              interval for Y minutes and fails if timed out.
262    ...              Default MAX timedout is 10 min, interval 10 seconds.
263    [arguments]      ${max_timeout}=${OPENBMC_REBOOT_TIMEOUT} min
264    ...              ${interval}=10 sec
265
266    Wait Until Keyword Succeeds
267    ...   ${max_timeout}  ${interval}   Verify Ping and REST Authentication
268
269
270Check If warmReset is Initiated
271    [Documentation]  Ping would be still alive, so try SSH to connect
272    ...              if fails the ports are down indicating reboot
273    ...              is in progress
274    ${alive}=   Run Keyword and Return Status
275    ...    Open Connection And Log In
276    Return From Keyword If   '${alive}' == '${False}'    ${False}
277    [return]    ${True}
278
279Flush REST Sessions
280    [Documentation]   Removes all the active session objects
281    Delete All Sessions
282
283Initialize DBUS cmd
284    [Documentation]  Initialize dbus string with property string to extract
285    [arguments]   ${boot_property}
286    ${cmd}=     Catenate  ${dbuscmdBase} ${dbuscmdGet} ${dbuscmdString}
287    ${cmd}=     Catenate  ${cmd}${boot_property}
288    Set Global Variable   ${dbuscmd}     ${cmd}
289
290
291Start SOL Console Logging
292    [Documentation]   Start logging to a file in /tmp so that it can
293    ...               be read by any other test cases. Stop existing
294    ...               running client processes if there is any.
295    ...               By default logging at /tmp/obmc-console.log else
296    ...               user input location.
297    ...               The File is appended with datetime and pid of
298    ...               process which created this log file.
299    [Arguments]       ${file_path}=/tmp/obmc-console.log
300
301    Open Connection And Log In
302
303    ${cur_time}=    Get Time Stamp
304    Set Global Variable   ${LOG_TIME}   ${cur_time}
305    Start Command
306    ...  obmc-console-client > ${file_path}-${LOG_TIME}_$$
307
308
309Stop SOL Console Logging
310    [Documentation]   Login to BMC and Stop the obmc-console-client process.
311    ...               Find the pids from the log to filter the one started by
312    ...               specific test datetime and stop that process only.
313    ...               Ignore if there is no process running and return message
314    ...               "No obmc-console-client process running"
315    ...               By default retrieving log from /tmp/obmc-console.log else
316    ...               user input location.
317    [Arguments]       ${file_path}=/tmp/obmc-console.log
318
319    Open Connection And Log In
320
321    ${pid}  ${stderr}=
322    ...  Execute Command
323    ...  ls ${file_path}-${LOG_TIME}_* | cut -d'_' -f 2
324    ...  return_stderr=True
325    Should Be Empty     ${stderr}
326
327    ${rc}=
328    ...  Execute Command
329    ...  ps ax | grep ${pid} | grep -v grep
330    ...  return_stdout=False  return_rc=True
331
332    Return From Keyword If   '${rc}' == '${1}'
333    ...   No obmc-console-client process running
334
335    ${console}  ${stderr}=
336    ...  Execute Command   kill -s KILL ${pid}
337    ...  return_stderr=True
338    Should Be Empty     ${stderr}
339    Log  Current Client PID:${pid}
340
341    ${console}  ${stderr}=
342    ...  Execute Command
343    ...  cat ${file_path}-${LOG_TIME}_${pid}
344    ...  return_stderr=True
345    Should Be Empty     ${stderr}
346
347    [Return]    ${console}
348
349
350Get Time Stamp
351    [Documentation]     Get the current time stamp data
352    ${cur_time}=    Get Current Date   result_format=%Y%m%d%H%M%S%f
353    [return]   ${cur_time}
354
355
356Verify BMC State
357    [Documentation]   Get the BMC state and verify if the current
358    ...               BMC state is as expected.
359    [Arguments]       ${expected}
360
361    ${current}=  Get BMC State
362    Should Contain  ${expected}   ${current}
363
364Start Journal Log
365    [Documentation]   Start capturing journal log to a file in /tmp using
366    ...               journalctl command. By default journal log is collected
367    ...               at /tmp/journal_log else user input location.
368    ...               The File is appended with datetime.
369    [Arguments]       ${file_path}=/tmp/journal_log
370
371    Open Connection And Log In
372
373    ${cur_time}=    Get Time Stamp
374    Set Global Variable   ${LOG_TIME}   ${cur_time}
375    Start Command
376    ...  journalctl -f > ${file_path}-${LOG_TIME}
377    Log    Journal Log Started: ${file_path}-${LOG_TIME}
378
379Stop Journal Log
380    [Documentation]   Stop journalctl process if its running.
381    ...               By default return log from /tmp/journal_log else
382    ...               user input location.
383    [Arguments]       ${file_path}=/tmp/journal_log
384
385    Open Connection And Log In
386
387    ${rc}=
388    ...  Execute Command
389    ...  ps ax | grep journalctl | grep -v grep
390    ...  return_stdout=False  return_rc=True
391
392    Return From Keyword If   '${rc}' == '${1}'
393    ...   No journal log process running
394
395    ${output}  ${stderr}=
396    ...  Execute Command   killall journalctl
397    ...  return_stderr=True
398    Should Be Empty     ${stderr}
399
400    ${journal_log}  ${stderr}=
401    ...  Execute Command
402    ...  cat ${file_path}-${LOG_TIME}
403    ...  return_stderr=True
404    Should Be Empty     ${stderr}
405
406    Log    ${journal_log}
407
408    Execute Command    rm ${file_path}-${LOG_TIME}
409
410    [Return]    ${journal_log}
411
412Mac Address To Hex String
413    [Documentation]   Converts MAC address into hex format.
414    ...               Example
415    ...               Given the following MAC: 00:01:6C:80:02:78
416    ...               This keyword will return: 0x00 0x01 0x6C 0x80 0x02 0x78
417    ...               Description of arguments:
418    ...               i_macaddress  MAC address in the following format 00:01:6C:80:02:78
419    [Arguments]    ${i_macaddress}
420
421    ${mac_hex}=  Catenate  0x${i_macaddress.replace(':', ' 0x')}
422    [return]    ${mac_hex}
423
424IP Address To Hex String
425    [Documentation]   Converts IP address into hex format.
426    ...               Example:
427    ...               Given the following IP: 10.3.164.100
428    ...               This keyword will return: 0xa 0x3 0xa4 0xa0
429    ...               Description of arguments:
430    ...               i_ipaddress  IP address in the following format 10.10.10.10
431    [Arguments]    ${i_ipaddress}
432
433    @{ip}=  Split String  ${i_ipaddress}    .
434    ${index}=  Set Variable  ${0}
435
436    :FOR    ${item}     IN      @{ip}
437    \   ${hex}=  Convert To Hex    ${item}    prefix=0x    lowercase=yes
438    \   Set List Value    ${ip}    ${index}    ${hex}
439    \   ${index}=  Set Variable    ${index + 1}
440    ${ip_hex}=  Catenate    @{ip}
441    [return]    ${ip_hex}
442