]> git.proxmox.com Git - ceph.git/blob - ceph/src/test/librbd/test_mock_fixture.cc
import ceph 15.2.10
[ceph.git] / ceph / src / test / librbd / test_mock_fixture.cc
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3
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"
8
9 // template definitions
10 #include "librbd/AsyncRequest.cc"
11 #include "librbd/AsyncObjectThrottle.cc"
12 #include "librbd/operation/Request.cc"
13
14 template class librbd::AsyncRequest<librbd::MockImageCtx>;
15 template class librbd::AsyncObjectThrottle<librbd::MockImageCtx>;
16 template class librbd::operation::Request<librbd::MockImageCtx>;
17
18 using ::testing::_;
19 using ::testing::DoDefault;
20 using ::testing::Return;
21 using ::testing::StrEq;
22 using ::testing::WithArg;
23
24 TestMockFixture::TestClusterRef TestMockFixture::s_test_cluster;
25
26 void TestMockFixture::SetUpTestCase() {
27 s_test_cluster = librados_test_stub::get_cluster();
28
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();
33 }
34
35 void TestMockFixture::TearDownTestCase() {
36 TestFixture::TearDownTestCase();
37 librados_test_stub::set_cluster(s_test_cluster);
38 }
39
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);
45
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();
50
51 TestFixture::TearDown();
52 }
53
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 }
59
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));
64 }
65
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;
73 }
74 if (ictx->test_features(RBD_FEATURE_JOURNALING)) {
75 mock_image_ctx.journal = &mock_journal;
76 }
77 if (ictx->test_features(RBD_FEATURE_OBJECT_MAP)) {
78 mock_image_ctx.object_map = &mock_object_map;
79 }
80 }
81
82 void TestMockFixture::expect_is_journal_appending(librbd::MockJournal &mock_journal,
83 bool appending) {
84 EXPECT_CALL(mock_journal, is_journal_appending()).WillOnce(Return(appending));
85 }
86
87 void TestMockFixture::expect_is_journal_replaying(librbd::MockJournal &mock_journal) {
88 EXPECT_CALL(mock_journal, is_journal_replaying()).WillOnce(Return(false));
89 }
90
91 void TestMockFixture::expect_is_journal_ready(librbd::MockJournal &mock_journal) {
92 EXPECT_CALL(mock_journal, is_journal_ready()).WillOnce(Return(true));
93 }
94
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));
99 }
100 }
101
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) {
105 if (can_affect_io) {
106 expect_is_journal_replaying(*mock_image_ctx.journal);
107 }
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)));
112 }
113 }
114
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)));
121 }
122 }
123