xref: /openbmc/openbmc-test-automation/lib/common_utils.robot (revision c8d6b2bed0034aa1ebb082e4de79f01aa8f6a4d1)
1*** Settings ***
2
3Documentation  Utilities for Robot keywords that do not use REST.
4
5Resource                ../lib/resource.robot
6Resource                ../lib/connection_client.robot
7Resource                ../lib/boot_utils.robot
8Library                 String
9Library                 DateTime
10Library                 Process
11Library                 OperatingSystem
12Library                 gen_print.py
13Library                 gen_robot_print.py
14Library                 gen_cmd.py
15Library                 gen_robot_valid.py
16Library                 gen_robot_keyword.py
17Library                 bmc_ssh_utils.py
18Library                 utils.py
19Library                 var_funcs.py
20Library                 SCPLibrary  AS  scp
21
22*** Variables ***
23
24${pflash_cmd}             /usr/sbin/pflash -r /dev/stdout -P VERSION
25
26# Assign default value to QUIET for programs which may not define it.
27${QUIET}  ${0}
28
29${bmc_mem_free_cmd}=   free | tr -s ' ' | sed '/^Mem/!d' | cut -d" " -f4
30${bmc_mem_total_cmd}=   free | tr -s ' ' | sed '/^Mem/!d' | cut -d" " -f2
31${bmc_cpu_usage_cmd}=   top -n 1  | grep CPU: | cut -c 7-9
32${HOST_SETTING}    ${SETTINGS_URI}host0
33
34# /run/initramfs/ro associate filesystem  should be 100% full always
35${bmc_file_system_usage_cmd}=  df -h | cut -c 52-54 | grep 100 | wc -l
36${total_pnor_ro_file_system_cmd}=  df -h | grep /media/pnor-ro | wc -l
37${total_bmc_ro_file_system_cmd}=  df -h | grep /media/rofs | wc -l
38
39${BOOT_TIME}     ${0}
40${BOOT_COUNT}    ${0}
41${count}  ${0}
42${devicetree_base}  /sys/firmware/devicetree/base/model
43
44# Initialize default debug value to 0.
45${DEBUG}         ${0}
46
47${probe_cpu_tool_path}     ${EXECDIR}/tools/ras/probe_cpus.sh
48${scom_addrs_tool_path}    ${EXECDIR}/tools/ras/scom_addr_p9.sh
49${target_file_path}        /root/
50
51${default_tarball}  ${EXECDIR}/obmc-phosphor-debug-tarball-witherspoon.tar.xz
52
53# These variables are used to straddle between new and old methods of setting
54# values.
55${bmc_power_policy_method}        ${EMPTY}
56@{valid_power_policy_vars}        RESTORE_LAST_STATE  ALWAYS_POWER_ON
57...                               ALWAYS_POWER_OFF
58
59${check_performance}              ${1}
60
61
62*** Keywords ***
63
64Check BMC Performance
65    [Documentation]  Check BMC basic CPU Mem File system performance.
66
67    Return From Keyword If   not ${check_performance}
68
69    Check BMC CPU Performance
70    Check BMC Mem Performance
71    Check BMC File System Performance
72
73
74Verify PNOR Update
75    [Documentation]  Verify that the PNOR is not corrupted.
76    # Example:
77    # FFS: Flash header not found. Code: 100
78    # Error 100 opening ffs !
79
80    ${stdout}  ${stderr}  ${rc}=
81    ...  BMC Execute Command  /usr/sbin/pflash -h | egrep -q skip
82    ...  ignore_err=${1}
83    ${pflash_cmd}=  Set Variable If  ${rc} == ${0}  ${pflash_cmd} --skip=4096
84    ...  ${pflash_cmd}
85    ${pnor_info}=  BMC Execute Command  ${pflash_cmd}
86    Should Not Contain Any  ${pnor_info}  Flash header not found  Error
87
88
89Get BMC System Model
90    [Documentation]  Get the BMC model from the device tree and return it.
91
92    ${bmc_model}  ${stderr}  ${rc}=  BMC Execute Command
93    ...  cat ${devicetree_base} | cut -d " " -f 1  return_stderr=True
94    ...  test_mode=0
95    Should Be Empty  ${stderr}
96    Should Not Be Empty  ${bmc_model}  msg=BMC model is empty.
97    RETURN  ${bmc_model}
98
99
100Verify BMC System Model
101    [Documentation]  Verify the BMC model with ${OPENBMC_MODEL}.
102    [Arguments]  ${bmc_model}
103
104    # Description of argument(s):
105    # bmc_model System model (e.g. "witherspoon").
106
107    ${tmp_bmc_model}=  Fetch From Right  ${OPENBMC_MODEL}  /
108    ${tmp_bmc_model}=  Fetch From Left  ${tmp_bmc_model}  .
109    ${ret}=  Run Keyword And Return Status  Should Contain  ${bmc_model}
110    ...  ${tmp_bmc_model}  ignore_case=True
111    RETURN  ${ret}
112
113
114Wait For Host To Ping
115    [Documentation]  Wait for the given host to ping.
116    [Arguments]  ${host}  ${timeout}=${OPENBMC_REBOOT_TIMEOUT}min
117    ...          ${interval}=5 sec  ${expected_rc}=${0}
118
119    # Description of argument(s):
120    # host           The host name or IP of the host to ping.
121    # timeout        The amount of time after which ping attempts cease.
122    #                This should be expressed in Robot Framework's time format
123    #                (e.g. "10 seconds").
124    # interval       The amount of time in between attempts to ping.
125    #                This should be expressed in Robot Framework's time format
126    #                (e.g. "5 seconds").
127    # expected_rc    Expected return code of ping command.
128
129    Wait Until Keyword Succeeds  ${timeout}  ${interval}  Ping Host  ${host}  ${expected_rc}
130
131
132Ping Host
133    [Documentation]  Ping the given host.
134    [Arguments]     ${host}  ${expected_rc}=${0}
135
136    # Description of argument(s):
137    # host           The host name or IP of the host to ping.
138    # expected_rc    Expected return code of ping command.
139
140    Should Not Be Empty    ${host}   msg=No host provided
141    ${rc}   ${output}=     Run and return RC and Output    ping -c 4 ${host}
142    Log     RC: ${rc}\nOutput:\n${output}
143    Should be equal     ${rc}   ${expected_rc}
144
145
146Check OS
147    [Documentation]  Attempts to ping the host OS and then checks that the host
148    ...              OS is up by running an SSH command.
149
150    [Arguments]  ${os_host}=${OS_HOST}  ${os_username}=${OS_USERNAME}
151    ...          ${os_password}=${OS_PASSWORD}  ${quiet}=${QUIET}
152    ...          ${print_string}=${EMPTY}
153    [Teardown]  SSHLibrary.Close Connection
154
155    # Description of argument(s):
156    # os_host           The DNS name/IP of the OS host associated with our BMC.
157    # os_username       The username to be used to sign on to the OS host.
158    # os_password       The password to be used to sign on to the OS host.
159    # quiet             Indicates whether this keyword should write to console.
160    # print_string      A string to be printed before checking the OS.
161
162    Log To Console  ${print_string}  no_newline=True
163
164    # Attempt to ping the OS. Store the return code to check later.
165    ${ping_rc}=  Run Keyword and Return Status  Ping Host  ${os_host}
166
167    SSHLibrary.Open connection  ${os_host}
168
169    ${status}  ${msg}=  Run Keyword And Ignore Error  SSHLibrary.Login  ${os_username}
170    ...  ${os_password}
171    ${err_msg1}=  Sprint Error  ${msg}
172    ${err_msg}=  Catenate  SEPARATOR=  \n  ${err_msg1}
173    Run Keyword If  '${status}' == 'FAIL'  Fail  msg=${err_msg}
174    ${output}  ${stderr}  ${rc}=  Execute Command  uptime  return_stderr=True
175    ...        return_rc=True
176
177    ${temp_msg}=  Catenate  Could not execute a command on the operating
178    ...  system.\n
179    ${err_msg1}=  Sprint Error  ${temp_msg}
180    ${err_msg}=  Catenate  SEPARATOR=  \n  ${err_msg1}
181
182    # If the return code returned by "Execute Command" is non-zero, this
183    # keyword will fail.
184    Should Be Equal  ${rc}  ${0}  msg=${err_msg}
185    # We will likewise fail if there is any stderr data.
186    Should Be Empty  ${stderr}
187
188    ${temp_msg}=  Set Variable  Could not ping the operating system.\n
189    ${err_msg1}=  Sprint Error  ${temp_msg}
190    ${err_msg}=  Catenate  SEPARATOR=  \n  ${err_msg1}
191    # We will likewise fail if the OS did not ping, as we could SSH but not
192    # ping
193    Should Be Equal As Strings  ${ping_rc}  ${TRUE}  msg=${err_msg}
194
195
196Wait for OS
197    [Documentation]  Waits for the host OS to come up via calls to "Check OS".
198    [Arguments]  ${os_host}=${OS_HOST}  ${os_username}=${OS_USERNAME}
199    ...          ${os_password}=${OS_PASSWORD}  ${timeout}=${OS_WAIT_TIMEOUT}
200    ...          ${quiet}=${0}
201    [Teardown]  Printn
202
203    # Description of argument(s):
204    # os_host           The DNS name or IP of the OS host associated with our
205    #                   BMC.
206    # os_username       The username to be used to sign on to the OS host.
207    # os_password       The password to be used to sign on to the OS host.
208    # timeout           The timeout in seconds indicating how long you're
209    #                   willing to wait for the OS to respond.
210    # quiet             Indicates whether this keyword should write to console.
211
212    # The interval to be used between calls to "Check OS".
213    ${interval}=  Set Variable  5
214
215    ${message}=  Catenate  Checking every ${interval} seconds for up to
216    ...  ${timeout} seconds for the operating system to communicate.
217    Qprint Timen  ${message}
218
219    Wait Until Keyword Succeeds  ${timeout} sec  ${interval}  Check OS
220    ...                          ${os_host}  ${os_username}  ${os_password}
221    ...                          print_string=\#
222
223    Qprintn
224
225    Qprint Timen  The operating system is now communicating.
226
227
228Copy PNOR to BMC
229    [Documentation]  Copy the PNOR image to the BMC.
230    Import Library      SCPLibrary      AS       scp
231    Open Connection for SCP
232    Log    Copying ${PNOR_IMAGE_PATH} to /tmp
233    scp.Put File    ${PNOR_IMAGE_PATH}   /tmp
234
235
236Is OS Starting
237    [Documentation]  Check if boot progress is OS starting.
238    ${boot_progress}=  Get Boot Progress
239    Should Be Equal  ${boot_progress}  OSStart
240
241
242Is OS Off
243    [Documentation]  Check if boot progress is "Off".
244    ${boot_progress}=  Get Boot Progress
245    Should Be Equal  ${boot_progress}  Off
246
247
248Get Boot Progress To OS Starting State
249    [Documentation]  Get the system to a boot progress state of 'FW Progress,
250    ...  Starting OS'.
251
252    ${boot_progress}=  Get Boot Progress
253    Run Keyword If  '${boot_progress}' == 'OSStart'
254    ...  Log  Host is already in OS starting state
255    ...  ELSE
256    ...  Run Keywords  Initiate Host PowerOff  AND  Initiate Host Boot
257    ...  AND  Wait Until Keyword Succeeds  10 min  10 sec  Is OS Starting
258
259
260Check If warmReset is Initiated
261    [Documentation]  Ping would be still alive, so try SSH to connect
262    ...              if fails the ports are down indicating reboot
263    ...              is in progress
264
265    # Warm reset adds 3 seconds delay before forcing reboot
266    # To minimize race conditions, we wait for 7 seconds
267    Sleep  7s
268    ${alive}=   Run Keyword and Return Status
269    ...    Open Connection And Log In
270    Return From Keyword If   '${alive}' == '${False}'    ${False}
271    RETURN    ${True}
272
273
274Create OS Console Command String
275    [Documentation]  Return a command string to start OS console logging.
276
277    # First make sure that the ssh_pw program is available.
278    ${cmd}=  Catenate  which ssh_pw 2>/dev/null || find
279    ...  ${EXECDIR} -name 'ssh_pw'
280
281    Dprint Issuing  ${cmd}
282    ${rc}  ${output}=  Run And Return Rc And Output  ${cmd}
283    Rdpvars  rc  output
284
285    Should Be Equal As Integers  0  ${rc}  msg=Could not find ssh_pw.
286
287    ${ssh_pw_file_path}=  Set Variable  ${output}
288
289    ${cmd}=  Catenate  ${ssh_pw_file_path} ${OPENBMC_PASSWORD} -p ${HOST_SOL_PORT}
290    ...  -o "StrictHostKeyChecking no" ${OPENBMC_USERNAME}@${OPENBMC_HOST} ${OPENBMC_CONSOLE_CLIENT}
291
292    RETURN  ${cmd.strip()}
293
294
295Get SOL Console Pid
296    [Documentation]  Get the pid of the active SOL console job.
297    [Arguments]  ${expect_running}=${0}  ${log_file_path}=${EMPTY}
298
299    # Description of argument(s):
300    # expect_running                If set and if no SOL console job is found, print debug info and fail.
301    # log_file_path                 Needed to print debug info if expect_running is set and no pid is found.
302
303    # Find the pid of the active system console logging session (if any).
304    ${search_string}=  Create OS Console Command String
305    # At least in some cases, ps output does not show double quotes so we must
306    # replace them in our search string with the regexes to indicate that they
307    # are optional.
308    ${search_string}=  Replace String  ${search_string}  "  ["]?
309    ${ps_cmd}=  Catenate  ps axwwo user,pid,cmd
310    ${cmd_buf}=  Catenate  echo $(${ps_cmd} | egrep '${search_string}' |
311    ...  egrep -v grep | cut -c10-14)
312    Dprint Issuing  ${cmd_buf}
313    ${rc}  ${os_con_pid}=  Run And Return Rc And Output  ${cmd_buf}
314    Rdpvars  os_con_pid
315    # If rc is not zero it just means that there is no OS Console process
316    # running.
317
318    Return From Keyword If  '${os_con_pid}' != '${EMPTY}'  ${os_con_pid}
319    Return From Keyword If  '${expect_running}' == '${0}'  ${os_con_pid}
320
321    Cmd Fnc  cat ${log_file_path} ; echo ; ${ps_cmd}  quiet=${0}
322    ...  print_output=${1}  show_err=${1}
323    Valid Value  os_con_pid
324
325
326Stop SOL Console Logging
327    [Documentation]  Stop system console logging and return log output.
328    [Arguments]  ${log_file_path}=${EMPTY}
329    ...          ${targ_file_path}=${EXECDIR}${/}logs${/}
330    ...          ${return_data}=${1}
331
332    # If there are multiple system console processes, they will all be stopped.
333    # If there is no existing log file this keyword will return an error
334    # message to that effect (and write that message to targ_file_path, if
335    # specified).
336    # NOTE: This keyword will not fail if there is no running system console
337    # process.
338
339    # Description of arguments:
340    # log_file_path   The file path that was used to call "Start SOL
341    #                 Console Logging".  See that keyword (above) for details.
342    # targ_file_path  If specified, the file path to which the source
343    #                 file path (i.e. "log_file_path") should be copied.
344    # return_data     If this is set to ${1}, this keyword will return the SOL
345    #                 data to the caller as a unicode string.
346
347    ${log_file_path}=  Create OS Console File Path  ${log_file_path}
348
349    ${os_con_pid}=  Get SOL Console Pid
350
351    ${cmd_buf}=  Catenate  kill -9 ${os_con_pid}
352    Run Keyword If  '${os_con_pid}' != '${EMPTY}'  Dprint Issuing  ${cmd_buf}
353    ${rc}  ${output}=  Run Keyword If  '${os_con_pid}' != '${EMPTY}'
354    ...  Run And Return Rc And Output  ${cmd_buf}
355    Run Keyword If  '${os_con_pid}' != '${EMPTY}'  Rdpvars  rc  output
356
357    Run Keyword If  '${targ_file_path}' != '${EMPTY}'
358    ...  Run Keyword And Ignore Error
359    ...  Copy File  ${log_file_path}  ${targ_file_path}
360
361    ${output}=  Set Variable  ${EMPTY}
362    ${loc_quiet}=  Evaluate  ${debug}^1
363    ${rc}  ${output}=  Run Keyword If  '${return_data}' == '${1}'
364    ...  Cmd Fnc  cat ${log_file_path} 2>/dev/null  quiet=${loc_quiet}
365    ...  print_output=${0}  show_err=${0}
366
367    RETURN  ${output}
368
369
370Start SOL Console Logging
371    [Documentation]  Start system console log to file.
372    [Arguments]  ${log_file_path}=${EMPTY}  ${return_data}=${1}
373
374    # This keyword will first call "Stop SOL Console Logging".  Only then will
375    # it start SOL console logging.  The data returned by "Stop SOL Console
376    # Logging" will in turn be returned by this keyword.
377
378    # Description of arguments:
379    # log_file_path   The file path to which system console log data should be
380    #                 written.  Note that this path is taken to be a location
381    #                 on the machine where this program is running rather than
382    #                 on the Open BMC system.
383    # return_data     If this is set to ${1}, this keyword will return any SOL
384    #                 data to the caller as a unicode string.
385
386    ${log_file_path}=  Create OS Console File Path  ${log_file_path}
387
388    ${log_output}=  Stop SOL Console Logging  ${log_file_path}
389    ...  return_data=${return_data}
390
391    # Validate by making sure we can create the file.  Problems creating the
392    # file would not be noticed by the subsequent ssh command because we fork
393    # the command.
394    Create File  ${log_file_path}
395    ${sub_cmd_buf}=  Create OS Console Command String
396    # Routing stderr to stdout so that any startup error text will go to the
397    # output file.
398    ${cmd_buf}=  Catenate  ${sub_cmd_buf} > ${log_file_path} 2>&1 &
399    Dprint Issuing  ${cmd_buf}
400    ${rc}  ${output}=  Run And Return Rc And Output  ${cmd_buf}
401    # Because we are forking this command, we essentially will never get a
402    # non-zero return code or any output.
403    Should Be Equal  ${rc}  ${0}
404
405    Wait Until Keyword Succeeds  10 seconds  0 seconds
406    ...   Get SOL Console Pid  ${1}  ${log_file_path}
407
408    RETURN  ${log_output}
409
410
411Get Time Stamp
412    [Documentation]     Get the current time stamp data
413    ${cur_time}=    Get Current Date   result_format=%Y%m%d%H%M%S%f
414    RETURN   ${cur_time}
415
416
417Start Journal Log
418    [Documentation]   Start capturing journal log to a file in /tmp using
419    ...               journalctl command. By default journal log is collected
420    ...               at /tmp/journal_log else user input location.
421    ...               The File is appended with datetime.
422    [Arguments]       ${file_path}=/tmp/journal_log  ${filter}=${EMPTY}
423
424    # Description of arguments:
425    # file_path   The file path of the journal file.
426
427    ${cur_time}=    Get Time Stamp
428    Set Global Variable   ${LOG_TIME}   ${cur_time}
429    Open Connection And Log In
430    Start Command
431    ...  journalctl -f ${filter} > ${file_path}-${LOG_TIME}
432    Log    Journal Log Started: ${file_path}-${LOG_TIME}
433
434
435Stop Journal Log
436    [Documentation]   Stop journalctl process if its running.
437    ...               By default return log from /tmp/journal_log else
438    ...               user input location.
439    [Arguments]       ${file_path}=/tmp/journal_log
440
441    # Description of arguments:
442    # file_path   The file path of the journal file.
443
444    Open Connection And Log In
445
446    ${rc}=
447    ...  Execute Command
448    ...  ps | grep journalctl | grep -v grep
449    ...  return_stdout=False  return_rc=True
450
451    Return From Keyword If   '${rc}' == '${1}'
452    ...   No journal log process running
453
454    ${output}  ${stderr}=
455    ...  Execute Command   killall journalctl
456    ...  return_stderr=True
457    Should Be Empty     ${stderr}
458
459    ${journal_log}  ${stderr}=
460    ...  Execute Command
461    ...  cat ${file_path}-${LOG_TIME}
462    ...  return_stderr=True
463    Should Be Empty     ${stderr}
464
465    Log    ${journal_log}
466
467    Execute Command    rm ${file_path}-${LOG_TIME}
468
469    RETURN    ${journal_log}
470
471
472Mac Address To Hex String
473    [Documentation]   Converts MAC address into hex format.
474    ...               Example
475    ...               Given the following MAC: 00:01:6C:80:02:78
476    ...               This keyword will return: 0x00 0x01 0x6C 0x80 0x02 0x78
477    ...               Description of arguments:
478    ...               i_macaddress  MAC address in the following format
479    ...               00:01:6C:80:02:78
480    [Arguments]    ${i_macaddress}
481
482    # Description of arguments:
483    # i_macaddress   The MAC address.
484
485    ${mac_hex}=  Catenate  0x${i_macaddress.replace(':', ' 0x')}
486    RETURN    ${mac_hex}
487
488
489IP Address To Hex String
490    [Documentation]   Converts IP address into hex format.
491    ...               Example:
492    ...               Given the following IP: 10.3.164.100
493    ...               This keyword will return: 0xa 0x3 0xa4 0xa0
494    [Arguments]    ${i_ipaddress}
495
496    # Description of arguments:
497    # i_macaddress   The IP address in the format 10.10.10.10.
498
499    @{ip}=  Split String  ${i_ipaddress}    .
500    ${index}=  Set Variable  ${0}
501
502    FOR    ${item}     IN      @{ip}
503        ${hex}=  Convert To Hex    ${item}    prefix=0x    lowercase=yes
504        Set List Value    ${ip}    ${index}    ${hex}
505        ${index}=  Set Variable    ${index + 1}
506    END
507    ${ip_hex}=  Catenate    @{ip}
508
509    RETURN    ${ip_hex}
510
511
512BMC CPU Performance Check
513   [Documentation]   Minimal 10% of proc should be free in this instance
514
515    ${bmc_cpu_usage_output}  ${stderr}  ${rc}=  BMC Execute Command
516    ...  ${bmc_cpu_usage_cmd}
517    ${bmc_cpu_usage_output}  ${stderr}  ${rc}=  BMC Execute Command
518    ...  ${bmc_cpu_usage_cmd}
519    ${bmc_cpu_percentage}=  Fetch From Left  ${bmc_cpu_usage_output}  %
520    Rprint Vars  bmc_cpu_percentage
521    Should be true  ${bmc_cpu_percentage} < 90
522
523
524BMC Mem Performance Check
525    [Documentation]   Minimal 10% of memory should be free in this instance
526
527    ${bmc_mem_free_output}  ${stderr}  ${rc}=   BMC Execute Command
528    ...  ${bmc_mem_free_cmd}
529
530    ${bmc_mem_total_output}  ${stderr}  ${rc}=  BMC Execute Command
531    ...  ${bmc_mem_total_cmd}
532    ${bmc_mem_free_output}  ${stderr}  ${rc}=   BMC Execute Command
533    ...  ${bmc_mem_free_cmd}
534
535    ${bmc_mem_total_output}  ${stderr}  ${rc}=  BMC Execute Command
536    ...  ${bmc_mem_total_cmd}
537
538    ${bmc_mem_percentage}=  Evaluate  ${bmc_mem_free_output}*100
539    ${bmc_mem_percentage}=  Evaluate
540    ...   ${bmc_mem_percentage}/${bmc_mem_total_output}
541    Rprint Vars  bmc_mem_percentage
542    Should be true  ${bmc_mem_percentage} > 10
543
544
545BMC File System Usage Check
546    [Documentation]   Check the file system space. 4 file system should be
547    ...  100% full which is expected
548    # Filesystem            Size    Used Available Use% Mounted on
549    # /dev/root            14.4M     14.4M       0 100% /
550    # /dev/ubiblock0_0     14.4M     14.4M       0 100% /media/rofs-c9249b0e
551    # /dev/ubiblock8_0     19.6M     19.6M       0 100% /media/pnor-ro-8764baa3
552    # /dev/ubiblock4_0     14.4M     14.4M       0 100% /media/rofs-407816c
553    # /dev/ubiblock8_4     21.1M     21.1M       0 100% /media/pnor-ro-cecc64c4
554    ${bmc_fs_usage_output}  ${stderr}  ${rc}=  BMC Execute Command
555    ...  ${bmc_file_system_usage_cmd}
556    ${bmc_pnor_fs_usage_output}  ${stderr}  ${rc}=  BMC Execute Command
557    ...  ${total_pnor_ro_file_system_cmd}
558    ${bmc_bmc_fs_usage_output}  ${stderr}  ${rc}=  BMC Execute Command
559    ...  ${total_bmc_ro_file_system_cmd}
560    ${total_bmc_pnor_image}=  Evaluate
561    ...  ${bmc_pnor_fs_usage_output}+${bmc_bmc_fs_usage_output}
562    # Considering /dev/root also in total 100% used file system
563    ${total_full_fs}=  Evaluate  ${total_bmc_pnor_image}+1
564    Rprint Vars  bmc_fs_usage_output  bmc_pnor_fs_usage_output  bmc_bmc_fs_usage_output
565    ...  total_full_fs
566    Should Be True  ${bmc_fs_usage_output}==${total_full_fs}
567
568
569Check BMC CPU Performance
570    [Documentation]   Minimal 10% of proc should be free in 3 sample
571    FOR  ${var}  IN RANGE  1  4
572        BMC CPU Performance Check
573    END
574
575Check BMC Mem Performance
576    [Documentation]   Minimal 10% of memory should be free
577
578    FOR  ${var}  IN RANGE  1  4
579        BMC Mem Performance Check
580    END
581
582Check BMC File System Performance
583    [Documentation]  Check for file system usage for 4 times
584
585    FOR  ${var}  IN RANGE  1  4
586        BMC File System Usage check
587    END
588
589Get URL List
590    [Documentation]  Return list of URLs under given URL.
591    [Arguments]  ${openbmc_url}
592
593    # Description of argument(s):
594    # openbmc_url  URL for list operation (e.g.
595    #              /xyz/openbmc_project/inventory).
596
597    ${url_list}=  Read Properties  ${openbmc_url}list  quiet=${1}
598    Sort List  ${url_list}
599
600    RETURN  ${url_list}
601
602
603Check Zombie Process
604    [Documentation]    Check if any defunct process exist or not on BMC
605    ${count}  ${stderr}  ${rc}=  Execute Command  ps -o stat | grep Z | wc -l
606    ...    return_stderr=True  return_rc=True
607    Should Be True    ${count}==0
608    Should Be Empty    ${stderr}
609
610
611Prune Journal Log
612    [Documentation]   Prune archived journal logs.
613    [Arguments]   ${vacuum_size}=1M
614
615    # This keyword can be used to prevent the journal
616    # log from filling up the /run filesystem.
617    # This command will retain only the latest logs
618    # of the user specified size.
619
620    # Description of argument(s):
621    # vacuum_size    Size of journal.
622
623    Open Connection And Log In
624    ${output}  ${stderr}  ${rc}=
625    ...  Execute Command
626    ...  journalctl --vacuum-size=${vacuum_size}
627    ...  return_stderr=True  return_rc=True
628
629    Should Be Equal  ${rc}  ${0}  msg=${stderr}
630
631
632Get System Power Policy
633    [Documentation]  Returns the BMC power policy.
634
635    # Set the bmc_power_policy_method to either 'Old' or 'New'.
636    Set Power Policy Method
637    ${cmd_buf}=  Create List  ${bmc_power_policy_method} Get Power Policy
638    # Run the appropriate keyword.
639    ${currentPolicy}=  Run Keyword  @{cmd_buf}
640
641    RETURN  ${currentPolicy}
642
643
644Set BMC Reset Reference Time
645    [Documentation]  Set current boot time as a reference and increment
646    ...              boot count.
647
648    ${cur_btime}=  Get BMC Boot Time
649    Run Keyword If  ${BOOT_TIME} == ${0} and ${BOOT_COUNT} == ${0}
650    ...  Set Global Variable  ${BOOT_TIME}  ${cur_btime}
651    ...  ELSE IF  ${cur_btime} > ${BOOT_TIME}
652    ...  Run Keywords  Set Global Variable  ${BOOT_TIME}  ${cur_btime}
653    ...  AND
654    ...  Set Global Variable  ${BOOT_COUNT}  ${BOOT_COUNT + 1}
655
656
657Get BMC Boot Time
658    [Documentation]  Returns boot time from /proc/stat.
659
660    Open Connection And Log In
661    ${output}  ${stderr}=
662    ...  Execute Command  egrep '^btime ' /proc/stat | cut -f 2 -d ' '
663    ...  return_stderr=True
664    Should Be Empty  ${stderr}
665    ${btime}=  Convert To Integer  ${output}
666    RETURN  ${btime}
667
668
669Enable Core Dump On BMC
670    [Documentation]  Enable core dump collection.
671    ${core_pattern}  ${stderr}  ${rc}=  BMC Execute Command
672    ...  echo '/tmp/core_%e.%p' | tee /proc/sys/kernel/core_pattern
673    Should Be Equal As Strings  ${core_pattern}  /tmp/core_%e.%p
674
675
676Get Number Of BMC Core Dump Files
677    [Documentation]  Returns number of core dump files on BMC.
678    Open Connection And Log In
679    ${num_of_core_dump}=  Execute Command
680    ...  ls /tmp/core* 2>/dev/null | wc -l
681    RETURN  ${num_of_core_dump}
682
683
684Set Core Dump File Size Unlimited
685    [Documentation]  Set core dump file size to unlimited.
686    BMC Execute Command  ulimit -c unlimited
687
688
689Check For Core Dumps
690    [Documentation]  Check for any core dumps exist.
691    ${output}=  Get Number Of BMC Core Dump Files
692    Run Keyword If  ${output} > 0
693    ...  Log  **Warning** BMC core dump files exist  level=WARN
694
695
696Configure Initial Settings
697    [Documentation]  Restore old IP and route.
698    ...  This keyword requires initial settings viz IP address,
699    ...  Network Mask, default gateway and serial console IP and port
700    ...  information which should be provided in command line.
701
702    [Arguments]  ${host}=${OPENBMC_HOST}  ${mask}=${NET_MASK}
703    ...          ${gw_ip}=${GW_IP}
704
705    # Description of arguments:
706    # host  IP address of the OS Host.
707    # mask  Network mask.
708    # gu_ip  Gateway IP address or hostname.
709
710    # Open telnet connection and ignore the error, in case telnet session is
711    # already opened by the program calling this keyword.
712    Run Keyword And Ignore Error  Open Telnet Connection to BMC Serial Console
713    Telnet.write  ifconfig eth0 ${host} netmask ${mask}
714    Telnet.write  route add default gw ${gw_ip}
715
716
717Install Debug Tarball On BMC
718    [Documentation]  Copy the debug tar file to BMC and install.
719    [Arguments]  ${tarball_file_path}=${default_tarball}
720    ...  ${targ_tarball_dir_path}=/tmp/tarball/
721
722    # Description of arguments:
723    # tarball_file_path      Path of the debug tarball file.
724    #                        The tar file is downloaded from the build page
725    #                        https://jenkins.openbmc.org/job/latest-master/
726    #                        obmc-phosphor-debug-tarball-witherspoon.tar.xz
727    #
728    # targ_tarball_dir_path  The directory path where the tarball is to be
729    #                        installed.
730
731    OperatingSystem.File Should Exist  ${tarball_file_path}
732    ...  msg=${tarball_file_path} doesn't exist.
733
734    # Upload the file to BMC.
735    Import Library  SCPLibrary  AS  scp
736    Open Connection for SCP
737    scp.Put File  ${tarball_file_path}  /tmp/debug-tarball.tar.xz
738
739    # Create tarball directory and install.
740    BMC Execute Command  mkdir -p ${targ_tarball_dir_path}
741    BMC Execute Command
742    ...  tar -xf /tmp/debug-tarball.tar.xz -C ${targ_tarball_dir_path}
743
744    # Remove the tarball file from BMC.
745    BMC Execute Command  rm -f /tmp/debug-tarball.tar.xz
746
747
748Get BMC Boot Count
749    [Documentation]  Returns BMC boot count based on boot time.
750    ${cur_btime}=  Get BMC Boot Time
751
752    # Set global variable BOOT_TIME to current boot time if current boot time
753    # is changed. Also increase value of global variable BOOT_COUNT by 1.
754    Run Keyword If  ${cur_btime} > ${BOOT_TIME}
755    ...  Run Keywords  Set Global Variable  ${BOOT_TIME}  ${cur_btime}
756    ...  AND
757    ...  Set Global Variable  ${BOOT_COUNT}  ${BOOT_COUNT + 1}
758
759    RETURN  ${BOOT_COUNT}
760
761
762Set BMC Boot Count
763    [Documentation]  Set BMC boot count to given value.
764    [Arguments]  ${count}
765
766    # Description of arguments:
767    # count  boot count value.
768    ${cur_btime}=  Get BMC Boot Time
769
770    # Set global variable BOOT_COUNT to given value.
771    Set Global Variable  ${BOOT_COUNT}  ${count}
772
773    # Set BOOT_TIME variable to current boot time.
774    Set Global Variable  ${BOOT_TIME}  ${cur_btime}
775
776
777Delete Error Log Entry
778    [Documentation]  Delete error log entry.
779    [Arguments]  ${entry_path}  ${quiet}=${0}
780
781    # Description of argument(s):
782    # quiet    If enabled, turns off logging to console.
783    # entry_path  Delete an error log entry.
784    #             Ex. /xyz/openbmc_project/logging/entry/1
785
786    # Skip delete if entry URI is a callout.
787    # Examples:
788    # /xyz/openbmc_project/logging/entry/1/callout
789    # /xyz/openbmc_project/logging/entry/1/callouts/0
790    ${callout_entry}=  Run Keyword And Return Status
791    ...  Should Match Regexp  ${entry_path}  /callout[s]?(/|$)
792    Return From Keyword If  ${callout_entry}
793
794    ${data}=  Create Dictionary  data=@{EMPTY}
795    ${resp}=  Openbmc Delete Request  ${entry_path}  data=${data}  quiet=${quiet}
796    Should Be Equal As Strings  ${resp.status_code}  ${HTTP_OK}
797
798
799Get BMC Version
800    [Documentation]  Returns BMC version from /etc/os-release.
801    ...              e.g. "v1.99.6-141-ge662190"
802
803    ${cmd}=  Set Variable  grep ^VERSION_ID= /etc/os-release | cut -f 2 -d '='
804    ${output}  ${stderr}  ${rc}=  BMC Execute Command  ${cmd}
805    RETURN  ${output}
806
807
808Get PNOR Version
809    [Documentation]  Returns the PNOR version from the BMC.
810
811    ${pnor_attrs}=  Get PNOR Attributes
812    RETURN  ${pnor_attrs['version']}
813
814
815Get PNOR Attributes
816    [Documentation]  Return PNOR software attributes as a dictionary.
817
818    # This keyword parses /var/lib/phosphor-software-manager/pnor/ro/pnor.toc
819    # into key/value pairs.
820
821    ${outbuf}  ${stderr}  ${rc}=  BMC Execute Command
822    ...  cat /var/lib/phosphor-software-manager/pnor/ro/pnor.toc
823    ${pnor_attrs}=  Key Value Outbuf To Dict  ${outbuf}  delim==
824
825    RETURN  ${pnor_attrs}
826
827
828GET BMC PNOR Version
829    [Documentation]  Return BMC & PNOR version from openbmc shell.
830
831    ${bmc_version}=  GET BMC Version
832    ${pnor_version}=  GET PNOR Version
833    Log  ${bmc_version}
834    Rprint Vars  bmc_version
835    Log  ${pnor_version}
836    Rprint Vars  pnor_version
837
838    RETURN  ${bmc_version}  ${pnor_version}
839
840
841Redfish Get BMC Version
842    [Documentation]  Get BMC version via Redfish.
843
844    ${output}=  Redfish.Get Attribute  ${REDFISH_BASE_URI}Managers/${MANAGER_ID}  FirmwareVersion
845    RETURN  ${output}
846
847Redfish Get Host Version
848    [Documentation]  Get host version via Redfish.
849
850    ${output}=  Redfish.Get Attribute  ${REDFISH_BASE_URI}Systems/${SYSTEM_ID}  BiosVersion
851    RETURN  ${output}
852
853
854Copy Address Translation Utils To HOST OS
855    [Documentation]  Copy address translation utils to host OS.
856
857    OperatingSystem.File Should Exist  ${probe_cpu_tool_path}
858    ...  msg=${probe_cpu_tool_path} doesn't exist.
859    OperatingSystem.File Should Exist  ${probe_cpu_tool_path}
860    ...  msg=${probe_cpu_tool_path} doesn't exist.
861
862    scp.Open connection  ${OS_HOST}  username=${OS_USERNAME}
863    ...  password=${OS_PASSWORD}
864    scp.Put File  ${probe_cpu_tool_path}  ${target_file_path}
865    scp.Put File  ${scom_addrs_tool_path}  ${target_file_path}
866
867
868Verify BMC RTC And UTC Time Drift
869    [Documentation]  Verify that the RTC and UTC time difference is less than
870    ...              the given time_drift_max.
871    [Arguments]  ${time_diff_max}=${10}
872
873    # Description of argument(s):
874    # time_diff_max   The max allowable RTC and UTC time difference in seconds.
875
876    # Example:
877    # time_dict:
878    #   [local_time]:               Fri 2017-11-03 152756 UTC
879    #   [local_time_seconds]:       1509740876
880    #   [universal_time]:           Fri 2017-11-03 152756 UTC
881    #   [universal_time_seconds]:   1509740876
882    #   [rtc_time]:                 Fri 2016-05-20 163403
883    #   [rtc_time_seconds]:         1463780043
884    #   [time_zone]:                n/a (UTC, +0000)
885    #   [network_time_on]:          yes
886    #   [ntp_synchronized]:         no
887    #   [rtc_in_local_tz]:          no
888
889    ${time}=  Get BMC Date Time
890    ${time_diff}=  Evaluate
891    ...  ${time['universal_time_seconds']} - ${time['rtc_time_seconds']}
892    Should Be True  ${time_diff} < ${time_diff_max}
893
894
895Validate IP On BMC
896    [Documentation]  Validate IP address is present in set of IP addresses.
897    [Arguments]  ${ip_address}  ${ip_data}
898
899    # Description of argument(s):
900    # ip_address  IP address to check (e.g. xx.xx.xx.xx).
901    # ip_data     Set of the IP addresses present.
902
903    Should Contain Match  ${ip_data}  ${ip_address}/*
904    ...  msg=${ip_address} not found in the list provided.
905
906
907Remove Journald Logs
908    [Documentation]  Remove all journald logs and restart service.
909
910    ${cmd}=  Catenate  systemctl stop systemd-journald.service &&
911    ...  rm -rf /var/log/journal && systemctl start systemd-journald.service
912
913    BMC Execute Command  ${cmd}
914
915
916Check For Regex In Journald
917    [Documentation]  Parse the journal log and check for regex string.
918    [Arguments]  ${regex}=${ERROR_REGEX}  ${error_check}=${0}  ${boot}=${EMPTY}
919    ...          ${filter_string}=${EMPTY}
920
921    # Description of argument(s):
922    # regex            Strings to be filter.
923    # error_check      Check for errors.
924    # boot             Argument to check current or persistent full boot log
925    #                  (e.g. "-b").
926    # filter_string    String to be stripped out.
927
928
929    ${cmd} =  Run Keyword If   '${filter_string}' == '${EMPTY}'
930    ...      Catenate  journalctl --no-pager ${boot} | egrep '${regex}'
931    ...   ELSE
932    ...      Catenate  journalctl --no-pager ${boot} | egrep '${regex}' |  sed '/${filter_string}/d'
933
934    ${journal_log}  ${stderr}  ${rc}=  BMC Execute Command   ${cmd}  ignore_err=1
935
936    Run Keyword If  ${error_check} == ${0}
937    ...    Should Be Empty  ${journal_log}
938    ...  ELSE
939    ...    Should Not Be Empty  ${journal_log}
940
941
942Get Service Attribute
943    [Documentation]  Get service attribute policy output.
944    [Arguments]  ${option}  ${servicename}
945
946    # Description of argument(s):
947    # option       systemctl supported options
948    # servicename  Qualified service name
949    ${cmd}=  Set Variable
950    ...  systemctl -p ${option} show ${servicename} | cut -d = -f2
951    ${attr}  ${stderr}  ${rc}=  BMC Execute Command  ${cmd}
952    RETURN  ${attr}
953
954
955Verify Watchdog Enabled
956    [Documentation]  Check that watchdog settings are enabled.
957
958    ${properties}=  Read Properties  /xyz/openbmc_project/watchdog/host0
959    Should Be Equal As Strings  ${properties["Enabled"]}  ${True}
960    Should Not Be Equal As Strings  ${properties["TimeRemaining"]}  0
961
962
963Is BMC Unpingable
964    [Documentation]  Check if BMC is unpingable.
965
966    ${RC}  ${output}=  Run and return RC and Output  ping -c 4 ${OPENBMC_HOST}
967    Log  RC: ${RC}\nOutput:\n${output}
968    Should be equal  ${RC}  ${1}
969
970
971Is Host Unpingable
972    [Documentation]  Check if Given IP is unpingable.
973    [Arguments]  ${ip}
974
975    # Description of argument(s):
976    # ip           HostOS IP address (e.g. "10.7.7.7").
977
978    ${RC}  ${output}=  Run and return RC and Output  ping -c 4 ${ip}
979    Log  RC: ${RC}\nOutput:\n${output}
980    Should be equal  ${RC}  ${1}
981
982
983Redfish BMC Match States
984    [Documentation]  Verify the BMC match state.
985    [Arguments]  ${match_state}
986
987    # Description of argument(s):
988    # match_state    Match the state of BMC.
989
990    ${bmc_state}=  Redfish Get BMC State
991    Should Be Equal As Strings  ${match_state}  ${bmc_state}
992
993
994Kernel Panic BMC Reset Operation
995    [Documentation]  Create kernel panic to reset BMC.
996
997    ${kernel_panic_cmd_file}=  Set Variable  /bin/sh -c "echo c > /proc/sysrq-trigger"
998    ${status}=  Run Keyword And Return Status  BMC Execute Command  ${kernel_panic_cmd_file}  time_out=20
999
1000    RETURN  ${status}
1001
1002
1003Get Property Value Of Systemd Service In BMC
1004    [Documentation]  Get property of systemd service in BMC.
1005    [Arguments]  ${service_name}  ${property_name}
1006
1007    # Description of argument(s):
1008    # service_name   Systemd service name. E.g. bmcweb etc.
1009    # property_name  Property name. E.g. MainPID etc.
1010
1011    # ~# systemctl show --property MainPID --value bmcweb
1012    # 1273
1013
1014    ${stdout}  ${stderr}  ${rc}=  BMC Execute Command
1015    ...  systemctl show --property ${property_name} --value ${service_name}
1016
1017    RETURN  ${stdout}
1018