1*** Settings ***
2
3Documentation    Test Lock Management feature of Management Console on BMC.
4
5Resource         ../../lib/resource.robot
6Resource         ../../lib/bmc_redfish_resource.robot
7Resource         ../../lib/openbmc_ffdc.robot
8
9Test Setup       Create New Session
10Test Teardown    Test Teardown Execution
11
12*** Variables ***
13
14# Data-sets for testing different test cases.
15&{LOCKALL_LEN1}                   LockFlag=LockAll                SegmentLength=${1}
16&{LOCKALL_LEN2}                   LockFlag=LockAll                SegmentLength=${2}
17&{LOCKALL_LEN3}                   LockFlag=LockAll                SegmentLength=${3}
18&{LOCKALL_LEN4}                   LockFlag=LockAll                SegmentLength=${4}
19&{LOCKALL_LEN5}                   LockFlag=LockAll                SegmentLength=${5}
20
21&{LOCKALL_INVALID_LOCKFLAG1}      LockFlag=LocAll                 SegmentLength=${2}
22&{LOCKALL_INVALID_LOCKFLAG2}      LockFlag=LOCKALL                SegmentLength=${3}
23&{LOCKALL_INVALID_LOCKFLAG3}      LOCKFLAG=LockAll                SegmentLength=${4}
24&{LOCKSAME_INVALID_LOCKFLAG3}     Lock=LockSame                   SegmentLength=${1}
25&{LOCKSAME_INVALID_LOCKFLAG4}     Lock=LockSame                   SegmentLength=${True}
26
27&{LOCKSAME_LEN1}                  LockFlag=LockSame               SegmentLength=${1}
28&{LOCKSAME_LEN2}                  LockFlag=LockSame               SegmentLength=${2}
29&{LOCKSAME_LEN3}                  LockFlag=LockSame               SegmentLength=${3}
30&{LOCKSAME_LEN4}                  LockFlag=LockSame               SegmentLength=${4}
31&{LOCKSAME_INVALID_LEN1}          LockFlag=LockSame               SegmentLength=${0}
32&{LOCKSAME_INVALID_LEN2}          LockFlag=LockSame               SegmentLength=${5}
33&{LOCKSAME_INVALID_LEN_STR}       LockFlag=LockSame               SegmentLength=2
34&{LOCKSAME_INVALID_LEN_NEG}       LockFlag=LockSame               SegmentLength=${-3}
35&{LOCKSAME_INVALID_LEN_BOOL}      Lock=LockSame                   SegmentLength=${True}
36
37&{DONTLOCK_LEN1}                  LockFlag=DontLock               SegmentLength=${1}
38&{DONTLOCK_LEN2}                  LockFlag=DontLock               SegmentLength=${2}
39&{DONTLOCK_LEN3}                  LockFlag=DontLock               SegmentLength=${3}
40&{DONTLOCK_LEN4}                  LockFlag=DontLock               SegmentLength=${4}
41&{DONTLOCK_INVALID_LEN}           LockFlag=DontLock               SegmentLength=${5}
42&{DONTLOCK_INVALID_LEN_BOOL}      LockFlag=DONTLOCK               SegmentLength=${False}
43&{DONTLOCK_INVALID_LOCKFLAG}      LOCKFLAG=LockAll                SegmentLength=${4}
44
45@{ONE_SEG_FLAG_ALL}               ${LOCKALL_LEN1}
46@{ONE_SEG_FLAG_SAME}              ${LOCKSAME_LEN3}
47@{ONE_SEG_FLAG_DONT}              ${DONTLOCK_LEN4}
48
49@{TWO_SEG_FLAG_1}                 ${LOCKALL_LEN1}                 ${LOCKSAME_LEN2}
50@{TWO_SEG_FLAG_2}                 ${DONTLOCK_LEN3}                ${LOCKALL_LEN1}
51@{TWO_SEG_FLAG_3}                 ${DONTLOCK_LEN4}                ${LOCKSAME_LEN3}
52@{TWO_SEG_FLAG_4}                 ${DONTLOCK_INVALID_LEN}         ${LOCKSAME_LEN3}
53@{TWO_SEG_FLAG_5}                 ${DONTLOCK_LEN2}                ${LOCKSAME_INVALID_LEN1}
54
55@{TWO_SEG_FLAG_INVALID1}          ${DONTLOCK_LEN4}                ${LOCKSAME_INVALID_LEN1}
56@{TWO_SEG_FLAG_INVALID2}          ${LOCKALL_LEN5}                 ${DONTLOCK_LEN1}
57@{TWO_SEG_FLAG_INVALID3}          ${DONTLOCK_LEN1}                ${LOCKALL_INVALID_LOCKFLAG1}
58@{TWO_SEG_FLAG_INVALID4}          ${DONTLOCK_LEN2}                ${LOCKALL_INVALID_LOCKFLAG2}
59@{TWO_SEG_FLAG_INVALID5}          ${DONTLOCK_LEN2}                ${LOCKALL_INVALID_LOCKFLAG3}
60@{TWO_SEG_FLAG_INVALID6}          ${LOCKALL_LEN3}                 ${LOCKSAME_INVALID_LOCKFLAG3}
61@{TWO_SEG_FLAG_INVALID7}          ${DONTLOCK_LEN2}                ${LOCKSAME_INVALID_LOCKFLAG4}
62@{TWO_SEG_FLAG_INVALID8}          ${DONTLOCK_INVALID_LOCKFLAG}    ${LOCKSAME_INVALID_LEN_BOOL}
63@{TWO_SEG_FLAG_INVALID9}          ${DONTLOCK_LEN2}                ${LOCKSAME_INVALID_LOCKFLAG4}
64
65@{THREE_SEG_FLAG_1}               ${LOCKALL_LEN1}                 @{TWO_SEG_FLAG_3}
66@{THREE_SEG_FLAG_2}               ${LOCKSAME_LEN4}                @{TWO_SEG_FLAG_2}
67@{THREE_SEG_FLAG_3}               ${DONTLOCK_LEN3}                @{TWO_SEG_FLAG_1}
68
69@{FOUR_SEG_FLAG_1}                ${LOCKALL_LEN1}                 @{THREE_SEG_FLAG_2}
70@{FOUR_SEG_FLAG_2}                ${LOCKSAME_LEN4}                @{THREE_SEG_FLAG_3}
71@{FOUR_SEG_FLAG_3}                ${DONTLOCK_LEN3}                @{THREE_SEG_FLAG_1}
72
73@{FIVE_SEG_FLAG_1}                ${LOCKALL_LEN1}                 @{FOUR_SEG_FLAG_2}
74@{FIVE_SEG_FLAG_2}                ${LOCKSAME_LEN4}                @{FOUR_SEG_FLAG_3}
75@{FIVE_SEG_FLAG_3}                ${DONTLOCK_LEN3}                @{FOUR_SEG_FLAG_1}
76
77@{SIX_SEG_FLAG_1}                 ${LOCKALL_LEN1}                 @{FIVE_SEG_FLAG_2}
78@{SIX_SEG_FLAG_2}                 ${LOCKSAME_LEN4}                @{FIVE_SEG_FLAG_3}
79@{SIX_SEG_FLAG_3}                 ${DONTLOCK_LEN3}                @{FIVE_SEG_FLAG_1}
80
81@{SEVEN_SEG_FLAG_1}               ${LOCKALL_LEN1}                 @{SIX_SEG_FLAG_2}
82@{SEVEN_SEG_FLAG_2}               ${LOCKSAME_LEN4}                @{SIX_SEG_FLAG_3}
83@{SEVEN_SEG_FLAG_3}               ${DONTLOCK_LEN3}                @{SIX_SEG_FLAG_1}
84
85# Different messages to be verified.
86${PROP_REQ_ERR}         is a required property and must be included in the request.
87${PROP_ERR}             is not in the list of valid properties for the resource.
88${PROP_TYPE_ERR}        is of a different type than the property can accept.
89
90# Build error patterns list.
91@{EMPTY LIST}
92@{ERR_PATTERN1}                   ${PROP_REQ_ERR}                ${PROP_ERR}
93@{ERR_PATTERN2}                   ${PROP_TYPE_ERR}
94@{ERR_PATTERN3}                   ${PROP_REQ_ERR}                ${PROP_ERR}                 ${PROP_TYPE_ERR}
95
96# Dictionary of Locks with Transaction ID as key and Session ID as a value.
97&{LOCKS}
98
99
100*** Test Cases ***
101
102Acquire And Release Different Read Locks
103    [Documentation]  Acquire and release different read locks.
104    [Tags]  Acquire_And_Release_Different_Read_Locks
105    [Template]  Acquire And Release Lock
106
107    # lock  seg_flags                     resource_id  hmc_id  exp_status_code      err_msgs         new_sess
108    # type                                                                                            req
109    Read    ${ONE_SEG_FLAG_ALL}           ${234}       hmc-id  ${HTTP_BAD_REQUEST}  ${EMPTY LIST}    ${True}
110    Read    ${ONE_SEG_FLAG_SAME}          ${234}       hmc-id  ${HTTP_BAD_REQUEST}  ${EMPTY LIST}    ${True}
111    Read    ${ONE_SEG_FLAG_DONT}          ${234}       hmc-id  ${HTTP_BAD_REQUEST}  ${EMPTY LIST}    ${True}
112    Read    ${TWO_SEG_FLAG_1}             ${234}       hmc-id  ${HTTP_OK}           ${EMPTY LIST}    ${True}
113    Read    ${TWO_SEG_FLAG_2}             ${234}       hmc-id  ${HTTP_OK}           ${EMPTY LIST}    ${True}
114    Read    ${TWO_SEG_FLAG_3}             ${234}       hmc-id  ${HTTP_OK}           ${EMPTY LIST}    ${True}
115    Read    ${TWO_SEG_FLAG_4}             ${234}       hmc-id  ${HTTP_BAD_REQUEST}  ${EMPTY LIST}    ${True}
116    Read    ${TWO_SEG_FLAG_5}             ${234}       hmc-id  ${HTTP_BAD_REQUEST}  ${EMPTY LIST}    ${True}
117    Read    ${THREE_SEG_FLAG_1}           ${234}       hmc-id  ${HTTP_OK}           ${EMPTY LIST}    ${True}
118    Read    ${THREE_SEG_FLAG_2}           ${234}       hmc-id  ${HTTP_OK}           ${EMPTY LIST}    ${True}
119    Read    ${THREE_SEG_FLAG_3}           ${234}       hmc-id  ${HTTP_OK}           ${EMPTY LIST}    ${True}
120    Read    ${FOUR_SEG_FLAG_1}            ${234}       hmc-id  ${HTTP_OK}           ${EMPTY LIST}    ${True}
121    Read    ${FOUR_SEG_FLAG_2}            ${234}       hmc-id  ${HTTP_OK}           ${EMPTY LIST}    ${True}
122    Read    ${FOUR_SEG_FLAG_3}            ${234}       hmc-id  ${HTTP_OK}           ${EMPTY LIST}    ${True}
123    Read    ${FIVE_SEG_FLAG_1}            ${234}       hmc-id  ${HTTP_OK}           ${EMPTY LIST}    ${True}
124    Read    ${FIVE_SEG_FLAG_2}            ${234}       hmc-id  ${HTTP_OK}           ${EMPTY LIST}    ${True}
125    Read    ${FIVE_SEG_FLAG_3}            ${234}       hmc-id  ${HTTP_OK}           ${EMPTY LIST}    ${True}
126    Read    ${SIX_SEG_FLAG_1}             ${234}       hmc-id  ${HTTP_OK}           ${EMPTY LIST}    ${True}
127    Read    ${SIX_SEG_FLAG_2}             ${234}       hmc-id  ${HTTP_OK}           ${EMPTY LIST}    ${True}
128    Read    ${SIX_SEG_FLAG_3}             ${234}       hmc-id  ${HTTP_OK}           ${EMPTY LIST}    ${True}
129    Read    ${SEVEN_SEG_FLAG_1}           ${234}       hmc-id  ${HTTP_BAD_REQUEST}  ${EMPTY LIST}    ${True}
130    Read    ${SEVEN_SEG_FLAG_2}           ${234}       hmc-id  ${HTTP_BAD_REQUEST}  ${EMPTY LIST}    ${True}
131    Read    ${SEVEN_SEG_FLAG_3}           ${234}       hmc-id  ${HTTP_BAD_REQUEST}  ${EMPTY LIST}    ${True}
132    Read    ${LOCKSAME_INVALID_LEN1}      ${234}       hmc-id  ${HTTP_BAD_REQUEST}  ${EMPTY LIST}    ${True}
133    Read    ${LOCKSAME_INVALID_LEN_STR}   ${234}       hmc-id  ${HTTP_BAD_REQUEST}  ${ERR_PATTERN2}  ${True}
134    Read    ${LOCKSAME_INVALID_LEN_NEG}   ${234}       hmc-id  ${HTTP_BAD_REQUEST}  ${ERR_PATTERN2}  ${True}
135    Read    ${LOCKSAME_INVALID_LEN_BOOL}  ${234}       hmc-id  ${HTTP_BAD_REQUEST}  ${ERR_PATTERN2}  ${True}
136    Read    ${DONTLOCK_INVALID_LEN_BOOL}  ${234}       hmc-id  ${HTTP_BAD_REQUEST}  ${ERR_PATTERN2}  ${True}
137    Read    ${TWO_SEG_FLAG_INVALID1}      ${234}       hmc-id  ${HTTP_BAD_REQUEST}  ${EMPTY LIST}    ${True}
138    Read    ${TWO_SEG_FLAG_INVALID2}      ${234}       hmc-id  ${HTTP_BAD_REQUEST}  ${EMPTY LIST}    ${True}
139    Read    ${TWO_SEG_FLAG_INVALID3}      ${234}       hmc-id  ${HTTP_BAD_REQUEST}  ${EMPTY LIST}    ${True}
140    Read    ${TWO_SEG_FLAG_INVALID4}      ${234}       hmc-id  ${HTTP_BAD_REQUEST}  ${EMPTY LIST}    ${True}
141    Read    ${TWO_SEG_FLAG_INVALID5}      ${234}       hmc-id  ${HTTP_BAD_REQUEST}  ${ERR_PATTERN1}  ${True}
142    Read    ${TWO_SEG_FLAG_INVALID6}      ${234}       hmc-id  ${HTTP_BAD_REQUEST}  ${ERR_PATTERN1}  ${True}
143    Read    ${TWO_SEG_FLAG_INVALID7}      ${234}       hmc-id  ${HTTP_BAD_REQUEST}  ${ERR_PATTERN3}  ${True}
144    Read    ${TWO_SEG_FLAG_INVALID8}      ${234}       hmc-id  ${HTTP_BAD_REQUEST}  ${ERR_PATTERN1}  ${True}
145    Read    ${TWO_SEG_FLAG_INVALID9}      ${234}       hmc-id  ${HTTP_BAD_REQUEST}  ${ERR_PATTERN2}  ${True}
146    Read    ${TWO_SEG_FLAG_3}             234          hmc-id  ${HTTP_BAD_REQUEST}  ${ERR_PATTERN2}  ${True}
147
148
149Acquire And Release Different Write Locks
150    [Documentation]  Acquire and release different write locks.
151    [Tags]  Acquire_And_Release_Different_Write_Locks
152    [Template]  Acquire And Release Lock
153
154    # lock  seg_flags                    resource_id  hmc_id  exp_status_code       err_msgs         new_sess
155    # type                                                                                            req
156    Write  ${ONE_SEG_FLAG_ALL}           ${234}       hmc-id  ${HTTP_BAD_REQUEST}   ${EMPTY LIST}    ${True}
157    Write  ${ONE_SEG_FLAG_SAME}          ${234}       hmc-id  ${HTTP_BAD_REQUEST}   ${EMPTY LIST}    ${True}
158    Write  ${ONE_SEG_FLAG_DONT}          ${234}       hmc-id  ${HTTP_BAD_REQUEST}   ${EMPTY LIST}    ${True}
159    Write  ${TWO_SEG_FLAG_1}             ${234}       hmc-id  ${HTTP_CONFLICT}      ${EMPTY LIST}    ${True}
160    Write  ${TWO_SEG_FLAG_2}             ${234}       hmc-id  ${HTTP_OK}            ${EMPTY LIST}    ${True}
161    Write  ${TWO_SEG_FLAG_3}             ${234}       hmc-id  ${HTTP_OK}            ${EMPTY LIST}    ${True}
162    Write  ${TWO_SEG_FLAG_INVALID4}      ${234}       hmc-id  ${HTTP_BAD_REQUEST}   ${EMPTY LIST}    ${True}
163    Write  ${THREE_SEG_FLAG_1}           ${234}       hmc-id  ${HTTP_CONFLICT}      ${EMPTY LIST}    ${True}
164    Write  ${THREE_SEG_FLAG_2}           ${234}       hmc-id  ${HTTP_CONFLICT}      ${EMPTY LIST}    ${True}
165    Write  ${THREE_SEG_FLAG_3}           ${234}       hmc-id  ${HTTP_CONFLICT}      ${EMPTY LIST}    ${True}
166    Write  ${FOUR_SEG_FLAG_1}            ${234}       hmc-id  ${HTTP_CONFLICT}      ${EMPTY LIST}    ${True}
167    Write  ${FOUR_SEG_FLAG_2}            ${234}       hmc-id  ${HTTP_CONFLICT}      ${EMPTY LIST}    ${True}
168    Write  ${FOUR_SEG_FLAG_3}            ${234}       hmc-id  ${HTTP_CONFLICT}      ${EMPTY LIST}    ${True}
169    Write  ${FIVE_SEG_FLAG_1}            ${234}       hmc-id  ${HTTP_CONFLICT}      ${EMPTY LIST}    ${True}
170    Write  ${FIVE_SEG_FLAG_2}            ${234}       hmc-id  ${HTTP_CONFLICT}      ${EMPTY LIST}    ${True}
171    Write  ${FIVE_SEG_FLAG_3}            ${234}       hmc-id  ${HTTP_CONFLICT}      ${EMPTY LIST}    ${True}
172    Write  ${SIX_SEG_FLAG_1}             ${234}       hmc-id  ${HTTP_CONFLICT}      ${EMPTY LIST}    ${True}
173    Write  ${SIX_SEG_FLAG_2}             ${234}       hmc-id  ${HTTP_CONFLICT}      ${EMPTY LIST}    ${True}
174    Write  ${SIX_SEG_FLAG_3}             ${234}       hmc-id  ${HTTP_CONFLICT}      ${EMPTY LIST}    ${True}
175    Write  ${SEVEN_SEG_FLAG_1}           ${234}       hmc-id  ${HTTP_BAD_REQUEST}   ${EMPTY LIST}    ${True}
176    Write  ${SEVEN_SEG_FLAG_2}           ${234}       hmc-id  ${HTTP_BAD_REQUEST}   ${EMPTY LIST}    ${True}
177    Write  ${SEVEN_SEG_FLAG_3}           ${234}       hmc-id  ${HTTP_BAD_REQUEST}   ${EMPTY LIST}    ${True}
178    Write  ${LOCKSAME_INVALID_LEN1}      ${234}       hmc-id  ${HTTP_BAD_REQUEST}   ${EMPTY LIST}    ${True}
179    Write  ${LOCKSAME_INVALID_LEN_STR}   ${234}       hmc-id  ${HTTP_BAD_REQUEST}   ${EMPTY LIST}    ${True}
180    Write  ${LOCKSAME_INVALID_LEN_NEG}   ${234}       hmc-id  ${HTTP_BAD_REQUEST}   ${EMPTY LIST}    ${True}
181    Write  ${LOCKSAME_INVALID_LEN_BOOL}  ${234}       hmc-id  ${HTTP_BAD_REQUEST}   ${EMPTY LIST}    ${True}
182    Write  ${DONTLOCK_INVALID_LEN_BOOL}  ${234}       hmc-id  ${HTTP_BAD_REQUEST}   ${EMPTY LIST}    ${True}
183    Write  ${TWO_SEG_FLAG_INVALID1}      ${234}       hmc-id  ${HTTP_BAD_REQUEST}   ${EMPTY LIST}    ${True}
184    Write  ${TWO_SEG_FLAG_INVALID2}      ${234}       hmc-id  ${HTTP_BAD_REQUEST}   ${EMPTY LIST}    ${True}
185    Write  ${TWO_SEG_FLAG_INVALID8}      ${234}       hmc-id  ${HTTP_BAD_REQUEST}   ${ERR_PATTERN1}  ${True}
186    Write  ${TWO_SEG_FLAG_INVALID5}      ${234}       hmc-id  ${HTTP_BAD_REQUEST}   ${ERR_PATTERN1}  ${True}
187    Write  ${TWO_SEG_FLAG_INVALID9}      ${234}       hmc-id  ${HTTP_BAD_REQUEST}   ${ERR_PATTERN2}  ${True}
188    Write  ${TWO_SEG_FLAG_3}             234          hmc-id  ${HTTP_BAD_REQUEST}   ${ERR_PATTERN2}  ${True}
189
190
191Verify GetLockList Returns An Empty Record For An Invalid Session Id
192    [Documentation]  Verify GetLockList returns an empty record for an invalid session id.
193    [Tags]  Verify_GetLockList_Returns_An_Empty_Record_For_An_Invalid_Session_Id
194
195    ${session_location}=  Redfish.Get Session Location
196    ${session_id}=  Evaluate  os.path.basename($session_location)  modules=os
197
198    ${records}=  Run Keyword  Get Locks List  ${session_id}
199    ${records}=  Run Keyword  Get Locks List  ZZzZZz9zzZ
200    ${length}=  Get Length  ${records}
201    Should Be Equal  ${length}  ${0}
202
203
204*** Keywords ***
205
206Return Data Dictionary For Single Request
207    [Documentation]  Return data dictionary for single request.
208    [Arguments]  ${lock_type}  ${seg_flags}  ${resource_id}
209
210    # Description of argument(s):
211    # lock_type    Type of lock (Read/Write).
212    # seg_flags    Segmentation Flags to identify lock elements under system level in the hierarchy.
213    # resource_id  Decimal +ve integer value of maximum 8 hex bytes.  Ex: 134, 2048 etc.
214
215    ${SEG_FLAGS_LOCK}=  Create Dictionary  LockType=${lock_type}  SegmentFlags=@{seg_flags}
216    ...  ResourceID=${resource_id}
217    ${SEG_FLAGS_ENTRIES}=  Create List  ${SEG_FLAGS_LOCK}
218    ${LOCK_REQUEST}=  Create Dictionary  Request=${SEG_FLAGS_ENTRIES}
219    Log To Console  ${LOCK_REQUEST}
220
221    [Return]  ${LOCK_REQUEST}
222
223
224Acquire Lock On A Given Resource
225    [Documentation]  Acquire lock on a given resource.
226    [Arguments]  ${lock_type}  ${seg_flags}  ${resource_id}  ${exp_status_code}=${HTTP_OK}
227    ...  ${err_msgs}=${EMPTY LIST}
228
229    # Description of argument(s):
230    # lock_type        Type of lock (Read/Write).
231    # seg_flags        Segmentation Flags to identify lock elements under system level in the hierarchy.
232    #                  Ex:  [{'LockFlag': 'LockAll', 'SegmentLength': 1},
233    #                   {'LockFlag': 'LockSame', 'SegmentLength': 2}]
234    # resource_id      Decimal +ve integer value of maximum 8 hex bytes.  Ex: 134, 2048 etc.
235    # exp_status_code  expected status code from the AcquireLock request for given inputs.
236
237    ${data}=  Return Data Dictionary For Single Request  ${lock_type}  ${seg_flags}  ${resource_id}
238    ${resp}=  Redfish.Post  /ibm/v1/HMC/LockService/Actions/LockService.AcquireLock
239    ...  body=${data}  valid_status_codes=[${exp_status_code}]
240
241    Log To Console  ${resp.text}
242    ${transaction_id}=  Run Keyword If  ${exp_status_code} != ${HTTP_OK}
243    ...  Set Variable  ${0}
244    ...  ELSE  Load Lock Record And Build Transaction To Session Map  ${resp.text}
245
246    Run Keyword If  ${exp_status_code} != ${HTTP_OK} and ${err_msgs} != ${EMPTY LIST}
247    ...  Load Response And Verify Error  ${resp.text}  err_msgs=${err_msgs}
248    Append Transaction Id And Session Id To Locks Dictionary  ${transaction_id}
249
250    [Return]  ${transaction_id}
251
252
253Load Lock Record And Build Transaction To Session Map
254    [Documentation]  Load lock record and build transaction to session map.
255    [Arguments]  ${resp_text}
256
257    # Description of argument(s):
258    # resp_text  Response test from a REST request.
259
260    ${acquire_lock}=  Evaluate  json.loads('''${resp_text}''')  json
261    Append Transaction Id And Session Id To Locks Dictionary  ${acquire_lock["TransactionID"]}
262
263    [Return]  ${acquire_lock["TransactionID"]}
264
265
266Load Response And Verify Error
267    [Documentation]  Load response and verify error.
268    [Arguments]  ${error_resp}  ${err_msgs}=${EMPTY LIST}
269
270    # Description of argument(s):
271    # error_resp  Error response from a REST request.
272    # err_msgs    List of error msg patterns.
273
274    ${error_resp}=  Replace String  ${error_resp}  \"  \\"
275    ${error_response}=  Evaluate  json.loads('''${error_resp}''')  json
276
277    ${errors}=  Get Dictionary Values  ${error_response}
278    ${extended_errors}=  Create List
279
280    FOR  ${error}  IN  @{errors}
281      Append To List  ${extended_errors}  ${error[0]["Message"]}
282    END
283
284    Log To Console  EXTENDED = ${extended_errors}
285
286    FOR  ${exp_error}  IN  @{err_msgs}
287        Run Keyword  Expect List Of Errors In An Extended Errors  ${exp_error}  ${extended_errors}
288    END
289
290
291Expect List Of Errors In An Extended Errors
292    [Documentation]  Expect list of errors in an extended errors.
293    [Arguments]  ${exp_error}  ${extended_errors}=${EMPTY LIST}
294
295    ${found}=  Set Variable  ${False}
296
297    FOR  ${error_record}  IN  @{extended_errors}
298      ${found}=  Evaluate  '${exp_error}' in '${error_record}'
299      Exit For Loop If  ${found} == ${True}
300    END
301
302    Should Be True  ${found}
303
304
305Append Transaction Id And Session Id To Locks Dictionary
306    [Documentation]  Append transaction id and session id to locks dictionary.
307    [Arguments]  ${transaction_id}
308
309    # Description of argument(s):
310    # transaction_id  Transaction ID created from acquire lock request. Ex: 8, 9 etc.
311
312    ${session_location}=  Redfish.Get Session Location
313    ${session_key}=  Redfish.Get Session Key
314    ${session_id}=  Evaluate  os.path.basename($session_location)  modules=os
315    Set To Dictionary  ${LOCKS}  ${${transaction_id}}  ${session_id}
316    Log To Console  session=${session_id}
317    Log To Console  X-AUTH-Token=${session_key}
318    Log To Console  LOCKS=${LOCKS}
319
320
321Get Locks List
322    [Documentation]  Get locks list.
323    [Arguments]  @{sessions}  ${exp_status_code}=${HTTP_OK}
324
325    # Description of argument(s):
326    # sessions         List of comma separated strings. Ex: ["euHoAQpvNe", "ecTjANqwFr"]
327    # exp_status_code  expected status code from the GetLockList request for given inputs.
328
329    ${sessions}=  Evaluate  json.dumps(${sessions})  json
330    ${data}=  Set Variable  {"SessionIDs": ${sessions}}
331    ${resp}=  Redfish.Post  /ibm/v1/HMC/LockService/Actions/LockService.GetLockList
332    ...  body=${data}  valid_status_codes=[${exp_status_code}]
333
334    ${locks}=  Evaluate  json.loads('''${resp.text}''')  json
335
336    [Return]  ${locks["Records"]}
337
338
339Release Lock
340    [Documentation]  Release lock.
341    [Arguments]  @{transaction_ids}  ${release_type}=Transaction  ${exp_status_code}=${HTTP_OK}
342
343    # Description of argument(s):
344    # transaction_ids  List of transaction ids. Ex: [15, 18]
345    # release_type     Release all locks acquired using current session or only given transaction numbers.
346    #                  Ex:  Session,  Transaction.  Default will be Transaction.
347    # exp_status_code  expected status code from the ReleaseLock request for given inputs.
348
349    ${data}=  Set Variable  {"Type": "${release_type}", "TransactionIDs": ${transaction_ids}}
350    ${data}=  Evaluate  json.dumps(${data})  json
351    Redfish.Post  /ibm/v1/HMC/LockService/Actions/LockService.ReleaseLock
352    ...  body=${data}  valid_status_codes=[${exp_status_code}]
353
354
355Verify Lock Record
356    [Documentation]  Verify lock record.
357    [Arguments]  ${lock_found}  &{lock_records}
358
359    # Description of argument(s):
360    # lock_found    True if lock record is expected to be present, else False.
361    # lock_records  A dictionary containing key value pairs of a lock record.
362
363    ${session}=  Get From Dictionary  ${LOCKS}  ${lock_records["TransactionID"]}
364    ${locks}=  Run Keyword  Get Locks List  ${session}
365
366    ${lock_record_found}=  Set Variable  ${False}
367
368    FOR  ${record}  IN  @{locks}
369      ${record}=  Evaluate  json.dumps(${record})  json
370      ${record}=  Evaluate  json.loads('''${record}''')  json
371      ${lock_record_found}=  Set Variable If  ${record["TransactionID"]} == ${lock_records["TransactionID"]}
372      ...  ${True}  ${False}
373
374      Continue For Loop If  ${lock_record_found} == ${False}
375      Dictionaries Should Be Equal  ${record}  ${lock_records}
376      Exit For Loop
377    END
378
379    Should Be Equal  ${lock_record_found}  ${lock_found}
380
381
382Acquire And Release Lock
383    [Documentation]  Acquire and release lock.
384    [Arguments]  ${lock_type}  ${seg_flags}  ${resource_id}  ${hmc_id}  ${exp_status_code}=${HTTP_OK}
385    ...  ${err_msgs}=${EMPTY LIST}  ${new_sess_req}=${True}
386
387    # Description of argument(s):
388    # lock_type        Type of lock (Read/Write).
389    # seg_flags        Segmentation Flags to identify lock elements under system level in the hierarchy.
390    #                  Ex:  [{'LockFlag': 'LockAll', 'SegmentLength': 1},
391    #                        {'LockFlag': 'LockSame', 'SegmentLength': 2}]
392    # resource_id      Decimal +ve integer value of maximum 8 hex bytes.  Ex: 134, 2048 etc.
393    # hmc_id           Hardware management console id.
394    # exp_status_code  Expected status code from the AcquireLock request for given inputs.
395
396    # Get REST session to BMC.
397    Run Keyword If  ${new_sess_req} == ${True}  Create New Session
398
399    ${inputs}=  Create Dictionary  LockType=${lock_type}  ResourceID=${resource_id}
400    ...  SegmentFlags=${seg_flags}  HMCID=${hmc_id}
401
402    ${transaction_id}=  Run Keyword  Acquire Lock On A Given Resource  ${inputs["LockType"]}
403    ...  ${inputs["SegmentFlags"]}  ${inputs["ResourceID"]}  ${exp_status_code}  err_msgs=${err_msgs}
404
405    ${session}=  Get From Dictionary  ${LOCKS}  ${transaction_id}
406    ${locks}=  Run Keyword  Get Locks List  ${session}
407
408    Set To Dictionary  ${inputs}  TransactionID=${${transaction_id}}  SessionID=${session}
409    ${lock_found}=  Set Variable If  ${exp_status_code} == ${HTTP_OK}  ${True}  ${False}
410    Verify Lock Record  ${lock_found}  &{inputs}
411
412    Return From Keyword If  ${exp_status_code} != ${HTTP_OK}
413
414    Release Lock  ${transaction_id}
415    ${locks}=  Run Keyword  Get Locks List  ${session}
416    Verify Lock Record  ${False}  &{inputs}
417
418    # Delete the session.
419    Redfish.Logout
420
421
422Create New Session
423    [Documentation]  Create new session.
424
425    # Delete current session if any.
426    Redfish.Logout
427
428    # Get a redfish session to BMC.
429    Redfish.Login
430    ${session_location}=  Redfish.Get Session Location
431    ${session_key}=  Redfish.Get Session Key
432
433
434Test Teardown Execution
435    [Documentation]  Test teardown execution.
436
437    FFDC On Test Case Fail
438    Redfish.Logout
439
440