1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 #include "test/librbd/test_mock_fixture.h"
5 #include "test/librbd/test_support.h"
6 #include "test/librbd/mock/MockImageCtx.h"
7 #include "test/librados_test_stub/MockTestMemIoCtxImpl.h"
8 #include "cls/lock/cls_lock_ops.h"
9 #include "librbd/ObjectMap.h"
10 #include "librbd/object_map/LockRequest.h"
12 // template definitions
13 #include "librbd/object_map/LockRequest.cc"
16 namespace object_map
{
19 using ::testing::DoAll
;
20 using ::testing::InSequence
;
21 using ::testing::Return
;
22 using ::testing::StrEq
;
23 using ::testing::WithArg
;
25 class TestMockObjectMapLockRequest
: public TestMockFixture
{
27 typedef LockRequest
<MockImageCtx
> MockLockRequest
;
29 void expect_lock(MockImageCtx
&mock_image_ctx
, int r
) {
30 std::string
oid(ObjectMap
<>::object_map_name(mock_image_ctx
.id
,
32 EXPECT_CALL(get_mock_io_ctx(mock_image_ctx
.md_ctx
),
33 exec(oid
, _
, StrEq("lock"), StrEq("lock"), _
, _
, _
))
37 void expect_get_lock_info(MockImageCtx
&mock_image_ctx
, int r
) {
38 std::string
oid(ObjectMap
<>::object_map_name(mock_image_ctx
.id
,
40 auto &expect
= EXPECT_CALL(get_mock_io_ctx(mock_image_ctx
.md_ctx
),
41 exec(oid
, _
, StrEq("lock"), StrEq("get_info"), _
, _
, _
));
43 expect
.WillOnce(Return(r
));
45 entity_name_t
entity1(entity_name_t::CLIENT(1));
46 entity_name_t
entity2(entity_name_t::CLIENT(2));
48 cls_lock_get_info_reply reply
;
49 reply
.lockers
= decltype(reply
.lockers
){
50 {rados::cls::lock::locker_id_t(entity1
, "cookie1"),
51 rados::cls::lock::locker_info_t()},
52 {rados::cls::lock::locker_id_t(entity2
, "cookie2"),
53 rados::cls::lock::locker_info_t()}};
56 encode(reply
, bl
, CEPH_FEATURES_SUPPORTED_DEFAULT
);
58 std::string
str(bl
.c_str(), bl
.length());
59 expect
.WillOnce(DoAll(WithArg
<5>(CopyInBufferlist(str
)), Return(r
)));
63 void expect_break_lock(MockImageCtx
&mock_image_ctx
, int r
) {
64 std::string
oid(ObjectMap
<>::object_map_name(mock_image_ctx
.id
,
66 auto &expect
= EXPECT_CALL(get_mock_io_ctx(mock_image_ctx
.md_ctx
),
67 exec(oid
, _
, StrEq("lock"), StrEq("break_lock"), _
, _
, _
));
69 expect
.WillOnce(Return(r
));
71 expect
.Times(2).WillRepeatedly(Return(0));
76 TEST_F(TestMockObjectMapLockRequest
, Success
) {
77 librbd::ImageCtx
*ictx
;
78 ASSERT_EQ(0, open_image(m_image_name
, &ictx
));
80 MockImageCtx
mock_image_ctx(*ictx
);
83 MockLockRequest
*req
= new MockLockRequest(mock_image_ctx
, &ctx
);
86 expect_lock(mock_image_ctx
, 0);
89 ASSERT_EQ(0, ctx
.wait());
92 TEST_F(TestMockObjectMapLockRequest
, LockBusy
) {
93 librbd::ImageCtx
*ictx
;
94 ASSERT_EQ(0, open_image(m_image_name
, &ictx
));
96 MockImageCtx
mock_image_ctx(*ictx
);
99 MockLockRequest
*req
= new MockLockRequest(mock_image_ctx
, &ctx
);
102 expect_lock(mock_image_ctx
, -EBUSY
);
103 expect_get_lock_info(mock_image_ctx
, 0);
104 expect_break_lock(mock_image_ctx
, 0);
105 expect_lock(mock_image_ctx
, 0);
108 ASSERT_EQ(0, ctx
.wait());
111 TEST_F(TestMockObjectMapLockRequest
, LockError
) {
112 librbd::ImageCtx
*ictx
;
113 ASSERT_EQ(0, open_image(m_image_name
, &ictx
));
115 MockImageCtx
mock_image_ctx(*ictx
);
118 MockLockRequest
*req
= new MockLockRequest(mock_image_ctx
, &ctx
);
121 expect_lock(mock_image_ctx
, -ENOENT
);
124 ASSERT_EQ(0, ctx
.wait());
127 TEST_F(TestMockObjectMapLockRequest
, GetLockInfoMissing
) {
128 librbd::ImageCtx
*ictx
;
129 ASSERT_EQ(0, open_image(m_image_name
, &ictx
));
131 MockImageCtx
mock_image_ctx(*ictx
);
134 MockLockRequest
*req
= new MockLockRequest(mock_image_ctx
, &ctx
);
137 expect_lock(mock_image_ctx
, -EBUSY
);
138 expect_get_lock_info(mock_image_ctx
, -ENOENT
);
139 expect_lock(mock_image_ctx
, 0);
142 ASSERT_EQ(0, ctx
.wait());
145 TEST_F(TestMockObjectMapLockRequest
, GetLockInfoError
) {
146 librbd::ImageCtx
*ictx
;
147 ASSERT_EQ(0, open_image(m_image_name
, &ictx
));
149 MockImageCtx
mock_image_ctx(*ictx
);
152 MockLockRequest
*req
= new MockLockRequest(mock_image_ctx
, &ctx
);
155 expect_lock(mock_image_ctx
, -EBUSY
);
156 expect_get_lock_info(mock_image_ctx
, -EINVAL
);
159 ASSERT_EQ(0, ctx
.wait());
162 TEST_F(TestMockObjectMapLockRequest
, BreakLockMissing
) {
163 librbd::ImageCtx
*ictx
;
164 ASSERT_EQ(0, open_image(m_image_name
, &ictx
));
166 MockImageCtx
mock_image_ctx(*ictx
);
169 MockLockRequest
*req
= new MockLockRequest(mock_image_ctx
, &ctx
);
172 expect_lock(mock_image_ctx
, -EBUSY
);
173 expect_get_lock_info(mock_image_ctx
, 0);
174 expect_break_lock(mock_image_ctx
, -ENOENT
);
175 expect_lock(mock_image_ctx
, 0);
178 ASSERT_EQ(0, ctx
.wait());
181 TEST_F(TestMockObjectMapLockRequest
, BreakLockError
) {
182 librbd::ImageCtx
*ictx
;
183 ASSERT_EQ(0, open_image(m_image_name
, &ictx
));
185 MockImageCtx
mock_image_ctx(*ictx
);
188 MockLockRequest
*req
= new MockLockRequest(mock_image_ctx
, &ctx
);
191 expect_lock(mock_image_ctx
, -EBUSY
);
192 expect_get_lock_info(mock_image_ctx
, 0);
193 expect_break_lock(mock_image_ctx
, -EINVAL
);
196 ASSERT_EQ(0, ctx
.wait());
199 TEST_F(TestMockObjectMapLockRequest
, LockErrorAfterBrokeLock
) {
200 librbd::ImageCtx
*ictx
;
201 ASSERT_EQ(0, open_image(m_image_name
, &ictx
));
203 MockImageCtx
mock_image_ctx(*ictx
);
206 MockLockRequest
*req
= new MockLockRequest(mock_image_ctx
, &ctx
);
209 expect_lock(mock_image_ctx
, -EBUSY
);
210 expect_get_lock_info(mock_image_ctx
, 0);
211 expect_break_lock(mock_image_ctx
, 0);
212 expect_lock(mock_image_ctx
, -EBUSY
);
215 ASSERT_EQ(0, ctx
.wait());
218 } // namespace object_map
219 } // namespace librbd