summaryrefslogtreecommitdiffstats
path: root/src/test/librbd/managed_lock/test_mock_GetLockerRequest.cc
blob: 8cad1f09258c55422b78c690ca1d1bad02ad3692 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab

#include "test/librbd/test_mock_fixture.h"
#include "test/librbd/test_support.h"
#include "test/librbd/mock/MockImageCtx.h"
#include "test/librados_test_stub/MockTestMemIoCtxImpl.h"
#include "test/librados_test_stub/MockTestMemRadosClient.h"
#include "cls/lock/cls_lock_ops.h"
#include "librbd/managed_lock/GetLockerRequest.h"
#include "librbd/managed_lock/Types.h"
#include "librbd/managed_lock/Utils.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include <arpa/inet.h>
#include <list>

namespace librbd {
namespace {

struct MockTestImageCtx : public librbd::MockImageCtx {
  MockTestImageCtx(librbd::ImageCtx &image_ctx)
    : librbd::MockImageCtx(image_ctx) {
  }
};

} // anonymous namespace
} // namespace librbd

// template definitions
#include "librbd/managed_lock/GetLockerRequest.cc"

namespace librbd {
namespace managed_lock {

using ::testing::_;
using ::testing::DoAll;
using ::testing::InSequence;
using ::testing::Return;
using ::testing::StrEq;
using ::testing::WithArg;

class TestMockManagedLockGetLockerRequest : public TestMockFixture {
public:
  typedef GetLockerRequest<MockTestImageCtx> MockGetLockerRequest;

  void expect_get_lock_info(MockTestImageCtx &mock_image_ctx, int r,
                            const entity_name_t &locker_entity,
                            const std::string &locker_address,
                            const std::string &locker_cookie,
                            const std::string &lock_tag,
                            ClsLockType lock_type) {
    auto &expect = EXPECT_CALL(get_mock_io_ctx(mock_image_ctx.md_ctx),
                               exec(mock_image_ctx.header_oid, _, StrEq("lock"),
                               StrEq("get_info"), _, _, _, _));
    if (r < 0 && r != -ENOENT) {
      expect.WillOnce(Return(r));
    } else {
      entity_name_t entity(locker_entity);
      entity_addr_t entity_addr;
      entity_addr.parse(locker_address.c_str(), NULL);

      cls_lock_get_info_reply reply;
      if (r != -ENOENT) {
        reply.lockers.emplace(
          rados::cls::lock::locker_id_t(entity, locker_cookie),
          rados::cls::lock::locker_info_t(utime_t(), entity_addr, ""));
        reply.tag = lock_tag;
        reply.lock_type = lock_type;
      }

      bufferlist bl;
      encode(reply, bl, CEPH_FEATURES_SUPPORTED_DEFAULT);

      std::string str(bl.c_str(), bl.length());
      expect.WillOnce(DoAll(WithArg<5>(CopyInBufferlist(str)), Return(0)));
    }
  }
};

TEST_F(TestMockManagedLockGetLockerRequest, SuccessExclusive) {
  REQUIRE_FEATURE(RBD_FEATURE_EXCLUSIVE_LOCK);

  librbd::ImageCtx *ictx;
  ASSERT_EQ(0, open_image(m_image_name, &ictx));

  MockTestImageCtx mock_image_ctx(*ictx);
  expect_op_work_queue(mock_image_ctx);

  InSequence seq;
  expect_get_lock_info(mock_image_ctx, 0, entity_name_t::CLIENT(1), "1.2.3.4",
                       "auto 123", util::get_watcher_lock_tag(),
                       ClsLockType::EXCLUSIVE);

  C_SaferCond ctx;
  Locker locker;
  MockGetLockerRequest *req = MockGetLockerRequest::create(
    mock_image_ctx.md_ctx, mock_image_ctx.header_oid, true, &locker, &ctx);
  req->send();
  ASSERT_EQ(0, ctx.wait());

  ASSERT_EQ(entity_name_t::CLIENT(1), locker.entity);
  ASSERT_EQ("1.2.3.4:0/0", locker.address);
  ASSERT_EQ("auto 123", locker.cookie);
  ASSERT_EQ(123U, locker.handle);
}

TEST_F(TestMockManagedLockGetLockerRequest, SuccessShared) {
  REQUIRE_FEATURE(RBD_FEATURE_EXCLUSIVE_LOCK);

  librbd::ImageCtx *ictx;
  ASSERT_EQ(0, open_image(m_image_name, &ictx));

  MockTestImageCtx mock_image_ctx(*ictx);
  expect_op_work_queue(mock_image_ctx);

  InSequence seq;
  expect_get_lock_info(mock_image_ctx, 0, entity_name_t::CLIENT(1), "1.2.3.4",
                       "auto 123", util::get_watcher_lock_tag(),
                       ClsLockType::SHARED);

  C_SaferCond ctx;
  Locker locker;
  MockGetLockerRequest *req = MockGetLockerRequest::create(
    mock_image_ctx.md_ctx, mock_image_ctx.header_oid, false, &locker, &ctx);
  req->send();
  ASSERT_EQ(0, ctx.wait());

  ASSERT_EQ(entity_name_t::CLIENT(1), locker.entity);
  ASSERT_EQ("1.2.3.4:0/0", locker.address);
  ASSERT_EQ("auto 123", locker.cookie);
  ASSERT_EQ(123U, locker.handle);
}

TEST_F(TestMockManagedLockGetLockerRequest, GetLockInfoError) {
  REQUIRE_FEATURE(RBD_FEATURE_EXCLUSIVE_LOCK);

  librbd::ImageCtx *ictx;
  ASSERT_EQ(0, open_image(m_image_name, &ictx));

  MockTestImageCtx mock_image_ctx(*ictx);
  expect_op_work_queue(mock_image_ctx);

  InSequence seq;
  expect_get_lock_info(mock_image_ctx, -EINVAL, entity_name_t::CLIENT(1), "",
                       "", "", ClsLockType::EXCLUSIVE);

  C_SaferCond ctx;
  Locker locker;
  MockGetLockerRequest *req = MockGetLockerRequest::create(
    mock_image_ctx.md_ctx, mock_image_ctx.header_oid, true, &locker, &ctx);
  req->send();
  ASSERT_EQ(-EINVAL, ctx.wait());
}

TEST_F(TestMockManagedLockGetLockerRequest, GetLockInfoEmpty) {
  REQUIRE_FEATURE(RBD_FEATURE_EXCLUSIVE_LOCK);

  librbd::ImageCtx *ictx;
  ASSERT_EQ(0, open_image(m_image_name, &ictx));

  MockTestImageCtx mock_image_ctx(*ictx);
  expect_op_work_queue(mock_image_ctx);

  InSequence seq;
  expect_get_lock_info(mock_image_ctx, -ENOENT, entity_name_t::CLIENT(1), "",
                       "", "", ClsLockType::EXCLUSIVE);

  C_SaferCond ctx;
  Locker locker;
  MockGetLockerRequest *req = MockGetLockerRequest::create(
    mock_image_ctx.md_ctx, mock_image_ctx.header_oid, true, &locker, &ctx);
  req->send();
  ASSERT_EQ(-ENOENT, ctx.wait());
}

TEST_F(TestMockManagedLockGetLockerRequest, GetLockInfoExternalTag) {
  REQUIRE_FEATURE(RBD_FEATURE_EXCLUSIVE_LOCK);

  librbd::ImageCtx *ictx;
  ASSERT_EQ(0, open_image(m_image_name, &ictx));

  MockTestImageCtx mock_image_ctx(*ictx);
  expect_op_work_queue(mock_image_ctx);

  InSequence seq;
  expect_get_lock_info(mock_image_ctx, 0, entity_name_t::CLIENT(1), "1.2.3.4",
                       "auto 123", "external tag", ClsLockType::EXCLUSIVE);

  C_SaferCond ctx;
  Locker locker;
  MockGetLockerRequest *req = MockGetLockerRequest::create(
    mock_image_ctx.md_ctx, mock_image_ctx.header_oid, true, &locker, &ctx);
  req->send();
  ASSERT_EQ(-EBUSY, ctx.wait());
}

TEST_F(TestMockManagedLockGetLockerRequest, GetLockInfoIncompatibleShared) {
  REQUIRE_FEATURE(RBD_FEATURE_EXCLUSIVE_LOCK);

  librbd::ImageCtx *ictx;
  ASSERT_EQ(0, open_image(m_image_name, &ictx));

  MockTestImageCtx mock_image_ctx(*ictx);
  expect_op_work_queue(mock_image_ctx);

  InSequence seq;
  expect_get_lock_info(mock_image_ctx, 0, entity_name_t::CLIENT(1), "1.2.3.4",
                       "auto 123", util::get_watcher_lock_tag(),
                       ClsLockType::SHARED);

  C_SaferCond ctx;
  Locker locker;
  MockGetLockerRequest *req = MockGetLockerRequest::create(
    mock_image_ctx.md_ctx, mock_image_ctx.header_oid, true, &locker, &ctx);
  req->send();
  ASSERT_EQ(-EBUSY, ctx.wait());
}

TEST_F(TestMockManagedLockGetLockerRequest, GetLockInfoIncompatibleExclusive) {
  REQUIRE_FEATURE(RBD_FEATURE_EXCLUSIVE_LOCK);

  librbd::ImageCtx *ictx;
  ASSERT_EQ(0, open_image(m_image_name, &ictx));

  MockTestImageCtx mock_image_ctx(*ictx);
  expect_op_work_queue(mock_image_ctx);

  InSequence seq;
  expect_get_lock_info(mock_image_ctx, 0, entity_name_t::CLIENT(1), "1.2.3.4",
                       "auto 123", util::get_watcher_lock_tag(),
                       ClsLockType::EXCLUSIVE);

  C_SaferCond ctx;
  Locker locker;
  MockGetLockerRequest *req = MockGetLockerRequest::create(
    mock_image_ctx.md_ctx, mock_image_ctx.header_oid, false, &locker, &ctx);
  req->send();
  ASSERT_EQ(-EBUSY, ctx.wait());
}

TEST_F(TestMockManagedLockGetLockerRequest, GetLockInfoExternalCookie) {
  REQUIRE_FEATURE(RBD_FEATURE_EXCLUSIVE_LOCK);

  librbd::ImageCtx *ictx;
  ASSERT_EQ(0, open_image(m_image_name, &ictx));

  MockTestImageCtx mock_image_ctx(*ictx);
  expect_op_work_queue(mock_image_ctx);

  InSequence seq;
  expect_get_lock_info(mock_image_ctx, 0, entity_name_t::CLIENT(1), "1.2.3.4",
                       "external cookie", util::get_watcher_lock_tag(),
                       ClsLockType::EXCLUSIVE);

  C_SaferCond ctx;
  Locker locker;
  MockGetLockerRequest *req = MockGetLockerRequest::create(
    mock_image_ctx.md_ctx, mock_image_ctx.header_oid, true, &locker, &ctx);
  req->send();
  ASSERT_EQ(-EBUSY, ctx.wait());
}

} // namespace managed_lock
} // namespace librbd