summaryrefslogtreecommitdiffstats
path: root/src/test/librbd/deep_copy/test_mock_SetHeadRequest.cc
blob: 48c2f3b0f40b04b99e219a8582c0fe7db53e95d1 (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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
// -*- 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/librados_test_stub/LibradosTestStub.h"
#include "include/rbd/librbd.hpp"
#include "librbd/ImageCtx.h"
#include "librbd/ImageState.h"
#include "osdc/Striper.h"
#include "test/librados_test_stub/MockTestMemIoCtxImpl.h"
#include "test/librbd/mock/MockImageCtx.h"
#include "librbd/deep_copy/SetHeadRequest.h"
#include "librbd/image/AttachParentRequest.h"
#include "librbd/image/DetachParentRequest.h"

namespace librbd {
namespace {

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

} // anonymous namespace

namespace image {

template <>
struct AttachParentRequest<MockTestImageCtx> {
  Context* on_finish = nullptr;
  static AttachParentRequest* s_instance;
  static AttachParentRequest* create(MockTestImageCtx&,
                                     const cls::rbd::ParentImageSpec& pspec,
                                     uint64_t parent_overlap, bool reattach,
                                     Context *on_finish) {
    ceph_assert(s_instance != nullptr);
    s_instance->on_finish = on_finish;
    return s_instance;
  }

  MOCK_METHOD0(send, void());

  AttachParentRequest() {
    s_instance = this;
  }
};

AttachParentRequest<MockTestImageCtx>* AttachParentRequest<MockTestImageCtx>::s_instance = nullptr;

template <>
class DetachParentRequest<MockTestImageCtx> {
public:
  static DetachParentRequest *s_instance;
  static DetachParentRequest *create(MockTestImageCtx &image_ctx,
                                     Context *on_finish) {
    ceph_assert(s_instance != nullptr);
    s_instance->on_finish = on_finish;
    return s_instance;
  }

  Context *on_finish = nullptr;

  DetachParentRequest() {
    s_instance = this;
  }

  MOCK_METHOD0(send, void());
};

DetachParentRequest<MockTestImageCtx> *DetachParentRequest<MockTestImageCtx>::s_instance;

} // namespace image
} // namespace librbd

// template definitions
#include "librbd/deep_copy/SetHeadRequest.cc"
template class librbd::deep_copy::SetHeadRequest<librbd::MockTestImageCtx>;

namespace librbd {
namespace deep_copy {

using ::testing::_;
using ::testing::DoAll;
using ::testing::InSequence;
using ::testing::Invoke;
using ::testing::InvokeWithoutArgs;
using ::testing::Return;
using ::testing::ReturnNew;
using ::testing::StrEq;
using ::testing::WithArg;

class TestMockDeepCopySetHeadRequest : public TestMockFixture {
public:
  typedef SetHeadRequest<librbd::MockTestImageCtx> MockSetHeadRequest;
  typedef image::AttachParentRequest<MockTestImageCtx> MockAttachParentRequest;
  typedef image::DetachParentRequest<MockTestImageCtx> MockDetachParentRequest;

  librbd::ImageCtx *m_image_ctx;
  ThreadPool *m_thread_pool;
  ContextWQ *m_work_queue;

  void SetUp() override {
    TestMockFixture::SetUp();

    ASSERT_EQ(0, open_image(m_image_name, &m_image_ctx));

    librbd::ImageCtx::get_thread_pool_instance(m_image_ctx->cct, &m_thread_pool,
                                               &m_work_queue);
  }

  void expect_start_op(librbd::MockExclusiveLock &mock_exclusive_lock) {
    EXPECT_CALL(mock_exclusive_lock, start_op(_)).WillOnce(
      ReturnNew<FunctionContext>([](int) {}));
  }

  void expect_test_features(librbd::MockTestImageCtx &mock_image_ctx,
                            uint64_t features, bool enabled) {
    EXPECT_CALL(mock_image_ctx, test_features(features))
                  .WillOnce(Return(enabled));
  }

  void expect_set_size(librbd::MockTestImageCtx &mock_image_ctx, int r) {
    EXPECT_CALL(get_mock_io_ctx(mock_image_ctx.md_ctx),
                exec(mock_image_ctx.header_oid, _, StrEq("rbd"), StrEq("set_size"), _, _, _))
                  .WillOnce(Return(r));
  }

  void expect_detach_parent(MockImageCtx &mock_image_ctx,
                            MockDetachParentRequest& mock_request, int r) {
    EXPECT_CALL(mock_request, send())
      .WillOnce(FinishRequest(&mock_request, r, &mock_image_ctx));
  }

  void expect_attach_parent(MockImageCtx &mock_image_ctx,
                            MockAttachParentRequest& mock_request, int r) {
    EXPECT_CALL(mock_request, send())
      .WillOnce(FinishRequest(&mock_request, r, &mock_image_ctx));
  }

  MockSetHeadRequest *create_request(
      librbd::MockTestImageCtx &mock_local_image_ctx, uint64_t size,
      const cls::rbd::ParentImageSpec &parent_spec, uint64_t parent_overlap,
      Context *on_finish) {
    return new MockSetHeadRequest(&mock_local_image_ctx, size, parent_spec,
                                  parent_overlap, on_finish);
  }
};

TEST_F(TestMockDeepCopySetHeadRequest, Resize) {
  librbd::MockTestImageCtx mock_image_ctx(*m_image_ctx);
  librbd::MockExclusiveLock mock_exclusive_lock;
  mock_image_ctx.exclusive_lock = &mock_exclusive_lock;

  InSequence seq;
  expect_start_op(mock_exclusive_lock);
  expect_set_size(mock_image_ctx, 0);

  C_SaferCond ctx;
  auto request = create_request(mock_image_ctx, 123, {}, 0, &ctx);
  request->send();
  ASSERT_EQ(0, ctx.wait());
}

TEST_F(TestMockDeepCopySetHeadRequest, ResizeError) {
  librbd::MockTestImageCtx mock_image_ctx(*m_image_ctx);
  librbd::MockExclusiveLock mock_exclusive_lock;
  mock_image_ctx.exclusive_lock = &mock_exclusive_lock;

  InSequence seq;
  expect_start_op(mock_exclusive_lock);
  expect_set_size(mock_image_ctx, -EINVAL);

  C_SaferCond ctx;
  auto request = create_request(mock_image_ctx, 123, {}, 0, &ctx);
  request->send();
  ASSERT_EQ(-EINVAL, ctx.wait());
}

TEST_F(TestMockDeepCopySetHeadRequest, RemoveParent) {
  librbd::MockTestImageCtx mock_image_ctx(*m_image_ctx);
  librbd::MockExclusiveLock mock_exclusive_lock;
  mock_image_ctx.exclusive_lock = &mock_exclusive_lock;

  mock_image_ctx.parent_md.spec.pool_id = 213;

  InSequence seq;
  expect_start_op(mock_exclusive_lock);
  MockDetachParentRequest mock_detach_parent;
  expect_detach_parent(mock_image_ctx, mock_detach_parent, 0);

  C_SaferCond ctx;
  auto request = create_request(mock_image_ctx, m_image_ctx->size, {}, 0, &ctx);
  request->send();
  ASSERT_EQ(0, ctx.wait());
}

TEST_F(TestMockDeepCopySetHeadRequest, RemoveParentError) {
  librbd::MockTestImageCtx mock_image_ctx(*m_image_ctx);
  librbd::MockExclusiveLock mock_exclusive_lock;
  mock_image_ctx.exclusive_lock = &mock_exclusive_lock;

  mock_image_ctx.parent_md.spec.pool_id = 213;

  InSequence seq;
  expect_start_op(mock_exclusive_lock);
  MockDetachParentRequest mock_detach_parent;
  expect_detach_parent(mock_image_ctx, mock_detach_parent, -EINVAL);

  C_SaferCond ctx;
  auto request = create_request(mock_image_ctx, m_image_ctx->size, {}, 0, &ctx);
  request->send();
  ASSERT_EQ(-EINVAL, ctx.wait());
}

TEST_F(TestMockDeepCopySetHeadRequest, RemoveSetParent) {
  librbd::MockTestImageCtx mock_image_ctx(*m_image_ctx);
  librbd::MockExclusiveLock mock_exclusive_lock;
  mock_image_ctx.exclusive_lock = &mock_exclusive_lock;

  mock_image_ctx.parent_md.spec.pool_id = 213;

  InSequence seq;
  expect_start_op(mock_exclusive_lock);
  MockDetachParentRequest mock_detach_parent;
  expect_detach_parent(mock_image_ctx, mock_detach_parent, 0);
  expect_start_op(mock_exclusive_lock);
  MockAttachParentRequest mock_attach_parent;
  expect_attach_parent(mock_image_ctx, mock_attach_parent, 0);

  C_SaferCond ctx;
  auto request = create_request(mock_image_ctx, m_image_ctx->size,
                                {123, "", "test", 0}, 0, &ctx);
  request->send();
  ASSERT_EQ(0, ctx.wait());
}

TEST_F(TestMockDeepCopySetHeadRequest, SetParentSpec) {
  librbd::MockTestImageCtx mock_image_ctx(*m_image_ctx);
  librbd::MockExclusiveLock mock_exclusive_lock;
  mock_image_ctx.exclusive_lock = &mock_exclusive_lock;

  InSequence seq;
  expect_start_op(mock_exclusive_lock);
  MockAttachParentRequest mock_attach_parent;
  expect_attach_parent(mock_image_ctx, mock_attach_parent, 0);

  C_SaferCond ctx;
  auto request = create_request(mock_image_ctx, m_image_ctx->size,
                                {123, "", "test", 0}, 0, &ctx);
  request->send();
  ASSERT_EQ(0, ctx.wait());
}

TEST_F(TestMockDeepCopySetHeadRequest, SetParentOverlap) {
  librbd::MockTestImageCtx mock_image_ctx(*m_image_ctx);
  librbd::MockExclusiveLock mock_exclusive_lock;
  mock_image_ctx.exclusive_lock = &mock_exclusive_lock;

  mock_image_ctx.parent_md.spec = {123, "", "test", 0};
  mock_image_ctx.parent_md.overlap = m_image_ctx->size;

  InSequence seq;
  expect_start_op(mock_exclusive_lock);
  expect_set_size(mock_image_ctx, 0);

  C_SaferCond ctx;
  auto request = create_request(mock_image_ctx, 123,
                                mock_image_ctx.parent_md.spec, 123, &ctx);
  request->send();
  ASSERT_EQ(0, ctx.wait());
  ASSERT_EQ(123U, mock_image_ctx.parent_md.overlap);
}

TEST_F(TestMockDeepCopySetHeadRequest, SetParentError) {
  librbd::MockTestImageCtx mock_image_ctx(*m_image_ctx);
  librbd::MockExclusiveLock mock_exclusive_lock;
  mock_image_ctx.exclusive_lock = &mock_exclusive_lock;

  InSequence seq;
  expect_start_op(mock_exclusive_lock);
  MockAttachParentRequest mock_attach_parent;
  expect_attach_parent(mock_image_ctx, mock_attach_parent, -ESTALE);

  C_SaferCond ctx;
  auto request = create_request(mock_image_ctx, m_image_ctx->size,
                                {123, "", "test", 0}, 0, &ctx);
  request->send();
  ASSERT_EQ(-ESTALE, ctx.wait());
}

} // namespace deep_copy
} // namespace librbd