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());
58 if (ictx
.test_features(RBD_FEATURE_DIRTY_CACHE
)) {
59 EXPECT_CALL(get_mock_io_ctx(ictx
.md_ctx
),
60 exec(ictx
.header_oid
, _
, StrEq("rbd"), StrEq("set_features"), _
, _
, _
, _
))
61 .WillOnce(DoDefault());
62 EXPECT_CALL(get_mock_io_ctx(ictx
.md_ctx
),
63 exec(ictx
.header_oid
, _
, StrEq("rbd"), StrEq("metadata_set"), _
, _
, _
, _
))
64 .WillOnce(DoDefault());
65 EXPECT_CALL(get_mock_io_ctx(ictx
.md_ctx
),
66 exec(ictx
.header_oid
, _
, StrEq("rbd"), StrEq("metadata_remove"), _
, _
, _
, _
))
67 .WillOnce(DoDefault());
71 void TestMockFixture::expect_op_work_queue(librbd::MockImageCtx
&mock_image_ctx
) {
72 EXPECT_CALL(*mock_image_ctx
.op_work_queue
, queue(_
, _
))
73 .WillRepeatedly(DispatchContext(
74 mock_image_ctx
.image_ctx
->op_work_queue
));
77 void TestMockFixture::initialize_features(librbd::ImageCtx
*ictx
,
78 librbd::MockImageCtx
&mock_image_ctx
,
79 librbd::MockExclusiveLock
&mock_exclusive_lock
,
80 librbd::MockJournal
&mock_journal
,
81 librbd::MockObjectMap
&mock_object_map
) {
82 if (ictx
->test_features(RBD_FEATURE_EXCLUSIVE_LOCK
)) {
83 mock_image_ctx
.exclusive_lock
= &mock_exclusive_lock
;
85 if (ictx
->test_features(RBD_FEATURE_JOURNALING
)) {
86 mock_image_ctx
.journal
= &mock_journal
;
88 if (ictx
->test_features(RBD_FEATURE_OBJECT_MAP
)) {
89 mock_image_ctx
.object_map
= &mock_object_map
;
93 void TestMockFixture::expect_is_journal_appending(librbd::MockJournal
&mock_journal
,
95 EXPECT_CALL(mock_journal
, is_journal_appending()).WillOnce(Return(appending
));
98 void TestMockFixture::expect_is_journal_replaying(librbd::MockJournal
&mock_journal
) {
99 EXPECT_CALL(mock_journal
, is_journal_replaying()).WillOnce(Return(false));
102 void TestMockFixture::expect_is_journal_ready(librbd::MockJournal
&mock_journal
) {
103 EXPECT_CALL(mock_journal
, is_journal_ready()).WillOnce(Return(true));
106 void TestMockFixture::expect_allocate_op_tid(librbd::MockImageCtx
&mock_image_ctx
) {
107 if (mock_image_ctx
.journal
!= nullptr) {
108 EXPECT_CALL(*mock_image_ctx
.journal
, allocate_op_tid())
109 .WillOnce(Return(1U));
113 void TestMockFixture::expect_append_op_event(librbd::MockImageCtx
&mock_image_ctx
,
114 bool can_affect_io
, int r
) {
115 if (mock_image_ctx
.journal
!= nullptr) {
117 expect_is_journal_replaying(*mock_image_ctx
.journal
);
119 expect_is_journal_appending(*mock_image_ctx
.journal
, true);
120 expect_allocate_op_tid(mock_image_ctx
);
121 EXPECT_CALL(*mock_image_ctx
.journal
, append_op_event_mock(_
, _
, _
))
122 .WillOnce(WithArg
<2>(CompleteContext(r
, mock_image_ctx
.image_ctx
->op_work_queue
)));
126 void TestMockFixture::expect_commit_op_event(librbd::MockImageCtx
&mock_image_ctx
, int r
) {
127 if (mock_image_ctx
.journal
!= nullptr) {
128 expect_is_journal_appending(*mock_image_ctx
.journal
, true);
129 expect_is_journal_ready(*mock_image_ctx
.journal
);
130 EXPECT_CALL(*mock_image_ctx
.journal
, commit_op_event(1U, r
, _
))
131 .WillOnce(WithArg
<2>(CompleteContext(r
, mock_image_ctx
.image_ctx
->op_work_queue
)));