summaryrefslogtreecommitdiffstats
path: root/src/test/librbd/operation/test_mock_DisableFeaturesRequest.cc
blob: e78435869fc8963ede1cf4e9eb1a702ddca05cc4 (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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
// -*- 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/librbd/mock/MockJournalPolicy.h"
#include "cls/rbd/cls_rbd_client.h"
#include "librbd/internal.h"
#include "librbd/image/SetFlagsRequest.h"
#include "librbd/io/AioCompletion.h"
#include "librbd/mirror/DisableRequest.h"
#include "librbd/journal/RemoveRequest.h"
#include "librbd/journal/StandardPolicy.h"
#include "librbd/journal/Types.h"
#include "librbd/object_map/RemoveRequest.h"
#include "librbd/operation/DisableFeaturesRequest.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"

namespace librbd {

namespace {

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

} // anonymous namespace

namespace image {

template<>
class SetFlagsRequest<MockOperationImageCtx> {
public:
  static SetFlagsRequest *s_instance;
  Context *on_finish = nullptr;

  static SetFlagsRequest *create(MockOperationImageCtx *image_ctx, uint64_t flags,
				 uint64_t mask, Context *on_finish) {
    ceph_assert(s_instance != nullptr);
    s_instance->on_finish = on_finish;
    return s_instance;
  }

  SetFlagsRequest() {
    s_instance = this;
  }

  MOCK_METHOD0(send, void());
};

SetFlagsRequest<MockOperationImageCtx> *SetFlagsRequest<MockOperationImageCtx>::s_instance;

} // namespace image

namespace journal {

template<>
class RemoveRequest<MockOperationImageCtx> {
public:
  static RemoveRequest *s_instance;
  Context *on_finish = nullptr;

  static RemoveRequest *create(IoCtx &ioctx, const std::string &imageid,
                               const std::string &client_id,
                               MockContextWQ *op_work_queue,
                               Context *on_finish) {
    ceph_assert(s_instance != nullptr);
    s_instance->on_finish = on_finish;
    return s_instance;
  }

  RemoveRequest() {
    s_instance = this;
  }

  MOCK_METHOD0(send, void());
};

RemoveRequest<MockOperationImageCtx> *RemoveRequest<MockOperationImageCtx>::s_instance;

template<>
class StandardPolicy<MockOperationImageCtx> : public MockJournalPolicy {
public:
  StandardPolicy(MockOperationImageCtx* image_ctx) {
  }
};

} // namespace journal

namespace mirror {

template<>
class DisableRequest<MockOperationImageCtx> {
public:
  static DisableRequest *s_instance;
  Context *on_finish = nullptr;

  static DisableRequest *create(MockOperationImageCtx *image_ctx, bool force,
                                bool remove, Context *on_finish) {
    ceph_assert(s_instance != nullptr);
    s_instance->on_finish = on_finish;
    return s_instance;
  }

  DisableRequest() {
    s_instance = this;
  }

  MOCK_METHOD0(send, void());
};

DisableRequest<MockOperationImageCtx> *DisableRequest<MockOperationImageCtx>::s_instance;

} // namespace mirror

namespace object_map {

template<>
class RemoveRequest<MockOperationImageCtx> {
public:
  static RemoveRequest *s_instance;
  Context *on_finish = nullptr;

  static RemoveRequest *create(MockOperationImageCtx *image_ctx, Context *on_finish) {
    ceph_assert(s_instance != nullptr);
    s_instance->on_finish = on_finish;
    return s_instance;
  }

  RemoveRequest() {
    s_instance = this;
  }

  MOCK_METHOD0(send, void());
};

RemoveRequest<MockOperationImageCtx> *RemoveRequest<MockOperationImageCtx>::s_instance;

} // namespace object_map

template <>
struct AsyncRequest<MockOperationImageCtx> : public AsyncRequest<MockImageCtx> {
  MockOperationImageCtx &m_image_ctx;

  AsyncRequest(MockOperationImageCtx &image_ctx, Context *on_finish)
    : AsyncRequest<MockImageCtx>(image_ctx, on_finish), m_image_ctx(image_ctx) {
  }
};

} // namespace librbd

// template definitions
#include "librbd/AsyncRequest.cc"
#include "librbd/AsyncObjectThrottle.cc"
#include "librbd/operation/Request.cc"
#include "librbd/operation/DisableFeaturesRequest.cc"

namespace librbd {
namespace operation {

using ::testing::Invoke;
using ::testing::Return;
using ::testing::WithArg;
using ::testing::_;

class TestMockOperationDisableFeaturesRequest : public TestMockFixture {
public:
  typedef librbd::image::SetFlagsRequest<MockOperationImageCtx> MockSetFlagsRequest;
  typedef librbd::journal::RemoveRequest<MockOperationImageCtx> MockRemoveJournalRequest;
  typedef librbd::mirror::DisableRequest<MockOperationImageCtx> MockDisableMirrorRequest;
  typedef librbd::object_map::RemoveRequest<MockOperationImageCtx> MockRemoveObjectMapRequest;
  typedef DisableFeaturesRequest<MockOperationImageCtx> MockDisableFeaturesRequest;

  class PoolMirrorModeEnabler {
  public:
    PoolMirrorModeEnabler(librados::IoCtx &ioctx) : m_ioctx(ioctx) {
      EXPECT_EQ(0, librbd::cls_client::mirror_uuid_set(&m_ioctx, "test-uuid"));
      EXPECT_EQ(0, librbd::cls_client::mirror_mode_set(
		  &m_ioctx, cls::rbd::MIRROR_MODE_POOL));
    }

    ~PoolMirrorModeEnabler() {
      EXPECT_EQ(0, librbd::cls_client::mirror_mode_set(
		&m_ioctx, cls::rbd::MIRROR_MODE_DISABLED));
    }
  private:
    librados::IoCtx &m_ioctx;
  };

  void expect_prepare_lock(MockOperationImageCtx &mock_image_ctx) {
    EXPECT_CALL(*mock_image_ctx.state, prepare_lock(_))
      .WillOnce(Invoke([](Context *on_ready) {
	    on_ready->complete(0);
	  }));
    expect_op_work_queue(mock_image_ctx);
  }

  void expect_handle_prepare_lock_complete(MockOperationImageCtx &mock_image_ctx) {
    EXPECT_CALL(*mock_image_ctx.state, handle_prepare_lock_complete());
  }

  void expect_block_writes(MockOperationImageCtx &mock_image_ctx) {
    EXPECT_CALL(*mock_image_ctx.io_work_queue, block_writes(_))
      .WillOnce(CompleteContext(0, mock_image_ctx.image_ctx->op_work_queue));
  }

  void expect_unblock_writes(MockOperationImageCtx &mock_image_ctx) {
    EXPECT_CALL(*mock_image_ctx.io_work_queue, unblock_writes()).Times(1);
  }

  void expect_verify_lock_ownership(MockOperationImageCtx &mock_image_ctx) {
    if (mock_image_ctx.exclusive_lock != nullptr) {
      EXPECT_CALL(*mock_image_ctx.exclusive_lock, is_lock_owner())
	.WillRepeatedly(Return(true));
    }
  }

  void expect_block_requests(MockOperationImageCtx &mock_image_ctx) {
    if (mock_image_ctx.exclusive_lock != nullptr) {
      EXPECT_CALL(*mock_image_ctx.exclusive_lock, block_requests(0)).Times(1);
    }
  }

  void expect_unblock_requests(MockOperationImageCtx &mock_image_ctx) {
    if (mock_image_ctx.exclusive_lock != nullptr) {
      EXPECT_CALL(*mock_image_ctx.exclusive_lock, unblock_requests()).Times(1);
    }
  }

  void expect_set_flags_request_send(
    MockOperationImageCtx &mock_image_ctx,
    MockSetFlagsRequest &mock_set_flags_request, int r) {
    EXPECT_CALL(mock_set_flags_request, send())
      .WillOnce(FinishRequest(&mock_set_flags_request, r,
                              &mock_image_ctx));
  }

  void expect_disable_mirror_request_send(
    MockOperationImageCtx &mock_image_ctx,
    MockDisableMirrorRequest &mock_disable_mirror_request, int r) {
    EXPECT_CALL(mock_disable_mirror_request, send())
      .WillOnce(FinishRequest(&mock_disable_mirror_request, r,
                              &mock_image_ctx));
  }

  void expect_close_journal(MockOperationImageCtx &mock_image_ctx, int r) {
    EXPECT_CALL(*mock_image_ctx.journal, close(_))
      .WillOnce(Invoke([&mock_image_ctx, r](Context *on_finish) {
	    mock_image_ctx.journal = nullptr;
	    mock_image_ctx.image_ctx->op_work_queue->queue(on_finish, r);
	  }));
  }

  void expect_remove_journal_request_send(
    MockOperationImageCtx &mock_image_ctx,
    MockRemoveJournalRequest &mock_remove_journal_request, int r) {
    EXPECT_CALL(mock_remove_journal_request, send())
      .WillOnce(FinishRequest(&mock_remove_journal_request, r,
                              &mock_image_ctx));
  }

  void expect_remove_object_map_request_send(
    MockOperationImageCtx &mock_image_ctx,
    MockRemoveObjectMapRequest &mock_remove_object_map_request, int r) {
    EXPECT_CALL(mock_remove_object_map_request, send())
      .WillOnce(FinishRequest(&mock_remove_object_map_request, r,
                              &mock_image_ctx));
  }

  void expect_notify_update(MockOperationImageCtx &mock_image_ctx) {
    EXPECT_CALL(mock_image_ctx, notify_update(_))
      .WillOnce(CompleteContext(0, mock_image_ctx.image_ctx->op_work_queue));
  }

};

TEST_F(TestMockOperationDisableFeaturesRequest, All) {
  REQUIRE_FORMAT_V2();

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

  uint64_t features;
  ASSERT_EQ(0, librbd::get_features(ictx, &features));

  uint64_t features_to_disable = RBD_FEATURES_MUTABLE & features;

  REQUIRE(features_to_disable);

  MockOperationImageCtx mock_image_ctx(*ictx);
  MockExclusiveLock mock_exclusive_lock;
  MockJournal mock_journal_stack;
  MockJournal *mock_journal = &mock_journal_stack;
  if (features_to_disable & RBD_FEATURE_JOURNALING) {
    mock_journal = new MockJournal();
  }
  MockObjectMap mock_object_map;
  initialize_features(ictx, mock_image_ctx, mock_exclusive_lock, *mock_journal,
		      mock_object_map);

  expect_verify_lock_ownership(mock_image_ctx);

  MockSetFlagsRequest mock_set_flags_request;
  MockRemoveJournalRequest mock_remove_journal_request;
  MockDisableMirrorRequest mock_disable_mirror_request;
  MockRemoveObjectMapRequest mock_remove_object_map_request;

  ::testing::InSequence seq;
  expect_prepare_lock(mock_image_ctx);
  expect_block_writes(mock_image_ctx);
  if (mock_image_ctx.journal != nullptr) {
    expect_is_journal_replaying(*mock_image_ctx.journal);
  }
  expect_block_requests(mock_image_ctx);
  if (features_to_disable & RBD_FEATURE_JOURNALING) {
    expect_disable_mirror_request_send(mock_image_ctx,
                                       mock_disable_mirror_request, 0);
    expect_close_journal(mock_image_ctx, 0);
    expect_remove_journal_request_send(mock_image_ctx,
                                       mock_remove_journal_request, 0);
  }
  if (features_to_disable & RBD_FEATURE_OBJECT_MAP) {
    expect_remove_object_map_request_send(mock_image_ctx,
					  mock_remove_object_map_request, 0);
  }
  if (features_to_disable & (RBD_FEATURE_OBJECT_MAP | RBD_FEATURE_FAST_DIFF)) {
    expect_set_flags_request_send(mock_image_ctx,
				  mock_set_flags_request, 0);
  }
  expect_notify_update(mock_image_ctx);
  expect_unblock_requests(mock_image_ctx);
  expect_unblock_writes(mock_image_ctx);
  expect_handle_prepare_lock_complete(mock_image_ctx);

  C_SaferCond cond_ctx;
  MockDisableFeaturesRequest *req = new MockDisableFeaturesRequest(
    mock_image_ctx, &cond_ctx, 0, features_to_disable, false);
  {
    RWLock::RLocker owner_locker(mock_image_ctx.owner_lock);
    req->send();
  }
  ASSERT_EQ(0, cond_ctx.wait());
}

TEST_F(TestMockOperationDisableFeaturesRequest, ObjectMap) {
  REQUIRE_FEATURE(RBD_FEATURE_OBJECT_MAP);

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

  MockOperationImageCtx mock_image_ctx(*ictx);
  MockExclusiveLock mock_exclusive_lock;
  MockJournal mock_journal;
  MockObjectMap mock_object_map;
  initialize_features(ictx, mock_image_ctx, mock_exclusive_lock, mock_journal,
		      mock_object_map);

  expect_verify_lock_ownership(mock_image_ctx);

  MockSetFlagsRequest mock_set_flags_request;
  MockRemoveObjectMapRequest mock_remove_object_map_request;

  ::testing::InSequence seq;
  expect_prepare_lock(mock_image_ctx);
  expect_block_writes(mock_image_ctx);
  if (mock_image_ctx.journal != nullptr) {
    expect_is_journal_replaying(*mock_image_ctx.journal);
  }
  expect_block_requests(mock_image_ctx);
  expect_append_op_event(mock_image_ctx, true, 0);
  expect_remove_object_map_request_send(mock_image_ctx,
                                        mock_remove_object_map_request, 0);
  expect_set_flags_request_send(mock_image_ctx,
                                mock_set_flags_request, 0);
  expect_notify_update(mock_image_ctx);
  expect_unblock_requests(mock_image_ctx);
  expect_unblock_writes(mock_image_ctx);
  expect_handle_prepare_lock_complete(mock_image_ctx);
  expect_commit_op_event(mock_image_ctx, 0);

  C_SaferCond cond_ctx;
  MockDisableFeaturesRequest *req = new MockDisableFeaturesRequest(
    mock_image_ctx, &cond_ctx, 0,
    RBD_FEATURE_OBJECT_MAP | RBD_FEATURE_FAST_DIFF, false);
  {
    RWLock::RLocker owner_locker(mock_image_ctx.owner_lock);
    req->send();
  }
  ASSERT_EQ(0, cond_ctx.wait());
}

TEST_F(TestMockOperationDisableFeaturesRequest, ObjectMapError) {
  REQUIRE_FEATURE(RBD_FEATURE_OBJECT_MAP);

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

  MockOperationImageCtx mock_image_ctx(*ictx);
  MockExclusiveLock mock_exclusive_lock;
  MockJournal mock_journal;
  MockObjectMap mock_object_map;
  initialize_features(ictx, mock_image_ctx, mock_exclusive_lock, mock_journal,
		      mock_object_map);

  expect_verify_lock_ownership(mock_image_ctx);

  MockSetFlagsRequest mock_set_flags_request;
  MockRemoveObjectMapRequest mock_remove_object_map_request;

  ::testing::InSequence seq;
  expect_prepare_lock(mock_image_ctx);
  expect_block_writes(mock_image_ctx);
  if (mock_image_ctx.journal != nullptr) {
    expect_is_journal_replaying(*mock_image_ctx.journal);
  }
  expect_block_requests(mock_image_ctx);
  expect_append_op_event(mock_image_ctx, true, 0);
  expect_remove_object_map_request_send(mock_image_ctx,
                                        mock_remove_object_map_request, -EINVAL);
  expect_unblock_requests(mock_image_ctx);
  expect_unblock_writes(mock_image_ctx);
  expect_handle_prepare_lock_complete(mock_image_ctx);
  expect_commit_op_event(mock_image_ctx, -EINVAL);

  C_SaferCond cond_ctx;
  MockDisableFeaturesRequest *req = new MockDisableFeaturesRequest(
    mock_image_ctx, &cond_ctx, 0,
    RBD_FEATURE_OBJECT_MAP | RBD_FEATURE_FAST_DIFF, false);
  {
    RWLock::RLocker owner_locker(mock_image_ctx.owner_lock);
    req->send();
  }
  ASSERT_EQ(-EINVAL, cond_ctx.wait());
}

TEST_F(TestMockOperationDisableFeaturesRequest, Mirroring) {
  REQUIRE_FEATURE(RBD_FEATURE_JOURNALING);

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

  MockOperationImageCtx mock_image_ctx(*ictx);
  MockExclusiveLock mock_exclusive_lock;
  MockJournal *mock_journal = new MockJournal();
  MockObjectMap mock_object_map;
  initialize_features(ictx, mock_image_ctx, mock_exclusive_lock, *mock_journal,
		      mock_object_map);

  expect_verify_lock_ownership(mock_image_ctx);

  MockRemoveJournalRequest mock_remove_journal_request;
  MockDisableMirrorRequest mock_disable_mirror_request;

  ::testing::InSequence seq;
  expect_prepare_lock(mock_image_ctx);
  expect_block_writes(mock_image_ctx);
  expect_is_journal_replaying(*mock_image_ctx.journal);
  expect_block_requests(mock_image_ctx);
  expect_disable_mirror_request_send(mock_image_ctx,
                                     mock_disable_mirror_request, 0);
  expect_close_journal(mock_image_ctx, 0);
  expect_remove_journal_request_send(mock_image_ctx,
                                     mock_remove_journal_request, 0);
  expect_notify_update(mock_image_ctx);
  expect_unblock_requests(mock_image_ctx);
  expect_unblock_writes(mock_image_ctx);
  expect_handle_prepare_lock_complete(mock_image_ctx);

  C_SaferCond cond_ctx;
  MockDisableFeaturesRequest *req = new MockDisableFeaturesRequest(
    mock_image_ctx, &cond_ctx, 0, RBD_FEATURE_JOURNALING, false);
  {
    RWLock::RLocker owner_locker(mock_image_ctx.owner_lock);
    req->send();
  }
  ASSERT_EQ(0, cond_ctx.wait());
}

TEST_F(TestMockOperationDisableFeaturesRequest, MirroringError) {
  REQUIRE_FEATURE(RBD_FEATURE_JOURNALING);

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

  MockOperationImageCtx mock_image_ctx(*ictx);
  MockExclusiveLock mock_exclusive_lock;
  MockJournal *mock_journal = new MockJournal();
  MockObjectMap mock_object_map;
  initialize_features(ictx, mock_image_ctx, mock_exclusive_lock, *mock_journal,
		      mock_object_map);

  expect_verify_lock_ownership(mock_image_ctx);

  MockRemoveJournalRequest mock_remove_journal_request;
  MockDisableMirrorRequest mock_disable_mirror_request;

  ::testing::InSequence seq;
  expect_prepare_lock(mock_image_ctx);
  expect_block_writes(mock_image_ctx);
  expect_is_journal_replaying(*mock_image_ctx.journal);
  expect_block_requests(mock_image_ctx);
  expect_disable_mirror_request_send(mock_image_ctx,
                                     mock_disable_mirror_request, -EINVAL);
  expect_close_journal(mock_image_ctx, 0);
  expect_remove_journal_request_send(mock_image_ctx,
                                     mock_remove_journal_request, 0);
  expect_notify_update(mock_image_ctx);
  expect_unblock_requests(mock_image_ctx);
  expect_unblock_writes(mock_image_ctx);
  expect_handle_prepare_lock_complete(mock_image_ctx);

  C_SaferCond cond_ctx;
  MockDisableFeaturesRequest *req = new MockDisableFeaturesRequest(
    mock_image_ctx, &cond_ctx, 0, RBD_FEATURE_JOURNALING, false);
  {
    RWLock::RLocker owner_locker(mock_image_ctx.owner_lock);
    req->send();
  }
  ASSERT_EQ(0, cond_ctx.wait());
}

} // namespace operation
} // namespace librbd