1*** Settings ***
2Resource                ../lib/resource.txt
3Resource                ../lib/rest_client.robot
4Resource                ../lib/connection_client.robot
5Library                 DateTime
6Library                 Process
7Library                 OperatingSystem
8
9*** Variables ***
10${SYSTEM_SHUTDOWN_TIME}       ${5}
11${dbuscmdBase} =    dbus-send --system --print-reply --dest=org.openbmc.settings.Host
12${dbuscmdGet} =   /org/openbmc/settings/host0  org.freedesktop.DBus.Properties.Get
13${dbuscmdString} =   string:"org.openbmc.settings.Host" string:
14
15*** Keywords ***
16Wait For Host To Ping
17    [Arguments]  ${host}  ${timeout}=${OPENBMC_REBOOT_TIMEOUT}min
18    ...          ${interval}=5 sec
19
20    # host      The DNS name or IP of the host to ping.
21    # timeout   The amount of time after which attempts to ping cease.
22    # interval  The amount of time in between attempts to ping.
23
24    Wait Until Keyword Succeeds  ${timeout}  ${interval}  Ping Host  ${host}
25
26Ping Host
27    [Arguments]     ${host}
28    Should Not Be Empty    ${host}   msg=No host provided
29    ${RC}   ${output} =     Run and return RC and Output    ping -c 4 ${host}
30    Log     RC: ${RC}\nOutput:\n${output}
31    Should be equal     ${RC}   ${0}
32
33Get Boot Progress
34    ${state} =     Read Attribute    /org/openbmc/sensors/host/BootProgress    value
35    [return]  ${state}
36
37Is Power On
38    ${state}=  Get Power State
39    Should be equal  ${state}  ${1}
40
41Is Power Off
42    ${state}=  Get Power State
43    Should be equal  ${state}  ${0}
44
45Initiate Power On
46    [Documentation]  Initiates the power on and waits until the Is Power On
47    ...  keyword returns that the power state has switched to on.
48    @{arglist}=   Create List
49    ${args}=     Create Dictionary    data=@{arglist}
50    ${resp}=   Call Method    /org/openbmc/control/chassis0/    powerOn    data=${args}
51    should be equal as strings      ${resp.status_code}     ${HTTP_OK}
52    Wait Until Keyword Succeeds	  3 min    	10 sec    Is Power On
53
54Initiate Power Off
55    [Documentation]  Initiates the power off and waits until the Is Power Off
56    ...  keyword returns that the power state has switched to off.
57    @{arglist}=   Create List
58    ${args}=     Create Dictionary    data=@{arglist}
59    ${resp}=   Call Method    /org/openbmc/control/chassis0/    powerOff   data=${args}
60    should be equal as strings      ${resp.status_code}     ${HTTP_OK}
61    Wait Until Keyword Succeeds	  1 min    	10 sec    Is Power Off
62
63Trigger Warm Reset
64    log to console    "Triggering warm reset"
65    ${data} =   create dictionary   data=@{EMPTY}
66    ${resp} =   openbmc post request    /org/openbmc/control/bmc0/action/warmReset     data=${data}
67    Should Be Equal As Strings      ${resp.status_code}     ${HTTP_OK}
68    ${session_active}=   Check If warmReset is Initiated
69    Run Keyword If   '${session_active}' == '${True}'
70    ...    Fail   msg=warm reset didn't occur
71
72    Sleep   ${SYSTEM_SHUTDOWN_TIME}min
73    Wait For Host To Ping   ${OPENBMC_HOST}
74
75Check OS
76    [Documentation]  Attempts to ping the host OS and then checks that the host
77    ...              OS is up by running an SSH command.
78
79    [Arguments]  ${os_host}=${OS_HOST}  ${os_username}=${OS_USERNAME}
80    ...          ${os_password}=${OS_PASSWORD}
81    [Teardown]  Close Connection
82
83    # os_host           The DNS name/IP of the OS host associated with our BMC.
84    # os_username       The username to be used to sign on to the OS host.
85    # os_password       The password to be used to sign on to the OS host.
86
87    # Attempt to ping the OS. Store the return code to check later.
88    ${ping_rc}=  Run Keyword and Return Status  Ping Host  ${os_host}
89
90    Open connection  ${os_host}
91    Login  ${os_username}  ${os_password}
92
93    ${output}  ${stderr}  ${rc}=  Execute Command  uptime  return_stderr=True
94    ...        return_rc=True
95
96    # If the return code returned by "Execute Command" is non-zero, this keyword
97    # will fail.
98    Should Be Equal  ${rc}      ${0}
99    # We will likewise fail if there is any stderr data.
100    Should Be Empty  ${stderr}
101
102    # We will likewise fail if the OS did not ping, as we could SSH but not ping
103    Should Be Equal As Strings  ${ping_rc}  ${TRUE}
104
105Wait for OS
106    [Documentation]  Waits for the host OS to come up via calls to "Check OS".
107    [Arguments]  ${os_host}=${OS_HOST}  ${os_username}=${OS_USERNAME}
108    ...          ${os_password}=${OS_PASSWORD}  ${timeout}=${OS_WAIT_TIMEOUT}
109
110    # os_host           The DNS name or IP of the OS host associated with our
111    #                   BMC.
112    # os_username       The username to be used to sign on to the OS host.
113    # os_password       The password to be used to sign on to the OS host.
114    # timeout           The timeout in seconds indicating how long you're
115    #                   willing to wait for the OS to respond.
116
117    # The interval to be used between calls to "Check OS".
118    ${interval}=  Set Variable  5
119
120    Wait Until Keyword Succeeds  ${timeout} sec  ${interval}  Check OS
121    ...                          ${os_host}  ${os_username}  ${os_password}
122
123Get BMC State
124    [Documentation]  Returns the state of the BMC as a string. (i.e: BMC_READY)
125    @{arglist}=  Create List
126    ${args}=  Create Dictionary  data=@{arglist}
127    ${resp}=  Call Method  /org/openbmc/managers/System/  getSystemState
128    ...        data=${args}
129    Should be equal as strings  ${resp.status_code}  ${HTTP_OK}
130    ${content}=  to json  ${resp.content}
131    [return]  ${content["data"]}
132
133Get Power State
134    [Documentation]  Returns the power state as an integer. Either 0 or 1.
135    @{arglist}=  Create List
136    ${args}=  Create Dictionary  data=@{arglist}
137    ${resp}=  Call Method  /org/openbmc/control/chassis0/  getPowerState
138    ...        data=${args}
139    Should be equal as strings  ${resp.status_code}  ${HTTP_OK}
140    ${content}=  to json  ${resp.content}
141    [return]  ${content["data"]}
142
143Clear BMC Record Log
144    [Documentation]  Clears all the event logs on the BMC. This would be
145    ...              equivalent to ipmitool sel clear.
146    @{arglist}=   Create List
147    ${args}=     Create Dictionary    data=@{arglist}
148    ${resp}=   Call Method    /org/openbmc/records/events/    clear  data=${args}
149    should be equal as strings      ${resp.status_code}     ${HTTP_OK}
150
151Copy PNOR to BMC
152    Import Library      SCPLibrary      WITH NAME       scp
153    Open Connection for SCP
154    Log    Copying ${PNOR_IMAGE_PATH} to /tmp
155    scp.Put File    ${PNOR_IMAGE_PATH}   /tmp
156
157Flash PNOR
158    [Documentation]    Calls flash bios update method to flash PNOR image
159    [arguments]    ${pnor_image}
160    @{arglist}=   Create List    ${pnor_image}
161    ${args}=     Create Dictionary    data=@{arglist}
162    ${resp}=   Call Method    /org/openbmc/control/flash/bios/    update  data=${args}
163    should be equal as strings      ${resp.status_code}     ${HTTP_OK}
164    Wait Until Keyword Succeeds    2 min   10 sec    Is PNOR Flashing
165
166Get Flash BIOS Status
167    [Documentation]  Returns the status of the flash BIOS API as a string. For
168    ...              example 'Flashing', 'Flash Done', etc
169    ${data}=      Read Properties     /org/openbmc/control/flash/bios
170    [return]    ${data['status']}
171
172Is PNOR Flashing
173    [Documentation]  Get BIOS 'Flashing' status. This indicates that PNOR
174    ...              flashing has started.
175    ${status}=    Get Flash BIOS Status
176    should be equal as strings     ${status}     Flashing
177
178Is PNOR Flash Done
179    [Documentation]  Get BIOS 'Flash Done' status.  This indicates that the
180    ...              PNOR flashing has completed.
181    ${status}=    Get Flash BIOS Status
182    should be equal as strings     ${status}     Flash Done
183
184Is System State Host Booted
185    [Documentation]  Checks whether system state is HOST_BOOTED.
186    ${state}=    Get BMC State
187    should be equal as strings     ${state}     HOST_BOOTED
188
189Verify Ping and REST Authentication
190    ${l_ping} =   Run Keyword And Return Status
191    ...    Ping Host  ${OPENBMC_HOST}
192    Run Keyword If  '${l_ping}' == '${False}'
193    ...    Fail   msg=Ping Failed
194
195    ${l_rest} =   Run Keyword And Return Status
196    ...    Initialize OpenBMC
197    Run Keyword If  '${l_rest}' == '${False}'
198    ...    Fail   msg=REST Authentication Failed
199
200    # Just to make sure the SSH is working for SCP
201    Open Connection And Log In
202    ${system}   ${stderr}=    Execute Command   hostname   return_stderr=True
203    Should Be Empty     ${stderr}
204
205
206Check If BMC is Up
207    [Documentation]  Wait for Host to be online. Checks every X seconds
208    ...              interval for Y minutes and fails if timed out.
209    ...              Default MAX timedout is 10 min, interval 10 seconds.
210    [arguments]      ${max_timeout}=${OPENBMC_REBOOT_TIMEOUT}
211    ...              ${interval}=10 sec
212
213    Wait Until Keyword Succeeds
214    ...   ${max_timeout}  ${interval}   Verify Ping and REST Authentication
215
216
217Check If warmReset is Initiated
218    [Documentation]  Ping would be still alive, so try SSH to connect
219    ...              if fails the ports are down indicating reboot
220    ...              is in progress
221    ${alive}=   Run Keyword and Return Status
222    ...    Open Connection And Log In
223    Return From Keyword If   '${alive}' == '${False}'    ${False}
224    [return]    ${True}
225
226Flush REST Sessions
227    [Documentation]   Removes all the active session objects
228    Delete All Sessions
229
230Initialize DBUS cmd
231    [Documentation]  Initialize dbus string with property string to extract
232    [arguments]   ${boot_property}
233    ${cmd} =     Catenate  ${dbuscmdBase} ${dbuscmdGet} ${dbuscmdString}
234    ${cmd} =     Catenate  ${cmd}${boot_property}
235    Set Global Variable   ${dbuscmd}     ${cmd}
236
237
238Start SOL Console Logging
239    [Documentation]   Start logging to a file in /tmp so that it can
240    ...               be read by any other test cases. Stop existing
241    ...               running client processes if there is any.
242    ...               By default logging at /tmp/obmc-console.log else
243    ...               user input location.
244    ...               The File is appended with datetime and pid of
245    ...               process which created this log file.
246    [Arguments]       ${file_path}=/tmp/obmc-console.log
247
248    Open Connection And Log In
249
250    ${cur_time}=    Get Time Stamp
251    Set Global Variable   ${LOG_TIME}   ${cur_time}
252    Start Command
253    ...  obmc-console-client > ${file_path}-${LOG_TIME}_$$
254
255
256Stop SOL Console Logging
257    [Documentation]   Login to BMC and Stop the obmc-console-client process.
258    ...               Find the pids from the log to filter the one started by
259    ...               specific test datetime and stop that process only.
260    ...               Ignore if there is no process running and return message
261    ...               "No obmc-console-client process running"
262    ...               By default retrieving log from /tmp/obmc-console.log else
263    ...               user input location.
264    [Arguments]       ${file_path}=/tmp/obmc-console.log
265
266    Open Connection And Log In
267
268    ${pid}  ${stderr} =
269    ...  Execute Command
270    ...  ls ${file_path}-${LOG_TIME}_* | cut -d'_' -f 2
271    ...  return_stderr=True
272    Return From Keyword If   '${pid}' == '${EMPTY}'
273    ...   No obmc-console-client process running
274    Should Be Empty     ${stderr}
275
276    ${console}  ${stderr}=
277    ...  Execute Command   kill -s KILL ${pid}
278    ...  return_stderr=True
279    Should Be Empty     ${stderr}
280    Log    Current Client PID:${pid}
281
282    ${console}  ${stderr}=
283    ...  Execute Command
284    ...  cat ${file_path}-${LOG_TIME}_${pid}
285    ...  return_stderr=True
286    Should Be Empty     ${stderr}
287
288    [Return]    ${console}
289
290
291Get Time Stamp
292    [Documentation]     Get the current time stamp data
293    ${cur_time}=    Get Current Date   result_format=%Y%m%d%H%M%S%f
294    [return]   ${cur_time}
295
296
297Verify BMC State
298    [Documentation]   Get the BMC state and verify if the current
299    ...               BMC state is as expected.
300    [Arguments]       ${expected}
301
302    ${current}=  Get BMC State
303    Should Contain  ${current}   ${expected}
304
305