1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 #include "test/rbd_mirror/test_mock_fixture.h"
5 #include "cls/rbd/cls_rbd_types.h"
6 #include "librbd/journal/TypeTraits.h"
7 #include "tools/rbd_mirror/image_replayer/PrepareLocalImageRequest.h"
8 #include "test/journal/mock/MockJournaler.h"
9 #include "test/librados_test_stub/MockTestMemIoCtxImpl.h"
10 #include "test/librbd/mock/MockImageCtx.h"
11 #include "test/librbd/mock/MockJournal.h"
17 struct MockTestImageCtx
: public librbd::MockImageCtx
{
18 MockTestImageCtx(librbd::ImageCtx
&image_ctx
)
19 : librbd::MockImageCtx(image_ctx
) {
23 } // anonymous namespace
26 // template definitions
27 #include "tools/rbd_mirror/image_replayer/PrepareLocalImageRequest.cc"
31 namespace image_replayer
{
34 using ::testing::DoAll
;
35 using ::testing::InSequence
;
36 using ::testing::Invoke
;
37 using ::testing::Return
;
38 using ::testing::StrEq
;
39 using ::testing::WithArg
;
40 using ::testing::WithArgs
;
42 class TestMockImageReplayerPrepareLocalImageRequest
: public TestMockFixture
{
44 typedef PrepareLocalImageRequest
<librbd::MockTestImageCtx
> MockPrepareLocalImageRequest
;
46 void expect_mirror_image_get_image_id(librados::IoCtx
&io_ctx
,
47 const std::string
&image_id
, int r
) {
49 ::encode(image_id
, bl
);
51 EXPECT_CALL(get_mock_io_ctx(io_ctx
),
52 exec(RBD_MIRRORING
, _
, StrEq("rbd"), StrEq("mirror_image_get_image_id"), _
, _
, _
))
53 .WillOnce(DoAll(WithArg
<5>(Invoke([bl
](bufferlist
*out_bl
) {
59 void expect_mirror_image_get(librados::IoCtx
&io_ctx
,
60 cls::rbd::MirrorImageState state
,
61 const std::string
&global_id
, int r
) {
62 cls::rbd::MirrorImage mirror_image
;
63 mirror_image
.state
= state
;
64 mirror_image
.global_image_id
= global_id
;
67 ::encode(mirror_image
, bl
);
69 EXPECT_CALL(get_mock_io_ctx(io_ctx
),
70 exec(RBD_MIRRORING
, _
, StrEq("rbd"), StrEq("mirror_image_get"), _
, _
, _
))
71 .WillOnce(DoAll(WithArg
<5>(Invoke([bl
](bufferlist
*out_bl
) {
77 void expect_get_tag_owner(librbd::MockJournal
&mock_journal
,
78 const std::string
&local_image_id
,
79 const std::string
&tag_owner
, int r
) {
80 EXPECT_CALL(mock_journal
, get_tag_owner(local_image_id
, _
, _
, _
))
81 .WillOnce(WithArgs
<1, 3>(Invoke([tag_owner
, r
](std::string
*owner
, Context
*on_finish
) {
83 on_finish
->complete(r
);
89 TEST_F(TestMockImageReplayerPrepareLocalImageRequest
, Success
) {
91 expect_mirror_image_get_image_id(m_local_io_ctx
, "local image id", 0);
92 expect_mirror_image_get(m_local_io_ctx
, cls::rbd::MIRROR_IMAGE_STATE_ENABLED
,
93 "global image id", 0);
95 librbd::MockJournal mock_journal
;
96 expect_get_tag_owner(mock_journal
, "local image id", "remote mirror uuid", 0);
98 std::string local_image_id
;
99 std::string tag_owner
;
101 auto req
= MockPrepareLocalImageRequest::create(m_local_io_ctx
,
105 m_threads
->work_queue
,
109 ASSERT_EQ(0, ctx
.wait());
110 ASSERT_EQ(std::string("local image id"), local_image_id
);
111 ASSERT_EQ(std::string("remote mirror uuid"), tag_owner
);
114 TEST_F(TestMockImageReplayerPrepareLocalImageRequest
, MirrorImageIdDNE
) {
116 expect_mirror_image_get_image_id(m_local_io_ctx
, "", -ENOENT
);
118 std::string local_image_id
;
119 std::string tag_owner
;
121 auto req
= MockPrepareLocalImageRequest::create(m_local_io_ctx
,
125 m_threads
->work_queue
,
129 ASSERT_EQ(-ENOENT
, ctx
.wait());
132 TEST_F(TestMockImageReplayerPrepareLocalImageRequest
, MirrorImageIdError
) {
134 expect_mirror_image_get_image_id(m_local_io_ctx
, "", -EINVAL
);
136 std::string local_image_id
;
137 std::string tag_owner
;
139 auto req
= MockPrepareLocalImageRequest::create(m_local_io_ctx
,
143 m_threads
->work_queue
,
147 ASSERT_EQ(-EINVAL
, ctx
.wait());
150 TEST_F(TestMockImageReplayerPrepareLocalImageRequest
, MirrorImageError
) {
152 expect_mirror_image_get_image_id(m_local_io_ctx
, "local image id", 0);
153 expect_mirror_image_get(m_local_io_ctx
, cls::rbd::MIRROR_IMAGE_STATE_DISABLED
,
156 std::string local_image_id
;
157 std::string tag_owner
;
159 auto req
= MockPrepareLocalImageRequest::create(m_local_io_ctx
,
163 m_threads
->work_queue
,
167 ASSERT_EQ(-EINVAL
, ctx
.wait());
170 TEST_F(TestMockImageReplayerPrepareLocalImageRequest
, TagOwnerError
) {
172 expect_mirror_image_get_image_id(m_local_io_ctx
, "local image id", 0);
173 expect_mirror_image_get(m_local_io_ctx
, cls::rbd::MIRROR_IMAGE_STATE_ENABLED
,
174 "global image id", 0);
176 librbd::MockJournal mock_journal
;
177 expect_get_tag_owner(mock_journal
, "local image id", "remote mirror uuid",
180 std::string local_image_id
;
181 std::string tag_owner
;
183 auto req
= MockPrepareLocalImageRequest::create(m_local_io_ctx
,
187 m_threads
->work_queue
,
191 ASSERT_EQ(-ENOENT
, ctx
.wait());
194 } // namespace image_replayer
195 } // namespace mirror