1*** Settings ***
2Documentation     Utilities for power management tests.
3
4Resource          ../lib/rest_client.robot
5Resource          ../lib/openbmc_ffdc.robot
6Resource          ../lib/boot_utils.robot
7Resource          ../lib/ipmi_client.robot
8Library           ../lib/var_funcs.py
9
10*** Variables ***
11
12${power_cap_uri}    /redfish/v1/Chassis/${CHASSIS_ID}/EnvironmentMetrics
13
14*** Keywords ***
15
16Get System Power Cap Limit
17    [Documentation]  Get the allowed MAX and MIN power limit of the chassis.
18
19    # GET request of /redfish/v1/Chassis/chassis/EnvironmentMetrics  | grep -A5 Power
20    #   "PowerLimitWatts": {
21    #       "AllowableMax": 2488,
22    #       "AllowableMin": 1778,
23    #       "ControlMode": "Disabled",
24    #       "SetPoint": 2488
25    # }
26
27    ${power_limit_watts}=  Redfish.Get Attribute  ${power_cap_uri}   PowerLimitWatts
28
29    [return]  ${power_limit_watts}
30
31
32DCMI Power Get Limits
33    [Documentation]  Run dcmi power get_limit and return values as a
34    ...  dictionary.
35
36    # This keyword packages the five lines returned by dcmi power get_limit
37    # command into a dictionary.  For example, the dcmi command may return:
38    #  Current Limit State: No Active Power Limit
39    #  Exception actions:   Hard Power Off & Log Event to SEL
40    #  Power Limit:         500   Watts
41    #  Correction time:     0 milliseconds
42    #  Sampling period:     0 seconds
43    # The power limit setting can be obtained with the following:
44    # &{limits}=  DCMI Power Get Limits
45    # ${power_setting}=  Set Variable  ${limits['power_limit']}
46
47    ${output}=  Run External IPMI Standard Command  dcmi power get_limit
48    ${output}=  Remove String  ${output}  Watts
49    ${output}=  Remove String  ${output}  milliseconds
50    ${output}=  Remove String  ${output}  seconds
51    &{limits}=  Key Value Outbuf To Dict  ${output}
52    [Return]  &{limits}
53
54
55Get DCMI Power Limit
56    [Documentation]  Return the system's current DCMI power_limit
57    ...  watts setting.
58
59    &{limits}=  DCMI Power Get Limits
60    ${power_setting}=  Get From Dictionary  ${limits}  power_limit
61    [Return]  ${power_setting}
62
63
64Set DCMI Power Limit And Verify
65    [Documentation]  Set system power limit via IPMI DCMI command.
66    [Arguments]  ${power_limit}
67
68    # Description of argument(s):
69    # limit      The power limit in watts
70
71    ${cmd}=  Catenate  dcmi power set_limit limit ${power_limit}
72    Run External IPMI Standard Command  ${cmd}
73    ${power}=  Get DCMI Power Limit
74    Should Be True  ${power} == ${power_limit}
75    ...  msg=Failed setting dcmi power limit to ${power_limit} watts.
76
77
78Activate DCMI Power And Verify
79    [Documentation]  Activate DCMI power limiting.
80
81    ${resp}=  Run External IPMI Standard Command  dcmi power activate
82    Should Contain  ${resp}  successfully activated
83    ...  msg=Command failed: dcmi power activate.
84
85
86Fail If DCMI Power Is Not Activated
87    [Documentation]  Fail if DCMI power limiting is not activated.
88
89    ${cmd}=  Catenate  dcmi power get_limit | grep State:
90    ${resp}=  Run External IPMI Standard Command  ${cmd}
91    Should Contain  ${resp}  Power Limit Active  msg=DCMI power is not active.
92
93
94Deactivate DCMI Power And Verify
95    [Documentation]  Deactivate DCMI power power limiting.
96
97    ${cmd}=  Catenate  dcmi power deactivate | grep deactivated
98    ${resp}=  Run External IPMI Standard Command  ${cmd}
99    Should Contain  ${resp}  successfully deactivated
100    ...  msg=Command failed: dcmi power deactivater.
101
102
103Fail If DCMI Power Is Not Deactivated
104    [Documentation]  Fail if DCMI power limiting is not deactivated.
105
106    ${cmd}=  Catenate  dcmi power get_limit | grep State:
107    ${resp}=  Run External IPMI Standard Command  ${cmd}
108    Should Contain  ${resp}  No Active Power Limit
109    ...  msg=DCMI power is not deactivated.
110
111
112Get DCMI Power Limit Via REST
113    [Documentation]  Return the system's current DCMI power_limit
114    ...  watts setting using REST interface.
115
116    ${power_limit}=  Read Attribute  ${CONTROL_HOST_URI}power_cap  PowerCap
117    [Return]  ${power_limit}
118
119
120Set DCMI Power Limit Via REST
121    [Documentation]  Set system power limit via REST command.
122    [Arguments]  ${power_limit}  ${verify}=${True}
123
124    # Description of argument(s):
125    # power_limit  The power limit in watts.
126    # verify       If True, read the power setting to confirm.
127
128    ${int_power_limit}=  Convert To Integer  ${power_limit}
129    ${data}=  Create Dictionary  data=${int_power_limit}
130    Write Attribute   ${CONTROL_HOST_URI}power_cap  PowerCap  data=${data}
131    Return From Keyword If  ${verify} == ${False}
132    ${power}=  Read Attribute  ${CONTROL_HOST_URI}power_cap  PowerCap
133    Should Be True  ${power} == ${power_limit}
134    ...  msg=Failed setting power limit to ${power_limit} watts via REST.
135
136
137Activate DCMI Power Via REST
138    [Documentation]  Activate DCMI power limiting via REST.
139    [Arguments]  ${verify}=${True}
140
141    # Description of argument(s):
142    # verify       If True, read the setting to confirm.
143
144    ${data}=  Create Dictionary  data=${True}
145    Write Attribute   ${CONTROL_HOST_URI}power_cap  PowerCapEnable
146    ...  data=${data}
147    Return From Keyword If  ${verify} == ${False}
148    ${setting}=  Get DCMI Power Acivation Via REST
149    Should Be True  ${setting} == ${1}
150    ...  msg=Failed to activate power limiting via REST.
151
152
153Deactivate DCMI Power Via REST
154    [Documentation]  Deactivate DCMI power limiting via REST.
155    [Arguments]  ${verify}=${True}
156
157    # Description of argument(s):
158    # verify       If True, read the setting to confirm.
159
160    ${data}=  Create Dictionary  data=${False}
161    Write Attribute   ${CONTROL_HOST_URI}power_cap  PowerCapEnable
162    ...  data=${data}
163    Return From Keyword If  ${verify} == ${False}
164    ${setting}=  Get DCMI Power Acivation Via REST
165    Should Be True  ${setting} == ${0}
166    ...  msg=Failed to deactivate power limiting via REST.
167
168
169Get DCMI Power Acivation Via REST
170    [Documentation]  Return the system's current DCMI power activation
171    ...  state setting using REST interface.
172
173    ${power_activation_setting}=  Read Attribute
174    ...  ${CONTROL_HOST_URI}power_cap  PowerCapEnable
175    [Return]  ${power_activation_setting}
176
177
178OCC Tool Upload Setup
179    [Documentation]  Upload occtoolp9 to /tmp on the OS.
180
181    ${cmd}=  Catenate  wget --no-check-certificate
182    ...  -O/tmp/occtoolp9 --content-disposition
183    ...  https://github.com/open-power/occ/raw/master/src/tools/occtoolp9
184    ...  && chmod 777 /tmp/occtoolp9
185    ${output}  ${stderr}  ${rc}=  OS Execute Command  ${cmd}
186