xref: /openbmc/openbmc-test-automation/lib/ipmi_client.robot (revision 4f360843e6c259cd5964d42084755a0f29d5111f)
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:
18a7913898SMichael Walsh${IPMI_EXT_CMD}=       ipmitool -I lanplus -C 3
19a7913898SMichael Walsh${IPMI_USER_OPTIONS}   ${EMPTY}
20ae7c2281SPrashanth Katti${IPMI_INBAND_CMD}=    ipmitool -C 3
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
72ae7c2281SPrashanth Katti    # Desription 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
91ae7c2281SPrashanth Katti    # Desription 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
11538032805SGunnar Mills    [Arguments]    ${args}
116a7913898SMichael Walsh
1177a212a09SRahul Maheshwari    ${ipmi_cmd}=  Catenate  SEPARATOR=
118a7913898SMichael Walsh    ...  ${IPMI_EXT_CMD} ${IPMI_USER_OPTIONS} -P${SPACE}${IPMI_PASSWORD}
119a7913898SMichael Walsh    ...  ${SPACE}${HOST}${SPACE}${OPENBMC_HOST}${SPACE}${args}
12004a2204bSRahul Maheshwari    ${rc}  ${output}=  Run And Return RC and Output  ${ipmi_cmd}
1217a212a09SRahul Maheshwari    Should Be Equal  ${rc}  ${0}  msg=${output}
122c9ea9368SGunnar Mills    [Return]  ${output}
1237a212a09SRahul Maheshwari
124ae7c2281SPrashanth KattiCheck If IPMI Tool Exist
125ae7c2281SPrashanth Katti    [Documentation]  Check if IPMI Tool installed or not.
126ae7c2281SPrashanth Katti    ${output}=  Execute Command  which ipmitool
127ae7c2281SPrashanth Katti    Should Not Be Empty  ${output}  msg=ipmitool not installed.
1287a212a09SRahul Maheshwari
12904a2204bSRahul Maheshwari
13004a2204bSRahul MaheshwariActivate SOL Via IPMI
13104a2204bSRahul Maheshwari    [Documentation]  Start SOL using IPMI and route output to a file.
13204a2204bSRahul Maheshwari    [Arguments]  ${file_path}=/tmp/sol_${OPENBMC_HOST}
13304a2204bSRahul Maheshwari    # Description of argument(s):
13404a2204bSRahul Maheshwari    # file_path  The file path on the local machine (vs OBMC) to collect SOL
13504a2204bSRahul Maheshwari    #            output. By default SOL output is collected at
13604a2204bSRahul Maheshwari    #            /tmp/sol_<BMC_IP> else user input location.
13704a2204bSRahul Maheshwari
13804a2204bSRahul Maheshwari    ${ipmi_cmd}=  Catenate  SEPARATOR=
13904a2204bSRahul Maheshwari    ...  ${IPMI_EXT_CMD} -P${SPACE}${IPMI_PASSWORD}${SPACE}
14004a2204bSRahul Maheshwari    ...  ${HOST}${SPACE}${OPENBMC_HOST}${SPACE}sol activate
14104a2204bSRahul Maheshwari
1422102f6bfSGeorge Keishing    Start Process  ${ipmi_cmd}  shell=True  stdout=${file_path}
143*4f360843SRahul Maheshwari    ...  alias=sol_proc
14404a2204bSRahul Maheshwari
14504a2204bSRahul Maheshwari
14604a2204bSRahul MaheshwariDeactivate SOL Via IPMI
14704a2204bSRahul Maheshwari    [Documentation]  Stop SOL using IPMI and return SOL output.
14804a2204bSRahul Maheshwari    [Arguments]  ${file_path}=/tmp/sol_${OPENBMC_HOST}
14904a2204bSRahul Maheshwari    # Description of argument(s):
15004a2204bSRahul Maheshwari    # file_path  The file path on the local machine to copy SOL output
15104a2204bSRahul Maheshwari    #            collected by above "Activate SOL Via IPMI" keyword.
15204a2204bSRahul Maheshwari    #            By default it copies log from /tmp/sol_<BMC_IP>.
15304a2204bSRahul Maheshwari
15404a2204bSRahul Maheshwari    ${ipmi_cmd}=  Catenate  SEPARATOR=
15504a2204bSRahul Maheshwari    ...  ${IPMI_EXT_CMD} -P${SPACE}${IPMI_PASSWORD}${SPACE}
15604a2204bSRahul Maheshwari    ...  ${HOST}${SPACE}${OPENBMC_HOST}${SPACE}sol deactivate
15704a2204bSRahul Maheshwari
15804a2204bSRahul Maheshwari    ${rc}  ${output}=  Run and Return RC and Output  ${ipmi_cmd}
159*4f360843SRahul Maheshwari    Run Keyword If  ${rc} > 0  Run Keywords
160*4f360843SRahul Maheshwari    ...  Terminate Process  sol_proc  AND  Return From Keyword  ${output}
16104a2204bSRahul Maheshwari
16204a2204bSRahul Maheshwari    ${rc}  ${output}=  Run and Return RC and Output  cat ${file_path}
16304a2204bSRahul Maheshwari    Should Be Equal  ${rc}  ${0}  msg=${output}
16404a2204bSRahul Maheshwari
16504a2204bSRahul Maheshwari    # Logging SOL output for debug purpose.
16604a2204bSRahul Maheshwari    Log  ${output}
16704a2204bSRahul Maheshwari
16804a2204bSRahul Maheshwari    [Return]  ${output}
16904a2204bSRahul Maheshwari
17004a2204bSRahul Maheshwari
171a2dab3c8SSivas SRRByte Conversion
172a2dab3c8SSivas SRR    [Documentation]   Byte Conversion method receives IPMI RAW commands as
173a2dab3c8SSivas SRR    ...               argument in string format.
174a2dab3c8SSivas SRR    ...               Sample argument is as follows
175a2dab3c8SSivas SRR    ...               "0x04 0x30 9 0x01 0x00 0x35 0x00 0x00 0x00 0x00 0x00
176a2dab3c8SSivas SRR    ...               0x00"
177a2dab3c8SSivas SRR    ...               IPMI RAW command format is as follows
178a2dab3c8SSivas SRR    ...               <netfn Byte> <cmd Byte> <Data Bytes..>
179a2dab3c8SSivas SRR    ...               This method converts IPMI command format into
180a2dab3c8SSivas SRR    ...               dbus command format  as follows
181a2dab3c8SSivas SRR    ...               <byte:seq-id> <byte:netfn> <byte:lun> <byte:cmd>
182a2dab3c8SSivas SRR    ...               <array:byte:data>
183a2dab3c8SSivas SRR    ...               Sample dbus  Host IPMI Received Message argument
184a2dab3c8SSivas SRR    ...               byte:0x00 byte:0x04 byte:0x00 byte:0x30
185a2dab3c8SSivas SRR    ...               array:byte:9,0x01,0x00,0x35,0x00,0x00,0x00,0x00,0x00,0x00
18638032805SGunnar Mills    [Arguments]     ${args}
187a2dab3c8SSivas SRR    ${argLength}=   Get Length  ${args}
188a2dab3c8SSivas SRR    Set Global Variable  ${arrayByte}   array:byte:
189a2dab3c8SSivas SRR    @{listargs}=   Split String  ${args}
190a2dab3c8SSivas SRR    ${index}=   Set Variable   ${0}
191a2dab3c8SSivas SRR    :FOR   ${word}   in   @{listargs}
192a2dab3c8SSivas SRR    \    Run Keyword if   ${index} == 0   Set NetFn Byte  ${word}
193a2dab3c8SSivas SRR    \    Run Keyword if   ${index} == 1   Set Cmd Byte    ${word}
194a2dab3c8SSivas SRR    \    Run Keyword if   ${index} > 1    Set Array Byte  ${word}
195a2dab3c8SSivas SRR    \    ${index}=    Set Variable    ${index + 1}
196a2dab3c8SSivas SRR    ${length}=   Get Length  ${arrayByte}
197a2dab3c8SSivas SRR    ${length}=   Evaluate  ${length} - 1
198a2dab3c8SSivas SRR    ${arrayByteLocal}=  Get Substring  ${arrayByte}  0   ${length}
199a2dab3c8SSivas SRR    Set Global Variable  ${arrayByte}   ${arrayByteLocal}
200a2dab3c8SSivas SRR    ${valueinBytesWithArray}=   Catenate  byte:0x00   ${netfnByte}  byte:0x00
201a2dab3c8SSivas SRR    ${valueinBytesWithArray}=   Catenate  ${valueinBytesWithArray}  ${cmdByte}
202a2dab3c8SSivas SRR    ${valueinBytesWithArray}=   Catenate  ${valueinBytesWithArray} ${arrayByte}
203a2dab3c8SSivas SRR    ${valueinBytesWithoutArray}=   Catenate  byte:0x00 ${netfnByte}  byte:0x00
204a2dab3c8SSivas SRR    ${valueinBytesWithoutArray}=   Catenate  ${valueinBytesWithoutArray} ${cmdByte}
205a2dab3c8SSivas SRR#   To Check scenario for smaller IPMI raw commands with only 2 arguments
206a2dab3c8SSivas SRR#   instead of usual 12 arguments.
207a2dab3c8SSivas SRR#   Sample small IPMI raw command: Run IPMI command 0x06 0x36
208a2dab3c8SSivas SRR#   If IPMI raw argument length is only 9 then return value in bytes without
209a2dab3c8SSivas SRR#   array population.
210a2dab3c8SSivas SRR#   Equivalent dbus-send argument for smaller IPMI raw command:
211a2dab3c8SSivas SRR#   byte:0x00 byte:0x06 byte:0x00 byte:0x36
212a2dab3c8SSivas SRR    Run Keyword if   ${argLength} == 9     Return from Keyword    ${valueinBytesWithoutArray}
213c9ea9368SGunnar Mills    [Return]    ${valueinBytesWithArray}
214a2dab3c8SSivas SRR
215a2dab3c8SSivas SRR
216a2dab3c8SSivas SRRSet NetFn Byte
21738032805SGunnar Mills   [Arguments]    ${word}
218a2dab3c8SSivas SRR   ${netfnByteLocal}=  Catenate   byte:${word}
219a2dab3c8SSivas SRR   Set Global Variable  ${netfnByte}  ${netfnByteLocal}
220a2dab3c8SSivas SRR
221a2dab3c8SSivas SRRSet Cmd Byte
22238032805SGunnar Mills   [Arguments]    ${word}
223a2dab3c8SSivas SRR   ${cmdByteLocal}=  Catenate   byte:${word}
224a2dab3c8SSivas SRR   Set Global Variable  ${cmdByte}  ${cmdByteLocal}
225a2dab3c8SSivas SRR
226a2dab3c8SSivas SRRSet Array Byte
22738032805SGunnar Mills   [Arguments]    ${word}
228a2dab3c8SSivas SRR   ${arrayByteLocal}=   Catenate   SEPARATOR=  ${arrayByte}  ${word}
229a2dab3c8SSivas SRR   ${arrayByteLocal}=   Catenate   SEPARATOR=  ${arrayByteLocal}   ,
230a2dab3c8SSivas SRR   Set Global Variable  ${arrayByte}   ${arrayByteLocal}
231a2dab3c8SSivas SRR
232b29d2e84SChris AustenCopy ipmitool
233b29d2e84SChris 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.
234b29d2e84SChris Austen
235b29d2e84SChris Austen    Import Library      SCPLibrary      WITH NAME       scp
236b29d2e84SChris Austen    scp.Open connection     ${OPENBMC_HOST}     username=${OPENBMC_USERNAME}      password=${OPENBMC_PASSWORD}
237b29d2e84SChris Austen    scp.Put File    tools/ipmitool   /tmp
238b29d2e84SChris Austen    SSHLibrary.Open Connection     ${OPENBMC_HOST}
239b29d2e84SChris Austen    Login   ${OPENBMC_USERNAME}    ${OPENBMC_PASSWORD}
240b29d2e84SChris Austen    Execute Command     chmod +x /tmp/ipmitool
2410c34f69cSSridevi Ramesh
2420c34f69cSSridevi RameshInitiate Host Boot Via External IPMI
2430c34f69cSSridevi Ramesh    [Documentation]  Initiate host power on using external IPMI.
244982fee42SRahul Maheshwari    [Arguments]  ${wait}=${1}
245982fee42SRahul Maheshwari    # Description of argument(s):
246982fee42SRahul Maheshwari    # wait  Indicates that this keyword should wait for host running state.
247982fee42SRahul Maheshwari
2480c34f69cSSridevi Ramesh    ${output}=  Run External IPMI Standard Command  chassis power on
2490c34f69cSSridevi Ramesh    Should Not Contain  ${output}  Error
250982fee42SRahul Maheshwari
251982fee42SRahul Maheshwari    Run Keyword If  '${wait}' == '${0}'  Return From Keyword
252982fee42SRahul Maheshwari    Wait Until Keyword Succeeds  10 min  10 sec  Is Host Running
2530c34f69cSSridevi Ramesh
2540c34f69cSSridevi RameshInitiate Host PowerOff Via External IPMI
2550c34f69cSSridevi Ramesh    [Documentation]  Initiate host power off using external IPMI.
256982fee42SRahul Maheshwari    [Arguments]  ${wait}=${1}
257982fee42SRahul Maheshwari    # Description of argument(s):
258982fee42SRahul Maheshwari    # wait  Indicates that this keyword should wait for host off state.
259982fee42SRahul Maheshwari
2600c34f69cSSridevi Ramesh    ${output}=  Run External IPMI Standard Command  chassis power off
2610c34f69cSSridevi Ramesh    Should Not Contain  ${output}  Error
262982fee42SRahul Maheshwari
263982fee42SRahul Maheshwari    Run Keyword If  '${wait}' == '${0}'  Return From Keyword
264982fee42SRahul Maheshwari    Wait Until Keyword Succeeds  3 min  10 sec  Is Host Off
265982fee42SRahul Maheshwari
266982fee42SRahul MaheshwariGet Host State Via External IPMI
267982fee42SRahul Maheshwari    [Documentation]  Returns host state using external IPMI.
268982fee42SRahul Maheshwari
269982fee42SRahul Maheshwari    ${output}=  Run External IPMI Standard Command  chassis power status
270982fee42SRahul Maheshwari    Should Not Contain  ${output}  Error
271982fee42SRahul Maheshwari    ${output}=  Fetch From Right  ${output}  ${SPACE}
272982fee42SRahul Maheshwari
273982fee42SRahul Maheshwari    [Return]  ${output}
274