]>
Commit | Line | Data |
---|---|---|
11fdf7f2 TL |
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/test_support.h" | |
6 | #include "test/librbd/mock/MockImageCtx.h" | |
7 | #include "test/journal/mock/MockJournaler.h" | |
11fdf7f2 TL |
8 | #include "cls/journal/cls_journal_types.h" |
9 | #include "librbd/journal/CreateRequest.h" | |
10 | #include "librbd/journal/RemoveRequest.h" | |
11 | #include "librbd/journal/ResetRequest.h" | |
12 | ||
13 | namespace librbd { | |
14 | ||
15 | namespace { | |
16 | ||
17 | struct MockTestImageCtx : public MockImageCtx { | |
18 | MockTestImageCtx(librbd::ImageCtx& image_ctx) : MockImageCtx(image_ctx) { | |
19 | } | |
20 | }; | |
21 | ||
22 | } // anonymous namespace | |
23 | ||
24 | namespace journal { | |
25 | ||
26 | template <> | |
27 | struct TypeTraits<MockTestImageCtx> { | |
28 | typedef ::journal::MockJournalerProxy Journaler; | |
29 | }; | |
30 | ||
31 | template <> | |
32 | struct CreateRequest<MockTestImageCtx> { | |
33 | static CreateRequest* s_instance; | |
34 | Context* on_finish = nullptr; | |
35 | ||
36 | static CreateRequest* create(IoCtx &ioctx, const std::string &imageid, | |
37 | uint8_t order, uint8_t splay_width, | |
38 | const std::string &object_pool, | |
39 | uint64_t tag_class, TagData &tag_data, | |
40 | const std::string &client_id, | |
41 | ContextWQ *op_work_queue, Context *on_finish) { | |
42 | ceph_assert(s_instance != nullptr); | |
43 | s_instance->on_finish = on_finish; | |
44 | return s_instance; | |
45 | } | |
46 | ||
47 | MOCK_METHOD0(send, void()); | |
48 | ||
49 | CreateRequest() { | |
50 | s_instance = this; | |
51 | } | |
52 | }; | |
53 | ||
54 | template <> | |
55 | struct RemoveRequest<MockTestImageCtx> { | |
56 | static RemoveRequest* s_instance; | |
57 | Context* on_finish = nullptr; | |
58 | ||
59 | static RemoveRequest* create(IoCtx &ioctx, const std::string &image_id, | |
60 | const std::string &client_id, | |
61 | ContextWQ *op_work_queue, Context *on_finish) { | |
62 | ceph_assert(s_instance != nullptr); | |
63 | s_instance->on_finish = on_finish; | |
64 | return s_instance; | |
65 | } | |
66 | ||
67 | MOCK_METHOD0(send, void()); | |
68 | ||
69 | RemoveRequest() { | |
70 | s_instance = this; | |
71 | } | |
72 | }; | |
73 | ||
74 | CreateRequest<MockTestImageCtx>* CreateRequest<MockTestImageCtx>::s_instance = nullptr; | |
75 | RemoveRequest<MockTestImageCtx>* RemoveRequest<MockTestImageCtx>::s_instance = nullptr; | |
76 | ||
77 | } // namespace journal | |
78 | } // namespace librbd | |
79 | ||
80 | #include "librbd/journal/ResetRequest.cc" | |
81 | ||
82 | namespace librbd { | |
83 | namespace journal { | |
84 | ||
85 | using ::testing::_; | |
86 | using ::testing::DoAll; | |
87 | using ::testing::InSequence; | |
88 | using ::testing::Invoke; | |
89 | using ::testing::Return; | |
90 | using ::testing::WithArg; | |
91 | ||
92 | class TestMockJournalResetRequest : public TestMockFixture { | |
93 | public: | |
94 | typedef ResetRequest<MockTestImageCtx> MockResetRequest; | |
95 | typedef CreateRequest<MockTestImageCtx> MockCreateRequest; | |
96 | typedef RemoveRequest<MockTestImageCtx> MockRemoveRequest; | |
97 | ||
98 | void expect_construct_journaler(::journal::MockJournaler &mock_journaler) { | |
99 | EXPECT_CALL(mock_journaler, construct()); | |
100 | } | |
101 | ||
102 | void expect_init_journaler(::journal::MockJournaler &mock_journaler, int r) { | |
103 | EXPECT_CALL(mock_journaler, init(_)) | |
104 | .WillOnce(CompleteContext(r, static_cast<ContextWQ*>(NULL))); | |
105 | } | |
106 | ||
107 | void expect_get_metadata(::journal::MockJournaler& mock_journaler) { | |
108 | EXPECT_CALL(mock_journaler, get_metadata(_, _, _)) | |
109 | .WillOnce(Invoke([](uint8_t* order, uint8_t* splay_width, | |
110 | int64_t* pool_id) { | |
111 | *order = 24; | |
112 | *splay_width = 4; | |
113 | *pool_id = -1; | |
114 | })); | |
115 | } | |
116 | ||
117 | void expect_shut_down_journaler(::journal::MockJournaler &mock_journaler, | |
118 | int r) { | |
119 | EXPECT_CALL(mock_journaler, shut_down(_)) | |
120 | .WillOnce(CompleteContext(r, static_cast<ContextWQ*>(NULL))); | |
121 | } | |
122 | ||
123 | void expect_remove(MockRemoveRequest& mock_remove_request, int r) { | |
124 | EXPECT_CALL(mock_remove_request, send()) | |
125 | .WillOnce(Invoke([&mock_remove_request, r]() { | |
126 | mock_remove_request.on_finish->complete(r); | |
127 | })); | |
128 | } | |
129 | ||
130 | void expect_create(MockCreateRequest& mock_create_request, int r) { | |
131 | EXPECT_CALL(mock_create_request, send()) | |
132 | .WillOnce(Invoke([&mock_create_request, r]() { | |
133 | mock_create_request.on_finish->complete(r); | |
134 | })); | |
135 | } | |
136 | }; | |
137 | ||
138 | TEST_F(TestMockJournalResetRequest, Success) { | |
139 | REQUIRE_FEATURE(RBD_FEATURE_JOURNALING); | |
140 | ||
141 | librbd::ImageCtx *ictx; | |
142 | ASSERT_EQ(0, open_image(m_image_name, &ictx)); | |
143 | ||
144 | InSequence seq; | |
145 | ::journal::MockJournaler mock_journaler; | |
146 | expect_construct_journaler(mock_journaler); | |
147 | expect_init_journaler(mock_journaler, 0); | |
148 | expect_get_metadata(mock_journaler); | |
149 | expect_shut_down_journaler(mock_journaler, 0); | |
150 | ||
151 | MockRemoveRequest mock_remove_request; | |
152 | expect_remove(mock_remove_request, 0); | |
153 | ||
154 | MockCreateRequest mock_create_request; | |
155 | expect_create(mock_create_request, 0); | |
156 | ||
157 | C_SaferCond ctx; | |
158 | auto req = MockResetRequest::create(m_ioctx, "image id", | |
159 | Journal<>::IMAGE_CLIENT_ID, | |
160 | Journal<>::LOCAL_MIRROR_UUID, | |
161 | ictx->op_work_queue , &ctx); | |
162 | req->send(); | |
163 | ASSERT_EQ(0, ctx.wait()); | |
164 | } | |
165 | ||
166 | TEST_F(TestMockJournalResetRequest, InitError) { | |
167 | REQUIRE_FEATURE(RBD_FEATURE_JOURNALING); | |
168 | ||
169 | librbd::ImageCtx *ictx; | |
170 | ASSERT_EQ(0, open_image(m_image_name, &ictx)); | |
171 | ||
172 | InSequence seq; | |
173 | ::journal::MockJournaler mock_journaler; | |
174 | expect_construct_journaler(mock_journaler); | |
175 | expect_init_journaler(mock_journaler, -EINVAL); | |
176 | expect_shut_down_journaler(mock_journaler, 0); | |
177 | ||
178 | C_SaferCond ctx; | |
179 | auto req = MockResetRequest::create(m_ioctx, "image id", | |
180 | Journal<>::IMAGE_CLIENT_ID, | |
181 | Journal<>::LOCAL_MIRROR_UUID, | |
182 | ictx->op_work_queue , &ctx); | |
183 | req->send(); | |
184 | ASSERT_EQ(-EINVAL, ctx.wait()); | |
185 | } | |
186 | ||
187 | TEST_F(TestMockJournalResetRequest, ShutDownError) { | |
188 | REQUIRE_FEATURE(RBD_FEATURE_JOURNALING); | |
189 | ||
190 | librbd::ImageCtx *ictx; | |
191 | ASSERT_EQ(0, open_image(m_image_name, &ictx)); | |
192 | ||
193 | InSequence seq; | |
194 | ::journal::MockJournaler mock_journaler; | |
195 | expect_construct_journaler(mock_journaler); | |
196 | expect_init_journaler(mock_journaler, 0); | |
197 | expect_get_metadata(mock_journaler); | |
198 | expect_shut_down_journaler(mock_journaler, -EINVAL); | |
199 | ||
200 | C_SaferCond ctx; | |
201 | auto req = MockResetRequest::create(m_ioctx, "image id", | |
202 | Journal<>::IMAGE_CLIENT_ID, | |
203 | Journal<>::LOCAL_MIRROR_UUID, | |
204 | ictx->op_work_queue , &ctx); | |
205 | req->send(); | |
206 | ASSERT_EQ(-EINVAL, ctx.wait()); | |
207 | } | |
208 | ||
209 | TEST_F(TestMockJournalResetRequest, RemoveError) { | |
210 | REQUIRE_FEATURE(RBD_FEATURE_JOURNALING); | |
211 | ||
212 | librbd::ImageCtx *ictx; | |
213 | ASSERT_EQ(0, open_image(m_image_name, &ictx)); | |
214 | ||
215 | InSequence seq; | |
216 | ::journal::MockJournaler mock_journaler; | |
217 | expect_construct_journaler(mock_journaler); | |
218 | expect_init_journaler(mock_journaler, 0); | |
219 | expect_get_metadata(mock_journaler); | |
220 | expect_shut_down_journaler(mock_journaler, 0); | |
221 | ||
222 | MockRemoveRequest mock_remove_request; | |
223 | expect_remove(mock_remove_request, -EINVAL); | |
224 | ||
225 | C_SaferCond ctx; | |
226 | auto req = MockResetRequest::create(m_ioctx, "image id", | |
227 | Journal<>::IMAGE_CLIENT_ID, | |
228 | Journal<>::LOCAL_MIRROR_UUID, | |
229 | ictx->op_work_queue , &ctx); | |
230 | req->send(); | |
231 | ASSERT_EQ(-EINVAL, ctx.wait()); | |
232 | } | |
233 | ||
234 | TEST_F(TestMockJournalResetRequest, CreateError) { | |
235 | REQUIRE_FEATURE(RBD_FEATURE_JOURNALING); | |
236 | ||
237 | librbd::ImageCtx *ictx; | |
238 | ASSERT_EQ(0, open_image(m_image_name, &ictx)); | |
239 | ||
240 | InSequence seq; | |
241 | ::journal::MockJournaler mock_journaler; | |
242 | expect_construct_journaler(mock_journaler); | |
243 | expect_init_journaler(mock_journaler, 0); | |
244 | expect_get_metadata(mock_journaler); | |
245 | expect_shut_down_journaler(mock_journaler, 0); | |
246 | ||
247 | MockRemoveRequest mock_remove_request; | |
248 | expect_remove(mock_remove_request, 0); | |
249 | ||
250 | MockCreateRequest mock_create_request; | |
251 | expect_create(mock_create_request, -EINVAL); | |
252 | ||
253 | C_SaferCond ctx; | |
254 | auto req = MockResetRequest::create(m_ioctx, "image id", | |
255 | Journal<>::IMAGE_CLIENT_ID, | |
256 | Journal<>::LOCAL_MIRROR_UUID, | |
257 | ictx->op_work_queue , &ctx); | |
258 | req->send(); | |
259 | ASSERT_EQ(-EINVAL, ctx.wait()); | |
260 | } | |
261 | ||
262 | } // namespace journal | |
263 | } // namespace librbd |