Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / 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 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
49   TestFixture::TearDown();
50 }
51
52 void TestMockFixture::expect_unlock_exclusive_lock(librbd::ImageCtx &ictx) {
53   EXPECT_CALL(get_mock_io_ctx(ictx.md_ctx),
54               exec(_, _, StrEq("lock"), StrEq("unlock"), _, _, _))
55                 .WillRepeatedly(DoDefault());
56 }
57
58 void TestMockFixture::expect_op_work_queue(librbd::MockImageCtx &mock_image_ctx) {
59   EXPECT_CALL(*mock_image_ctx.op_work_queue, queue(_, _))
60                 .WillRepeatedly(DispatchContext(
61                   mock_image_ctx.image_ctx->op_work_queue));
62 }
63
64 void TestMockFixture::initialize_features(librbd::ImageCtx *ictx,
65                                           librbd::MockImageCtx &mock_image_ctx,
66                                           librbd::MockExclusiveLock &mock_exclusive_lock,
67                                           librbd::MockJournal &mock_journal,
68                                           librbd::MockObjectMap &mock_object_map) {
69   if (ictx->test_features(RBD_FEATURE_EXCLUSIVE_LOCK)) {
70     mock_image_ctx.exclusive_lock = &mock_exclusive_lock;
71   }
72   if (ictx->test_features(RBD_FEATURE_JOURNALING)) {
73     mock_image_ctx.journal = &mock_journal;
74   }
75   if (ictx->test_features(RBD_FEATURE_OBJECT_MAP)) {
76     mock_image_ctx.object_map = &mock_object_map;
77   }
78 }
79
80 void TestMockFixture::expect_is_journal_appending(librbd::MockJournal &mock_journal,
81                                                   bool appending) {
82   EXPECT_CALL(mock_journal, is_journal_appending()).WillOnce(Return(appending));
83 }
84
85 void TestMockFixture::expect_is_journal_replaying(librbd::MockJournal &mock_journal) {
86   EXPECT_CALL(mock_journal, is_journal_replaying()).WillOnce(Return(false));
87 }
88
89 void TestMockFixture::expect_is_journal_ready(librbd::MockJournal &mock_journal) {
90   EXPECT_CALL(mock_journal, is_journal_ready()).WillOnce(Return(true));
91 }
92
93 void TestMockFixture::expect_allocate_op_tid(librbd::MockImageCtx &mock_image_ctx) {
94   if (mock_image_ctx.journal != nullptr) {
95     EXPECT_CALL(*mock_image_ctx.journal, allocate_op_tid())
96                   .WillOnce(Return(1U));
97   }
98 }
99
100 void TestMockFixture::expect_append_op_event(librbd::MockImageCtx &mock_image_ctx,
101                                              bool can_affect_io, int r) {
102   if (mock_image_ctx.journal != nullptr) {
103     if (can_affect_io) {
104       expect_is_journal_replaying(*mock_image_ctx.journal);
105     }
106     expect_is_journal_appending(*mock_image_ctx.journal, true);
107     expect_allocate_op_tid(mock_image_ctx);
108     EXPECT_CALL(*mock_image_ctx.journal, append_op_event_mock(_, _, _))
109                   .WillOnce(WithArg<2>(CompleteContext(r, mock_image_ctx.image_ctx->op_work_queue)));
110   }
111 }
112
113 void TestMockFixture::expect_commit_op_event(librbd::MockImageCtx &mock_image_ctx, int r) {
114   if (mock_image_ctx.journal != nullptr) {
115     expect_is_journal_appending(*mock_image_ctx.journal, true);
116     expect_is_journal_ready(*mock_image_ctx.journal);
117     EXPECT_CALL(*mock_image_ctx.journal, commit_op_event(1U, r, _))
118                   .WillOnce(WithArg<2>(CompleteContext(r, mock_image_ctx.image_ctx->op_work_queue)));
119   }
120 }
121