1*** Settings ***
2
3Documentation    Test Lock Management feature of Management Console on BMC.
4
5Resource         ../../lib/resource.robot
6Resource         ../../lib/openbmc_ffdc.robot
7Resource         ../../lib/bmc_redfish_utils.robot
8
9Suite Setup      Run Keyword And Ignore Error  Delete All Redfish Sessions
10Test Setup       Test Setup Execution
11Test Teardown    Test Teardown Execution
12Suite Teardown   Run Keyword And Ignore Error  Delete All Redfish Sessions
13
14*** Variables ***
15
16# Data-sets for testing different test cases.
17&{LOCKALL_LEN1}                   LockFlag=LockAll                SegmentLength=${1}
18&{LOCKALL_LEN2}                   LockFlag=LockAll                SegmentLength=${2}
19&{LOCKALL_LEN3}                   LockFlag=LockAll                SegmentLength=${3}
20&{LOCKALL_LEN4}                   LockFlag=LockAll                SegmentLength=${4}
21&{LOCKALL_LEN5}                   LockFlag=LockAll                SegmentLength=${5}
22
23&{LOCKALL_INVALID_LOCKFLAG1}      LockFlag=LocAll                 SegmentLength=${2}
24&{LOCKALL_INVALID_LOCKFLAG2}      LockFlag=LOCKALL                SegmentLength=${3}
25&{LOCKALL_INVALID_LOCKFLAG3}      LOCKFLAG=LockAll                SegmentLength=${4}
26&{LOCKSAME_INVALID_LOCKFLAG3}     Lock=LockSame                   SegmentLength=${1}
27&{LOCKSAME_INVALID_LOCKFLAG4}     Lock=LockSame                   SegmentLength=${True}
28
29&{LOCKSAME_LEN1}                  LockFlag=LockSame               SegmentLength=${1}
30&{LOCKSAME_LEN2}                  LockFlag=LockSame               SegmentLength=${2}
31&{LOCKSAME_LEN3}                  LockFlag=LockSame               SegmentLength=${3}
32&{LOCKSAME_LEN4}                  LockFlag=LockSame               SegmentLength=${4}
33&{LOCKSAME_INVALID_LEN1}          LockFlag=LockSame               SegmentLength=${0}
34&{LOCKSAME_INVALID_LEN2}          LockFlag=LockSame               SegmentLength=${5}
35&{LOCKSAME_INVALID_LEN_STR}       LockFlag=LockSame               SegmentLength=2
36&{LOCKSAME_INVALID_LEN_NEG}       LockFlag=LockSame               SegmentLength=${-3}
37&{LOCKSAME_INVALID_LEN_BOOL}      Lock=LockSame                   SegmentLength=${True}
38
39&{DONTLOCK_LEN1}                  LockFlag=DontLock               SegmentLength=${1}
40&{DONTLOCK_LEN2}                  LockFlag=DontLock               SegmentLength=${2}
41&{DONTLOCK_LEN3}                  LockFlag=DontLock               SegmentLength=${3}
42&{DONTLOCK_LEN4}                  LockFlag=DontLock               SegmentLength=${4}
43&{DONTLOCK_INVALID_LEN}           LockFlag=DontLock               SegmentLength=${5}
44&{DONTLOCK_INVALID_LEN_BOOL}      LockFlag=DONTLOCK               SegmentLength=${False}
45&{DONTLOCK_INVALID_LOCKFLAG}      LOCKFLAG=LockAll                SegmentLength=${4}
46
47@{ONE_SEG_FLAG_ALL}               ${LOCKALL_LEN1}
48@{ONE_SEG_FLAG_SAME}              ${LOCKSAME_LEN3}
49@{ONE_SEG_FLAG_DONT}              ${DONTLOCK_LEN4}
50
51@{TWO_SEG_FLAG_1}                 ${LOCKALL_LEN1}                 ${LOCKSAME_LEN2}
52@{TWO_SEG_FLAG_2}                 ${DONTLOCK_LEN3}                ${LOCKALL_LEN1}
53@{TWO_SEG_FLAG_3}                 ${DONTLOCK_LEN4}                ${LOCKSAME_LEN3}
54@{TWO_SEG_FLAG_4}                 ${DONTLOCK_INVALID_LEN}         ${LOCKSAME_LEN3}
55@{TWO_SEG_FLAG_5}                 ${DONTLOCK_LEN2}                ${LOCKSAME_INVALID_LEN1}
56
57@{TWO_SEG_FLAG_INVALID1}          ${DONTLOCK_LEN4}                ${LOCKSAME_INVALID_LEN1}
58@{TWO_SEG_FLAG_INVALID2}          ${LOCKALL_LEN5}                 ${DONTLOCK_LEN1}
59@{TWO_SEG_FLAG_INVALID3}          ${DONTLOCK_LEN1}                ${LOCKALL_INVALID_LOCKFLAG1}
60@{TWO_SEG_FLAG_INVALID4}          ${DONTLOCK_LEN2}                ${LOCKALL_INVALID_LOCKFLAG2}
61@{TWO_SEG_FLAG_INVALID5}          ${DONTLOCK_LEN2}                ${LOCKALL_INVALID_LOCKFLAG3}
62@{TWO_SEG_FLAG_INVALID6}          ${LOCKALL_LEN3}                 ${LOCKSAME_INVALID_LOCKFLAG3}
63@{TWO_SEG_FLAG_INVALID7}          ${DONTLOCK_LEN2}                ${LOCKSAME_INVALID_LOCKFLAG4}
64@{TWO_SEG_FLAG_INVALID8}          ${DONTLOCK_INVALID_LOCKFLAG}    ${LOCKSAME_INVALID_LEN_BOOL}
65@{TWO_SEG_FLAG_INVALID9}          ${DONTLOCK_LEN2}                ${LOCKSAME_INVALID_LOCKFLAG4}
66
67@{THREE_SEG_FLAG_1}               ${LOCKALL_LEN1}                 @{TWO_SEG_FLAG_3}
68@{THREE_SEG_FLAG_2}               ${LOCKSAME_LEN4}                @{TWO_SEG_FLAG_2}
69@{THREE_SEG_FLAG_3}               ${DONTLOCK_LEN3}                @{TWO_SEG_FLAG_1}
70
71@{FOUR_SEG_FLAG_1}                ${LOCKALL_LEN1}                 @{THREE_SEG_FLAG_2}
72@{FOUR_SEG_FLAG_2}                ${LOCKSAME_LEN4}                @{THREE_SEG_FLAG_3}
73@{FOUR_SEG_FLAG_3}                ${DONTLOCK_LEN3}                @{THREE_SEG_FLAG_1}
74
75@{FIVE_SEG_FLAG_1}                ${LOCKALL_LEN1}                 @{FOUR_SEG_FLAG_2}
76@{FIVE_SEG_FLAG_2}                ${LOCKSAME_LEN4}                @{FOUR_SEG_FLAG_3}
77@{FIVE_SEG_FLAG_3}                ${DONTLOCK_LEN3}                @{FOUR_SEG_FLAG_1}
78
79@{SIX_SEG_FLAG_1}                 ${LOCKALL_LEN1}                 @{FIVE_SEG_FLAG_2}
80@{SIX_SEG_FLAG_2}                 ${LOCKSAME_LEN4}                @{FIVE_SEG_FLAG_3}
81@{SIX_SEG_FLAG_3}                 ${DONTLOCK_LEN3}                @{FIVE_SEG_FLAG_1}
82
83@{SEVEN_SEG_FLAG_1}               ${LOCKALL_LEN1}                 @{SIX_SEG_FLAG_2}
84@{SEVEN_SEG_FLAG_2}               ${LOCKSAME_LEN4}                @{SIX_SEG_FLAG_3}
85@{SEVEN_SEG_FLAG_3}               ${DONTLOCK_LEN3}                @{SIX_SEG_FLAG_1}
86
87# Different messages to be verified.
88${PROP_REQ_ERR}         is a required property and must be included in the request.
89${PROP_ERR}             is not in the list of valid properties for the resource.
90${PROP_TYPE_ERR}        is of a different type than the property can accept.
91
92# Build error patterns list.
93@{EMPTY_LIST}
94@{ERR_PATTERN1}                   ${PROP_REQ_ERR}                ${PROP_ERR}
95@{ERR_PATTERN2}                   ${PROP_TYPE_ERR}
96@{ERR_PATTERN3}                   ${PROP_REQ_ERR}                ${PROP_ERR}                 ${PROP_TYPE_ERR}
97
98# Dictionary of Locks with Transaction ID as key and Session ID as a value.
99&{LOCKS}
100
101
102*** Test Cases ***
103
104Acquire And Release Different Read Locks
105    [Documentation]  Acquire and release different read locks.
106    [Tags]  Acquire_And_Release_Different_Read_Locks
107    [Template]  Acquire And Release Lock
108
109    # lock  seg_flags                     resource_id  hmc_id  exp_status_code      err_msgs         new_sess
110    # type                                                                                            req
111    Read    ${ONE_SEG_FLAG_ALL}           ${234}       hmc-id  ${HTTP_BAD_REQUEST}  ${EMPTY_LIST}    ${True}
112    Read    ${ONE_SEG_FLAG_SAME}          ${234}       hmc-id  ${HTTP_BAD_REQUEST}  ${EMPTY_LIST}    ${True}
113    Read    ${ONE_SEG_FLAG_DONT}          ${234}       hmc-id  ${HTTP_BAD_REQUEST}  ${EMPTY_LIST}    ${True}
114    Read    ${TWO_SEG_FLAG_1}             ${234}       hmc-id  ${HTTP_BAD_REQUEST}  ${EMPTY_LIST}    ${True}
115    Read    ${TWO_SEG_FLAG_2}             ${234}       hmc-id  ${HTTP_OK}           ${EMPTY_LIST}    ${True}
116    Read    ${TWO_SEG_FLAG_3}             ${234}       hmc-id  ${HTTP_OK}           ${EMPTY_LIST}    ${True}
117    Read    ${TWO_SEG_FLAG_4}             ${234}       hmc-id  ${HTTP_BAD_REQUEST}  ${EMPTY_LIST}    ${True}
118    Read    ${TWO_SEG_FLAG_5}             ${234}       hmc-id  ${HTTP_BAD_REQUEST}  ${EMPTY_LIST}    ${True}
119    Read    ${THREE_SEG_FLAG_1}           ${234}       hmc-id  ${HTTP_BAD_REQUEST}  ${EMPTY_LIST}    ${True}
120    Read    ${THREE_SEG_FLAG_2}           ${234}       hmc-id  ${HTTP_BAD_REQUEST}  ${EMPTY_LIST}    ${True}
121    Read    ${THREE_SEG_FLAG_3}           ${234}       hmc-id  ${HTTP_BAD_REQUEST}  ${EMPTY_LIST}    ${True}
122    Read    ${FOUR_SEG_FLAG_1}            ${234}       hmc-id  ${HTTP_BAD_REQUEST}  ${EMPTY_LIST}    ${True}
123    Read    ${FOUR_SEG_FLAG_2}            ${234}       hmc-id  ${HTTP_BAD_REQUEST}  ${EMPTY_LIST}    ${True}
124    Read    ${FOUR_SEG_FLAG_3}            ${234}       hmc-id  ${HTTP_BAD_REQUEST}  ${EMPTY_LIST}    ${True}
125    Read    ${FIVE_SEG_FLAG_1}            ${234}       hmc-id  ${HTTP_BAD_REQUEST}  ${EMPTY_LIST}    ${True}
126    Read    ${FIVE_SEG_FLAG_2}            ${234}       hmc-id  ${HTTP_BAD_REQUEST}  ${EMPTY_LIST}    ${True}
127    Read    ${FIVE_SEG_FLAG_3}            ${234}       hmc-id  ${HTTP_BAD_REQUEST}  ${EMPTY_LIST}    ${True}
128    Read    ${SIX_SEG_FLAG_1}             ${234}       hmc-id  ${HTTP_BAD_REQUEST}  ${EMPTY_LIST}    ${True}
129    Read    ${SIX_SEG_FLAG_2}             ${234}       hmc-id  ${HTTP_BAD_REQUEST}  ${EMPTY_LIST}    ${True}
130    Read    ${SIX_SEG_FLAG_3}             ${234}       hmc-id  ${HTTP_BAD_REQUEST}  ${EMPTY_LIST}    ${True}
131    Read    ${SEVEN_SEG_FLAG_1}           ${234}       hmc-id  ${HTTP_BAD_REQUEST}  ${EMPTY_LIST}    ${True}
132    Read    ${SEVEN_SEG_FLAG_2}           ${234}       hmc-id  ${HTTP_BAD_REQUEST}  ${EMPTY_LIST}    ${True}
133    Read    ${SEVEN_SEG_FLAG_3}           ${234}       hmc-id  ${HTTP_BAD_REQUEST}  ${EMPTY_LIST}    ${True}
134    Read    ${LOCKSAME_INVALID_LEN1}      ${234}       hmc-id  ${HTTP_BAD_REQUEST}  ${EMPTY_LIST}    ${True}
135    Read    ${LOCKSAME_INVALID_LEN_STR}   ${234}       hmc-id  ${HTTP_BAD_REQUEST}  ${ERR_PATTERN2}  ${True}
136    Read    ${LOCKSAME_INVALID_LEN_NEG}   ${234}       hmc-id  ${HTTP_BAD_REQUEST}  ${ERR_PATTERN2}  ${True}
137    Read    ${LOCKSAME_INVALID_LEN_BOOL}  ${234}       hmc-id  ${HTTP_BAD_REQUEST}  ${ERR_PATTERN2}  ${True}
138    Read    ${DONTLOCK_INVALID_LEN_BOOL}  ${234}       hmc-id  ${HTTP_BAD_REQUEST}  ${ERR_PATTERN2}  ${True}
139    Read    ${TWO_SEG_FLAG_INVALID1}      ${234}       hmc-id  ${HTTP_BAD_REQUEST}  ${EMPTY_LIST}    ${True}
140    Read    ${TWO_SEG_FLAG_INVALID2}      ${234}       hmc-id  ${HTTP_BAD_REQUEST}  ${EMPTY_LIST}    ${True}
141    Read    ${TWO_SEG_FLAG_INVALID3}      ${234}       hmc-id  ${HTTP_BAD_REQUEST}  ${EMPTY_LIST}    ${True}
142    Read    ${TWO_SEG_FLAG_INVALID4}      ${234}       hmc-id  ${HTTP_BAD_REQUEST}  ${EMPTY_LIST}    ${True}
143    Read    ${TWO_SEG_FLAG_INVALID5}      ${234}       hmc-id  ${HTTP_BAD_REQUEST}  ${ERR_PATTERN1}  ${True}
144    Read    ${TWO_SEG_FLAG_INVALID6}      ${234}       hmc-id  ${HTTP_BAD_REQUEST}  ${ERR_PATTERN1}  ${True}
145    Read    ${TWO_SEG_FLAG_INVALID7}      ${234}       hmc-id  ${HTTP_BAD_REQUEST}  ${ERR_PATTERN3}  ${True}
146    Read    ${TWO_SEG_FLAG_INVALID8}      ${234}       hmc-id  ${HTTP_BAD_REQUEST}  ${ERR_PATTERN1}  ${True}
147    Read    ${TWO_SEG_FLAG_INVALID9}      ${234}       hmc-id  ${HTTP_BAD_REQUEST}  ${ERR_PATTERN2}  ${True}
148    Read    ${TWO_SEG_FLAG_3}             234          hmc-id  ${HTTP_BAD_REQUEST}  ${ERR_PATTERN2}  ${True}
149
150
151Acquire And Release Different Write Locks
152    [Documentation]  Acquire and release different write locks.
153    [Tags]  Acquire_And_Release_Different_Write_Locks
154    [Template]  Acquire And Release Lock
155
156    # lock  seg_flags                    resource_id  hmc_id  exp_status_code       err_msgs         new_sess
157    # type                                                                                            req
158    Write  ${ONE_SEG_FLAG_ALL}           ${234}       hmc-id  ${HTTP_BAD_REQUEST}   ${EMPTY_LIST}    ${True}
159    Write  ${ONE_SEG_FLAG_SAME}          ${234}       hmc-id  ${HTTP_BAD_REQUEST}   ${EMPTY_LIST}    ${True}
160    Write  ${ONE_SEG_FLAG_DONT}          ${234}       hmc-id  ${HTTP_BAD_REQUEST}   ${EMPTY_LIST}    ${True}
161    Write  ${TWO_SEG_FLAG_1}             ${234}       hmc-id  ${HTTP_BAD_REQUEST}   ${EMPTY_LIST}    ${True}
162    Write  ${TWO_SEG_FLAG_2}             ${234}       hmc-id  ${HTTP_OK}            ${EMPTY_LIST}    ${True}
163    Write  ${TWO_SEG_FLAG_3}             ${234}       hmc-id  ${HTTP_OK}            ${EMPTY_LIST}    ${True}
164    Write  ${TWO_SEG_FLAG_INVALID4}      ${234}       hmc-id  ${HTTP_BAD_REQUEST}   ${EMPTY_LIST}    ${True}
165    Write  ${THREE_SEG_FLAG_1}           ${234}       hmc-id  ${HTTP_BAD_REQUEST}   ${EMPTY_LIST}    ${True}
166    Write  ${THREE_SEG_FLAG_2}           ${234}       hmc-id  ${HTTP_BAD_REQUEST}   ${EMPTY_LIST}    ${True}
167    Write  ${THREE_SEG_FLAG_3}           ${234}       hmc-id  ${HTTP_BAD_REQUEST}   ${EMPTY_LIST}    ${True}
168    Write  ${FOUR_SEG_FLAG_1}            ${234}       hmc-id  ${HTTP_BAD_REQUEST}   ${EMPTY_LIST}    ${True}
169    Write  ${FOUR_SEG_FLAG_2}            ${234}       hmc-id  ${HTTP_BAD_REQUEST}   ${EMPTY_LIST}    ${True}
170    Write  ${FOUR_SEG_FLAG_3}            ${234}       hmc-id  ${HTTP_BAD_REQUEST}   ${EMPTY_LIST}    ${True}
171    Write  ${FIVE_SEG_FLAG_1}            ${234}       hmc-id  ${HTTP_BAD_REQUEST}   ${EMPTY_LIST}    ${True}
172    Write  ${FIVE_SEG_FLAG_2}            ${234}       hmc-id  ${HTTP_BAD_REQUEST}   ${EMPTY_LIST}    ${True}
173    Write  ${FIVE_SEG_FLAG_3}            ${234}       hmc-id  ${HTTP_BAD_REQUEST}   ${EMPTY_LIST}    ${True}
174    Write  ${SIX_SEG_FLAG_1}             ${234}       hmc-id  ${HTTP_BAD_REQUEST}   ${EMPTY_LIST}    ${True}
175    Write  ${SIX_SEG_FLAG_2}             ${234}       hmc-id  ${HTTP_BAD_REQUEST}   ${EMPTY_LIST}    ${True}
176    Write  ${SIX_SEG_FLAG_3}             ${234}       hmc-id  ${HTTP_BAD_REQUEST}   ${EMPTY_LIST}    ${True}
177    Write  ${SEVEN_SEG_FLAG_1}           ${234}       hmc-id  ${HTTP_BAD_REQUEST}   ${EMPTY_LIST}    ${True}
178    Write  ${SEVEN_SEG_FLAG_2}           ${234}       hmc-id  ${HTTP_BAD_REQUEST}   ${EMPTY_LIST}    ${True}
179    Write  ${SEVEN_SEG_FLAG_3}           ${234}       hmc-id  ${HTTP_BAD_REQUEST}   ${EMPTY_LIST}    ${True}
180    Write  ${LOCKSAME_INVALID_LEN1}      ${234}       hmc-id  ${HTTP_BAD_REQUEST}   ${EMPTY_LIST}    ${True}
181    Write  ${LOCKSAME_INVALID_LEN_STR}   ${234}       hmc-id  ${HTTP_BAD_REQUEST}   ${EMPTY_LIST}    ${True}
182    Write  ${LOCKSAME_INVALID_LEN_NEG}   ${234}       hmc-id  ${HTTP_BAD_REQUEST}   ${EMPTY_LIST}    ${True}
183    Write  ${LOCKSAME_INVALID_LEN_BOOL}  ${234}       hmc-id  ${HTTP_BAD_REQUEST}   ${EMPTY_LIST}    ${True}
184    Write  ${DONTLOCK_INVALID_LEN_BOOL}  ${234}       hmc-id  ${HTTP_BAD_REQUEST}   ${EMPTY_LIST}    ${True}
185    Write  ${TWO_SEG_FLAG_INVALID1}      ${234}       hmc-id  ${HTTP_BAD_REQUEST}   ${EMPTY_LIST}    ${True}
186    Write  ${TWO_SEG_FLAG_INVALID2}      ${234}       hmc-id  ${HTTP_BAD_REQUEST}   ${EMPTY_LIST}    ${True}
187    Write  ${TWO_SEG_FLAG_INVALID8}      ${234}       hmc-id  ${HTTP_BAD_REQUEST}   ${ERR_PATTERN1}  ${True}
188    Write  ${TWO_SEG_FLAG_INVALID5}      ${234}       hmc-id  ${HTTP_BAD_REQUEST}   ${ERR_PATTERN1}  ${True}
189    Write  ${TWO_SEG_FLAG_INVALID9}      ${234}       hmc-id  ${HTTP_BAD_REQUEST}   ${ERR_PATTERN2}  ${True}
190    Write  ${TWO_SEG_FLAG_3}             234          hmc-id  ${HTTP_BAD_REQUEST}   ${ERR_PATTERN2}  ${True}
191
192
193Verify GetLockList Returns An Empty Record For An Invalid Session Id
194    [Documentation]  Verify GetLockList returns an empty record for an invalid session id.
195    [Tags]  Verify_GetLockList_Returns_An_Empty_Record_For_An_Invalid_Session_Id
196
197    ${resp}=  Redfish Login  kwargs= "Oem":{"OpenBMC" : {"ClientID":"${hmc_id}"}}
198
199    ${records}=  Get Locks List  ${resp['Id']}
200    ${records}=  Get Locks List  ZZzZZz9zzZ
201    ${length}=  Get Length  ${records}
202    Should Be Equal  ${length}  ${0}
203
204
205Verify Lock Conflicts
206    [Documentation]  Verify lock conflicts.
207    [Tags]  Verify_Lock_Conflicts
208    [Template]  Acquire And Release Lock
209
210    Write  ${TWO_SEG_FLAG_2}  ${234}  hmc-id  ${HTTP_OK}        ['NA']  ${True}
211    Read   ${TWO_SEG_FLAG_2}  ${234}  hmc-id  ${HTTP_CONFLICT}  ['NA']  ${False}
212    Read   ${TWO_SEG_FLAG_2}  ${234}  hmc-id  ${HTTP_OK}        ['NA']  ${True}
213    Write  ${TWO_SEG_FLAG_2}  ${234}  hmc-id  ${HTTP_CONFLICT}  ['NA']  ${False}
214    Write  ${TWO_SEG_FLAG_2}  ${234}  hmc-id  ${HTTP_OK}        ['NA']  ${True}
215    Write  ${TWO_SEG_FLAG_2}  ${234}  hmc-id  ${HTTP_CONFLICT}  ['NA']  ${False}
216
217
218Verify Persistency Of Read Locks After BMC Reboot
219    [Documentation]  Verify persistency of locks after BMC reboot.
220    [Tags]  Verify_Persistency_Of_Read_Locks_After_BMC_Reboot
221    [Template]  Locks Persistency Check After BMC Reboot
222
223    # lock_type  seg_flags          resource_id
224    Read         ${TWO_SEG_FLAG_2}  ${234}
225
226
227Verify Persistency Of Write Locks After BMC Reboot
228    [Documentation]  Verify persistency of locks after BMC reboot.
229    [Tags]  Verify_Persistency_Of_Write_Locks_After_BMC_Reboot
230    [Template]  Locks Persistency Check After BMC Reboot
231
232    # lock_type  seg_flags          resource_id
233    Write        ${TWO_SEG_FLAG_2}  ${234}
234
235
236Verify Valid Lock Transactions Release
237    [Documentation]  Verify valid lock transactions release.
238    [Tags]  Verify_Valid_Lock_Transactions_Release
239
240    ${transaction_id1}=  Acquire Lock On A Given Resource  Read  ${TWO_SEG_FLAG_2}  ${234}
241    ${locks_before}=  Get Locks List  ${SESSION_ID}
242
243    ${transaction_id2}=  Acquire Lock On A Given Resource  Read  ${TWO_SEG_FLAG_3}  ${234}
244    ${transaction_id3}=  Acquire Lock On A Given Resource  Read  ${TWO_SEG_FLAG_2}  ${234}
245    ${transaction_id4}=  Acquire Lock On A Given Resource  Read  ${TWO_SEG_FLAG_3}  ${234}
246
247    ${transaction_ids}=  Create List  ${transaction_id2}  ${transaction_id3}  ${transaction_id4}
248    Release Locks  ${transaction_ids}
249    ${locks_after}=  Get Locks List  ${SESSION_ID}
250    Should Be Equal  ${locks_before}  ${locks_after}
251
252
253Verify Invalid Lock Transactions Release
254    [Documentation]  Verify invalid lock transactions release.
255    [Tags]  Verify_Invalid_Lock_Transactions_Release
256
257    ${transaction_id1}=  Acquire Lock On A Given Resource  Read  ${TWO_SEG_FLAG_2}  ${234}
258    ${locks_before}=  Get Locks List  ${SESSION_ID}
259
260    ${transaction_id2}=  Evaluate  ${transaction_id1} + 1
261    ${transaction_id3}=  Evaluate  ${transaction_id1} - 1
262    ${transaction_ids}=  Create List  ${transaction_id2}  ${transaction_id1}  ${transaction_id3}
263
264    # If any transaction/s in the list does not belong to current session then it will be a bad request.
265    Release Locks  ${transaction_ids}  exp_status_code=${HTTP_BAD_REQUEST}
266    ${locks_after}=  Get Locks List  ${SESSION_ID}
267    Should Be Equal  ${locks_before}  ${locks_after}
268
269
270Verify Locks Release By Session
271    [Documentation]  Verify locks release by session.
272    [Tags]  Verify_Locks_Release_By_Session
273
274    ${locks_before}=  Get Locks List  ${SESSION_ID}
275    ${transaction_id1}=  Acquire Lock On A Given Resource  Write  ${TWO_SEG_FLAG_2}  ${234}
276
277    # Release Lock by Session without mentioning transaction_ids.
278    Release Locks  release_type=Session
279    ${locks_after}=  Get Locks List  ${SESSION_ID}
280    Should Be Equal  ${locks_before}  ${locks_after}
281
282    ${transaction_id1}=  Acquire Lock On A Given Resource  Read  ${TWO_SEG_FLAG_2}  ${234}
283    ${transaction_id2}=  Acquire Lock On A Given Resource  Read  ${TWO_SEG_FLAG_3}  ${234}
284    ${transaction_ids}=  Create List  ${transaction_id1}  ${transaction_id2}
285
286    # Release Lock by Session by mentioning transaction_ids also in the request.
287    Release Locks  ${transaction_ids}  release_type=Session
288
289
290Verify Locks Created By One Session Cannot Be Deleted By Another Session
291    [Documentation]  Verify locks created by one session cannot be deleted by another session.
292    [Tags]  Verify_Locks_Created_By_One_Session_Cannot_Be_Deleted_By_Another_Session
293    [Setup]  No Operation
294
295    ${resp}=  Redfish Login  kwargs= "Oem":{"OpenBMC" : {"ClientID":"hmc-id"}}
296    Set Test Variable  ${SESSION_ID}  ${resp['Id']}
297    ${transaction_id1}=  Acquire Lock On A Given Resource  Read  ${TWO_SEG_FLAG_2}  ${234}
298    ${locks_tran1}=  Get Locks List  ${resp['Id']}
299
300    ${resp}=  Redfish Login  kwargs= "Oem":{"OpenBMC" : {"ClientID":"hmc-id"}}
301    Set Test Variable  ${SESSION_ID}  ${resp['Id']}
302
303    ${transaction_id2}=  Acquire Lock On A Given Resource  Read  ${TWO_SEG_FLAG_3}  ${234}
304    ${locks_before}=  Get Locks List  ${resp['Id']}
305
306    ${transaction_ids}=  Create List  ${transaction_id1}  ${transaction_id2}
307    Release Locks  ${transaction_ids}  exp_status_code=${HTTP_UNAUTHORIZED}  conflict_record=${locks_tran1}
308    ${locks_after}=  Get Locks List  ${SESSION_ID}
309    Should Be Equal  ${locks_before}  ${locks_after}
310
311    # When release_type=Session, transaction_ids should be ignored.
312    Release Locks  ${transaction_ids}  release_type=Session
313    ${locks_after}=  Get Locks List  ${SESSION_ID}
314    Should Be Equal  ${EMPTY_LIST}  ${locks_after}
315
316
317*** Keywords ***
318
319Locks Persistency Check After BMC Reboot
320    [Documentation]  Locks persistency check after BMC reboot.
321    [Arguments]  ${lock_type}  ${seg_flags}  ${resource_id}
322
323    # Description of argument(s):
324    # lock_type    Type of lock (Read/Write).
325    # seg_flags    Segmentation Flags to identify lock elements under system level in the hierarchy.
326    # resource_id  Decimal +ve integer value of maximum 8 hex bytes.  Ex: 134, 2048 etc.
327
328    ${transaction_id}=  Acquire Lock On A Given Resource  ${lock_type}  ${seg_flags}  ${resource_id}
329
330    ${locks_prev}=  Get Locks List  ${SESSION_ID}
331
332    Redfish OBMC Reboot (off)
333    Redfish Login
334
335    ${locks_curr}=  Get Locks List  ${SESSION_ID}
336    Should Be Equal  ${locks_prev}  ${locks_curr}
337
338    ${transaction_ids}=  Create List  ${transaction_id}
339    Release Locks  ${transaction_ids}
340
341
342Return Data Dictionary For Single Request
343    [Documentation]  Return data dictionary for single request.
344    [Arguments]  ${lock_type}  ${seg_flags}  ${resource_id}
345
346    # Description of argument(s):
347    # lock_type    Type of lock (Read/Write).
348    # seg_flags    Segmentation Flags to identify lock elements under system level in the hierarchy.
349    # resource_id  Decimal +ve integer value of maximum 8 hex bytes.  Ex: 134, 2048 etc.
350
351    ${SEG_FLAGS_LOCK}=  Create Dictionary
352    ...  LockType=${lock_type}
353    ...  SegmentFlags=@{seg_flags}
354    ...  ResourceID=${resource_id}
355
356    ${SEG_FLAGS_ENTRIES}=  Create List  ${SEG_FLAGS_LOCK}
357    ${LOCK_REQUEST}=  Create Dictionary  Request=${SEG_FLAGS_ENTRIES}
358    Log To Console  \n${SPACE}${LOCK_REQUEST}
359
360    [Return]  ${LOCK_REQUEST}
361
362
363Acquire Lock On A Given Resource
364    [Documentation]  Acquire lock on a given resource.
365    [Arguments]  ${lock_type}  ${seg_flags}  ${resource_id}  ${exp_status_code}=${HTTP_OK}
366    ...  ${err_msgs}=${EMPTY_LIST}
367
368    # Description of argument(s):
369    # lock_type        Type of lock (Read/Write).
370    # seg_flags        Segmentation Flags to identify lock elements under system level in the hierarchy.
371    #                  Ex:  [{'LockFlag': 'LockAll', 'SegmentLength': 1},
372    #                        {'LockFlag': 'LockSame', 'SegmentLength': 2}]
373    # resource_id      Decimal +ve integer value of maximum 8 hex bytes.  Ex: 134, 2048 etc.
374    # exp_status_code  Expected status code from the AcquireLock request for given inputs.
375    # err_msgs         List of expected error messages.
376
377    ${data}=  Return Data Dictionary For Single Request  ${lock_type}  ${seg_flags}  ${resource_id}
378    ${resp}=  Redfish Post Request  /ibm/v1/HMC/LockService/Actions/LockService.AcquireLock  data=${data}
379
380    ${transaction_id}=  Run Keyword If  ${exp_status_code} != ${HTTP_OK}
381    ...      Set Variable  ${0}
382    ...  ELSE
383    ...     Load Lock Record And Build Transaction To Session Map  ${resp.text}
384
385    Run Keyword If  ${exp_status_code} == ${HTTP_CONFLICT} and ${err_msgs} == ['NA']
386    ...      Load Response And Verify Conflict  ${resp.text}  ${SESSION_ID}
387    ...  ELSE  Run Keyword If  ${exp_status_code} != ${HTTP_OK} and ${err_msgs} != ${EMPTY_LIST}
388    ...     Load Response And Verify Error  ${resp.text}  err_msgs=${err_msgs}
389
390    Append Transaction Id And Session Id To Locks Dictionary  ${transaction_id}
391
392    [Return]  ${transaction_id}
393
394
395Load Lock Record And Build Transaction To Session Map
396    [Documentation]  Load lock record and build transaction to session map.
397    [Arguments]  ${resp_text}
398
399    # Description of argument(s):
400    # resp_text  Response test from a REST request.
401
402    ${acquire_lock}=  Evaluate  json.loads('''${resp_text}''')  json
403    Append Transaction Id And Session Id To Locks Dictionary  ${acquire_lock["TransactionID"]}
404
405    [Return]  ${acquire_lock["TransactionID"]}
406
407
408Load Response And Verify Error
409    [Documentation]  Load response and verify error.
410    [Arguments]  ${error_resp}  ${err_msgs}=${EMPTY_LIST}
411
412    # Description of argument(s):
413    # error_resp  Error response from a REST request.
414    # err_msgs    List of error msg patterns.
415
416    ${error_resp}=  Replace String  ${error_resp}  \"  \\"
417    ${error_response}=  Evaluate  json.loads('''${error_resp}''')  json
418
419    ${errors}=  Get Dictionary Values  ${error_response}
420    ${extended_errors}=  Create List
421
422    FOR  ${error}  IN  @{errors}
423      Append To List  ${extended_errors}  ${error[0]["Message"]}
424    END
425
426    Log To Console  \n ${extended_errors}
427
428    FOR  ${exp_error}  IN  @{err_msgs}
429        Run Keyword  Expect List Of Errors In An Extended Errors  ${exp_error}  ${extended_errors}
430    END
431
432
433Expect List Of Errors In An Extended Errors
434    [Documentation]  Expect list of errors in an extended errors.
435    [Arguments]  ${exp_error}  ${extended_errors}=${EMPTY_LIST}
436
437    ${found}=  Set Variable  ${False}
438
439    FOR  ${error_record}  IN  @{extended_errors}
440      ${found}=  Evaluate  '${exp_error}' in '${error_record}'
441      Exit For Loop If  ${found} == ${True}
442    END
443
444    Should Be True  ${found}
445
446
447Append Transaction Id And Session Id To Locks Dictionary
448    [Documentation]  Append transaction id and session id to locks dictionary.
449    [Arguments]  ${transaction_id}
450
451    # Description of argument(s):
452    # transaction_id  Transaction ID created from acquire lock request. Ex: 8, 9 etc.
453
454    Set To Dictionary  ${LOCKS}  ${${transaction_id}}  ${session_id}
455
456
457Get Locks List
458    [Documentation]  Get locks list.
459    [Arguments]  @{sessions}  ${exp_status_code}=${HTTP_OK}
460
461    # Description of argument(s):
462    # sessions         List of comma separated strings. Ex: ["euHoAQpvNe", "ecTjANqwFr"]
463    # exp_status_code  expected status code from the GetLockList request for given inputs.
464
465    ${sessions}=  Evaluate  json.dumps(${sessions})  json
466    ${data}=  Set Variable  {"SessionIDs": ${sessions}}
467    ${resp}=  Redfish Post Request  /ibm/v1/HMC/LockService/Actions/LockService.GetLockList
468    ...  data=${data}
469    ${locks}=  Evaluate  json.loads('''${resp.text}''')  json
470
471    [Return]  ${locks["Records"]}
472
473
474Release Locks
475    [Documentation]  Release locks.
476    [Arguments]  ${transaction_ids}=${EMPTY_LIST}  ${release_type}=Transaction  ${exp_status_code}=${HTTP_OK}
477    ...  ${conflict_record}=${EMPTY_LIST}
478
479    # Description of argument(s):
480    # transaction_ids  List of transaction ids or session ids. Ex: [15, 18]  or ["euHoAQpvNe", "ecTjANqwFr"]
481    # release_type     Release all locks acquired using current session or only given transaction numbers.
482    #                  Ex:  Session,  Transaction.  Default will be Transaction.
483    # exp_status_code  expected status code from the ReleaseLock request for given inputs.
484    # conflict_record  Expected conflict record.
485
486    # When release_type=Session then TransactionIDs list will be ignored.
487    ${data}=  Set Variable  {"Type": "${release_type}", "TransactionIDs": ${transaction_ids}}
488    ${data}=  Evaluate  json.dumps(${data})  json
489    ${resp}=  Redfish Post Request  /ibm/v1/HMC/LockService/Actions/LockService.ReleaseLock  data=${data}
490    Should Be True  ${resp.status_code}  ${exp_status_code}
491    Return From Keyword If  ${conflict_record} == ${EMPTY_LIST}
492
493    ${conflict}=  Evaluate  json.loads('''${resp.text}''')  json
494
495    # Example of conflict
496    # {
497    #    "Record":{
498    #       "HMCID":"hmc-id",
499    #       "LockType":"Read",
500    #       "ResourceID":234,
501    #       "SegmentFlags":[
502    #          {
503    #             "LockFlag":"DontLock",
504    #             "SegmentLength":3
505    #          },
506    #          {
507    #             "LockFlag":"LockAll",
508    #             "SegmentLength":1
509    #          }
510    #       ],
511    #       "SessionID":"OorUVwrXuT",
512    #       "TransactionID":47
513    #    }
514    # }
515
516    Should Be Equal  ${conflict_record[0]}  ${conflict["Record"]}
517
518
519Verify Lock Record
520    [Documentation]  Verify lock record.
521    [Arguments]  ${lock_found}  &{lock_records}
522
523    # Description of argument(s):
524    # lock_found    True if lock record is expected to be present, else False.
525    # lock_records  A dictionary containing key value pairs of a lock record.
526
527    ${session}=  Get From Dictionary  ${LOCKS}  ${lock_records["TransactionID"]}
528    ${locks}=  Get Locks List  ${session}
529
530    ${lock_record_found}=  Set Variable  ${False}
531
532    FOR  ${record}  IN  @{locks}
533      ${record}=  Evaluate  json.dumps(${record})  json
534      ${record}=  Evaluate  json.loads('''${record}''')  json
535      ${lock_record_found}=  Set Variable If  ${record["TransactionID"]} == ${lock_records["TransactionID"]}
536      ...  ${True}  ${False}
537
538      Continue For Loop If  ${lock_record_found} == ${False}
539      Dictionaries Should Be Equal  ${record}  ${lock_records}
540      Exit For Loop
541    END
542
543    Should Be Equal  ${lock_record_found}  ${lock_found}
544
545
546Load Response And Verify Conflict
547    [Documentation]  Load response and verify conflict.
548    [Arguments]  ${conflict_resp}  ${sessions}
549
550    # Description of argument(s):
551    # conflict_resp  Conflict response from a REST request.
552    #                Example : { "Record": { "HMCID": "hmc-id", "LockType": "Write", "ResourceID": 234,
553    #                            "SegmentFlags": [ { "LockFlag": "DontLock", "SegmentLength": 3},
554    #                                              { "LockFlag": "LockAll",  "SegmentLength": 1}],
555    #                            "SessionID": "B6geYEdo6T", "TransactionID": 104 } }
556    # sessions       Comma separated list of sessions
557
558    ${curr_locks}=  Get Locks List  ${sessions}
559    ${conflict_resp}=  Replace String  ${conflict_resp}  \"  \\"
560    ${conflict_response}=  Evaluate  json.loads('''${conflict_resp}''')  json
561
562    ${conflicts}=  Get Dictionary Values  ${conflict_response}
563    List Should Contain Value  ${conflicts}  ${PREV_INPUTS}
564
565
566Acquire And Release Lock
567    [Documentation]  Acquire and release lock.
568    [Arguments]  ${lock_type}  ${seg_flags}  ${resource_id}  ${hmc_id}  ${exp_status_code}=${HTTP_OK}
569    ...  ${err_msgs}=${EMPTY_LIST}  ${new_sess_req}=${True}
570
571    # Description of argument(s):
572    # lock_type        Type of lock (Read/Write).
573    # seg_flags        Segmentation Flags to identify lock elements under system level in the hierarchy.
574    #                  Ex:  [{'LockFlag': 'LockAll', 'SegmentLength': 1},
575    #                        {'LockFlag': 'LockSame', 'SegmentLength': 2}]
576    # resource_id      Decimal +ve integer value of maximum 8 hex bytes.  Ex: 134, 2048 etc.
577    # hmc_id           Hardware management console id.
578    # exp_status_code  Expected status code from the AcquireLock request for given inputs.
579    # err_msgs         List of expected error messages.
580    # new_sess_req     Create a new session before acquiring a lock if True.
581
582    # Delete the session.
583    Run Keyword If  ${new_sess_req} == ${True}  Run Keyword And Ignore Error  Delete All Redfish Sessions
584
585    # Get REST session to BMC.
586    Run Keyword If  ${new_sess_req} == ${True}  Create New Session
587
588    ${inputs}=  Create Dictionary
589    ...  LockType=${lock_type}
590    ...  ResourceID=${resource_id}
591    ...  SegmentFlags=${seg_flags}
592    ...  HMCID=${hmc_id}
593
594    ${transaction_id}=  Acquire Lock On A Given Resource  ${inputs["LockType"]}
595    ...  ${inputs["SegmentFlags"]}  ${inputs["ResourceID"]}  ${exp_status_code}  err_msgs=${err_msgs}
596
597    # Each lock request from a new session is saved so that for next lock request using same session
598    # can refer to previous lock data to verify conflict records if any.
599    Run Keyword If  ${new_sess_req} == ${True}  Set Test Variable Dictionary Of Previous Lock Request
600    ...  ${lock_type}  ${seg_flags}  ${resource_id}  ${hmc_id}  ${SESSION_ID}  ${transaction_id}
601
602    ${session}=  Get From Dictionary  ${LOCKS}  ${transaction_id}
603    Set To Dictionary  ${inputs}  TransactionID=${${transaction_id}}  SessionID=${session}
604
605    ${lock_found}=  Set Variable If  ${exp_status_code} == ${HTTP_OK}  ${True}  ${False}
606    Verify Lock Record  ${lock_found}  &{inputs}
607
608    Return From Keyword If  '${exp_status_code}' != '${HTTP_OK}' or ${err_msgs} == ['NA']
609    ${transaction_ids}=  Create List  ${transaction_id}
610    Release Locks  ${transaction_ids}
611    Verify Lock Record  ${False}  &{inputs}
612
613
614Create New Session
615    [Documentation]  Create new session.
616
617    ${resp}=  Redfish Login  kwargs= "Oem":{"OpenBMC" : {"ClientID":"${hmc_id}"}}
618    Set Test Variable  ${SESSION_ID}  ${resp['Id']}
619
620
621Test Teardown Execution
622    [Documentation]  Test teardown execution.
623
624    FFDC On Test Case Fail
625    Run Keyword And Ignore Error  Delete All Redfish Sessions
626
627
628Return Session Id And Session Key
629    [Documentation]  Return session id and sesion key.
630
631    ${session_location}=  Redfish.Get Session Location
632    ${session_id}=  Evaluate  os.path.basename($session_location)  modules=os
633    ${session_key}=  Redfish.Get Session Key
634
635    [Return]  ${session_id}  ${session_key}
636
637
638Test Setup Execution
639    [Documentation]  Test setup execution.
640
641    # This is a test constant value.
642    Set Test Variable  ${hmc_id}  hmc-id
643    Create New Session
644
645    Set Test Variable Dictionary Of Previous Lock Request
646    ...  ${EMPTY}  ${EMPTY_LIST}  ${EMPTY}  ${EMPTY}  ${EMPTY}  ${EMPTY}
647
648
649Set Test Variable Dictionary Of Previous Lock Request
650    [Documentation]  Set test variable dictionary of previous lock request.
651    [Arguments]  ${lock_type}  ${seg_flags}  ${resource_id}  ${hmc_id}  ${session_id}  ${transaction_id}
652
653    # Description of argument(s):
654    # lock_type            Type of lock (Read/Write).
655    # seg_flags            Segmentation Flags to identify lock elements under system level in the hierarchy.
656    #                      Ex:  [{'LockFlag': 'LockAll', 'SegmentLength': 1},
657    #                           {'LockFlag': 'LockSame', 'SegmentLength': 2}]
658    # resource_id          Decimal +ve integer value of maximum 8 hex bytes.  Ex: 134, 2048 etc.
659    # hmc_id               Hardware management console id.
660    # session_id           Session id of the transaction.
661    # transaction_id       Transaction_id of the lock request.
662
663    ${prev_inputs}=  Create Dictionary
664    ...  LockType=${lock_type}
665    ...  ResourceID=${resource_id}
666    ...  SegmentFlags=${seg_flags}
667    ...  HMCID=${hmc_id}
668    ...  SessionID=${session_id}
669    ...  TransactionID=${transaction_id}
670
671    Set Test Variable  ${PREV_INPUTS}  ${prev_inputs}
672