xref: /openbmc/openbmc-test-automation/lib/ipmi_client.robot (revision 39967ebce46487b39a7f0277369eece3afc22e90)
1b29d2e84SChris Austen*** Settings ***
2b29d2e84SChris AustenDocumentation   This module is for IPMI client for copying ipmitool to
3a2dab3c8SSivas SRR...             openbmc box and execute ipmitool IPMI standard
4a2dab3c8SSivas SRR...             command. IPMI raw command will use dbus-send command
5b29d2e84SChris AustenResource        ../lib/resource.txt
64d6c1dabSGeorge KeishingResource        ../lib/connection_client.robot
7ae7c2281SPrashanth KattiResource        ../lib/utils.robot
8ae7c2281SPrashanth KattiResource        ../lib/state_manager.robot
9ae7c2281SPrashanth Katti
10a2dab3c8SSivas SRRLibrary         String
11a2dab3c8SSivas SRR
12a2dab3c8SSivas SRR*** Variables ***
13d7f11f70SGeorge Keishing${dbusHostIpmicmd1}=   dbus-send --system  ${OPENBMC_BASE_URI}HostIpmi/1
14d7f11f70SGeorge Keishing${dbusHostIpmiCmdReceivedMsg}=   ${OPENBMC_BASE_DBUS}.HostIpmi.ReceivedMessage
15a2dab3c8SSivas SRR${netfnByte}=          ${EMPTY}
16a2dab3c8SSivas SRR${cmdByte}=            ${EMPTY}
17a2dab3c8SSivas SRR${arrayByte}=          array:byte:
18*39967ebcSGeorge Keishing${IPMI_EXT_CMD}=       ipmitool -I lanplus -C ${IPMI_CIPHER_LEVEL}
19a7913898SMichael Walsh${IPMI_USER_OPTIONS}   ${EMPTY}
20*39967ebcSGeorge Keishing${IPMI_INBAND_CMD}=    ipmitool -C ${IPMI_CIPHER_LEVEL}
217a212a09SRahul Maheshwari${HOST}=               -H
227a212a09SRahul Maheshwari${RAW}=                raw
23b29d2e84SChris Austen
24b29d2e84SChris Austen*** Keywords ***
257a212a09SRahul Maheshwari
26b29d2e84SChris AustenRun IPMI Command
2738032805SGunnar Mills    [Arguments]  ${args}
287a212a09SRahul Maheshwari    ${resp}=  Run Keyword If  '${IPMI_COMMAND}' == 'External'
297a212a09SRahul Maheshwari    ...  Run External IPMI RAW Command  ${args}
30ae7c2281SPrashanth Katti    ...  ELSE IF  '${IPMI_COMMAND}' == 'Inband'
31ae7c2281SPrashanth Katti    ...  Run Inband IPMI RAW Command  ${args}
327a212a09SRahul Maheshwari    ...  ELSE IF  '${IPMI_COMMAND}' == 'Dbus'
337a212a09SRahul Maheshwari    ...  Run Dbus IPMI RAW Command  ${args}
34ae7c2281SPrashanth Katti    ...  ELSE  Fail  msg=Invalid IPMI Command type provided : ${IPMI_COMMAND}
35c9ea9368SGunnar Mills    [Return]  ${resp}
367a212a09SRahul Maheshwari
377a212a09SRahul MaheshwariRun IPMI Standard Command
3838032805SGunnar Mills    [Arguments]  ${args}
397a212a09SRahul Maheshwari    ${resp}=  Run Keyword If  '${IPMI_COMMAND}' == 'External'
407a212a09SRahul Maheshwari    ...  Run External IPMI Standard Command  ${args}
41ae7c2281SPrashanth Katti    ...  ELSE IF  '${IPMI_COMMAND}' == 'Inband'
42ae7c2281SPrashanth Katti    ...  Run Inband IPMI Standard Command  ${args}
437a212a09SRahul Maheshwari    ...  ELSE IF  '${IPMI_COMMAND}' == 'Dbus'
447a212a09SRahul Maheshwari    ...  Run Dbus IPMI Standard Command  ${args}
45ae7c2281SPrashanth Katti    ...  ELSE  Fail  msg=Invalid IPMI Command type provided : ${IPMI_COMMAND}
467a212a09SRahul Maheshwari
47c9ea9368SGunnar Mills    [Return]  ${resp}
487a212a09SRahul Maheshwari
497a212a09SRahul MaheshwariRun Dbus IPMI RAW Command
5038032805SGunnar Mills    [Arguments]    ${args}
51a2dab3c8SSivas SRR    ${valueinBytes}=   Byte Conversion  ${args}
52a2dab3c8SSivas SRR    ${cmd}=   Catenate   ${dbushostipmicmd1} ${dbusHostIpmiCmdReceivedMsg}
53a2dab3c8SSivas SRR    ${cmd}=   Catenate   ${cmd} ${valueinBytes}
54a2dab3c8SSivas SRR    ${output}   ${stderr}=  Execute Command  ${cmd}  return_stderr=True
55b29d2e84SChris Austen    Should Be Empty      ${stderr}
56b29d2e84SChris Austen    set test variable    ${OUTPUT}     "${output}"
57b29d2e84SChris Austen
587a212a09SRahul MaheshwariRun Dbus IPMI Standard Command
5938032805SGunnar Mills    [Arguments]    ${args}
604d6c1dabSGeorge Keishing    Copy ipmitool
617a212a09SRahul Maheshwari    ${stdout}    ${stderr}    ${output}=  Execute Command
627a212a09SRahul Maheshwari    ...    /tmp/ipmitool -I dbus ${args}    return_stdout=True
637a212a09SRahul Maheshwari    ...    return_stderr= True    return_rc=True
64b29d2e84SChris Austen    Should Be Equal    ${output}    ${0}    msg=${stderr}
65c9ea9368SGunnar Mills    [Return]    ${stdout}
66b29d2e84SChris Austen
67ae7c2281SPrashanth KattiRun Inband IPMI Raw Command
68ae7c2281SPrashanth Katti    [Documentation]  Run Inband IPMI raw command.
69ae7c2281SPrashanth Katti    [Arguments]  ${args}  ${os_host}=${OS_HOST}  ${os_username}=${OS_USERNAME}
70ae7c2281SPrashanth Katti    ...          ${os_password}=${OS_PASSWORD}
71ae7c2281SPrashanth Katti
7228e403b8SGunnar Mills    # Description of arguments:
73ae7c2281SPrashanth Katti    # ${args}  parameters to IPMI command.
74ae7c2281SPrashanth Katti    # ${os_host} IP address of the OS Host.
75ae7c2281SPrashanth Katti    # ${os_username}  OS Host Login user name.
76ae7c2281SPrashanth Katti    # ${os_password}  OS Host Login passwrd.
77ae7c2281SPrashanth Katti
78ae7c2281SPrashanth Katti    Login To OS Host  ${os_host}  ${os_username}  ${os_password}
79ae7c2281SPrashanth Katti    Check If IPMI Tool Exist
80ae7c2281SPrashanth Katti
81ae7c2281SPrashanth Katti    ${inband_raw_cmd}=  Catenate  ${IPMI_INBAND_CMD}  ${RAW}  ${args}
82ae7c2281SPrashanth Katti    ${stdout}  ${stderr}=  Execute Command  ${inband_raw_cmd}  return_stderr=True
83ae7c2281SPrashanth Katti    Should Be Empty  ${stderr}  msg=${stdout}
84ae7c2281SPrashanth Katti    [Return]  ${stdout}
85ae7c2281SPrashanth Katti
86ae7c2281SPrashanth KattiRun Inband IPMI Standard Command
87ae7c2281SPrashanth Katti    [Documentation]  Run Inband IPMI standard command.
88ae7c2281SPrashanth Katti    [Arguments]  ${args}  ${os_host}=${OS_HOST}  ${os_username}=${OS_USERNAME}
89ae7c2281SPrashanth Katti    ...          ${os_password}=${OS_PASSWORD}
90ae7c2281SPrashanth Katti
9128e403b8SGunnar Mills    # Description of arguments:
92ae7c2281SPrashanth Katti    # ${args}  parameters to IPMI command.
93ae7c2281SPrashanth Katti    # ${os_host} IP address of the OS Host.
94ae7c2281SPrashanth Katti    # ${os_username}  OS Host Login user name.
95ae7c2281SPrashanth Katti    # ${os_password}  OS Host Login passwrd.
96ae7c2281SPrashanth Katti
97ae7c2281SPrashanth Katti    Login To OS Host  ${os_host}  ${os_username}  ${os_password}
98ae7c2281SPrashanth Katti    Check If IPMI Tool Exist
99ae7c2281SPrashanth Katti
100ae7c2281SPrashanth Katti    ${inband_std_cmd}=  Catenate  ${IPMI_INBAND_CMD}  ${args}
101ae7c2281SPrashanth Katti    ${stdout}  ${stderr}=  Execute Command  ${inband_std_cmd}  return_stderr=True
102ae7c2281SPrashanth Katti    Should Be Empty  ${stderr}  msg=${stdout}
103ae7c2281SPrashanth Katti    [Return]  ${stdout}
104ae7c2281SPrashanth Katti
1057a212a09SRahul MaheshwariRun External IPMI RAW Command
10638032805SGunnar Mills    [Arguments]    ${args}
1077a212a09SRahul Maheshwari    ${ipmi_raw_cmd}=   Catenate  SEPARATOR=
108a7913898SMichael Walsh    ...    ${IPMI_EXT_CMD} -P${SPACE}${IPMI_PASSWORD}${SPACE}
1097a212a09SRahul Maheshwari    ...    ${HOST}${SPACE}${OPENBMC_HOST}${SPACE}${RAW}${SPACE}${args}
1107a212a09SRahul Maheshwari    ${rc}    ${output}=    Run and Return RC and Output    ${ipmi_raw_cmd}
1117a212a09SRahul Maheshwari    Should Be Equal    ${rc}    ${0}    msg=${output}
112c9ea9368SGunnar Mills    [Return]    ${output}
1137a212a09SRahul Maheshwari
1147a212a09SRahul MaheshwariRun External IPMI Standard Command
11502651f0cSGeorge Keishing    [Arguments]  ${args}  ${fail_on_err}=${1}
11602651f0cSGeorge Keishing
11702651f0cSGeorge Keishing    # Description of argument(s):
11802651f0cSGeorge Keishing    # args         IPMI command to be executed.
11902651f0cSGeorge Keishing    # fail_on_err  Fail if keyword the IPMI command fails
120a7913898SMichael Walsh
1217a212a09SRahul Maheshwari    ${ipmi_cmd}=  Catenate  SEPARATOR=
122a7913898SMichael Walsh    ...  ${IPMI_EXT_CMD} ${IPMI_USER_OPTIONS} -P${SPACE}${IPMI_PASSWORD}
123a7913898SMichael Walsh    ...  ${SPACE}${HOST}${SPACE}${OPENBMC_HOST}${SPACE}${args}
12404a2204bSRahul Maheshwari    ${rc}  ${output}=  Run And Return RC and Output  ${ipmi_cmd}
12502651f0cSGeorge Keishing    Return From Keyword If  ${fail_on_err} == ${0}  ${output}
1267a212a09SRahul Maheshwari    Should Be Equal  ${rc}  ${0}  msg=${output}
127c9ea9368SGunnar Mills    [Return]  ${output}
1287a212a09SRahul Maheshwari
129ae7c2281SPrashanth KattiCheck If IPMI Tool Exist
130ae7c2281SPrashanth Katti    [Documentation]  Check if IPMI Tool installed or not.
131ae7c2281SPrashanth Katti    ${output}=  Execute Command  which ipmitool
132ae7c2281SPrashanth Katti    Should Not Be Empty  ${output}  msg=ipmitool not installed.
1337a212a09SRahul Maheshwari
13404a2204bSRahul Maheshwari
13504a2204bSRahul MaheshwariActivate SOL Via IPMI
13604a2204bSRahul Maheshwari    [Documentation]  Start SOL using IPMI and route output to a file.
13704a2204bSRahul Maheshwari    [Arguments]  ${file_path}=/tmp/sol_${OPENBMC_HOST}
13804a2204bSRahul Maheshwari    # Description of argument(s):
13904a2204bSRahul Maheshwari    # file_path  The file path on the local machine (vs OBMC) to collect SOL
14004a2204bSRahul Maheshwari    #            output. By default SOL output is collected at
14104a2204bSRahul Maheshwari    #            /tmp/sol_<BMC_IP> else user input location.
14204a2204bSRahul Maheshwari
14304a2204bSRahul Maheshwari    ${ipmi_cmd}=  Catenate  SEPARATOR=
144c8470881SRahul Maheshwari    ...  ${IPMI_EXT_CMD} -P${SPACE}${IPMI_PASSWORD}${SPACE}${HOST}
145c8470881SRahul Maheshwari    ...  ${SPACE}${OPENBMC_HOST}${SPACE}sol activate usesolkeepalive
14604a2204bSRahul Maheshwari
1472102f6bfSGeorge Keishing    Start Process  ${ipmi_cmd}  shell=True  stdout=${file_path}
1484f360843SRahul Maheshwari    ...  alias=sol_proc
14904a2204bSRahul Maheshwari
15004a2204bSRahul Maheshwari
15104a2204bSRahul MaheshwariDeactivate SOL Via IPMI
15204a2204bSRahul Maheshwari    [Documentation]  Stop SOL using IPMI and return SOL output.
15304a2204bSRahul Maheshwari    [Arguments]  ${file_path}=/tmp/sol_${OPENBMC_HOST}
15404a2204bSRahul Maheshwari    # Description of argument(s):
15504a2204bSRahul Maheshwari    # file_path  The file path on the local machine to copy SOL output
15604a2204bSRahul Maheshwari    #            collected by above "Activate SOL Via IPMI" keyword.
15704a2204bSRahul Maheshwari    #            By default it copies log from /tmp/sol_<BMC_IP>.
15804a2204bSRahul Maheshwari
15904a2204bSRahul Maheshwari    ${ipmi_cmd}=  Catenate  SEPARATOR=
16004a2204bSRahul Maheshwari    ...  ${IPMI_EXT_CMD} -P${SPACE}${IPMI_PASSWORD}${SPACE}
16104a2204bSRahul Maheshwari    ...  ${HOST}${SPACE}${OPENBMC_HOST}${SPACE}sol deactivate
16204a2204bSRahul Maheshwari
16304a2204bSRahul Maheshwari    ${rc}  ${output}=  Run and Return RC and Output  ${ipmi_cmd}
1644f360843SRahul Maheshwari    Run Keyword If  ${rc} > 0  Run Keywords
1657f48a2c6SRahul Maheshwari    ...  Run Keyword And Ignore Error  Terminate Process  sol_proc
1667f48a2c6SRahul Maheshwari    ...  AND  Return From Keyword  ${output}
16704a2204bSRahul Maheshwari
16804a2204bSRahul Maheshwari    ${rc}  ${output}=  Run and Return RC and Output  cat ${file_path}
16904a2204bSRahul Maheshwari    Should Be Equal  ${rc}  ${0}  msg=${output}
17004a2204bSRahul Maheshwari
17104a2204bSRahul Maheshwari    # Logging SOL output for debug purpose.
17204a2204bSRahul Maheshwari    Log  ${output}
17304a2204bSRahul Maheshwari
17404a2204bSRahul Maheshwari    [Return]  ${output}
17504a2204bSRahul Maheshwari
17604a2204bSRahul Maheshwari
177a2dab3c8SSivas SRRByte Conversion
178a2dab3c8SSivas SRR    [Documentation]   Byte Conversion method receives IPMI RAW commands as
179a2dab3c8SSivas SRR    ...               argument in string format.
180a2dab3c8SSivas SRR    ...               Sample argument is as follows
181a2dab3c8SSivas SRR    ...               "0x04 0x30 9 0x01 0x00 0x35 0x00 0x00 0x00 0x00 0x00
182a2dab3c8SSivas SRR    ...               0x00"
183a2dab3c8SSivas SRR    ...               IPMI RAW command format is as follows
184a2dab3c8SSivas SRR    ...               <netfn Byte> <cmd Byte> <Data Bytes..>
185a2dab3c8SSivas SRR    ...               This method converts IPMI command format into
186a2dab3c8SSivas SRR    ...               dbus command format  as follows
187a2dab3c8SSivas SRR    ...               <byte:seq-id> <byte:netfn> <byte:lun> <byte:cmd>
188a2dab3c8SSivas SRR    ...               <array:byte:data>
189a2dab3c8SSivas SRR    ...               Sample dbus  Host IPMI Received Message argument
190a2dab3c8SSivas SRR    ...               byte:0x00 byte:0x04 byte:0x00 byte:0x30
191a2dab3c8SSivas SRR    ...               array:byte:9,0x01,0x00,0x35,0x00,0x00,0x00,0x00,0x00,0x00
19238032805SGunnar Mills    [Arguments]     ${args}
193a2dab3c8SSivas SRR    ${argLength}=   Get Length  ${args}
194a2dab3c8SSivas SRR    Set Global Variable  ${arrayByte}   array:byte:
195a2dab3c8SSivas SRR    @{listargs}=   Split String  ${args}
196a2dab3c8SSivas SRR    ${index}=   Set Variable   ${0}
197a2dab3c8SSivas SRR    :FOR   ${word}   in   @{listargs}
198a2dab3c8SSivas SRR    \    Run Keyword if   ${index} == 0   Set NetFn Byte  ${word}
199a2dab3c8SSivas SRR    \    Run Keyword if   ${index} == 1   Set Cmd Byte    ${word}
200a2dab3c8SSivas SRR    \    Run Keyword if   ${index} > 1    Set Array Byte  ${word}
201a2dab3c8SSivas SRR    \    ${index}=    Set Variable    ${index + 1}
202a2dab3c8SSivas SRR    ${length}=   Get Length  ${arrayByte}
203a2dab3c8SSivas SRR    ${length}=   Evaluate  ${length} - 1
204a2dab3c8SSivas SRR    ${arrayByteLocal}=  Get Substring  ${arrayByte}  0   ${length}
205a2dab3c8SSivas SRR    Set Global Variable  ${arrayByte}   ${arrayByteLocal}
206a2dab3c8SSivas SRR    ${valueinBytesWithArray}=   Catenate  byte:0x00   ${netfnByte}  byte:0x00
207a2dab3c8SSivas SRR    ${valueinBytesWithArray}=   Catenate  ${valueinBytesWithArray}  ${cmdByte}
208a2dab3c8SSivas SRR    ${valueinBytesWithArray}=   Catenate  ${valueinBytesWithArray} ${arrayByte}
209a2dab3c8SSivas SRR    ${valueinBytesWithoutArray}=   Catenate  byte:0x00 ${netfnByte}  byte:0x00
210a2dab3c8SSivas SRR    ${valueinBytesWithoutArray}=   Catenate  ${valueinBytesWithoutArray} ${cmdByte}
211a2dab3c8SSivas SRR#   To Check scenario for smaller IPMI raw commands with only 2 arguments
212a2dab3c8SSivas SRR#   instead of usual 12 arguments.
213a2dab3c8SSivas SRR#   Sample small IPMI raw command: Run IPMI command 0x06 0x36
214a2dab3c8SSivas SRR#   If IPMI raw argument length is only 9 then return value in bytes without
215a2dab3c8SSivas SRR#   array population.
216a2dab3c8SSivas SRR#   Equivalent dbus-send argument for smaller IPMI raw command:
217a2dab3c8SSivas SRR#   byte:0x00 byte:0x06 byte:0x00 byte:0x36
218a2dab3c8SSivas SRR    Run Keyword if   ${argLength} == 9     Return from Keyword    ${valueinBytesWithoutArray}
219c9ea9368SGunnar Mills    [Return]    ${valueinBytesWithArray}
220a2dab3c8SSivas SRR
221a2dab3c8SSivas SRR
222a2dab3c8SSivas SRRSet NetFn Byte
22338032805SGunnar Mills   [Arguments]    ${word}
224a2dab3c8SSivas SRR   ${netfnByteLocal}=  Catenate   byte:${word}
225a2dab3c8SSivas SRR   Set Global Variable  ${netfnByte}  ${netfnByteLocal}
226a2dab3c8SSivas SRR
227a2dab3c8SSivas SRRSet Cmd Byte
22838032805SGunnar Mills   [Arguments]    ${word}
229a2dab3c8SSivas SRR   ${cmdByteLocal}=  Catenate   byte:${word}
230a2dab3c8SSivas SRR   Set Global Variable  ${cmdByte}  ${cmdByteLocal}
231a2dab3c8SSivas SRR
232a2dab3c8SSivas SRRSet Array Byte
23338032805SGunnar Mills   [Arguments]    ${word}
234a2dab3c8SSivas SRR   ${arrayByteLocal}=   Catenate   SEPARATOR=  ${arrayByte}  ${word}
235a2dab3c8SSivas SRR   ${arrayByteLocal}=   Catenate   SEPARATOR=  ${arrayByteLocal}   ,
236a2dab3c8SSivas SRR   Set Global Variable  ${arrayByte}   ${arrayByteLocal}
237a2dab3c8SSivas SRR
238b29d2e84SChris AustenCopy ipmitool
239b29d2e84SChris Austen    OperatingSystem.File Should Exist   tools/ipmitool      msg=The ipmitool program could not be found in the tools directory. It is not part of the automation code by default. You must manually copy or link the correct openbmc version of the tool in to the tools directory in order to run this test suite.
240b29d2e84SChris Austen
241b29d2e84SChris Austen    Import Library      SCPLibrary      WITH NAME       scp
242b29d2e84SChris Austen    scp.Open connection     ${OPENBMC_HOST}     username=${OPENBMC_USERNAME}      password=${OPENBMC_PASSWORD}
243b29d2e84SChris Austen    scp.Put File    tools/ipmitool   /tmp
244b29d2e84SChris Austen    SSHLibrary.Open Connection     ${OPENBMC_HOST}
245b29d2e84SChris Austen    Login   ${OPENBMC_USERNAME}    ${OPENBMC_PASSWORD}
246b29d2e84SChris Austen    Execute Command     chmod +x /tmp/ipmitool
2470c34f69cSSridevi Ramesh
2480c34f69cSSridevi RameshInitiate Host Boot Via External IPMI
2490c34f69cSSridevi Ramesh    [Documentation]  Initiate host power on using external IPMI.
250982fee42SRahul Maheshwari    [Arguments]  ${wait}=${1}
251982fee42SRahul Maheshwari    # Description of argument(s):
252982fee42SRahul Maheshwari    # wait  Indicates that this keyword should wait for host running state.
253982fee42SRahul Maheshwari
2540c34f69cSSridevi Ramesh    ${output}=  Run External IPMI Standard Command  chassis power on
2550c34f69cSSridevi Ramesh    Should Not Contain  ${output}  Error
256982fee42SRahul Maheshwari
257982fee42SRahul Maheshwari    Run Keyword If  '${wait}' == '${0}'  Return From Keyword
258982fee42SRahul Maheshwari    Wait Until Keyword Succeeds  10 min  10 sec  Is Host Running
2590c34f69cSSridevi Ramesh
2600c34f69cSSridevi RameshInitiate Host PowerOff Via External IPMI
2610c34f69cSSridevi Ramesh    [Documentation]  Initiate host power off using external IPMI.
262982fee42SRahul Maheshwari    [Arguments]  ${wait}=${1}
263982fee42SRahul Maheshwari    # Description of argument(s):
264982fee42SRahul Maheshwari    # wait  Indicates that this keyword should wait for host off state.
265982fee42SRahul Maheshwari
2660c34f69cSSridevi Ramesh    ${output}=  Run External IPMI Standard Command  chassis power off
2670c34f69cSSridevi Ramesh    Should Not Contain  ${output}  Error
268982fee42SRahul Maheshwari
269982fee42SRahul Maheshwari    Run Keyword If  '${wait}' == '${0}'  Return From Keyword
270982fee42SRahul Maheshwari    Wait Until Keyword Succeeds  3 min  10 sec  Is Host Off
271982fee42SRahul Maheshwari
272982fee42SRahul MaheshwariGet Host State Via External IPMI
273982fee42SRahul Maheshwari    [Documentation]  Returns host state using external IPMI.
274982fee42SRahul Maheshwari
275982fee42SRahul Maheshwari    ${output}=  Run External IPMI Standard Command  chassis power status
276982fee42SRahul Maheshwari    Should Not Contain  ${output}  Error
277982fee42SRahul Maheshwari    ${output}=  Fetch From Right  ${output}  ${SPACE}
278982fee42SRahul Maheshwari
279982fee42SRahul Maheshwari    [Return]  ${output}
280