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 "librbd/journal/Types.h"
7 #include "librbd/journal/TypeTraits.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 "tools/rbd_mirror/image_sync/SyncPointCreateRequest.h"
17 struct MockTestImageCtx : public librbd::MockImageCtx {
18 MockTestImageCtx(librbd::ImageCtx &image_ctx)
19 : librbd::MockImageCtx(image_ctx) {
23 } // anonymous namespace
28 struct TypeTraits<librbd::MockTestImageCtx> {
29 typedef ::journal::MockJournaler Journaler;
32 } // namespace journal
35 // template definitions
36 #include "tools/rbd_mirror/image_sync/SyncPointCreateRequest.cc"
37 template class rbd::mirror::image_sync::SyncPointCreateRequest<librbd::MockTestImageCtx>;
41 namespace image_sync {
44 using ::testing::InSequence;
45 using ::testing::WithArg;
47 class TestMockImageSyncSyncPointCreateRequest : public TestMockFixture {
49 typedef SyncPointCreateRequest<librbd::MockTestImageCtx> MockSyncPointCreateRequest;
51 void SetUp() override {
52 TestMockFixture::SetUp();
55 ASSERT_EQ(0, create_image(rbd, m_remote_io_ctx, m_image_name, m_image_size));
56 ASSERT_EQ(0, open_image(m_remote_io_ctx, m_image_name, &m_remote_image_ctx));
59 void expect_update_client(journal::MockJournaler &mock_journaler, int r) {
60 EXPECT_CALL(mock_journaler, update_client(_, _))
61 .WillOnce(WithArg<1>(CompleteContext(r)));
64 void expect_image_refresh(librbd::MockTestImageCtx &mock_remote_image_ctx, int r) {
65 EXPECT_CALL(*mock_remote_image_ctx.state, refresh(_))
66 .WillOnce(CompleteContext(r));
69 void expect_snap_create(librbd::MockTestImageCtx &mock_remote_image_ctx, int r) {
70 EXPECT_CALL(*mock_remote_image_ctx.operations, snap_create(_, _, _))
71 .WillOnce(WithArg<2>(CompleteContext(r)));
74 MockSyncPointCreateRequest *create_request(librbd::MockTestImageCtx &mock_remote_image_ctx,
75 journal::MockJournaler &mock_journaler,
77 return new MockSyncPointCreateRequest(&mock_remote_image_ctx, "uuid",
78 &mock_journaler, &m_client_meta, ctx);
81 librbd::ImageCtx *m_remote_image_ctx;
82 librbd::journal::MirrorPeerClientMeta m_client_meta;
85 TEST_F(TestMockImageSyncSyncPointCreateRequest, Success) {
86 librbd::MockTestImageCtx mock_remote_image_ctx(*m_remote_image_ctx);
87 journal::MockJournaler mock_journaler;
90 expect_update_client(mock_journaler, 0);
91 expect_image_refresh(mock_remote_image_ctx, 0);
92 expect_snap_create(mock_remote_image_ctx, 0);
93 expect_image_refresh(mock_remote_image_ctx, 0);
96 MockSyncPointCreateRequest *req = create_request(mock_remote_image_ctx,
97 mock_journaler, &ctx);
99 ASSERT_EQ(0, ctx.wait());
101 ASSERT_EQ(1U, m_client_meta.sync_points.size());
104 TEST_F(TestMockImageSyncSyncPointCreateRequest, ResyncSuccess) {
105 m_client_meta.sync_points.emplace_front(cls::rbd::UserSnapshotNamespace(),
109 auto sync_point = m_client_meta.sync_points.front();
111 librbd::MockTestImageCtx mock_remote_image_ctx(*m_remote_image_ctx);
112 journal::MockJournaler mock_journaler;
115 expect_update_client(mock_journaler, 0);
116 expect_image_refresh(mock_remote_image_ctx, 0);
117 expect_snap_create(mock_remote_image_ctx, 0);
118 expect_image_refresh(mock_remote_image_ctx, 0);
121 MockSyncPointCreateRequest *req = create_request(mock_remote_image_ctx,
122 mock_journaler, &ctx);
124 ASSERT_EQ(0, ctx.wait());
126 ASSERT_EQ(2U, m_client_meta.sync_points.size());
127 ASSERT_EQ(sync_point, m_client_meta.sync_points.front());
128 ASSERT_EQ("start snap", m_client_meta.sync_points.back().from_snap_name);
131 TEST_F(TestMockImageSyncSyncPointCreateRequest, SnapshotExists) {
132 librbd::MockTestImageCtx mock_remote_image_ctx(*m_remote_image_ctx);
133 journal::MockJournaler mock_journaler;
136 expect_update_client(mock_journaler, 0);
137 expect_image_refresh(mock_remote_image_ctx, 0);
138 expect_snap_create(mock_remote_image_ctx, -EEXIST);
139 expect_update_client(mock_journaler, 0);
140 expect_image_refresh(mock_remote_image_ctx, 0);
141 expect_snap_create(mock_remote_image_ctx, 0);
142 expect_image_refresh(mock_remote_image_ctx, 0);
145 MockSyncPointCreateRequest *req = create_request(mock_remote_image_ctx,
146 mock_journaler, &ctx);
148 ASSERT_EQ(0, ctx.wait());
150 ASSERT_EQ(1U, m_client_meta.sync_points.size());
153 TEST_F(TestMockImageSyncSyncPointCreateRequest, ClientUpdateError) {
154 librbd::MockTestImageCtx mock_remote_image_ctx(*m_remote_image_ctx);
155 journal::MockJournaler mock_journaler;
158 expect_update_client(mock_journaler, -EINVAL);
161 MockSyncPointCreateRequest *req = create_request(mock_remote_image_ctx,
162 mock_journaler, &ctx);
164 ASSERT_EQ(-EINVAL, ctx.wait());
166 ASSERT_TRUE(m_client_meta.sync_points.empty());
169 } // namespace image_sync
170 } // namespace mirror