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 "include/rbd/librbd.hpp"
6 #include "test/librados_test_stub/MockTestMemIoCtxImpl.h"
7 #include "test/librbd/mock/MockImageCtx.h"
8 #include "test/rbd_mirror/mock/image_sync/MockSyncPointHandler.h"
9 #include "tools/rbd_mirror/image_sync/SyncPointCreateRequest.h"
15 struct MockTestImageCtx
: public librbd::MockImageCtx
{
16 explicit MockTestImageCtx(librbd::ImageCtx
&image_ctx
)
17 : librbd::MockImageCtx(image_ctx
) {
21 } // anonymous namespace
25 // template definitions
26 #include "tools/rbd_mirror/image_sync/SyncPointCreateRequest.cc"
30 namespace image_sync
{
33 using ::testing::DoAll
;
34 using ::testing::InSequence
;
35 using ::testing::Invoke
;
36 using ::testing::Return
;
37 using ::testing::WithArg
;
39 class TestMockImageSyncSyncPointCreateRequest
: public TestMockFixture
{
41 typedef SyncPointCreateRequest
<librbd::MockTestImageCtx
> MockSyncPointCreateRequest
;
43 void SetUp() override
{
44 TestMockFixture::SetUp();
47 ASSERT_EQ(0, create_image(rbd
, m_remote_io_ctx
, m_image_name
, m_image_size
));
48 ASSERT_EQ(0, open_image(m_remote_io_ctx
, m_image_name
, &m_remote_image_ctx
));
51 void expect_get_snap_seqs(MockSyncPointHandler
& mock_sync_point_handler
) {
52 EXPECT_CALL(mock_sync_point_handler
, get_snap_seqs())
53 .WillRepeatedly(Return(librbd::SnapSeqs
{}));
56 void expect_get_sync_points(MockSyncPointHandler
& mock_sync_point_handler
) {
57 EXPECT_CALL(mock_sync_point_handler
, get_sync_points())
58 .WillRepeatedly(Invoke([this]() {
63 void expect_update_sync_points(MockSyncPointHandler
& mock_sync_point_handler
,
65 EXPECT_CALL(mock_sync_point_handler
, update_sync_points(_
, _
, false, _
))
66 .WillOnce(DoAll(WithArg
<1>(Invoke([this, r
](const SyncPoints
& sync_points
) {
68 m_sync_points
= sync_points
;
71 WithArg
<3>(CompleteContext(r
))));
74 void expect_image_refresh(librbd::MockTestImageCtx
&mock_remote_image_ctx
, int r
) {
75 EXPECT_CALL(*mock_remote_image_ctx
.state
, refresh(_
))
76 .WillOnce(CompleteContext(r
));
79 void expect_snap_create(librbd::MockTestImageCtx
&mock_remote_image_ctx
, int r
) {
80 EXPECT_CALL(*mock_remote_image_ctx
.operations
, snap_create(_
, _
, _
))
81 .WillOnce(WithArg
<2>(CompleteContext(r
)));
84 MockSyncPointCreateRequest
*create_request(librbd::MockTestImageCtx
&mock_remote_image_ctx
,
85 MockSyncPointHandler
& mock_sync_point_handler
,
87 return new MockSyncPointCreateRequest(&mock_remote_image_ctx
, "uuid",
88 &mock_sync_point_handler
, ctx
);
91 librbd::ImageCtx
*m_remote_image_ctx
;
92 SyncPoints m_sync_points
;
95 TEST_F(TestMockImageSyncSyncPointCreateRequest
, Success
) {
96 librbd::MockTestImageCtx
mock_remote_image_ctx(*m_remote_image_ctx
);
97 MockSyncPointHandler mock_sync_point_handler
;
99 expect_get_snap_seqs(mock_sync_point_handler
);
100 expect_get_sync_points(mock_sync_point_handler
);
103 expect_update_sync_points(mock_sync_point_handler
, 0);
104 expect_image_refresh(mock_remote_image_ctx
, 0);
105 expect_snap_create(mock_remote_image_ctx
, 0);
106 expect_image_refresh(mock_remote_image_ctx
, 0);
109 MockSyncPointCreateRequest
*req
= create_request(mock_remote_image_ctx
,
110 mock_sync_point_handler
,
113 ASSERT_EQ(0, ctx
.wait());
115 ASSERT_EQ(1U, m_sync_points
.size());
118 TEST_F(TestMockImageSyncSyncPointCreateRequest
, ResyncSuccess
) {
119 m_sync_points
.emplace_front(cls::rbd::UserSnapshotNamespace(), "start snap",
121 auto sync_point
= m_sync_points
.front();
123 librbd::MockTestImageCtx
mock_remote_image_ctx(*m_remote_image_ctx
);
124 MockSyncPointHandler mock_sync_point_handler
;
126 expect_get_snap_seqs(mock_sync_point_handler
);
127 expect_get_sync_points(mock_sync_point_handler
);
130 expect_update_sync_points(mock_sync_point_handler
, 0);
131 expect_image_refresh(mock_remote_image_ctx
, 0);
132 expect_snap_create(mock_remote_image_ctx
, 0);
133 expect_image_refresh(mock_remote_image_ctx
, 0);
136 MockSyncPointCreateRequest
*req
= create_request(mock_remote_image_ctx
,
137 mock_sync_point_handler
,
140 ASSERT_EQ(0, ctx
.wait());
142 ASSERT_EQ(2U, m_sync_points
.size());
143 ASSERT_EQ(sync_point
, m_sync_points
.front());
144 ASSERT_EQ("start snap", m_sync_points
.back().from_snap_name
);
147 TEST_F(TestMockImageSyncSyncPointCreateRequest
, SnapshotExists
) {
148 librbd::MockTestImageCtx
mock_remote_image_ctx(*m_remote_image_ctx
);
149 MockSyncPointHandler mock_sync_point_handler
;
151 expect_get_snap_seqs(mock_sync_point_handler
);
152 expect_get_sync_points(mock_sync_point_handler
);
155 expect_update_sync_points(mock_sync_point_handler
, 0);
156 expect_image_refresh(mock_remote_image_ctx
, 0);
157 expect_snap_create(mock_remote_image_ctx
, -EEXIST
);
158 expect_update_sync_points(mock_sync_point_handler
, 0);
159 expect_image_refresh(mock_remote_image_ctx
, 0);
160 expect_snap_create(mock_remote_image_ctx
, 0);
161 expect_image_refresh(mock_remote_image_ctx
, 0);
164 MockSyncPointCreateRequest
*req
= create_request(mock_remote_image_ctx
,
165 mock_sync_point_handler
,
168 ASSERT_EQ(0, ctx
.wait());
170 ASSERT_EQ(1U, m_sync_points
.size());
173 TEST_F(TestMockImageSyncSyncPointCreateRequest
, ClientUpdateError
) {
174 librbd::MockTestImageCtx
mock_remote_image_ctx(*m_remote_image_ctx
);
175 MockSyncPointHandler mock_sync_point_handler
;
177 expect_get_snap_seqs(mock_sync_point_handler
);
178 expect_get_sync_points(mock_sync_point_handler
);
181 expect_update_sync_points(mock_sync_point_handler
, -EINVAL
);
184 MockSyncPointCreateRequest
*req
= create_request(mock_remote_image_ctx
,
185 mock_sync_point_handler
,
188 ASSERT_EQ(-EINVAL
, ctx
.wait());
190 ASSERT_TRUE(m_sync_points
.empty());
193 } // namespace image_sync
194 } // namespace mirror