xref: /openbmc/openbmc-test-automation/lib/state_manager.robot (revision 2bd6fc0ea4f0e0b224830335dffe8cc7cee8d7a5)
1*** Settings ***
2Resource          ../lib/utils.robot
3Variables         ../data/variables.py
4
5*** Variables ***
6
7${BMC_READY_STATE}           Ready
8${BMC_NOT_READY_STATE}       NotReady
9${QUIET}  ${0}
10
11# "1" indicates that the new "xyz" interface should be used.
12${OBMC_STATES_VERSION}    ${1}
13
14*** Keywords ***
15
16Initiate Host Boot
17    [Documentation]  Initiate host power on.
18    [Arguments]  ${wait}=${1}
19
20    # Description of arguments:
21    # wait  Indicates that this keyword should wait for host running state.
22
23    ${args}=  Create Dictionary   data=${HOST_POWERON_TRANS}
24    Write Attribute
25    ...  ${HOST_STATE_URI}  RequestedHostTransition   data=${args}
26
27    # Does caller want to wait for status?
28    Run Keyword If  '${wait}' == '${0}'  Return From Keyword
29
30    Wait Until Keyword Succeeds
31    ...  10 min  10 sec  Is Host Running
32
33
34Initiate Host PowerOff
35    [Documentation]  Initiate host power off.
36    [Arguments]  ${wait}=${1}
37
38    # Description of arguments:
39    # wait  Indicates that this keyword should wait for host off state.
40
41    ${args}=  Create Dictionary   data=${HOST_POWEROFF_TRANS}
42    Write Attribute
43    ...  ${HOST_STATE_URI}  RequestedHostTransition   data=${args}
44
45    # Does caller want to wait for status?
46    Run Keyword If  '${wait}' == '${0}'  Return From Keyword
47
48    # TODO: Reference to open-power/skiboot#81.
49    # Revert to 3 minutes once fixed.
50    Wait Until Keyword Succeeds
51    ...  6 min  10 sec  Is Host Off
52
53
54Initiate Host Reboot
55    [Documentation]  Initiate host reboot via REST.
56
57    ${args}=  Create Dictionary  data=${HOST_REBOOT_TRANS}
58    Write Attribute
59    ...  ${HOST_STATE_URI}  RequestedHostTransition  data=${args}
60    Is Host Rebooted
61
62
63Is Host Running
64    [Documentation]  Check if host state is "Running".
65    # Chassis state should be "On" before we check the host state.
66    Is Chassis On
67    ${host_state}=  Get Host State
68    Should Be Equal  Running  ${host_state}
69    # Check to verify that the host is really booted.
70    Is OS Booted
71
72
73Get Host State Attribute
74    [Documentation]  Return the state of the host as a string.
75    [Arguments]  ${host_attribute}  ${quiet}=${QUIET}
76
77    # Description of argument(s):
78    # host_attribute   Host attribute name.
79    # quiet            Suppress REST output logging to console.
80
81    ${state}=
82    ...  Read Attribute  ${HOST_STATE_URI}  ${host_attribute}  quiet=${quiet}
83    [Return]  ${state}
84
85
86Is OS Booted
87    [Documentation]  Check OS status.
88
89    # Example:
90    # "/xyz/openbmc_project/state/host0": {
91    #    "AttemptsLeft": 0,
92    #    "BootProgress": "xyz.openbmc_project.State.Boot.Progress.ProgressStages.OSStart",
93    #    "CurrentHostState": "xyz.openbmc_project.State.Host.HostState.Running",
94    #    "OperatingSystemState": "xyz.openbmc_project.State.OperatingSystem.Status.OSStatus.BootComplete",
95    #    "RequestedHostTransition": "xyz.openbmc_project.State.Host.Transition.On"
96    # }
97
98    # TODO: Remove this logic once migration is complete.
99    ${status}=  Run Keyword And Return Status  Is OS Starting
100    Return From Keyword If   '${status}' == '${True}'    ${True}
101
102    ${boot_stage}=  Get Host State Attribute  BootProgress
103    Should Be Equal  ${OS_BOOT_START}  ${boot_stage}
104
105    ${os_state}=  Get Host State Attribute  OperatingSystemState
106    Should Be Equal  ${OS_BOOT_COMPLETE}  ${os_state}
107
108
109Is Host Off
110    [Documentation]  Check if host state is "Off".
111    # Chassis state should be "Off" before we check the host state.
112    Is Chassis Off
113    ${host_state}=  Get Host State
114    Should Be Equal  Off  ${host_state}
115    # Check to verify that the host shutdown completely.
116    # TODO openbmc/openbmc#2049 - boot sensor not cleared on power off
117    #Is OS Off
118
119
120Is Host Rebooted
121    [Documentation]  Checks if host rebooted.
122
123    ${host_trans_state}=  Get Host Trans State
124    Should Be Equal  ${host_trans_state}  Reboot
125    Is Host Running
126
127
128Is Chassis On
129    [Documentation]  Check if chassis state is "On".
130    ${power_state}=  Get Chassis Power State
131    Should Be Equal  On  ${power_state}
132
133
134Is Chassis Off
135    [Documentation]  Check if chassis state is "Off".
136    ${power_state}=  Get Chassis Power State
137    Should Be Equal  Off  ${power_state}
138
139Is Host Quiesced
140    [Documentation]  Check if host state is quiesced.
141    ${host_state}=  Get Host State
142    ${status}=  Run Keyword And Return Status  Should Be Equal
143    ...  ${host_state}  Quiesced
144    [Return]  ${status}
145
146
147Recover Quiesced Host
148    [Documentation]  Recover host from quisced state.
149
150    ${resp}=  Run Keyword And Return Status  Is Host Quiesced
151    Run Keyword If  '${resp}' == 'True'
152    ...  Run Keywords  Initiate Host PowerOff  AND
153    ...  Log  HOST is recovered from quiesced state
154
155
156Get Host State
157    [Documentation]  Return the state of the host as a string.
158    [Arguments]  ${quiet}=${QUIET}
159    # quiet - Suppress REST output logging to console.
160    ${state}=
161    ...  Read Attribute  ${HOST_STATE_URI}  CurrentHostState
162    ...  quiet=${quiet}
163    [Return]  ${state.rsplit('.', 1)[1]}
164
165Get Host Trans State
166    [Documentation]  Return the transition state of host as a string.
167    ...              e.g. On, Off, Reboot
168    [Arguments]  ${quiet}=${QUIET}
169    # Description of arguments:
170    # quiet  Suppress REST output logging to console.
171
172    ${state}=
173    ...  Read Attribute  ${HOST_STATE_URI}  RequestedHostTransition
174    ...  quiet=${quiet}
175    [Return]  ${state.rsplit('.', 1)[1]}
176
177Get Chassis Power State
178    [Documentation]  Return the power state of the Chassis
179    ...              as a string.
180    [Arguments]  ${quiet}=${QUIET}
181    # quiet - Suppress REST output logging to console.
182    ${state}=
183    ...  Read Attribute  ${CHASSIS_STATE_URI}  CurrentPowerState
184    ...  quiet=${quiet}
185    [Return]  ${state.rsplit('.', 1)[1]}
186
187
188Get BMC State
189    [Documentation]  Return the state of the BMC.
190    [Arguments]  ${quiet}=${QUIET}
191    # quiet - Suppress REST output logging to console.
192    ${state}=
193    ...  Read Attribute  ${BMC_STATE_URI}  CurrentBMCState  quiet=${quiet}
194    [Return]  ${state.rsplit('.', 1)[1]}
195
196
197Put BMC State
198    [Documentation]  Put BMC in given state.
199    [Arguments]  ${expected_state}
200    # expected_state - expected BMC state
201
202    ${bmc_state}=  Get BMC State
203    Run Keyword If  '${bmc_state}' == '${expected_state}'
204    ...  Log  BMC is already in ${expected_state} state
205    ...  ELSE  Run Keywords  Initiate BMC Reboot  AND
206    ...  Wait for BMC state  ${expected_state}
207
208
209Initiate BMC Reboot
210    [Documentation]  Initiate BMC reboot.
211    ${args}=  Create Dictionary   data=${BMC_REBOOT_TRANS}
212
213    Run Keyword And Ignore Error  Write Attribute
214    ...  ${BMC_STATE_URI}  RequestedBMCTransition   data=${args}
215
216    ${session_active}=   Check If BMC Reboot Is Initiated
217    Run Keyword If   '${session_active}' == '${True}'
218    ...    Fail   msg=BMC Reboot didn't occur
219
220    Check If BMC is Up
221
222Check If BMC Reboot Is Initiated
223    [Documentation]  Checks whether BMC Reboot is initiated by checking
224    ...              BMC connection loss.
225    # Reboot adds 3 seconds delay before forcing reboot
226    # To minimize race conditions, we wait for 7 seconds
227    Sleep  7s
228    ${alive}=   Run Keyword and Return Status
229    ...    Open Connection And Log In
230    Return From Keyword If   '${alive}' == '${False}'    ${False}
231    [Return]    ${True}
232
233Is BMC Ready
234    [Documentation]  Check if BMC state is Ready.
235    ${bmc_state}=  Get BMC State
236    Should Be Equal  ${BMC_READY_STATE}  ${bmc_state}
237
238Is BMC Not Ready
239    [Documentation]  Check if BMC state is Not Ready.
240    ${bmc_state}=  Get BMC State
241    Should Be Equal  ${BMC_NOT_READY_STATE}  ${bmc_state}
242
243Wait for BMC state
244    [Documentation]  Wait until given BMC state is reached.
245    [Arguments]  ${state}
246    # state - BMC state to wait for
247    Run Keyword If  '${state}' == '${BMC_READY_STATE}'
248    ...    Wait Until Keyword Succeeds
249    ...    10 min  10 sec  Is BMC Ready
250    ...  ELSE IF  '${state}' == '${BMC_NOT_READY_STATE}'
251    ...    Wait Until Keyword Succeeds
252    ...    10 min  10 sec  Is BMC Not Ready
253    ...  ELSE  Fail  msg=Invalid BMC state
254
255
256Set State Interface Version
257    [Documentation]  Set version to indicate which interface to use.
258    ${resp}=  Openbmc Get Request  ${CHASSIS_STATE_URI}
259    ${status}=  Run Keyword And Return Status
260    ...  Should Be Equal As Strings  ${resp.status_code}  ${HTTP_OK}
261    Run Keyword If  '${status}' == '${True}'
262    ...  Set Global Variable  ${OBMC_STATES_VERSION}  ${1}
263    ...  ELSE
264    ...  Set Global Variable  ${OBMC_STATES_VERSION}  ${0}
265
266
267Power Off Request
268    [Documentation]  Select appropriate poweroff keyword.
269    Run Keyword If  '${OBMC_STATES_VERSION}' == '${0}'
270    ...  Initiate Power Off
271    ...  ELSE
272    ...  Initiate Host PowerOff
273
274
275Wait For BMC Ready
276    [Documentation]  Check BMC state and wait for BMC Ready.
277    @{states}=  Create List  BMC_READY  HOST_POWERED_OFF
278    Run Keyword If  '${OBMC_STATES_VERSION}' == '${0}'
279    ...  Wait Until Keyword Succeeds  10 min  10 sec
280    ...  Verify BMC State  ${states}
281    ...  ELSE
282    ...  Wait Until Keyword Succeeds  10 min  10 sec
283    ...  Is BMC Ready
284
285
286