xref: /openbmc/openbmc-test-automation/lib/ipmi_client.robot (revision 04a2204bc1724ce64451f2ab745128a9654a064a)
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}
120*04a2204bSRahul 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
129*04a2204bSRahul Maheshwari
130*04a2204bSRahul MaheshwariActivate SOL Via IPMI
131*04a2204bSRahul Maheshwari    [Documentation]  Start SOL using IPMI and route output to a file.
132*04a2204bSRahul Maheshwari    [Arguments]  ${file_path}=/tmp/sol_${OPENBMC_HOST}
133*04a2204bSRahul Maheshwari    # Description of argument(s):
134*04a2204bSRahul Maheshwari    # file_path  The file path on the local machine (vs OBMC) to collect SOL
135*04a2204bSRahul Maheshwari    #            output. By default SOL output is collected at
136*04a2204bSRahul Maheshwari    #            /tmp/sol_<BMC_IP> else user input location.
137*04a2204bSRahul Maheshwari
138*04a2204bSRahul Maheshwari    ${ipmi_cmd}=  Catenate  SEPARATOR=
139*04a2204bSRahul Maheshwari    ...  ${IPMI_EXT_CMD} -P${SPACE}${IPMI_PASSWORD}${SPACE}
140*04a2204bSRahul Maheshwari    ...  ${HOST}${SPACE}${OPENBMC_HOST}${SPACE}sol activate
141*04a2204bSRahul Maheshwari
142*04a2204bSRahul Maheshwari    Run Process  ${ipmi_cmd}  shell=True  stdout=${file_path}
143*04a2204bSRahul Maheshwari    ...  timeout=5s  on_timeout=continue
144*04a2204bSRahul Maheshwari
145*04a2204bSRahul Maheshwari
146*04a2204bSRahul MaheshwariDeactivate SOL Via IPMI
147*04a2204bSRahul Maheshwari    [Documentation]  Stop SOL using IPMI and return SOL output.
148*04a2204bSRahul Maheshwari    [Arguments]  ${file_path}=/tmp/sol_${OPENBMC_HOST}
149*04a2204bSRahul Maheshwari    # Description of argument(s):
150*04a2204bSRahul Maheshwari    # file_path  The file path on the local machine to copy SOL output
151*04a2204bSRahul Maheshwari    #            collected by above "Activate SOL Via IPMI" keyword.
152*04a2204bSRahul Maheshwari    #            By default it copies log from /tmp/sol_<BMC_IP>.
153*04a2204bSRahul Maheshwari
154*04a2204bSRahul Maheshwari    ${ipmi_cmd}=  Catenate  SEPARATOR=
155*04a2204bSRahul Maheshwari    ...  ${IPMI_EXT_CMD} -P${SPACE}${IPMI_PASSWORD}${SPACE}
156*04a2204bSRahul Maheshwari    ...  ${HOST}${SPACE}${OPENBMC_HOST}${SPACE}sol deactivate
157*04a2204bSRahul Maheshwari
158*04a2204bSRahul Maheshwari    ${rc}  ${output}=  Run and Return RC and Output  ${ipmi_cmd}
159*04a2204bSRahul Maheshwari    Run Keyword If  ${rc} > 0  Return From Keyword  ${output}
160*04a2204bSRahul Maheshwari
161*04a2204bSRahul Maheshwari    ${rc}  ${output}=  Run and Return RC and Output  cat ${file_path}
162*04a2204bSRahul Maheshwari    Should Be Equal  ${rc}  ${0}  msg=${output}
163*04a2204bSRahul Maheshwari
164*04a2204bSRahul Maheshwari    # Logging SOL output for debug purpose.
165*04a2204bSRahul Maheshwari    Log  ${output}
166*04a2204bSRahul Maheshwari
167*04a2204bSRahul Maheshwari    [Return]  ${output}
168*04a2204bSRahul Maheshwari
169*04a2204bSRahul Maheshwari
170a2dab3c8SSivas SRRByte Conversion
171a2dab3c8SSivas SRR    [Documentation]   Byte Conversion method receives IPMI RAW commands as
172a2dab3c8SSivas SRR    ...               argument in string format.
173a2dab3c8SSivas SRR    ...               Sample argument is as follows
174a2dab3c8SSivas SRR    ...               "0x04 0x30 9 0x01 0x00 0x35 0x00 0x00 0x00 0x00 0x00
175a2dab3c8SSivas SRR    ...               0x00"
176a2dab3c8SSivas SRR    ...               IPMI RAW command format is as follows
177a2dab3c8SSivas SRR    ...               <netfn Byte> <cmd Byte> <Data Bytes..>
178a2dab3c8SSivas SRR    ...               This method converts IPMI command format into
179a2dab3c8SSivas SRR    ...               dbus command format  as follows
180a2dab3c8SSivas SRR    ...               <byte:seq-id> <byte:netfn> <byte:lun> <byte:cmd>
181a2dab3c8SSivas SRR    ...               <array:byte:data>
182a2dab3c8SSivas SRR    ...               Sample dbus  Host IPMI Received Message argument
183a2dab3c8SSivas SRR    ...               byte:0x00 byte:0x04 byte:0x00 byte:0x30
184a2dab3c8SSivas SRR    ...               array:byte:9,0x01,0x00,0x35,0x00,0x00,0x00,0x00,0x00,0x00
18538032805SGunnar Mills    [Arguments]     ${args}
186a2dab3c8SSivas SRR    ${argLength}=   Get Length  ${args}
187a2dab3c8SSivas SRR    Set Global Variable  ${arrayByte}   array:byte:
188a2dab3c8SSivas SRR    @{listargs}=   Split String  ${args}
189a2dab3c8SSivas SRR    ${index}=   Set Variable   ${0}
190a2dab3c8SSivas SRR    :FOR   ${word}   in   @{listargs}
191a2dab3c8SSivas SRR    \    Run Keyword if   ${index} == 0   Set NetFn Byte  ${word}
192a2dab3c8SSivas SRR    \    Run Keyword if   ${index} == 1   Set Cmd Byte    ${word}
193a2dab3c8SSivas SRR    \    Run Keyword if   ${index} > 1    Set Array Byte  ${word}
194a2dab3c8SSivas SRR    \    ${index}=    Set Variable    ${index + 1}
195a2dab3c8SSivas SRR    ${length}=   Get Length  ${arrayByte}
196a2dab3c8SSivas SRR    ${length}=   Evaluate  ${length} - 1
197a2dab3c8SSivas SRR    ${arrayByteLocal}=  Get Substring  ${arrayByte}  0   ${length}
198a2dab3c8SSivas SRR    Set Global Variable  ${arrayByte}   ${arrayByteLocal}
199a2dab3c8SSivas SRR    ${valueinBytesWithArray}=   Catenate  byte:0x00   ${netfnByte}  byte:0x00
200a2dab3c8SSivas SRR    ${valueinBytesWithArray}=   Catenate  ${valueinBytesWithArray}  ${cmdByte}
201a2dab3c8SSivas SRR    ${valueinBytesWithArray}=   Catenate  ${valueinBytesWithArray} ${arrayByte}
202a2dab3c8SSivas SRR    ${valueinBytesWithoutArray}=   Catenate  byte:0x00 ${netfnByte}  byte:0x00
203a2dab3c8SSivas SRR    ${valueinBytesWithoutArray}=   Catenate  ${valueinBytesWithoutArray} ${cmdByte}
204a2dab3c8SSivas SRR#   To Check scenario for smaller IPMI raw commands with only 2 arguments
205a2dab3c8SSivas SRR#   instead of usual 12 arguments.
206a2dab3c8SSivas SRR#   Sample small IPMI raw command: Run IPMI command 0x06 0x36
207a2dab3c8SSivas SRR#   If IPMI raw argument length is only 9 then return value in bytes without
208a2dab3c8SSivas SRR#   array population.
209a2dab3c8SSivas SRR#   Equivalent dbus-send argument for smaller IPMI raw command:
210a2dab3c8SSivas SRR#   byte:0x00 byte:0x06 byte:0x00 byte:0x36
211a2dab3c8SSivas SRR    Run Keyword if   ${argLength} == 9     Return from Keyword    ${valueinBytesWithoutArray}
212c9ea9368SGunnar Mills    [Return]    ${valueinBytesWithArray}
213a2dab3c8SSivas SRR
214a2dab3c8SSivas SRR
215a2dab3c8SSivas SRRSet NetFn Byte
21638032805SGunnar Mills   [Arguments]    ${word}
217a2dab3c8SSivas SRR   ${netfnByteLocal}=  Catenate   byte:${word}
218a2dab3c8SSivas SRR   Set Global Variable  ${netfnByte}  ${netfnByteLocal}
219a2dab3c8SSivas SRR
220a2dab3c8SSivas SRRSet Cmd Byte
22138032805SGunnar Mills   [Arguments]    ${word}
222a2dab3c8SSivas SRR   ${cmdByteLocal}=  Catenate   byte:${word}
223a2dab3c8SSivas SRR   Set Global Variable  ${cmdByte}  ${cmdByteLocal}
224a2dab3c8SSivas SRR
225a2dab3c8SSivas SRRSet Array Byte
22638032805SGunnar Mills   [Arguments]    ${word}
227a2dab3c8SSivas SRR   ${arrayByteLocal}=   Catenate   SEPARATOR=  ${arrayByte}  ${word}
228a2dab3c8SSivas SRR   ${arrayByteLocal}=   Catenate   SEPARATOR=  ${arrayByteLocal}   ,
229a2dab3c8SSivas SRR   Set Global Variable  ${arrayByte}   ${arrayByteLocal}
230a2dab3c8SSivas SRR
231b29d2e84SChris AustenCopy ipmitool
232b29d2e84SChris 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.
233b29d2e84SChris Austen
234b29d2e84SChris Austen    Import Library      SCPLibrary      WITH NAME       scp
235b29d2e84SChris Austen    scp.Open connection     ${OPENBMC_HOST}     username=${OPENBMC_USERNAME}      password=${OPENBMC_PASSWORD}
236b29d2e84SChris Austen    scp.Put File    tools/ipmitool   /tmp
237b29d2e84SChris Austen    SSHLibrary.Open Connection     ${OPENBMC_HOST}
238b29d2e84SChris Austen    Login   ${OPENBMC_USERNAME}    ${OPENBMC_PASSWORD}
239b29d2e84SChris Austen    Execute Command     chmod +x /tmp/ipmitool
2400c34f69cSSridevi Ramesh
2410c34f69cSSridevi RameshInitiate Host Boot Via External IPMI
2420c34f69cSSridevi Ramesh    [Documentation]  Initiate host power on using external IPMI.
2430c34f69cSSridevi Ramesh    ${output}=  Run External IPMI Standard Command  chassis power on
2440c34f69cSSridevi Ramesh    Should Not Contain  ${output}  Error
2450c34f69cSSridevi Ramesh    Wait Until Keyword Succeeds
2460c34f69cSSridevi Ramesh    ...  10 min  10 sec  Is Host Running
2470c34f69cSSridevi Ramesh
2480c34f69cSSridevi RameshInitiate Host PowerOff Via External IPMI
2490c34f69cSSridevi Ramesh    [Documentation]  Initiate host power off using external IPMI.
2500c34f69cSSridevi Ramesh    ${output}=  Run External IPMI Standard Command  chassis power off
2510c34f69cSSridevi Ramesh    Should Not Contain  ${output}  Error
2520c34f69cSSridevi Ramesh    Wait Until Keyword Succeeds
2530c34f69cSSridevi Ramesh    ...  3 min  10 sec  Is Host Off
254