xref: /openbmc/openbmc-test-automation/lib/bmc_redfish_utils.robot (revision 45aa881d9a9900b5440ee9b696df4ac9e8118ee6)
10047de86SSridevi Ramesh*** Settings ***
2b10eacafSGeorge KeishingDocumentation   BMC and host redfish utility keywords.
30047de86SSridevi Ramesh
42db7bcaeSGeorge KeishingResource        resource.robot
50047de86SSridevi RameshResource        bmc_redfish_resource.robot
60047de86SSridevi Ramesh
70047de86SSridevi Ramesh
80047de86SSridevi Ramesh*** Keywords ***
90047de86SSridevi Ramesh
100047de86SSridevi RameshRedfish Power Operation
11b10eacafSGeorge Keishing    [Documentation]  Do Redfish host power operation.
120047de86SSridevi Ramesh    [Arguments]      ${reset_type}
13b910d89bSSushil Singh
140047de86SSridevi Ramesh    # Description of arguments:
150047de86SSridevi Ramesh    # reset_type     Type of power operation.
160047de86SSridevi Ramesh    #                (e.g. On/ForceOff/GracefulRestart/GracefulShutdown)
170047de86SSridevi Ramesh
180047de86SSridevi Ramesh    # Example:
190047de86SSridevi Ramesh    # "Actions": {
200047de86SSridevi Ramesh    # "#ComputerSystem.Reset": {
210047de86SSridevi Ramesh    #  "ResetType@Redfish.AllowableValues": [
220047de86SSridevi Ramesh    #    "On",
230047de86SSridevi Ramesh    #    "ForceOff",
24eeb526ceSGeorge Keishing    #    "ForceOn",
25eeb526ceSGeorge Keishing    #    "ForceRestart",
260047de86SSridevi Ramesh    #    "GracefulRestart",
270047de86SSridevi Ramesh    #    "GracefulShutdown"
28eeb526ceSGeorge Keishing    #    "PowerCycle",
29eeb526ceSGeorge Keishing    #    "Nmi"
300047de86SSridevi Ramesh    #  ],
312deec3cfSGeorge Keishing    #  "target": "/redfish/v1/Systems/system/Actions/ComputerSystem.Reset"
322deec3cfSGeorge Keishing    #  }
332deec3cfSGeorge Keishing    # }
340047de86SSridevi Ramesh
35c2b176e2SGeorge Keishing    ${target}=  redfish_utils.Get Target Actions  /redfish/v1/Systems/system/  ComputerSystem.Reset
360047de86SSridevi Ramesh    ${payload}=  Create Dictionary  ResetType=${reset_type}
37213feb34SMichael Walsh    ${resp}=  Redfish.Post  ${target}  body=&{payload}
380047de86SSridevi Ramesh
390047de86SSridevi Ramesh
40b10eacafSGeorge KeishingRedfish BMC Reset Operation
41b10eacafSGeorge Keishing    [Documentation]  Do Redfish BMC reset operation.
42b910d89bSSushil Singh    [Arguments]  ${reset_type}=GracefulRestart
430047de86SSridevi Ramesh
44b10eacafSGeorge Keishing    # Example:
45b10eacafSGeorge Keishing    # "Actions": {
46b10eacafSGeorge Keishing    # "#Manager.Reset": {
47b10eacafSGeorge Keishing    #  "ResetType@Redfish.AllowableValues": [
48b910d89bSSushil Singh    #    "GracefulRestart",
49b910d89bSSushil Singh    #    "ForceRestart"
50b10eacafSGeorge Keishing    #  ],
51b10eacafSGeorge Keishing    #  "target": "/redfish/v1/Managers/bmc/Actions/Manager.Reset"
52b10eacafSGeorge Keishing    # }
53b10eacafSGeorge Keishing
54c2b176e2SGeorge Keishing    ${target}=  redfish_utils.Get Target Actions  /redfish/v1/Managers/bmc/  Manager.Reset
55b910d89bSSushil Singh    ${payload}=  Create Dictionary  ResetType=${reset_type}
5607fb41f7SGeorge Keishing    Redfish.Post  ${target}  body=&{payload}
57caa718bfSGeorge Keishing
58caa718bfSGeorge Keishing
590296f1d2SAnvesh Kumar RayankulaReset BIOS Via Redfish
600296f1d2SAnvesh Kumar Rayankula    [Documentation]  Do BIOS reset through Redfish.
610296f1d2SAnvesh Kumar Rayankula
620296f1d2SAnvesh Kumar Rayankula    ${target}=  redfish_utils.Get Target Actions  /redfish/v1/Systems/system/Bios/  Bios.ResetBios
630296f1d2SAnvesh Kumar Rayankula    Redfish.Post  ${target}  valid_status_codes=[${HTTP_OK}]
640296f1d2SAnvesh Kumar Rayankula
650296f1d2SAnvesh Kumar Rayankula
6687e984c8SSushil SinghRedfish Delete Session
6787e984c8SSushil Singh    [Documentation]  Redfish delete session.
6887e984c8SSushil Singh    [Arguments]  ${session_info}
6987e984c8SSushil Singh
7087e984c8SSushil Singh    # Description of argument(s):
7187e984c8SSushil Singh    # session_info      Session information are stored in dictionary.
7287e984c8SSushil Singh
7387e984c8SSushil Singh    # ${session_info} = {
7487e984c8SSushil Singh    #     'SessionIDs': 'XXXXXXXXX',
7587e984c8SSushil Singh    #     'ClientID': 'XXXXXX',
7687e984c8SSushil Singh    #     'SessionToken': 'XXXXXXXXX',
7787e984c8SSushil Singh    #     'SessionResp': session response from redfish login
7887e984c8SSushil Singh    # }
7987e984c8SSushil Singh
8087e984c8SSushil Singh    # SessionIDs   : Session IDs
8187e984c8SSushil Singh    # ClientID     : Client ID
8287e984c8SSushil Singh    # SessionToken : Session token
8387e984c8SSushil Singh    # SessionResp  : Response of creating an redfish login session
8487e984c8SSushil Singh
8587e984c8SSushil Singh    Redfish.Delete  /redfish/v1/SessionService/Sessions/${session_info["SessionIDs"]}
8687e984c8SSushil Singh
8787e984c8SSushil Singh
8887e984c8SSushil SinghRedfish Delete List Of Session
8987e984c8SSushil Singh    [Documentation]  Redfish delete session from list of session records, individual session information
9087e984c8SSushil Singh    ...              are stored in dictionary.
9187e984c8SSushil Singh    [Arguments]  ${session_info_list}
9287e984c8SSushil Singh
9387e984c8SSushil Singh    # Description of argument(s):
9487e984c8SSushil Singh    # session_info_list    List contains individual session record are stored in dictionary.
9587e984c8SSushil Singh
9687e984c8SSushil Singh    # ${session_info_list} = [{
9787e984c8SSushil Singh    #     'SessionIDs': 'XXXXXXXXX',
9887e984c8SSushil Singh    #     'ClientID': 'XXXXXX',
9987e984c8SSushil Singh    #     'SessionToken': 'XXXXXXXXX',
10087e984c8SSushil Singh    #     'SessionResp': session response from redfish login
10187e984c8SSushil Singh    # }]
10287e984c8SSushil Singh
10387e984c8SSushil Singh    # SessionIDs   : Session IDs
10487e984c8SSushil Singh    # ClientID     : Client ID
10587e984c8SSushil Singh    # SessionToken : Session token
10687e984c8SSushil Singh    # SessionResp  : Response of creating an redfish login session
10787e984c8SSushil Singh
10887e984c8SSushil Singh    FOR  ${session_record}  IN  @{session_info_list}
10987e984c8SSushil Singh      Redfish.Delete  /redfish/v1/SessionService/Sessions/${session_record["SessionIDs"]}
11087e984c8SSushil Singh    END
11187e984c8SSushil Singh
11287e984c8SSushil Singh
113caa718bfSGeorge KeishingDelete All Redfish Sessions
114caa718bfSGeorge Keishing    [Documentation]  Delete all active redfish sessions.
115caa718bfSGeorge Keishing
116caa718bfSGeorge Keishing    ${saved_session_info}=  Get Redfish Session Info
117caa718bfSGeorge Keishing
118caa718bfSGeorge Keishing    ${resp_list}=  Redfish_Utils.Get Member List
119caa718bfSGeorge Keishing    ...  /redfish/v1/SessionService/Sessions
120caa718bfSGeorge Keishing
121caa718bfSGeorge Keishing    # Remove the current login session from the list.
122caa718bfSGeorge Keishing    Remove Values From List  ${resp_list}  ${saved_session_info["location"]}
123caa718bfSGeorge Keishing
12420ccfc71SMarissa Garza    FOR  ${session}  IN  @{resp_list}
12574c1c856SGeorge Keishing        Run Keyword And Ignore Error  Redfish.Delete  ${session}
12620ccfc71SMarissa Garza    END
127cf163321SMichael Walsh
12887e984c8SSushil Singh
129cf163321SMichael WalshGet Valid FRUs
130cf163321SMichael Walsh    [Documentation]  Return a dictionary containing all of the valid FRU records for the given fru_type.
131cf163321SMichael Walsh    [Arguments]  ${fru_type}
132cf163321SMichael Walsh
133cf163321SMichael Walsh    # NOTE: A valid FRU record will have a "State" key of "Enabled" and a "Health" key of "OK".
134cf163321SMichael Walsh
135cf163321SMichael Walsh    # Description of argument(s):
136cf163321SMichael Walsh    # fru_type  The type of fru (e.g. "Processors", "Memory", etc.).
137cf163321SMichael Walsh
138cf163321SMichael Walsh    ${fru_records}=  Redfish_Utils.Enumerate Request
139cf163321SMichael Walsh    ...  /redfish/v1/Systems/system/${fru_type}  return_json=0
140e256a4f6SMichael Walsh    ${fru_records}=  Filter Struct  ${fru_records}  [('State', 'Enabled'), ('Health', 'OK')]
141cf163321SMichael Walsh
142cf163321SMichael Walsh    [Return]  ${fru_records}
143cf163321SMichael Walsh
144cf163321SMichael Walsh
145cf163321SMichael WalshGet Num Valid FRUs
146cf163321SMichael Walsh    [Documentation]  Return the number of valid FRU records for the given fru_type.
147cf163321SMichael Walsh    [Arguments]  ${fru_type}
148cf163321SMichael Walsh
149cf163321SMichael Walsh    # Description of argument(s):
150cf163321SMichael Walsh    # fru_type  The type of fru (e.g. "Processors", "Memory", etc.).
151cf163321SMichael Walsh
152cf163321SMichael Walsh    ${fru_records}=  Get Valid FRUs  ${fru_type}
153cf163321SMichael Walsh    ${num_valid_frus}=  Get length  ${fru_records}
154cf163321SMichael Walsh
155cf163321SMichael Walsh    [Return]  ${num_valid_frus}
156d76b1423SMarissa Garza
157d76b1423SMarissa Garza
158d76b1423SMarissa GarzaVerify Valid Records
159d76b1423SMarissa Garza    [Documentation]  Verify all records retrieved with the given arguments are valid.
160d76b1423SMarissa Garza    [Arguments]  ${record_type}  ${redfish_uri}  ${reading_type}
161d76b1423SMarissa Garza
162d76b1423SMarissa Garza    # Description of Argument(s):
163d76b1423SMarissa Garza    # record_type    The sensor record type (e.g. "PowerSupplies")
164d76b1423SMarissa Garza    # redfish_uri    The power supply URI (e.g. /redfish/v1/Chassis/chassis/Power)
165d76b1423SMarissa Garza    # reading_type   The power watt readings (e.g. "PowerInputWatts")
166d76b1423SMarissa Garza
167d76b1423SMarissa Garza    # A valid record will have "State" key "Enabled" and "Health" key "OK".
168d76b1423SMarissa Garza    ${records}=  Redfish.Get Attribute  ${redfish_uri}  ${record_type}
169d76b1423SMarissa Garza
170d76b1423SMarissa Garza    Rprint Vars  records
171d76b1423SMarissa Garza
172d76b1423SMarissa Garza    # Example output:
173d76b1423SMarissa Garza    # records:
174d76b1423SMarissa Garza    #   [0]:
175d76b1423SMarissa Garza    #     [@odata.id]:                 /redfish/v1/Chassis/chassis/Power#/PowerControl/0
176d76b1423SMarissa Garza    #     [@odata.type]:               #Power.v1_0_0.PowerControl
177d76b1423SMarissa Garza    #     [MemberId]:                  0
178d76b1423SMarissa Garza    #     [Name]:                      Chassis Power Control
179d76b1423SMarissa Garza    #     [PowerConsumedWatts]:        264.0
180d76b1423SMarissa Garza    #     [PowerLimit]:
181d76b1423SMarissa Garza    #       [LimitInWatts]:            None
182d76b1423SMarissa Garza    #     [PowerMetrics]:
183d76b1423SMarissa Garza    #       [AverageConsumedWatts]:    325
184d76b1423SMarissa Garza    #       [IntervalInMin]:           3
185d76b1423SMarissa Garza    #       [MaxConsumedWatts]:        538
186d76b1423SMarissa Garza    #     [Status]:
187d76b1423SMarissa Garza    #       [Health]:                  OK
188d76b1423SMarissa Garza    #       [State]:                   Enabled
189d76b1423SMarissa Garza
190d76b1423SMarissa Garza    ${invalid_records}=  Filter Struct  ${records}
191d76b1423SMarissa Garza    ...  [('Health', '^OK$'), ('State', '^Enabled$'), ('${reading_type}', '')]  regex=1  invert=1
192d76b1423SMarissa Garza    Valid Length  invalid_records  max_length=0
193fdee1b05SMarissa Garza
194fdee1b05SMarissa Garza    [Return]  ${records}
195ff2c0bc4SVijay
196ff2c0bc4SVijay
197ff2c0bc4SVijayRedfish Create User
198ff2c0bc4SVijay    [Documentation]  Redfish create user.
199ff2c0bc4SVijay    [Arguments]   ${user_name}  ${password}  ${role_id}  ${enabled}  ${force}=${False}
200ff2c0bc4SVijay
201ff2c0bc4SVijay    # Description of argument(s):
202ff2c0bc4SVijay    # user_name           The user name to be created.
203ff2c0bc4SVijay    # password            The password to be assigned.
204ff2c0bc4SVijay    # role_id             The role ID of the user to be created.
205ff2c0bc4SVijay    #                     (e.g. "Administrator", "Operator", etc.).
206ff2c0bc4SVijay    # enabled             Indicates whether the username being created.
207ff2c0bc4SVijay    #                     should be enabled (${True}, ${False}).
208ff2c0bc4SVijay    # force               Delete user account and re-create if force is True.
209ff2c0bc4SVijay
210ff2c0bc4SVijay    ${curr_role}=  Run Keyword And Ignore Error  Get User Role  ${user_name}
211ff2c0bc4SVijay    # Ex: ${curr_role} = ('PASS', 'Administrator')
212ff2c0bc4SVijay
213ff2c0bc4SVijay    ${user_exists}=  Run Keyword And Return Status  Should Be Equal As Strings  ${curr_role}[0]  PASS
214ff2c0bc4SVijay
215ff2c0bc4SVijay    # Delete user account when force is True.
216ff2c0bc4SVijay    Run Keyword If  ${force} == ${True}  Redfish.Delete  ${REDFISH_ACCOUNTS_URI}${user_name}
217ff2c0bc4SVijay    ...  valid_status_codes=[${HTTP_OK}, ${HTTP_NOT_FOUND}]
218ff2c0bc4SVijay
219ff2c0bc4SVijay    # Create specified user when force is True or User does not exist.
220ff2c0bc4SVijay    ${payload}=  Create Dictionary
221ff2c0bc4SVijay    ...  UserName=${user_name}  Password=${password}  RoleId=${role_id}  Enabled=${enabled}
222ff2c0bc4SVijay
223ff2c0bc4SVijay    Run Keyword If  ${force} == ${True} or ${user_exists} == ${False}
224ff2c0bc4SVijay    ...  Redfish.Post  ${REDFISH_ACCOUNTS_URI}  body=&{payload}
225ff2c0bc4SVijay    ...  valid_status_codes=[${HTTP_CREATED}]
226ff2c0bc4SVijay
227ff2c0bc4SVijay
228ff2c0bc4SVijayGet User Role
229ff2c0bc4SVijay    [Documentation]  Get User Role.
230ff2c0bc4SVijay    [Arguments]  ${user_name}
231ff2c0bc4SVijay
232ff2c0bc4SVijay    # Description of argument(s):
233ff2c0bc4SVijay    # user_name    User name to get it's role.
234ff2c0bc4SVijay
235ff2c0bc4SVijay    ${role_config}=  Redfish_Utils.Get Attribute
236ff2c0bc4SVijay    ...  ${REDFISH_ACCOUNTS_URI}${user_name}  RoleId
237ff2c0bc4SVijay
238ff2c0bc4SVijay    [Return]  ${role_config}
239ff2c0bc4SVijay
240ff2c0bc4SVijay
241ff2c0bc4SVijayCreate Users With Different Roles
242ff2c0bc4SVijay    [Documentation]  Create users with different roles.
243ff2c0bc4SVijay    [Arguments]  ${users}  ${force}=${False}
244ff2c0bc4SVijay
245ff2c0bc4SVijay    # Description of argument(s):
246ff2c0bc4SVijay    # users    Dictionary of roles and user credentails to be created.
247ff2c0bc4SVijay    #          Ex:  {'Administrator': '[admin_user, TestPwd123]', 'Operator': '[operator_user, TestPwd123]'}
248ff2c0bc4SVijay    # force    Delete given user account if already exists when force is True.
249ff2c0bc4SVijay
250ff2c0bc4SVijay    FOR  ${role}  IN  @{users}
251ff2c0bc4SVijay      Redfish Create User  ${users['${role}'][0]}  ${users['${role}']}[1]  ${role}  ${True}  ${force}
252ff2c0bc4SVijay    END
253ff2c0bc4SVijay
25439373158SVijay
25539373158SVijayDelete BMC Users Via Redfish
25639373158SVijay    [Documentation]  Delete BMC users via redfish.
25739373158SVijay    [Arguments]  ${users}
25839373158SVijay
25939373158SVijay    # Description of argument(s):
26039373158SVijay    # users    Dictionary of roles and user credentials to be deleted.
26139373158SVijay
26239373158SVijay    FOR  ${role}  IN  @{users}
26339373158SVijay        Redfish.Delete  /redfish/v1/AccountService/Accounts/${users['${role}'][0]}
26439373158SVijay        ...  valid_status_codes=[${HTTP_OK}, ${HTTP_NOT_FOUND}]
26539373158SVijay    END
26639373158SVijay
2677ed1ae88SAnves Kumar rayankula
2687ed1ae88SAnves Kumar rayankulaExpire And Update New Password Via Redfish
2697ed1ae88SAnves Kumar rayankula    [Documentation]  Expire and change password and verify using password.
2707ed1ae88SAnves Kumar rayankula    [Arguments]  ${username}  ${password}  ${new_password}
2717ed1ae88SAnves Kumar rayankula
2727ed1ae88SAnves Kumar rayankula    # Description of argument(s):
2737ed1ae88SAnves Kumar rayankula    # username        The username to be used to login to the BMC.
2747ed1ae88SAnves Kumar rayankula    # password        The password to be used to login to the BMC.
2757ed1ae88SAnves Kumar rayankula    # new_password    The new password to be used to update password.
2767ed1ae88SAnves Kumar rayankula
2777ed1ae88SAnves Kumar rayankula    # Expire admin password using ssh.
2787ed1ae88SAnves Kumar rayankula    Open Connection And Log In  ${username}  ${password}
2797ed1ae88SAnves Kumar rayankula    ${output}  ${stderr}  ${rc}=  BMC Execute Command  passwd --expire ${username}
280*45aa881dSBrian Ma    Should Contain Any  ${output}  password expiry information changed
281*45aa881dSBrian Ma    ...  password changed
2827ed1ae88SAnves Kumar rayankula
2837ed1ae88SAnves Kumar rayankula    # Verify user password expired using Redfish
2847ed1ae88SAnves Kumar rayankula    Verify User Password Expired Using Redfish  ${username}  ${password}
2857ed1ae88SAnves Kumar rayankula
2867ed1ae88SAnves Kumar rayankula    # Change user password.
287*45aa881dSBrian Ma    Redfish.Patch  /redfish/v1/AccountService/Accounts/${username}
2887ed1ae88SAnves Kumar rayankula    ...  body={'Password': '${new_password}'}
2897ed1ae88SAnves Kumar rayankula    Redfish.Logout
2907ed1ae88SAnves Kumar rayankula
2917ed1ae88SAnves Kumar rayankula
2927ed1ae88SAnves Kumar rayankulaVerify User Password Expired Using Redfish
2937ed1ae88SAnves Kumar rayankula    [Documentation]  Checking whether user password expired or not using redfish.
2947ed1ae88SAnves Kumar rayankula
2957ed1ae88SAnves Kumar rayankula    # Description of argument(s):
2967ed1ae88SAnves Kumar rayankula    # username        The username to be used to login to the BMC.
2977ed1ae88SAnves Kumar rayankula    # password        The password to be used to login to the BMC.
2987ed1ae88SAnves Kumar rayankula
2997ed1ae88SAnves Kumar rayankula    [Arguments]  ${username}  ${password}  ${expected_result}=${True}
3007ed1ae88SAnves Kumar rayankula    Redfish.Login  ${username}  ${password}
3017ed1ae88SAnves Kumar rayankula    ${resp}=  Redfish.Get  /redfish/v1/AccountService/Accounts/${username}
3027ed1ae88SAnves Kumar rayankula    Should Be Equal  ${resp.dict["PasswordChangeRequired"]}  ${expected_result}
3037ed1ae88SAnves Kumar rayankula
304