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