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
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
|
// -*- 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/exclusive_lock/MockPolicy.h"
#include "test/librbd/mock/io/MockImageDispatch.h"
#include "librbd/ExclusiveLock.h"
#include "librbd/ManagedLock.h"
#include "librbd/exclusive_lock/ImageDispatch.h"
#include "librbd/exclusive_lock/PreAcquireRequest.h"
#include "librbd/exclusive_lock/PostAcquireRequest.h"
#include "librbd/exclusive_lock/PreReleaseRequest.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include <list>
#include <boost/scope_exit.hpp>
namespace librbd {
namespace {
struct MockExclusiveLockImageCtx : public MockImageCtx {
asio::ContextWQ *op_work_queue;
MockExclusiveLockImageCtx(ImageCtx &image_ctx) : MockImageCtx(image_ctx) {
op_work_queue = image_ctx.op_work_queue;
}
};
} // anonymous namespace
namespace watcher {
template <>
struct Traits<MockExclusiveLockImageCtx> {
typedef librbd::MockImageWatcher Watcher;
};
}
template <>
struct ManagedLock<MockExclusiveLockImageCtx> {
ManagedLock(librados::IoCtx& ioctx, AsioEngine& asio_engine,
const std::string& oid, librbd::MockImageWatcher *watcher,
managed_lock::Mode mode, bool blocklist_on_break_lock,
uint32_t blocklist_expire_seconds)
{}
virtual ~ManagedLock() = default;
mutable ceph::mutex m_lock = ceph::make_mutex("ManagedLock::m_lock");
virtual void shutdown_handler(int r, Context *) = 0;
virtual void pre_acquire_lock_handler(Context *) = 0;
virtual void post_acquire_lock_handler(int, Context *) = 0;
virtual void pre_release_lock_handler(bool, Context *) = 0;
virtual void post_release_lock_handler(bool, int, Context *) = 0;
virtual void post_reacquire_lock_handler(int, Context *) = 0;
MOCK_CONST_METHOD0(is_lock_owner, bool());
MOCK_METHOD1(shut_down, void(Context*));
MOCK_METHOD1(acquire_lock, void(Context*));
void set_state_uninitialized() {
}
MOCK_METHOD0(set_state_initializing, void());
MOCK_METHOD0(set_state_unlocked, void());
MOCK_METHOD0(set_state_waiting_for_lock, void());
MOCK_METHOD0(set_state_post_acquiring, void());
MOCK_CONST_METHOD0(is_state_shutdown, bool());
MOCK_CONST_METHOD0(is_state_acquiring, bool());
MOCK_CONST_METHOD0(is_state_post_acquiring, bool());
MOCK_CONST_METHOD0(is_state_releasing, bool());
MOCK_CONST_METHOD0(is_state_pre_releasing, bool());
MOCK_CONST_METHOD0(is_state_locked, bool());
MOCK_CONST_METHOD0(is_state_waiting_for_lock, bool());
MOCK_CONST_METHOD0(is_action_acquire_lock, bool());
MOCK_METHOD0(execute_next_action, void());
};
namespace exclusive_lock {
using librbd::ImageWatcher;
template<typename T>
struct BaseRequest {
static std::list<T *> s_requests;
Context *on_lock_unlock = nullptr;
Context *on_finish = nullptr;
static T* create(MockExclusiveLockImageCtx &image_ctx,
Context *on_lock_unlock, Context *on_finish) {
ceph_assert(!s_requests.empty());
T* req = s_requests.front();
req->on_lock_unlock = on_lock_unlock;
req->on_finish = on_finish;
s_requests.pop_front();
return req;
}
BaseRequest() {
s_requests.push_back(reinterpret_cast<T*>(this));
}
};
template<typename T>
std::list<T *> BaseRequest<T>::s_requests;
template<>
struct ImageDispatch<MockExclusiveLockImageCtx>
: public librbd::io::MockImageDispatch {
static ImageDispatch* s_instance;
static ImageDispatch* create(MockExclusiveLockImageCtx*) {
ceph_assert(s_instance != nullptr);
return s_instance;
}
void destroy() {
}
ImageDispatch() {
s_instance = this;
}
io::ImageDispatchLayer get_dispatch_layer() const override {
return io::IMAGE_DISPATCH_LAYER_EXCLUSIVE_LOCK;
}
MOCK_METHOD3(set_require_lock, void(bool, io::Direction, Context*));
MOCK_METHOD1(unset_require_lock, void(io::Direction));
};
ImageDispatch<MockExclusiveLockImageCtx>* ImageDispatch<MockExclusiveLockImageCtx>::s_instance = nullptr;
template <>
struct PreAcquireRequest<MockExclusiveLockImageCtx> : public BaseRequest<PreAcquireRequest<MockExclusiveLockImageCtx> > {
static PreAcquireRequest<MockExclusiveLockImageCtx> *create(
MockExclusiveLockImageCtx &image_ctx, Context *on_finish) {
return BaseRequest::create(image_ctx, nullptr, on_finish);
}
MOCK_METHOD0(send, void());
};
template <>
struct PostAcquireRequest<MockExclusiveLockImageCtx> : public BaseRequest<PostAcquireRequest<MockExclusiveLockImageCtx> > {
MOCK_METHOD0(send, void());
};
template <>
struct PreReleaseRequest<MockExclusiveLockImageCtx> : public BaseRequest<PreReleaseRequest<MockExclusiveLockImageCtx> > {
static PreReleaseRequest<MockExclusiveLockImageCtx> *create(
MockExclusiveLockImageCtx &image_ctx,
ImageDispatch<MockExclusiveLockImageCtx>* ImageDispatch,
bool shutting_down, AsyncOpTracker &async_op_tracker,
Context *on_finish) {
return BaseRequest::create(image_ctx, nullptr, on_finish);
}
MOCK_METHOD0(send, void());
};
} // namespace exclusive_lock
} // namespace librbd
// template definitions
#include "librbd/ExclusiveLock.cc"
ACTION_P(FinishLockUnlock, request) {
if (request->on_lock_unlock != nullptr) {
request->on_lock_unlock->complete(0);
}
}
ACTION_P2(CompleteRequest, request, ret) {
request->on_finish->complete(ret);
}
namespace librbd {
using ::testing::_;
using ::testing::DoAll;
using ::testing::Invoke;
using ::testing::InSequence;
using ::testing::Return;
using ::testing::WithArg;
class TestMockExclusiveLock : public TestMockFixture {
public:
typedef ManagedLock<MockExclusiveLockImageCtx> MockManagedLock;
typedef ExclusiveLock<MockExclusiveLockImageCtx> MockExclusiveLock;
typedef exclusive_lock::ImageDispatch<MockExclusiveLockImageCtx> MockImageDispatch;
typedef exclusive_lock::PreAcquireRequest<MockExclusiveLockImageCtx> MockPreAcquireRequest;
typedef exclusive_lock::PostAcquireRequest<MockExclusiveLockImageCtx> MockPostAcquireRequest;
typedef exclusive_lock::PreReleaseRequest<MockExclusiveLockImageCtx> MockPreReleaseRequest;
void expect_set_state_initializing(MockManagedLock *managed_lock) {
EXPECT_CALL(*managed_lock, set_state_initializing());
}
void expect_set_state_unlocked(MockManagedLock *managed_lock) {
EXPECT_CALL(*managed_lock, set_state_unlocked());
}
void expect_set_state_waiting_for_lock(MockManagedLock *managed_lock) {
EXPECT_CALL(*managed_lock, set_state_waiting_for_lock());
}
void expect_set_state_post_acquiring(MockManagedLock *managed_lock) {
EXPECT_CALL(*managed_lock, set_state_post_acquiring());
}
void expect_is_state_acquiring(MockManagedLock *managed_lock, bool ret_val) {
EXPECT_CALL(*managed_lock, is_state_acquiring())
.WillOnce(Return(ret_val));
}
void expect_is_state_waiting_for_lock(MockManagedLock *managed_lock,
bool ret_val) {
EXPECT_CALL(*managed_lock, is_state_waiting_for_lock())
.WillOnce(Return(ret_val));
}
void expect_is_state_pre_releasing(MockManagedLock *managed_lock,
bool ret_val) {
EXPECT_CALL(*managed_lock, is_state_pre_releasing())
.WillOnce(Return(ret_val));
}
void expect_is_state_releasing(MockManagedLock *managed_lock, bool ret_val) {
EXPECT_CALL(*managed_lock, is_state_releasing())
.WillOnce(Return(ret_val));
}
void expect_is_state_locked(MockManagedLock *managed_lock, bool ret_val) {
EXPECT_CALL(*managed_lock, is_state_locked())
.WillOnce(Return(ret_val));
}
void expect_is_state_shutdown(MockManagedLock *managed_lock, bool ret_val) {
EXPECT_CALL(*managed_lock, is_state_shutdown())
.WillOnce(Return(ret_val));
}
void expect_is_action_acquire_lock(MockManagedLock *managed_lock,
bool ret_val) {
EXPECT_CALL(*managed_lock, is_action_acquire_lock())
.WillOnce(Return(ret_val));
}
void expect_set_require_lock(MockImageDispatch &mock_image_dispatch,
bool init_shutdown, io::Direction direction) {
EXPECT_CALL(mock_image_dispatch, set_require_lock(init_shutdown,
direction, _))
.WillOnce(WithArg<2>(Invoke([](Context* ctx) { ctx->complete(0); })));
}
void expect_set_require_lock(MockExclusiveLockImageCtx &mock_image_ctx,
MockImageDispatch &mock_image_dispatch,
bool init_shutdown) {
if (mock_image_ctx.clone_copy_on_read ||
(mock_image_ctx.features & RBD_FEATURE_JOURNALING) != 0 ||
is_rbd_pwl_enabled(mock_image_ctx.cct)) {
expect_set_require_lock(mock_image_dispatch, init_shutdown,
io::DIRECTION_BOTH);
} else {
expect_set_require_lock(mock_image_dispatch, init_shutdown,
io::DIRECTION_WRITE);
}
}
void expect_unset_require_lock(MockImageDispatch &mock_image_dispatch) {
EXPECT_CALL(mock_image_dispatch, unset_require_lock(io::DIRECTION_BOTH));
}
void expect_register_dispatch(MockExclusiveLockImageCtx &mock_image_ctx) {
EXPECT_CALL(*mock_image_ctx.io_image_dispatcher, register_dispatch(_));
}
void expect_shut_down_dispatch(MockExclusiveLockImageCtx &mock_image_ctx) {
EXPECT_CALL(*mock_image_ctx.io_image_dispatcher, shut_down_dispatch(_, _))
.WillOnce(WithArg<1>(Invoke([](Context* ctx) {
ctx->complete(0);
})));
}
void expect_prepare_lock_complete(MockExclusiveLockImageCtx &mock_image_ctx) {
EXPECT_CALL(*mock_image_ctx.state, handle_prepare_lock_complete());
}
void expect_pre_acquire_request(MockPreAcquireRequest &pre_acquire_request,
int r) {
EXPECT_CALL(pre_acquire_request, send())
.WillOnce(CompleteRequest(&pre_acquire_request, r));
}
void expect_post_acquire_request(MockExclusiveLock *mock_exclusive_lock,
MockPostAcquireRequest &post_acquire_request,
int r) {
EXPECT_CALL(post_acquire_request, send())
.WillOnce(DoAll(FinishLockUnlock(&post_acquire_request),
CompleteRequest(&post_acquire_request, r)));
expect_set_state_post_acquiring(mock_exclusive_lock);
}
void expect_pre_release_request(MockPreReleaseRequest &pre_release_request,
int r) {
EXPECT_CALL(pre_release_request, send())
.WillOnce(CompleteRequest(&pre_release_request, r));
}
void expect_notify_request_lock(MockExclusiveLockImageCtx &mock_image_ctx,
MockExclusiveLock *mock_exclusive_lock) {
EXPECT_CALL(*mock_image_ctx.image_watcher, notify_request_lock());
}
void expect_notify_acquired_lock(MockExclusiveLockImageCtx &mock_image_ctx) {
EXPECT_CALL(*mock_image_ctx.image_watcher, notify_acquired_lock())
.Times(1);
}
void expect_notify_released_lock(MockExclusiveLockImageCtx &mock_image_ctx) {
EXPECT_CALL(*mock_image_ctx.image_watcher, notify_released_lock())
.Times(1);
}
void expect_flush_notifies(MockExclusiveLockImageCtx &mock_image_ctx) {
EXPECT_CALL(*mock_image_ctx.image_watcher, flush(_))
.WillOnce(CompleteContext(0, mock_image_ctx.image_ctx->op_work_queue));
}
void expect_shut_down(MockManagedLock *managed_lock) {
EXPECT_CALL(*managed_lock, shut_down(_))
.WillOnce(CompleteContext(0, static_cast<asio::ContextWQ*>(nullptr)));
}
void expect_accept_blocked_request(
MockExclusiveLockImageCtx &mock_image_ctx,
exclusive_lock::MockPolicy &policy,
exclusive_lock::OperationRequestType request_type, bool value) {
EXPECT_CALL(mock_image_ctx, get_exclusive_lock_policy())
.WillOnce(Return(&policy));
EXPECT_CALL(policy, accept_blocked_request(request_type))
.WillOnce(Return(value));
}
int when_init(MockExclusiveLockImageCtx &mock_image_ctx,
MockExclusiveLock *exclusive_lock) {
C_SaferCond ctx;
{
std::unique_lock owner_locker{mock_image_ctx.owner_lock};
exclusive_lock->init(mock_image_ctx.features, &ctx);
}
return ctx.wait();
}
int when_pre_acquire_lock_handler(MockManagedLock *managed_lock) {
C_SaferCond ctx;
managed_lock->pre_acquire_lock_handler(&ctx);
return ctx.wait();
}
int when_post_acquire_lock_handler(MockManagedLock *managed_lock, int r) {
C_SaferCond ctx;
managed_lock->post_acquire_lock_handler(r, &ctx);
return ctx.wait();
}
int when_pre_release_lock_handler(MockManagedLock *managed_lock,
bool shutting_down) {
C_SaferCond ctx;
managed_lock->pre_release_lock_handler(shutting_down, &ctx);
return ctx.wait();
}
int when_post_release_lock_handler(MockManagedLock *managed_lock,
bool shutting_down, int r) {
C_SaferCond ctx;
managed_lock->post_release_lock_handler(shutting_down, r, &ctx);
return ctx.wait();
}
int when_post_reacquire_lock_handler(MockManagedLock *managed_lock, int r) {
C_SaferCond ctx;
managed_lock->post_reacquire_lock_handler(r, &ctx);
return ctx.wait();
}
int when_shut_down(MockExclusiveLockImageCtx &mock_image_ctx,
MockExclusiveLock *exclusive_lock) {
C_SaferCond ctx;
{
std::unique_lock owner_locker{mock_image_ctx.owner_lock};
exclusive_lock->shut_down(&ctx);
}
return ctx.wait();
}
bool is_lock_owner(MockExclusiveLockImageCtx &mock_image_ctx,
MockExclusiveLock *exclusive_lock) {
std::shared_lock owner_locker{mock_image_ctx.owner_lock};
return exclusive_lock->is_lock_owner();
}
};
TEST_F(TestMockExclusiveLock, StateTransitions) {
REQUIRE_FEATURE(RBD_FEATURE_EXCLUSIVE_LOCK);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockExclusiveLockImageCtx mock_image_ctx(*ictx);
MockExclusiveLock *exclusive_lock = new MockExclusiveLock(mock_image_ctx);
expect_op_work_queue(mock_image_ctx);
BOOST_SCOPE_EXIT(&exclusive_lock) {
exclusive_lock->put();
} BOOST_SCOPE_EXIT_END
InSequence seq;
expect_set_state_initializing(exclusive_lock);
MockImageDispatch mock_image_dispatch;
expect_register_dispatch(mock_image_ctx);
expect_set_require_lock(mock_image_ctx, mock_image_dispatch, true);
expect_set_state_unlocked(exclusive_lock);
ASSERT_EQ(0, when_init(mock_image_ctx, exclusive_lock));
// (try) acquire lock
MockPreAcquireRequest try_lock_pre_acquire;
expect_pre_acquire_request(try_lock_pre_acquire, 0);
ASSERT_EQ(0, when_pre_acquire_lock_handler(exclusive_lock));
MockPostAcquireRequest try_lock_post_acquire;
expect_post_acquire_request(exclusive_lock, try_lock_post_acquire, 0);
expect_is_state_acquiring(exclusive_lock, true);
expect_notify_acquired_lock(mock_image_ctx);
expect_unset_require_lock(mock_image_dispatch);
ASSERT_EQ(0, when_post_acquire_lock_handler(exclusive_lock, 0));
// release lock
MockPreReleaseRequest pre_request_release;
expect_pre_release_request(pre_request_release, 0);
ASSERT_EQ(0, when_pre_release_lock_handler(exclusive_lock, false));
expect_is_state_pre_releasing(exclusive_lock, false);
expect_is_state_releasing(exclusive_lock, true);
expect_notify_released_lock(mock_image_ctx);
ASSERT_EQ(0, when_post_release_lock_handler(exclusive_lock, false, 0));
// (try) acquire lock
MockPreAcquireRequest request_lock_pre_acquire;
expect_pre_acquire_request(request_lock_pre_acquire, 0);
ASSERT_EQ(0, when_pre_acquire_lock_handler(exclusive_lock));
MockPostAcquireRequest request_lock_post_acquire;
expect_post_acquire_request(exclusive_lock, request_lock_post_acquire, 0);
expect_is_state_acquiring(exclusive_lock, true);
expect_notify_acquired_lock(mock_image_ctx);
expect_unset_require_lock(mock_image_dispatch);
ASSERT_EQ(0, when_post_acquire_lock_handler(exclusive_lock, 0));
// shut down (and release)
expect_shut_down(exclusive_lock);
expect_is_state_waiting_for_lock(exclusive_lock, false);
ASSERT_EQ(0, when_shut_down(mock_image_ctx, exclusive_lock));
MockPreReleaseRequest shutdown_pre_release;
expect_pre_release_request(shutdown_pre_release, 0);
ASSERT_EQ(0, when_pre_release_lock_handler(exclusive_lock, true));
expect_shut_down_dispatch(mock_image_ctx);
expect_notify_released_lock(mock_image_ctx);
ASSERT_EQ(0, when_post_release_lock_handler(exclusive_lock, true, 0));
}
TEST_F(TestMockExclusiveLock, TryLockAlreadyLocked) {
REQUIRE_FEATURE(RBD_FEATURE_EXCLUSIVE_LOCK);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockExclusiveLockImageCtx mock_image_ctx(*ictx);
MockExclusiveLock *exclusive_lock = new MockExclusiveLock(mock_image_ctx);
expect_op_work_queue(mock_image_ctx);
BOOST_SCOPE_EXIT(&exclusive_lock) {
exclusive_lock->put();
} BOOST_SCOPE_EXIT_END
expect_set_state_initializing(exclusive_lock);
MockImageDispatch mock_image_dispatch;
expect_register_dispatch(mock_image_ctx);
expect_set_require_lock(mock_image_ctx, mock_image_dispatch, true);
expect_set_state_unlocked(exclusive_lock);
ASSERT_EQ(0, when_init(mock_image_ctx, exclusive_lock));
// try acquire lock
MockPreAcquireRequest try_lock_pre_acquire;
expect_pre_acquire_request(try_lock_pre_acquire, 0);
ASSERT_EQ(0, when_pre_acquire_lock_handler(exclusive_lock));
expect_is_state_acquiring(exclusive_lock, true);
expect_prepare_lock_complete(mock_image_ctx);
expect_is_action_acquire_lock(exclusive_lock, false);
ASSERT_EQ(0, when_post_acquire_lock_handler(exclusive_lock, -EAGAIN));
}
TEST_F(TestMockExclusiveLock, TryLockError) {
REQUIRE_FEATURE(RBD_FEATURE_EXCLUSIVE_LOCK);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockExclusiveLockImageCtx mock_image_ctx(*ictx);
MockExclusiveLock *exclusive_lock = new MockExclusiveLock(mock_image_ctx);
expect_op_work_queue(mock_image_ctx);
BOOST_SCOPE_EXIT(&exclusive_lock) {
exclusive_lock->put();
} BOOST_SCOPE_EXIT_END
InSequence seq;
expect_set_state_initializing(exclusive_lock);
MockImageDispatch mock_image_dispatch;
expect_register_dispatch(mock_image_ctx);
expect_set_require_lock(mock_image_ctx, mock_image_dispatch, true);
expect_set_state_unlocked(exclusive_lock);
ASSERT_EQ(0, when_init(mock_image_ctx, exclusive_lock));
// try acquire lock
MockPreAcquireRequest try_lock_pre_acquire;
expect_pre_acquire_request(try_lock_pre_acquire, 0);
ASSERT_EQ(0, when_pre_acquire_lock_handler(exclusive_lock));
expect_is_state_acquiring(exclusive_lock, true);
expect_prepare_lock_complete(mock_image_ctx);
expect_is_action_acquire_lock(exclusive_lock, false);
ASSERT_EQ(-EBUSY, when_post_acquire_lock_handler(exclusive_lock, -EBUSY));
}
TEST_F(TestMockExclusiveLock, AcquireLockAlreadyLocked) {
REQUIRE_FEATURE(RBD_FEATURE_EXCLUSIVE_LOCK);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockExclusiveLockImageCtx mock_image_ctx(*ictx);
MockExclusiveLock *exclusive_lock = new MockExclusiveLock(mock_image_ctx);
expect_op_work_queue(mock_image_ctx);
BOOST_SCOPE_EXIT(&exclusive_lock) {
exclusive_lock->put();
} BOOST_SCOPE_EXIT_END
InSequence seq;
expect_set_state_initializing(exclusive_lock);
MockImageDispatch mock_image_dispatch;
expect_register_dispatch(mock_image_ctx);
expect_set_require_lock(mock_image_ctx, mock_image_dispatch, true);
expect_set_state_unlocked(exclusive_lock);
ASSERT_EQ(0, when_init(mock_image_ctx, exclusive_lock));
// acquire lock
MockPreAcquireRequest try_lock_pre_acquire;
expect_pre_acquire_request(try_lock_pre_acquire, 0);
ASSERT_EQ(0, when_pre_acquire_lock_handler(exclusive_lock));
expect_is_state_acquiring(exclusive_lock, true);
expect_prepare_lock_complete(mock_image_ctx);
expect_is_action_acquire_lock(exclusive_lock, true);
expect_set_state_waiting_for_lock(exclusive_lock);
expect_notify_request_lock(mock_image_ctx, exclusive_lock);
ASSERT_EQ(-ECANCELED, when_post_acquire_lock_handler(exclusive_lock,
-EAGAIN));
}
TEST_F(TestMockExclusiveLock, AcquireLockBusy) {
REQUIRE_FEATURE(RBD_FEATURE_EXCLUSIVE_LOCK);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockExclusiveLockImageCtx mock_image_ctx(*ictx);
MockExclusiveLock *exclusive_lock = new MockExclusiveLock(mock_image_ctx);
expect_op_work_queue(mock_image_ctx);
BOOST_SCOPE_EXIT(&exclusive_lock) {
exclusive_lock->put();
} BOOST_SCOPE_EXIT_END
InSequence seq;
expect_set_state_initializing(exclusive_lock);
MockImageDispatch mock_image_dispatch;
expect_register_dispatch(mock_image_ctx);
expect_set_require_lock(mock_image_ctx, mock_image_dispatch, true);
expect_set_state_unlocked(exclusive_lock);
ASSERT_EQ(0, when_init(mock_image_ctx, exclusive_lock));
// acquire lock
MockPreAcquireRequest try_lock_pre_acquire;
expect_pre_acquire_request(try_lock_pre_acquire, 0);
ASSERT_EQ(0, when_pre_acquire_lock_handler(exclusive_lock));
expect_is_state_acquiring(exclusive_lock, true);
expect_prepare_lock_complete(mock_image_ctx);
expect_is_action_acquire_lock(exclusive_lock, true);
expect_set_state_waiting_for_lock(exclusive_lock);
expect_notify_request_lock(mock_image_ctx, exclusive_lock);
ASSERT_EQ(-ECANCELED, when_post_acquire_lock_handler(exclusive_lock,
-EBUSY));
}
TEST_F(TestMockExclusiveLock, AcquireLockError) {
REQUIRE_FEATURE(RBD_FEATURE_EXCLUSIVE_LOCK);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockExclusiveLockImageCtx mock_image_ctx(*ictx);
MockExclusiveLock *exclusive_lock = new MockExclusiveLock(mock_image_ctx);
expect_op_work_queue(mock_image_ctx);
BOOST_SCOPE_EXIT(&exclusive_lock) {
exclusive_lock->put();
} BOOST_SCOPE_EXIT_END
InSequence seq;
expect_set_state_initializing(exclusive_lock);
MockImageDispatch mock_image_dispatch;
expect_register_dispatch(mock_image_ctx);
expect_set_require_lock(mock_image_ctx, mock_image_dispatch, true);
expect_set_state_unlocked(exclusive_lock);
ASSERT_EQ(0, when_init(mock_image_ctx, exclusive_lock));
// acquire lock
MockPreAcquireRequest try_lock_pre_acquire;
expect_pre_acquire_request(try_lock_pre_acquire, 0);
ASSERT_EQ(0, when_pre_acquire_lock_handler(exclusive_lock));
expect_is_state_acquiring(exclusive_lock, true);
expect_prepare_lock_complete(mock_image_ctx);
expect_is_action_acquire_lock(exclusive_lock, true);
ASSERT_EQ(-EBLOCKLISTED, when_post_acquire_lock_handler(exclusive_lock,
-EBLOCKLISTED));
}
TEST_F(TestMockExclusiveLock, PostAcquireLockError) {
REQUIRE_FEATURE(RBD_FEATURE_EXCLUSIVE_LOCK);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockExclusiveLockImageCtx mock_image_ctx(*ictx);
MockExclusiveLock *exclusive_lock = new MockExclusiveLock(mock_image_ctx);
expect_op_work_queue(mock_image_ctx);
BOOST_SCOPE_EXIT(&exclusive_lock) {
exclusive_lock->put();
} BOOST_SCOPE_EXIT_END
InSequence seq;
expect_set_state_initializing(exclusive_lock);
MockImageDispatch mock_image_dispatch;
expect_register_dispatch(mock_image_ctx);
expect_set_require_lock(mock_image_ctx, mock_image_dispatch, true);
expect_set_state_unlocked(exclusive_lock);
ASSERT_EQ(0, when_init(mock_image_ctx, exclusive_lock));
// (try) acquire lock
MockPreAcquireRequest request_lock_pre_acquire;
expect_pre_acquire_request(request_lock_pre_acquire, 0);
ASSERT_EQ(0, when_pre_acquire_lock_handler(exclusive_lock));
MockPostAcquireRequest request_lock_post_acquire;
expect_post_acquire_request(exclusive_lock, request_lock_post_acquire,
-EPERM);
expect_is_state_acquiring(exclusive_lock, true);
ASSERT_EQ(-EPERM, when_post_acquire_lock_handler(exclusive_lock, 0));
}
TEST_F(TestMockExclusiveLock, PreReleaseLockError) {
REQUIRE_FEATURE(RBD_FEATURE_EXCLUSIVE_LOCK);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockExclusiveLockImageCtx mock_image_ctx(*ictx);
MockExclusiveLock *exclusive_lock = new MockExclusiveLock(mock_image_ctx);
expect_op_work_queue(mock_image_ctx);
BOOST_SCOPE_EXIT(&exclusive_lock) {
exclusive_lock->put();
} BOOST_SCOPE_EXIT_END
InSequence seq;
expect_set_state_initializing(exclusive_lock);
MockImageDispatch mock_image_dispatch;
expect_register_dispatch(mock_image_ctx);
expect_set_require_lock(mock_image_ctx, mock_image_dispatch, true);
expect_set_state_unlocked(exclusive_lock);
ASSERT_EQ(0, when_init(mock_image_ctx, exclusive_lock));
// release lock
MockPreReleaseRequest pre_request_release;
expect_pre_release_request(pre_request_release, -EINVAL);
ASSERT_EQ(-EINVAL, when_pre_release_lock_handler(exclusive_lock, false));
expect_is_state_pre_releasing(exclusive_lock, true);
ASSERT_EQ(-EINVAL, when_post_release_lock_handler(exclusive_lock, false,
-EINVAL));
}
TEST_F(TestMockExclusiveLock, ReacquireLock) {
REQUIRE_FEATURE(RBD_FEATURE_EXCLUSIVE_LOCK);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockExclusiveLockImageCtx mock_image_ctx(*ictx);
MockExclusiveLock *exclusive_lock = new MockExclusiveLock(mock_image_ctx);
expect_op_work_queue(mock_image_ctx);
BOOST_SCOPE_EXIT(&exclusive_lock) {
exclusive_lock->put();
} BOOST_SCOPE_EXIT_END
InSequence seq;
expect_set_state_initializing(exclusive_lock);
MockImageDispatch mock_image_dispatch;
expect_register_dispatch(mock_image_ctx);
expect_set_require_lock(mock_image_ctx, mock_image_dispatch, true);
expect_set_state_unlocked(exclusive_lock);
ASSERT_EQ(0, when_init(mock_image_ctx, exclusive_lock));
// (try) acquire lock
MockPreAcquireRequest try_lock_pre_acquire;
expect_pre_acquire_request(try_lock_pre_acquire, 0);
ASSERT_EQ(0, when_pre_acquire_lock_handler(exclusive_lock));
MockPostAcquireRequest try_lock_post_acquire;
expect_post_acquire_request(exclusive_lock, try_lock_post_acquire, 0);
expect_is_state_acquiring(exclusive_lock, true);
expect_notify_acquired_lock(mock_image_ctx);
expect_unset_require_lock(mock_image_dispatch);
ASSERT_EQ(0, when_post_acquire_lock_handler(exclusive_lock, 0));
// reacquire lock
expect_notify_acquired_lock(mock_image_ctx);
ASSERT_EQ(0, when_post_reacquire_lock_handler(exclusive_lock, 0));
// shut down (and release)
expect_shut_down(exclusive_lock);
expect_is_state_waiting_for_lock(exclusive_lock, false);
ASSERT_EQ(0, when_shut_down(mock_image_ctx, exclusive_lock));
MockPreReleaseRequest shutdown_pre_release;
expect_pre_release_request(shutdown_pre_release, 0);
ASSERT_EQ(0, when_pre_release_lock_handler(exclusive_lock, true));
expect_shut_down_dispatch(mock_image_ctx);
expect_notify_released_lock(mock_image_ctx);
ASSERT_EQ(0, when_post_release_lock_handler(exclusive_lock, true, 0));
}
TEST_F(TestMockExclusiveLock, BlockRequests) {
REQUIRE_FEATURE(RBD_FEATURE_EXCLUSIVE_LOCK);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockExclusiveLockImageCtx mock_image_ctx(*ictx);
MockExclusiveLock *exclusive_lock = new MockExclusiveLock(mock_image_ctx);
exclusive_lock::MockPolicy mock_exclusive_lock_policy;
expect_op_work_queue(mock_image_ctx);
BOOST_SCOPE_EXIT(&exclusive_lock) {
exclusive_lock->put();
} BOOST_SCOPE_EXIT_END
InSequence seq;
expect_set_state_initializing(exclusive_lock);
MockImageDispatch mock_image_dispatch;
expect_register_dispatch(mock_image_ctx);
expect_set_require_lock(mock_image_ctx, mock_image_dispatch, true);
expect_set_state_unlocked(exclusive_lock);
ASSERT_EQ(0, when_init(mock_image_ctx, exclusive_lock));
int ret_val;
expect_is_state_shutdown(exclusive_lock, false);
expect_is_state_locked(exclusive_lock, true);
ASSERT_TRUE(exclusive_lock->accept_request(
exclusive_lock::OPERATION_REQUEST_TYPE_GENERAL, &ret_val));
ASSERT_EQ(0, ret_val);
exclusive_lock->block_requests(-EROFS);
expect_is_state_shutdown(exclusive_lock, false);
expect_is_state_locked(exclusive_lock, true);
expect_accept_blocked_request(mock_image_ctx, mock_exclusive_lock_policy,
exclusive_lock::OPERATION_REQUEST_TYPE_GENERAL,
false);
ASSERT_FALSE(exclusive_lock->accept_request(
exclusive_lock::OPERATION_REQUEST_TYPE_GENERAL, &ret_val));
ASSERT_EQ(-EROFS, ret_val);
expect_is_state_shutdown(exclusive_lock, false);
expect_is_state_locked(exclusive_lock, true);
expect_accept_blocked_request(
mock_image_ctx, mock_exclusive_lock_policy,
exclusive_lock::OPERATION_REQUEST_TYPE_TRASH_SNAP_REMOVE, true);
ASSERT_TRUE(exclusive_lock->accept_request(
exclusive_lock::OPERATION_REQUEST_TYPE_TRASH_SNAP_REMOVE,
&ret_val));
ASSERT_EQ(0, ret_val);
exclusive_lock->unblock_requests();
expect_is_state_shutdown(exclusive_lock, false);
expect_is_state_locked(exclusive_lock, true);
ASSERT_TRUE(exclusive_lock->accept_request(
exclusive_lock::OPERATION_REQUEST_TYPE_GENERAL, &ret_val));
ASSERT_EQ(0, ret_val);
}
} // namespace librbd
|