Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / test / librbd / managed_lock / test_mock_GetLockerRequest.cc
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3
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>
16 #include <list>
17
18 namespace librbd {
19 namespace {
20
21 struct MockTestImageCtx : public librbd::MockImageCtx {
22   MockTestImageCtx(librbd::ImageCtx &image_ctx)
23     : librbd::MockImageCtx(image_ctx) {
24   }
25 };
26
27 } // anonymous namespace
28 } // namespace librbd
29
30 // template definitions
31 #include "librbd/managed_lock/GetLockerRequest.cc"
32
33 namespace librbd {
34 namespace managed_lock {
35
36 using ::testing::_;
37 using ::testing::DoAll;
38 using ::testing::InSequence;
39 using ::testing::Return;
40 using ::testing::StrEq;
41 using ::testing::WithArg;
42
43 class TestMockManagedLockGetLockerRequest : public TestMockFixture {
44 public:
45   typedef GetLockerRequest<MockTestImageCtx> MockGetLockerRequest;
46
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));
58     } else {
59       entity_name_t entity(locker_entity);
60       entity_addr_t entity_addr;
61       entity_addr.parse(locker_address.c_str(), NULL);
62
63       cls_lock_get_info_reply reply;
64       if (r != -ENOENT) {
65         reply.lockers = decltype(reply.lockers){
66           {rados::cls::lock::locker_id_t(entity, locker_cookie),
67            rados::cls::lock::locker_info_t(utime_t(), entity_addr, "")}};
68         reply.tag = lock_tag;
69         reply.lock_type = lock_type;
70       }
71
72       bufferlist bl;
73       ::encode(reply, bl, CEPH_FEATURES_SUPPORTED_DEFAULT);
74
75       std::string str(bl.c_str(), bl.length());
76       expect.WillOnce(DoAll(WithArg<5>(CopyInBufferlist(str)), Return(0)));
77     }
78   }
79 };
80
81 TEST_F(TestMockManagedLockGetLockerRequest, SuccessExclusive) {
82   REQUIRE_FEATURE(RBD_FEATURE_EXCLUSIVE_LOCK);
83
84   librbd::ImageCtx *ictx;
85   ASSERT_EQ(0, open_image(m_image_name, &ictx));
86
87   MockTestImageCtx mock_image_ctx(*ictx);
88   expect_op_work_queue(mock_image_ctx);
89
90   InSequence seq;
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(),
93                        LOCK_EXCLUSIVE);
94
95   C_SaferCond ctx;
96   Locker locker;
97   MockGetLockerRequest *req = MockGetLockerRequest::create(
98     mock_image_ctx.md_ctx, mock_image_ctx.header_oid, true, &locker, &ctx);
99   req->send();
100   ASSERT_EQ(0, ctx.wait());
101
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);
106 }
107
108 TEST_F(TestMockManagedLockGetLockerRequest, SuccessShared) {
109   REQUIRE_FEATURE(RBD_FEATURE_EXCLUSIVE_LOCK);
110
111   librbd::ImageCtx *ictx;
112   ASSERT_EQ(0, open_image(m_image_name, &ictx));
113
114   MockTestImageCtx mock_image_ctx(*ictx);
115   expect_op_work_queue(mock_image_ctx);
116
117   InSequence seq;
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(),
120                        LOCK_SHARED);
121
122   C_SaferCond ctx;
123   Locker locker;
124   MockGetLockerRequest *req = MockGetLockerRequest::create(
125     mock_image_ctx.md_ctx, mock_image_ctx.header_oid, false, &locker, &ctx);
126   req->send();
127   ASSERT_EQ(0, ctx.wait());
128
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);
133 }
134
135 TEST_F(TestMockManagedLockGetLockerRequest, GetLockInfoError) {
136   REQUIRE_FEATURE(RBD_FEATURE_EXCLUSIVE_LOCK);
137
138   librbd::ImageCtx *ictx;
139   ASSERT_EQ(0, open_image(m_image_name, &ictx));
140
141   MockTestImageCtx mock_image_ctx(*ictx);
142   expect_op_work_queue(mock_image_ctx);
143
144   InSequence seq;
145   expect_get_lock_info(mock_image_ctx, -EINVAL, entity_name_t::CLIENT(1), "",
146                        "", "", LOCK_EXCLUSIVE);
147
148   C_SaferCond ctx;
149   Locker locker;
150   MockGetLockerRequest *req = MockGetLockerRequest::create(
151     mock_image_ctx.md_ctx, mock_image_ctx.header_oid, true, &locker, &ctx);
152   req->send();
153   ASSERT_EQ(-EINVAL, ctx.wait());
154 }
155
156 TEST_F(TestMockManagedLockGetLockerRequest, GetLockInfoEmpty) {
157   REQUIRE_FEATURE(RBD_FEATURE_EXCLUSIVE_LOCK);
158
159   librbd::ImageCtx *ictx;
160   ASSERT_EQ(0, open_image(m_image_name, &ictx));
161
162   MockTestImageCtx mock_image_ctx(*ictx);
163   expect_op_work_queue(mock_image_ctx);
164
165   InSequence seq;
166   expect_get_lock_info(mock_image_ctx, -ENOENT, entity_name_t::CLIENT(1), "",
167                        "", "", LOCK_EXCLUSIVE);
168
169   C_SaferCond ctx;
170   Locker locker;
171   MockGetLockerRequest *req = MockGetLockerRequest::create(
172     mock_image_ctx.md_ctx, mock_image_ctx.header_oid, true, &locker, &ctx);
173   req->send();
174   ASSERT_EQ(-ENOENT, ctx.wait());
175 }
176
177 TEST_F(TestMockManagedLockGetLockerRequest, GetLockInfoExternalTag) {
178   REQUIRE_FEATURE(RBD_FEATURE_EXCLUSIVE_LOCK);
179
180   librbd::ImageCtx *ictx;
181   ASSERT_EQ(0, open_image(m_image_name, &ictx));
182
183   MockTestImageCtx mock_image_ctx(*ictx);
184   expect_op_work_queue(mock_image_ctx);
185
186   InSequence seq;
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);
189
190   C_SaferCond ctx;
191   Locker locker;
192   MockGetLockerRequest *req = MockGetLockerRequest::create(
193     mock_image_ctx.md_ctx, mock_image_ctx.header_oid, true, &locker, &ctx);
194   req->send();
195   ASSERT_EQ(-EBUSY, ctx.wait());
196 }
197
198 TEST_F(TestMockManagedLockGetLockerRequest, GetLockInfoIncompatibleShared) {
199   REQUIRE_FEATURE(RBD_FEATURE_EXCLUSIVE_LOCK);
200
201   librbd::ImageCtx *ictx;
202   ASSERT_EQ(0, open_image(m_image_name, &ictx));
203
204   MockTestImageCtx mock_image_ctx(*ictx);
205   expect_op_work_queue(mock_image_ctx);
206
207   InSequence seq;
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(),
210                        LOCK_SHARED);
211
212   C_SaferCond ctx;
213   Locker locker;
214   MockGetLockerRequest *req = MockGetLockerRequest::create(
215     mock_image_ctx.md_ctx, mock_image_ctx.header_oid, true, &locker, &ctx);
216   req->send();
217   ASSERT_EQ(-EBUSY, ctx.wait());
218 }
219
220 TEST_F(TestMockManagedLockGetLockerRequest, GetLockInfoIncompatibleExclusive) {
221   REQUIRE_FEATURE(RBD_FEATURE_EXCLUSIVE_LOCK);
222
223   librbd::ImageCtx *ictx;
224   ASSERT_EQ(0, open_image(m_image_name, &ictx));
225
226   MockTestImageCtx mock_image_ctx(*ictx);
227   expect_op_work_queue(mock_image_ctx);
228
229   InSequence seq;
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(),
232                        LOCK_EXCLUSIVE);
233
234   C_SaferCond ctx;
235   Locker locker;
236   MockGetLockerRequest *req = MockGetLockerRequest::create(
237     mock_image_ctx.md_ctx, mock_image_ctx.header_oid, false, &locker, &ctx);
238   req->send();
239   ASSERT_EQ(-EBUSY, ctx.wait());
240 }
241
242 TEST_F(TestMockManagedLockGetLockerRequest, GetLockInfoExternalCookie) {
243   REQUIRE_FEATURE(RBD_FEATURE_EXCLUSIVE_LOCK);
244
245   librbd::ImageCtx *ictx;
246   ASSERT_EQ(0, open_image(m_image_name, &ictx));
247
248   MockTestImageCtx mock_image_ctx(*ictx);
249   expect_op_work_queue(mock_image_ctx);
250
251   InSequence seq;
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(),
254                        LOCK_EXCLUSIVE);
255
256   C_SaferCond ctx;
257   Locker locker;
258   MockGetLockerRequest *req = MockGetLockerRequest::create(
259     mock_image_ctx.md_ctx, mock_image_ctx.header_oid, true, &locker, &ctx);
260   req->send();
261   ASSERT_EQ(-EBUSY, ctx.wait());
262 }
263
264 } // namespace managed_lock
265 } // namespace librbd