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 "test/librados_test_stub/MockTestMemRadosClient.h"
9 #include "cls/lock/cls_lock_ops.h"
10 #include "librbd/managed_lock/GetLockerRequest.h"
11 #include "librbd/managed_lock/Types.h"
12 #include "librbd/managed_lock/Utils.h"
13 #include "gmock/gmock.h"
14 #include "gtest/gtest.h"
15 #include <arpa/inet.h>
21 struct MockTestImageCtx
: public librbd::MockImageCtx
{
22 MockTestImageCtx(librbd::ImageCtx
&image_ctx
)
23 : librbd::MockImageCtx(image_ctx
) {
27 } // anonymous namespace
30 // template definitions
31 #include "librbd/managed_lock/GetLockerRequest.cc"
34 namespace managed_lock
{
37 using ::testing::DoAll
;
38 using ::testing::InSequence
;
39 using ::testing::Return
;
40 using ::testing::StrEq
;
41 using ::testing::WithArg
;
43 class TestMockManagedLockGetLockerRequest
: public TestMockFixture
{
45 typedef GetLockerRequest
<MockTestImageCtx
> MockGetLockerRequest
;
47 void expect_get_lock_info(MockTestImageCtx
&mock_image_ctx
, int r
,
48 const entity_name_t
&locker_entity
,
49 const std::string
&locker_address
,
50 const std::string
&locker_cookie
,
51 const std::string
&lock_tag
,
52 ClsLockType lock_type
) {
53 auto &expect
= EXPECT_CALL(get_mock_io_ctx(mock_image_ctx
.md_ctx
),
54 exec(mock_image_ctx
.header_oid
, _
, StrEq("lock"),
55 StrEq("get_info"), _
, _
, _
));
56 if (r
< 0 && r
!= -ENOENT
) {
57 expect
.WillOnce(Return(r
));
59 entity_name_t
entity(locker_entity
);
60 entity_addr_t entity_addr
;
61 entity_addr
.parse(locker_address
.c_str(), NULL
);
63 cls_lock_get_info_reply reply
;
65 reply
.lockers
.emplace(
66 rados::cls::lock::locker_id_t(entity
, locker_cookie
),
67 rados::cls::lock::locker_info_t(utime_t(), entity_addr
, ""));
69 reply
.lock_type
= lock_type
;
73 encode(reply
, bl
, CEPH_FEATURES_SUPPORTED_DEFAULT
);
75 std::string
str(bl
.c_str(), bl
.length());
76 expect
.WillOnce(DoAll(WithArg
<5>(CopyInBufferlist(str
)), Return(0)));
81 TEST_F(TestMockManagedLockGetLockerRequest
, SuccessExclusive
) {
82 REQUIRE_FEATURE(RBD_FEATURE_EXCLUSIVE_LOCK
);
84 librbd::ImageCtx
*ictx
;
85 ASSERT_EQ(0, open_image(m_image_name
, &ictx
));
87 MockTestImageCtx
mock_image_ctx(*ictx
);
88 expect_op_work_queue(mock_image_ctx
);
91 expect_get_lock_info(mock_image_ctx
, 0, entity_name_t::CLIENT(1), "1.2.3.4",
92 "auto 123", util::get_watcher_lock_tag(),
97 MockGetLockerRequest
*req
= MockGetLockerRequest::create(
98 mock_image_ctx
.md_ctx
, mock_image_ctx
.header_oid
, true, &locker
, &ctx
);
100 ASSERT_EQ(0, ctx
.wait());
102 ASSERT_EQ(entity_name_t::CLIENT(1), locker
.entity
);
103 ASSERT_EQ("1.2.3.4:0/0", locker
.address
);
104 ASSERT_EQ("auto 123", locker
.cookie
);
105 ASSERT_EQ(123U, locker
.handle
);
108 TEST_F(TestMockManagedLockGetLockerRequest
, SuccessShared
) {
109 REQUIRE_FEATURE(RBD_FEATURE_EXCLUSIVE_LOCK
);
111 librbd::ImageCtx
*ictx
;
112 ASSERT_EQ(0, open_image(m_image_name
, &ictx
));
114 MockTestImageCtx
mock_image_ctx(*ictx
);
115 expect_op_work_queue(mock_image_ctx
);
118 expect_get_lock_info(mock_image_ctx
, 0, entity_name_t::CLIENT(1), "1.2.3.4",
119 "auto 123", util::get_watcher_lock_tag(),
124 MockGetLockerRequest
*req
= MockGetLockerRequest::create(
125 mock_image_ctx
.md_ctx
, mock_image_ctx
.header_oid
, false, &locker
, &ctx
);
127 ASSERT_EQ(0, ctx
.wait());
129 ASSERT_EQ(entity_name_t::CLIENT(1), locker
.entity
);
130 ASSERT_EQ("1.2.3.4:0/0", locker
.address
);
131 ASSERT_EQ("auto 123", locker
.cookie
);
132 ASSERT_EQ(123U, locker
.handle
);
135 TEST_F(TestMockManagedLockGetLockerRequest
, GetLockInfoError
) {
136 REQUIRE_FEATURE(RBD_FEATURE_EXCLUSIVE_LOCK
);
138 librbd::ImageCtx
*ictx
;
139 ASSERT_EQ(0, open_image(m_image_name
, &ictx
));
141 MockTestImageCtx
mock_image_ctx(*ictx
);
142 expect_op_work_queue(mock_image_ctx
);
145 expect_get_lock_info(mock_image_ctx
, -EINVAL
, entity_name_t::CLIENT(1), "",
146 "", "", LOCK_EXCLUSIVE
);
150 MockGetLockerRequest
*req
= MockGetLockerRequest::create(
151 mock_image_ctx
.md_ctx
, mock_image_ctx
.header_oid
, true, &locker
, &ctx
);
153 ASSERT_EQ(-EINVAL
, ctx
.wait());
156 TEST_F(TestMockManagedLockGetLockerRequest
, GetLockInfoEmpty
) {
157 REQUIRE_FEATURE(RBD_FEATURE_EXCLUSIVE_LOCK
);
159 librbd::ImageCtx
*ictx
;
160 ASSERT_EQ(0, open_image(m_image_name
, &ictx
));
162 MockTestImageCtx
mock_image_ctx(*ictx
);
163 expect_op_work_queue(mock_image_ctx
);
166 expect_get_lock_info(mock_image_ctx
, -ENOENT
, entity_name_t::CLIENT(1), "",
167 "", "", LOCK_EXCLUSIVE
);
171 MockGetLockerRequest
*req
= MockGetLockerRequest::create(
172 mock_image_ctx
.md_ctx
, mock_image_ctx
.header_oid
, true, &locker
, &ctx
);
174 ASSERT_EQ(-ENOENT
, ctx
.wait());
177 TEST_F(TestMockManagedLockGetLockerRequest
, GetLockInfoExternalTag
) {
178 REQUIRE_FEATURE(RBD_FEATURE_EXCLUSIVE_LOCK
);
180 librbd::ImageCtx
*ictx
;
181 ASSERT_EQ(0, open_image(m_image_name
, &ictx
));
183 MockTestImageCtx
mock_image_ctx(*ictx
);
184 expect_op_work_queue(mock_image_ctx
);
187 expect_get_lock_info(mock_image_ctx
, 0, entity_name_t::CLIENT(1), "1.2.3.4",
188 "auto 123", "external tag", LOCK_EXCLUSIVE
);
192 MockGetLockerRequest
*req
= MockGetLockerRequest::create(
193 mock_image_ctx
.md_ctx
, mock_image_ctx
.header_oid
, true, &locker
, &ctx
);
195 ASSERT_EQ(-EBUSY
, ctx
.wait());
198 TEST_F(TestMockManagedLockGetLockerRequest
, GetLockInfoIncompatibleShared
) {
199 REQUIRE_FEATURE(RBD_FEATURE_EXCLUSIVE_LOCK
);
201 librbd::ImageCtx
*ictx
;
202 ASSERT_EQ(0, open_image(m_image_name
, &ictx
));
204 MockTestImageCtx
mock_image_ctx(*ictx
);
205 expect_op_work_queue(mock_image_ctx
);
208 expect_get_lock_info(mock_image_ctx
, 0, entity_name_t::CLIENT(1), "1.2.3.4",
209 "auto 123", util::get_watcher_lock_tag(),
214 MockGetLockerRequest
*req
= MockGetLockerRequest::create(
215 mock_image_ctx
.md_ctx
, mock_image_ctx
.header_oid
, true, &locker
, &ctx
);
217 ASSERT_EQ(-EBUSY
, ctx
.wait());
220 TEST_F(TestMockManagedLockGetLockerRequest
, GetLockInfoIncompatibleExclusive
) {
221 REQUIRE_FEATURE(RBD_FEATURE_EXCLUSIVE_LOCK
);
223 librbd::ImageCtx
*ictx
;
224 ASSERT_EQ(0, open_image(m_image_name
, &ictx
));
226 MockTestImageCtx
mock_image_ctx(*ictx
);
227 expect_op_work_queue(mock_image_ctx
);
230 expect_get_lock_info(mock_image_ctx
, 0, entity_name_t::CLIENT(1), "1.2.3.4",
231 "auto 123", util::get_watcher_lock_tag(),
236 MockGetLockerRequest
*req
= MockGetLockerRequest::create(
237 mock_image_ctx
.md_ctx
, mock_image_ctx
.header_oid
, false, &locker
, &ctx
);
239 ASSERT_EQ(-EBUSY
, ctx
.wait());
242 TEST_F(TestMockManagedLockGetLockerRequest
, GetLockInfoExternalCookie
) {
243 REQUIRE_FEATURE(RBD_FEATURE_EXCLUSIVE_LOCK
);
245 librbd::ImageCtx
*ictx
;
246 ASSERT_EQ(0, open_image(m_image_name
, &ictx
));
248 MockTestImageCtx
mock_image_ctx(*ictx
);
249 expect_op_work_queue(mock_image_ctx
);
252 expect_get_lock_info(mock_image_ctx
, 0, entity_name_t::CLIENT(1), "1.2.3.4",
253 "external cookie", util::get_watcher_lock_tag(),
258 MockGetLockerRequest
*req
= MockGetLockerRequest::create(
259 mock_image_ctx
.md_ctx
, mock_image_ctx
.header_oid
, true, &locker
, &ctx
);
261 ASSERT_EQ(-EBUSY
, ctx
.wait());
264 } // namespace managed_lock
265 } // namespace librbd