1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 #include "test/librbd/test_mock_fixture.h"
5 #include "test/librbd/mock/MockImageCtx.h"
6 #include "test/librados_test_stub/LibradosTestStub.h"
7 #include "test/librados_test_stub/MockTestMemCluster.h"
9 // template definitions
10 #include "librbd/AsyncRequest.cc"
11 #include "librbd/AsyncObjectThrottle.cc"
12 #include "librbd/operation/Request.cc"
14 template class librbd::AsyncRequest
<librbd::MockImageCtx
>;
15 template class librbd::AsyncObjectThrottle
<librbd::MockImageCtx
>;
16 template class librbd::operation::Request
<librbd::MockImageCtx
>;
19 using ::testing::DoDefault
;
20 using ::testing::Return
;
21 using ::testing::StrEq
;
22 using ::testing::WithArg
;
24 TestMockFixture::TestClusterRef
TestMockFixture::s_test_cluster
;
26 void TestMockFixture::SetUpTestCase() {
27 s_test_cluster
= librados_test_stub::get_cluster();
29 // use a mock version of the in-memory cluster
30 librados_test_stub::set_cluster(boost::shared_ptr
<librados::TestCluster
>(
31 new ::testing::NiceMock
<librados::MockTestMemCluster
>()));
32 TestFixture::SetUpTestCase();
35 void TestMockFixture::TearDownTestCase() {
36 TestFixture::TearDownTestCase();
37 librados_test_stub::set_cluster(s_test_cluster
);
40 void TestMockFixture::TearDown() {
41 // Mock rados client lives across tests -- reset it to initial state
42 librados::MockTestMemRadosClient
*mock_rados_client
=
43 get_mock_io_ctx(m_ioctx
).get_mock_rados_client();
44 ASSERT_TRUE(mock_rados_client
!= nullptr);
46 ::testing::Mock::VerifyAndClear(mock_rados_client
);
47 mock_rados_client
->default_to_dispatch();
48 dynamic_cast<librados::MockTestMemCluster
*>(
49 librados_test_stub::get_cluster().get())->default_to_dispatch();
51 TestFixture::TearDown();
54 void TestMockFixture::expect_unlock_exclusive_lock(librbd::ImageCtx
&ictx
) {
55 EXPECT_CALL(get_mock_io_ctx(ictx
.md_ctx
),
56 exec(_
, _
, StrEq("lock"), StrEq("unlock"), _
, _
, _
))
57 .WillRepeatedly(DoDefault());
60 void TestMockFixture::expect_op_work_queue(librbd::MockImageCtx
&mock_image_ctx
) {
61 EXPECT_CALL(*mock_image_ctx
.op_work_queue
, queue(_
, _
))
62 .WillRepeatedly(DispatchContext(
63 mock_image_ctx
.image_ctx
->op_work_queue
));
66 void TestMockFixture::initialize_features(librbd::ImageCtx
*ictx
,
67 librbd::MockImageCtx
&mock_image_ctx
,
68 librbd::MockExclusiveLock
&mock_exclusive_lock
,
69 librbd::MockJournal
&mock_journal
,
70 librbd::MockObjectMap
&mock_object_map
) {
71 if (ictx
->test_features(RBD_FEATURE_EXCLUSIVE_LOCK
)) {
72 mock_image_ctx
.exclusive_lock
= &mock_exclusive_lock
;
74 if (ictx
->test_features(RBD_FEATURE_JOURNALING
)) {
75 mock_image_ctx
.journal
= &mock_journal
;
77 if (ictx
->test_features(RBD_FEATURE_OBJECT_MAP
)) {
78 mock_image_ctx
.object_map
= &mock_object_map
;
82 void TestMockFixture::expect_is_journal_appending(librbd::MockJournal
&mock_journal
,
84 EXPECT_CALL(mock_journal
, is_journal_appending()).WillOnce(Return(appending
));
87 void TestMockFixture::expect_is_journal_replaying(librbd::MockJournal
&mock_journal
) {
88 EXPECT_CALL(mock_journal
, is_journal_replaying()).WillOnce(Return(false));
91 void TestMockFixture::expect_is_journal_ready(librbd::MockJournal
&mock_journal
) {
92 EXPECT_CALL(mock_journal
, is_journal_ready()).WillOnce(Return(true));
95 void TestMockFixture::expect_allocate_op_tid(librbd::MockImageCtx
&mock_image_ctx
) {
96 if (mock_image_ctx
.journal
!= nullptr) {
97 EXPECT_CALL(*mock_image_ctx
.journal
, allocate_op_tid())
98 .WillOnce(Return(1U));
102 void TestMockFixture::expect_append_op_event(librbd::MockImageCtx
&mock_image_ctx
,
103 bool can_affect_io
, int r
) {
104 if (mock_image_ctx
.journal
!= nullptr) {
106 expect_is_journal_replaying(*mock_image_ctx
.journal
);
108 expect_is_journal_appending(*mock_image_ctx
.journal
, true);
109 expect_allocate_op_tid(mock_image_ctx
);
110 EXPECT_CALL(*mock_image_ctx
.journal
, append_op_event_mock(_
, _
, _
))
111 .WillOnce(WithArg
<2>(CompleteContext(r
, mock_image_ctx
.image_ctx
->op_work_queue
)));
115 void TestMockFixture::expect_commit_op_event(librbd::MockImageCtx
&mock_image_ctx
, int r
) {
116 if (mock_image_ctx
.journal
!= nullptr) {
117 expect_is_journal_appending(*mock_image_ctx
.journal
, true);
118 expect_is_journal_ready(*mock_image_ctx
.journal
);
119 EXPECT_CALL(*mock_image_ctx
.journal
, commit_op_event(1U, r
, _
))
120 .WillOnce(WithArg
<2>(CompleteContext(r
, mock_image_ctx
.image_ctx
->op_work_queue
)));