summaryrefslogtreecommitdiffstats
path: root/src/test/librbd/deep_copy
diff options
context:
space:
mode:
Diffstat (limited to 'src/test/librbd/deep_copy')
-rw-r--r--src/test/librbd/deep_copy/test_mock_ImageCopyRequest.cc805
-rw-r--r--src/test/librbd/deep_copy/test_mock_MetadataCopyRequest.cc220
-rw-r--r--src/test/librbd/deep_copy/test_mock_ObjectCopyRequest.cc1103
-rw-r--r--src/test/librbd/deep_copy/test_mock_SetHeadRequest.cc296
-rw-r--r--src/test/librbd/deep_copy/test_mock_SnapshotCopyRequest.cc923
-rw-r--r--src/test/librbd/deep_copy/test_mock_SnapshotCreateRequest.cc262
6 files changed, 3609 insertions, 0 deletions
diff --git a/src/test/librbd/deep_copy/test_mock_ImageCopyRequest.cc b/src/test/librbd/deep_copy/test_mock_ImageCopyRequest.cc
new file mode 100644
index 000000000..c56b681b9
--- /dev/null
+++ b/src/test/librbd/deep_copy/test_mock_ImageCopyRequest.cc
@@ -0,0 +1,805 @@
+// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
+// vim: ts=8 sw=2 smarttab
+
+#include "test/librbd/test_mock_fixture.h"
+#include "include/rbd/librbd.hpp"
+#include "librbd/AsioEngine.h"
+#include "librbd/ImageCtx.h"
+#include "librbd/ImageState.h"
+#include "librbd/internal.h"
+#include "librbd/Operations.h"
+#include "librbd/deep_copy/Handler.h"
+#include "librbd/deep_copy/ImageCopyRequest.h"
+#include "librbd/deep_copy/ObjectCopyRequest.h"
+#include "librbd/object_map/DiffRequest.h"
+#include "test/librados_test_stub/MockTestMemIoCtxImpl.h"
+#include "test/librbd/mock/MockImageCtx.h"
+#include "test/librbd/test_support.h"
+#include <boost/scope_exit.hpp>
+
+namespace librbd {
+
+namespace {
+
+struct MockTestImageCtx : public librbd::MockImageCtx {
+ static MockTestImageCtx* s_instance;
+ static MockTestImageCtx* create(const std::string &image_name,
+ const std::string &image_id,
+ librados::snap_t snap_id, librados::IoCtx& p,
+ bool read_only) {
+ ceph_assert(s_instance != nullptr);
+ return s_instance;
+ }
+
+ explicit MockTestImageCtx(librbd::ImageCtx &image_ctx)
+ : librbd::MockImageCtx(image_ctx) {
+ s_instance = this;
+ }
+
+ MOCK_METHOD0(destroy, void());
+};
+
+MockTestImageCtx* MockTestImageCtx::s_instance = nullptr;
+
+} // anonymous namespace
+
+namespace deep_copy {
+
+template <>
+struct ObjectCopyRequest<librbd::MockTestImageCtx> {
+ static ObjectCopyRequest* s_instance;
+ static ObjectCopyRequest* create(
+ librbd::MockTestImageCtx *src_image_ctx,
+ librbd::MockTestImageCtx *dst_image_ctx,
+ librados::snap_t src_snap_id_start,
+ librados::snap_t dst_snap_id_start,
+ const SnapMap &snap_map,
+ uint64_t object_number, uint32_t flags, Handler* handler,
+ Context *on_finish) {
+ ceph_assert(s_instance != nullptr);
+ std::lock_guard locker{s_instance->lock};
+ s_instance->snap_map = &snap_map;
+ s_instance->flags = flags;
+ s_instance->object_contexts[object_number] = on_finish;
+ s_instance->cond.notify_all();
+ return s_instance;
+ }
+
+ MOCK_METHOD0(send, void());
+
+ ceph::mutex lock = ceph::make_mutex("lock");
+ ceph::condition_variable cond;
+
+ const SnapMap *snap_map = nullptr;
+ std::map<uint64_t, Context *> object_contexts;
+ uint32_t flags = 0;
+
+ ObjectCopyRequest() {
+ s_instance = this;
+ }
+};
+
+ObjectCopyRequest<librbd::MockTestImageCtx>* ObjectCopyRequest<librbd::MockTestImageCtx>::s_instance = nullptr;
+
+} // namespace deep_copy
+
+namespace object_map {
+
+template <>
+struct DiffRequest<MockTestImageCtx> {
+ BitVector<2>* object_diff_state = nullptr;
+ Context* on_finish = nullptr;
+ static DiffRequest* s_instance;
+ static DiffRequest* create(MockTestImageCtx *image_ctx,
+ uint64_t snap_id_start, uint64_t snap_id_end,
+ BitVector<2>* object_diff_state,
+ Context* on_finish) {
+ ceph_assert(s_instance != nullptr);
+ s_instance->object_diff_state = object_diff_state;
+ s_instance->on_finish = on_finish;
+ return s_instance;
+ }
+
+ DiffRequest() {
+ s_instance = this;
+ }
+
+ MOCK_METHOD0(send, void());
+};
+
+DiffRequest<MockTestImageCtx>* DiffRequest<MockTestImageCtx>::s_instance = nullptr;
+
+} // namespace object_map
+} // namespace librbd
+
+// template definitions
+#include "librbd/deep_copy/ImageCopyRequest.cc"
+template class librbd::deep_copy::ImageCopyRequest<librbd::MockTestImageCtx>;
+
+namespace librbd {
+namespace deep_copy {
+
+using ::testing::_;
+using ::testing::InSequence;
+using ::testing::Invoke;
+using ::testing::Return;
+
+class TestMockDeepCopyImageCopyRequest : public TestMockFixture {
+public:
+ typedef ImageCopyRequest<librbd::MockTestImageCtx> MockImageCopyRequest;
+ typedef ObjectCopyRequest<librbd::MockTestImageCtx> MockObjectCopyRequest;
+ typedef object_map::DiffRequest<librbd::MockTestImageCtx> MockDiffRequest;
+
+ librbd::ImageCtx *m_src_image_ctx;
+ librbd::ImageCtx *m_dst_image_ctx;
+
+ std::shared_ptr<librbd::AsioEngine> m_asio_engine;
+ asio::ContextWQ *m_work_queue;
+
+ librbd::SnapSeqs m_snap_seqs;
+ SnapMap m_snap_map;
+
+ void SetUp() override {
+ TestMockFixture::SetUp();
+
+ ASSERT_EQ(0, open_image(m_image_name, &m_src_image_ctx));
+
+ librbd::RBD rbd;
+ std::string dst_image_name = get_temp_image_name();
+ ASSERT_EQ(0, create_image_pp(rbd, m_ioctx, dst_image_name, m_image_size));
+ ASSERT_EQ(0, open_image(dst_image_name, &m_dst_image_ctx));
+
+ m_asio_engine = std::make_shared<librbd::AsioEngine>(
+ m_src_image_ctx->md_ctx);
+ m_work_queue = m_asio_engine->get_work_queue();
+ }
+
+ void expect_get_image_size(librbd::MockTestImageCtx &mock_image_ctx,
+ uint64_t size) {
+ EXPECT_CALL(mock_image_ctx, get_image_size(_))
+ .WillOnce(Return(size)).RetiresOnSaturation();
+ }
+
+ void expect_diff_send(MockDiffRequest& mock_request,
+ const BitVector<2>& diff_state, int r) {
+ EXPECT_CALL(mock_request, send())
+ .WillOnce(Invoke([this, &mock_request, diff_state, r]() {
+ if (r >= 0) {
+ *mock_request.object_diff_state = diff_state;
+ }
+ m_work_queue->queue(mock_request.on_finish, r);
+ }));
+ }
+
+ void expect_object_copy_send(MockObjectCopyRequest &mock_object_copy_request,
+ uint32_t flags) {
+ EXPECT_CALL(mock_object_copy_request, send())
+ .WillOnce(Invoke([&mock_object_copy_request, flags]() {
+ ASSERT_EQ(flags, mock_object_copy_request.flags);
+ }));
+ }
+
+ bool complete_object_copy(MockObjectCopyRequest &mock_object_copy_request,
+ uint64_t object_num, Context **object_ctx, int r) {
+ std::unique_lock locker{mock_object_copy_request.lock};
+ while (mock_object_copy_request.object_contexts.count(object_num) == 0) {
+ if (mock_object_copy_request.cond.wait_for(locker, 10s) ==
+ std::cv_status::timeout) {
+ return false;
+ }
+ }
+
+ if (object_ctx != nullptr) {
+ *object_ctx = mock_object_copy_request.object_contexts[object_num];
+ } else {
+ m_work_queue->queue(mock_object_copy_request.object_contexts[object_num],
+ r);
+ }
+ return true;
+ }
+
+ SnapMap wait_for_snap_map(MockObjectCopyRequest &mock_object_copy_request) {
+ std::unique_lock locker{mock_object_copy_request.lock};
+ while (mock_object_copy_request.snap_map == nullptr) {
+ if (mock_object_copy_request.cond.wait_for(locker, 10s) ==
+ std::cv_status::timeout) {
+ return SnapMap();
+ }
+ }
+ return *mock_object_copy_request.snap_map;
+ }
+
+ int create_snap(librbd::ImageCtx *image_ctx, const char* snap_name,
+ librados::snap_t *snap_id) {
+ NoOpProgressContext prog_ctx;
+ int r = image_ctx->operations->snap_create(
+ cls::rbd::UserSnapshotNamespace(), snap_name, 0, prog_ctx);
+ if (r < 0) {
+ return r;
+ }
+
+ r = image_ctx->state->refresh();
+ if (r < 0) {
+ return r;
+ }
+
+ if (image_ctx->snap_ids.count({cls::rbd::UserSnapshotNamespace(),
+ snap_name}) == 0) {
+ return -ENOENT;
+ }
+
+ if (snap_id != nullptr) {
+ *snap_id = image_ctx->snap_ids[{cls::rbd::UserSnapshotNamespace(),
+ snap_name}];
+ }
+ return 0;
+ }
+
+ int create_snap(const char* snap_name,
+ librados::snap_t *src_snap_id_ = nullptr) {
+ librados::snap_t src_snap_id;
+ int r = create_snap(m_src_image_ctx, snap_name, &src_snap_id);
+ if (r < 0) {
+ return r;
+ }
+
+ if (src_snap_id_ != nullptr) {
+ *src_snap_id_ = src_snap_id;
+ }
+
+ librados::snap_t dst_snap_id;
+ r = create_snap(m_dst_image_ctx, snap_name, &dst_snap_id);
+ if (r < 0) {
+ return r;
+ }
+
+ // collection of all existing snaps in dst image
+ SnapIds dst_snap_ids({dst_snap_id});
+ if (!m_snap_map.empty()) {
+ dst_snap_ids.insert(dst_snap_ids.end(),
+ m_snap_map.rbegin()->second.begin(),
+ m_snap_map.rbegin()->second.end());
+ }
+ m_snap_map[src_snap_id] = dst_snap_ids;
+ m_snap_seqs[src_snap_id] = dst_snap_id;
+ return 0;
+ }
+};
+
+TEST_F(TestMockDeepCopyImageCopyRequest, SimpleImage) {
+ librados::snap_t snap_id_end;
+ ASSERT_EQ(0, create_snap("copy", &snap_id_end));
+
+ librbd::MockTestImageCtx mock_src_image_ctx(*m_src_image_ctx);
+ librbd::MockTestImageCtx mock_dst_image_ctx(*m_dst_image_ctx);
+ MockObjectCopyRequest mock_object_copy_request;
+
+ InSequence seq;
+ MockDiffRequest mock_diff_request;
+ expect_diff_send(mock_diff_request, {}, -EINVAL);
+ expect_get_image_size(mock_src_image_ctx, 1 << m_src_image_ctx->order);
+ expect_get_image_size(mock_src_image_ctx, 0);
+ expect_object_copy_send(mock_object_copy_request, 0);
+
+ librbd::deep_copy::NoOpHandler no_op;
+ C_SaferCond ctx;
+ auto request = new MockImageCopyRequest(&mock_src_image_ctx,
+ &mock_dst_image_ctx,
+ 0, snap_id_end, 0, false, boost::none,
+ m_snap_seqs, &no_op, &ctx);
+ request->send();
+
+ ASSERT_EQ(m_snap_map, wait_for_snap_map(mock_object_copy_request));
+ ASSERT_TRUE(complete_object_copy(mock_object_copy_request, 0, nullptr, 0));
+ ASSERT_EQ(0, ctx.wait());
+}
+
+TEST_F(TestMockDeepCopyImageCopyRequest, FastDiffNonExistent) {
+ librados::snap_t snap_id_end;
+ ASSERT_EQ(0, create_snap("copy", &snap_id_end));
+
+ librbd::MockTestImageCtx mock_src_image_ctx(*m_src_image_ctx);
+ librbd::MockTestImageCtx mock_dst_image_ctx(*m_dst_image_ctx);
+
+ InSequence seq;
+
+ MockDiffRequest mock_diff_request;
+ BitVector<2> diff_state;
+ diff_state.resize(1);
+ expect_diff_send(mock_diff_request, diff_state, 0);
+
+ expect_get_image_size(mock_src_image_ctx, 1 << m_src_image_ctx->order);
+ expect_get_image_size(mock_src_image_ctx, 0);
+ expect_op_work_queue(mock_src_image_ctx);
+
+ librbd::deep_copy::NoOpHandler no_op;
+ C_SaferCond ctx;
+ auto request = new MockImageCopyRequest(&mock_src_image_ctx,
+ &mock_dst_image_ctx,
+ 0, snap_id_end, 0, false, boost::none,
+ m_snap_seqs, &no_op, &ctx);
+ request->send();
+
+ ASSERT_EQ(0, ctx.wait());
+}
+
+TEST_F(TestMockDeepCopyImageCopyRequest, FastDiffExistsDirty) {
+ librados::snap_t snap_id_end;
+ ASSERT_EQ(0, create_snap("copy", &snap_id_end));
+
+ librbd::MockTestImageCtx mock_src_image_ctx(*m_src_image_ctx);
+ librbd::MockTestImageCtx mock_dst_image_ctx(*m_dst_image_ctx);
+
+ InSequence seq;
+
+ MockDiffRequest mock_diff_request;
+ BitVector<2> diff_state;
+ diff_state.resize(1);
+ diff_state[0] = object_map::DIFF_STATE_DATA_UPDATED;
+ expect_diff_send(mock_diff_request, diff_state, 0);
+
+ expect_get_image_size(mock_src_image_ctx, 1 << m_src_image_ctx->order);
+ expect_get_image_size(mock_src_image_ctx, 0);
+ MockObjectCopyRequest mock_object_copy_request;
+ expect_object_copy_send(mock_object_copy_request, 0);
+
+ librbd::deep_copy::NoOpHandler no_op;
+ C_SaferCond ctx;
+ auto request = new MockImageCopyRequest(&mock_src_image_ctx,
+ &mock_dst_image_ctx,
+ 0, snap_id_end, 0, false, boost::none,
+ m_snap_seqs, &no_op, &ctx);
+ request->send();
+
+ ASSERT_EQ(m_snap_map, wait_for_snap_map(mock_object_copy_request));
+ ASSERT_TRUE(complete_object_copy(mock_object_copy_request, 0, nullptr, 0));
+ ASSERT_EQ(0, ctx.wait());
+}
+
+TEST_F(TestMockDeepCopyImageCopyRequest, FastDiffExistsClean) {
+ librados::snap_t snap_id_end;
+ ASSERT_EQ(0, create_snap("copy", &snap_id_end));
+
+ librbd::MockTestImageCtx mock_src_image_ctx(*m_src_image_ctx);
+ librbd::MockTestImageCtx mock_dst_image_ctx(*m_dst_image_ctx);
+
+ InSequence seq;
+
+ MockDiffRequest mock_diff_request;
+ BitVector<2> diff_state;
+ diff_state.resize(1);
+ diff_state[0] = object_map::DIFF_STATE_DATA;
+ expect_diff_send(mock_diff_request, diff_state, 0);
+
+ expect_get_image_size(mock_src_image_ctx, 1 << m_src_image_ctx->order);
+ expect_get_image_size(mock_src_image_ctx, 0);
+ MockObjectCopyRequest mock_object_copy_request;
+ expect_object_copy_send(mock_object_copy_request,
+ OBJECT_COPY_REQUEST_FLAG_EXISTS_CLEAN);
+
+ librbd::deep_copy::NoOpHandler no_op;
+ C_SaferCond ctx;
+ auto request = new MockImageCopyRequest(&mock_src_image_ctx,
+ &mock_dst_image_ctx,
+ 0, snap_id_end, 0, false, boost::none,
+ m_snap_seqs, &no_op, &ctx);
+ request->send();
+
+ ASSERT_EQ(m_snap_map, wait_for_snap_map(mock_object_copy_request));
+ ASSERT_TRUE(complete_object_copy(mock_object_copy_request, 0, nullptr, 0));
+ ASSERT_EQ(0, ctx.wait());
+}
+
+TEST_F(TestMockDeepCopyImageCopyRequest, FastDiffMix) {
+ librados::snap_t snap_id_end;
+ ASSERT_EQ(0, create_snap("copy", &snap_id_end));
+
+ uint64_t object_count = 12;
+
+ librbd::MockTestImageCtx mock_src_image_ctx(*m_src_image_ctx);
+ librbd::MockTestImageCtx mock_dst_image_ctx(*m_dst_image_ctx);
+ MockObjectCopyRequest mock_object_copy_request;
+
+ InSequence seq;
+
+ MockDiffRequest mock_diff_request;
+ BitVector<2> diff_state;
+ diff_state.resize(object_count);
+ diff_state[1] = object_map::DIFF_STATE_DATA_UPDATED;
+ diff_state[2] = object_map::DIFF_STATE_DATA_UPDATED;
+ diff_state[3] = object_map::DIFF_STATE_DATA;
+ diff_state[5] = object_map::DIFF_STATE_DATA_UPDATED;
+ diff_state[8] = object_map::DIFF_STATE_DATA;
+ diff_state[9] = object_map::DIFF_STATE_DATA;
+ diff_state[10] = object_map::DIFF_STATE_DATA_UPDATED;
+ expect_diff_send(mock_diff_request, diff_state, 0);
+
+ expect_get_image_size(mock_src_image_ctx,
+ object_count * (1 << m_src_image_ctx->order));
+ expect_get_image_size(mock_src_image_ctx, 0);
+
+ expect_op_work_queue(mock_src_image_ctx);
+ expect_object_copy_send(mock_object_copy_request, 0);
+ expect_object_copy_send(mock_object_copy_request, 0);
+ expect_object_copy_send(mock_object_copy_request,
+ OBJECT_COPY_REQUEST_FLAG_EXISTS_CLEAN);
+ expect_op_work_queue(mock_src_image_ctx);
+ expect_object_copy_send(mock_object_copy_request, 0);
+ expect_op_work_queue(mock_src_image_ctx);
+ expect_object_copy_send(mock_object_copy_request,
+ OBJECT_COPY_REQUEST_FLAG_EXISTS_CLEAN);
+ expect_object_copy_send(mock_object_copy_request,
+ OBJECT_COPY_REQUEST_FLAG_EXISTS_CLEAN);
+ expect_object_copy_send(mock_object_copy_request, 0);
+ expect_op_work_queue(mock_src_image_ctx);
+
+ std::vector<bool> seen(object_count);
+ struct Handler : public librbd::deep_copy::NoOpHandler {
+ Handler(std::vector<bool>* seen) : m_seen(seen) {}
+
+ int update_progress(uint64_t object_no, uint64_t end_object_no) override {
+ EXPECT_THAT(object_no, ::testing::AllOf(::testing::Ge(1),
+ ::testing::Le(m_seen->size())));
+ EXPECT_EQ(end_object_no, m_seen->size());
+ EXPECT_FALSE((*m_seen)[object_no - 1]);
+ (*m_seen)[object_no - 1] = true;
+ return 0;
+ }
+
+ std::vector<bool>* m_seen;
+ } handler(&seen);
+
+ C_SaferCond ctx;
+ auto request = new MockImageCopyRequest(&mock_src_image_ctx,
+ &mock_dst_image_ctx,
+ 0, snap_id_end, 0, false, boost::none,
+ m_snap_seqs, &handler, &ctx);
+ request->send();
+
+ ASSERT_EQ(m_snap_map, wait_for_snap_map(mock_object_copy_request));
+ ASSERT_TRUE(complete_object_copy(mock_object_copy_request, 1, nullptr, 0));
+ ASSERT_TRUE(complete_object_copy(mock_object_copy_request, 2, nullptr, 0));
+ ASSERT_TRUE(complete_object_copy(mock_object_copy_request, 3, nullptr, 0));
+ ASSERT_TRUE(complete_object_copy(mock_object_copy_request, 5, nullptr, 0));
+ ASSERT_TRUE(complete_object_copy(mock_object_copy_request, 8, nullptr, 0));
+ ASSERT_TRUE(complete_object_copy(mock_object_copy_request, 9, nullptr, 0));
+ ASSERT_TRUE(complete_object_copy(mock_object_copy_request, 10, nullptr, 0));
+ ASSERT_EQ(0, ctx.wait());
+
+ EXPECT_THAT(seen, ::testing::Each(::testing::IsTrue()));
+}
+
+TEST_F(TestMockDeepCopyImageCopyRequest, OutOfOrder) {
+ std::string max_ops_str;
+ ASSERT_EQ(0, _rados.conf_get("rbd_concurrent_management_ops", max_ops_str));
+ ASSERT_EQ(0, _rados.conf_set("rbd_concurrent_management_ops", "10"));
+ BOOST_SCOPE_EXIT( (max_ops_str) ) {
+ ASSERT_EQ(0, _rados.conf_set("rbd_concurrent_management_ops",
+ max_ops_str.c_str()));
+ } BOOST_SCOPE_EXIT_END;
+
+ librados::snap_t snap_id_end;
+ ASSERT_EQ(0, create_snap("copy", &snap_id_end));
+
+ uint64_t object_count = 55;
+
+ librbd::MockTestImageCtx mock_src_image_ctx(*m_src_image_ctx);
+ librbd::MockTestImageCtx mock_dst_image_ctx(*m_dst_image_ctx);
+ MockObjectCopyRequest mock_object_copy_request;
+
+ MockDiffRequest mock_diff_request;
+ expect_diff_send(mock_diff_request, {}, -EINVAL);
+ expect_get_image_size(mock_src_image_ctx,
+ object_count * (1 << m_src_image_ctx->order));
+ expect_get_image_size(mock_src_image_ctx, 0);
+
+ EXPECT_CALL(mock_object_copy_request, send()).Times(object_count);
+
+ class Handler : public librbd::deep_copy::NoOpHandler {
+ public:
+ uint64_t object_count;
+ librbd::deep_copy::ObjectNumber expected_object_number;
+
+ Handler(uint64_t object_count)
+ : object_count(object_count) {
+ }
+
+ int update_progress(uint64_t object_no, uint64_t end_object_no) override {
+ EXPECT_LE(object_no, object_count);
+ EXPECT_EQ(end_object_no, object_count);
+ if (!expected_object_number) {
+ expected_object_number = 0;
+ } else {
+ expected_object_number = *expected_object_number + 1;
+ }
+ EXPECT_EQ(*expected_object_number, object_no - 1);
+
+ return 0;
+ }
+ } handler(object_count);
+
+ C_SaferCond ctx;
+ auto request = new MockImageCopyRequest(&mock_src_image_ctx,
+ &mock_dst_image_ctx,
+ 0, snap_id_end, 0, false, boost::none,
+ m_snap_seqs, &handler, &ctx);
+ request->send();
+
+ std::map<uint64_t, Context*> copy_contexts;
+ ASSERT_EQ(m_snap_map, wait_for_snap_map(mock_object_copy_request));
+ for (uint64_t i = 0; i < object_count; ++i) {
+ if (i % 10 == 0) {
+ ASSERT_TRUE(complete_object_copy(mock_object_copy_request, i,
+ &copy_contexts[i], 0));
+ } else {
+ ASSERT_TRUE(complete_object_copy(mock_object_copy_request, i, nullptr,
+ 0));
+ }
+ }
+
+ for (auto& pair : copy_contexts) {
+ pair.second->complete(0);
+ }
+
+ ASSERT_EQ(0, ctx.wait());
+}
+
+TEST_F(TestMockDeepCopyImageCopyRequest, SnapshotSubset) {
+ librados::snap_t snap_id_start;
+ librados::snap_t snap_id_end;
+ ASSERT_EQ(0, create_snap("snap1"));
+ ASSERT_EQ(0, create_snap("snap2", &snap_id_start));
+ ASSERT_EQ(0, create_snap("copy", &snap_id_end));
+
+ librbd::MockTestImageCtx mock_src_image_ctx(*m_src_image_ctx);
+ librbd::MockTestImageCtx mock_dst_image_ctx(*m_dst_image_ctx);
+ MockObjectCopyRequest mock_object_copy_request;
+
+ InSequence seq;
+ MockDiffRequest mock_diff_request;
+ expect_diff_send(mock_diff_request, {}, -EINVAL);
+ expect_get_image_size(mock_src_image_ctx, 1 << m_src_image_ctx->order);
+ expect_get_image_size(mock_src_image_ctx, 0);
+ expect_get_image_size(mock_src_image_ctx, 0);
+ expect_get_image_size(mock_src_image_ctx, 0);
+ expect_object_copy_send(mock_object_copy_request, 0);
+
+ librbd::deep_copy::NoOpHandler no_op;
+ C_SaferCond ctx;
+ auto request = new MockImageCopyRequest(&mock_src_image_ctx,
+ &mock_dst_image_ctx,
+ snap_id_start, snap_id_end, 0, false,
+ boost::none, m_snap_seqs, &no_op,
+ &ctx);
+ request->send();
+
+ SnapMap snap_map(m_snap_map);
+ snap_map.erase(snap_map.begin());
+ ASSERT_EQ(snap_map, wait_for_snap_map(mock_object_copy_request));
+
+ ASSERT_TRUE(complete_object_copy(mock_object_copy_request, 0, nullptr, 0));
+ ASSERT_EQ(0, ctx.wait());
+}
+
+TEST_F(TestMockDeepCopyImageCopyRequest, RestartPartialSync) {
+ librados::snap_t snap_id_end;
+ ASSERT_EQ(0, create_snap("copy", &snap_id_end));
+
+ librbd::MockTestImageCtx mock_src_image_ctx(*m_src_image_ctx);
+ librbd::MockTestImageCtx mock_dst_image_ctx(*m_dst_image_ctx);
+ MockObjectCopyRequest mock_object_copy_request;
+
+ InSequence seq;
+ MockDiffRequest mock_diff_request;
+ expect_diff_send(mock_diff_request, {}, -EINVAL);
+ expect_get_image_size(mock_src_image_ctx, 2 * (1 << m_src_image_ctx->order));
+ expect_get_image_size(mock_src_image_ctx, 0);
+ expect_object_copy_send(mock_object_copy_request, 0);
+
+ librbd::deep_copy::NoOpHandler no_op;
+ C_SaferCond ctx;
+ auto request = new MockImageCopyRequest(&mock_src_image_ctx,
+ &mock_dst_image_ctx,
+ 0, snap_id_end, 0, false,
+ librbd::deep_copy::ObjectNumber{0U},
+ m_snap_seqs, &no_op, &ctx);
+ request->send();
+
+ ASSERT_TRUE(complete_object_copy(mock_object_copy_request, 1, nullptr, 0));
+ ASSERT_EQ(0, ctx.wait());
+}
+
+TEST_F(TestMockDeepCopyImageCopyRequest, Cancel) {
+ std::string max_ops_str;
+ ASSERT_EQ(0, _rados.conf_get("rbd_concurrent_management_ops", max_ops_str));
+ ASSERT_EQ(0, _rados.conf_set("rbd_concurrent_management_ops", "1"));
+ BOOST_SCOPE_EXIT( (max_ops_str) ) {
+ ASSERT_EQ(0, _rados.conf_set("rbd_concurrent_management_ops",
+ max_ops_str.c_str()));
+ } BOOST_SCOPE_EXIT_END;
+
+ librados::snap_t snap_id_end;
+ ASSERT_EQ(0, create_snap("copy", &snap_id_end));
+
+ librbd::MockTestImageCtx mock_src_image_ctx(*m_src_image_ctx);
+ librbd::MockTestImageCtx mock_dst_image_ctx(*m_dst_image_ctx);
+ MockObjectCopyRequest mock_object_copy_request;
+
+ InSequence seq;
+ MockDiffRequest mock_diff_request;
+ expect_diff_send(mock_diff_request, {}, -EINVAL);
+ expect_get_image_size(mock_src_image_ctx, 1 << m_src_image_ctx->order);
+ expect_get_image_size(mock_src_image_ctx, 0);
+ expect_object_copy_send(mock_object_copy_request, 0);
+
+ librbd::deep_copy::NoOpHandler no_op;
+ C_SaferCond ctx;
+ auto request = new MockImageCopyRequest(&mock_src_image_ctx,
+ &mock_dst_image_ctx,
+ 0, snap_id_end, 0, false, boost::none,
+ m_snap_seqs, &no_op, &ctx);
+ request->send();
+
+ ASSERT_EQ(m_snap_map, wait_for_snap_map(mock_object_copy_request));
+ request->cancel();
+
+ ASSERT_TRUE(complete_object_copy(mock_object_copy_request, 0, nullptr, 0));
+ ASSERT_EQ(-ECANCELED, ctx.wait());
+}
+
+TEST_F(TestMockDeepCopyImageCopyRequest, Cancel_Inflight_Sync) {
+ std::string max_ops_str;
+ ASSERT_EQ(0, _rados.conf_get("rbd_concurrent_management_ops", max_ops_str));
+ ASSERT_EQ(0, _rados.conf_set("rbd_concurrent_management_ops", "3"));
+ BOOST_SCOPE_EXIT( (max_ops_str) ) {
+ ASSERT_EQ(0, _rados.conf_set("rbd_concurrent_management_ops",
+ max_ops_str.c_str()));
+ } BOOST_SCOPE_EXIT_END;
+
+ librados::snap_t snap_id_end;
+ ASSERT_EQ(0, create_snap("copy", &snap_id_end));
+
+ librbd::MockTestImageCtx mock_src_image_ctx(*m_src_image_ctx);
+ librbd::MockTestImageCtx mock_dst_image_ctx(*m_dst_image_ctx);
+ MockObjectCopyRequest mock_object_copy_request;
+
+ InSequence seq;
+ MockDiffRequest mock_diff_request;
+ expect_diff_send(mock_diff_request, {}, -EINVAL);
+ expect_get_image_size(mock_src_image_ctx, 6 * (1 << m_src_image_ctx->order));
+ expect_get_image_size(mock_src_image_ctx, m_image_size);
+
+ EXPECT_CALL(mock_object_copy_request, send()).Times(6);
+
+ struct Handler : public librbd::deep_copy::NoOpHandler {
+ librbd::deep_copy::ObjectNumber object_number;
+
+ int update_progress(uint64_t object_no, uint64_t end_object_no) override {
+ object_number = object_number ? *object_number + 1 : 0;
+ return 0;
+ }
+ } handler;
+
+ C_SaferCond ctx;
+ auto request = new MockImageCopyRequest(&mock_src_image_ctx,
+ &mock_dst_image_ctx,
+ 0, snap_id_end, 0, false, boost::none,
+ m_snap_seqs, &handler, &ctx);
+ request->send();
+
+ ASSERT_EQ(m_snap_map, wait_for_snap_map(mock_object_copy_request));
+
+ Context *cancel_ctx = nullptr;
+ ASSERT_TRUE(complete_object_copy(mock_object_copy_request, 0, nullptr, 0));
+ ASSERT_TRUE(complete_object_copy(mock_object_copy_request, 1, nullptr, 0));
+ ASSERT_TRUE(complete_object_copy(mock_object_copy_request, 2, nullptr, 0));
+ ASSERT_TRUE(complete_object_copy(mock_object_copy_request, 3, &cancel_ctx,
+ 0));
+ ASSERT_TRUE(complete_object_copy(mock_object_copy_request, 4, nullptr, 0));
+ ASSERT_TRUE(complete_object_copy(mock_object_copy_request, 5, nullptr, 0));
+
+ request->cancel();
+ cancel_ctx->complete(0);
+
+ ASSERT_EQ(-ECANCELED, ctx.wait());
+ ASSERT_EQ(5u, handler.object_number.get());
+}
+
+TEST_F(TestMockDeepCopyImageCopyRequest, CancelBeforeSend) {
+ librados::snap_t snap_id_end;
+ ASSERT_EQ(0, create_snap("copy", &snap_id_end));
+
+ librbd::MockTestImageCtx mock_src_image_ctx(*m_src_image_ctx);
+ librbd::MockTestImageCtx mock_dst_image_ctx(*m_dst_image_ctx);
+
+ InSequence seq;
+
+ MockDiffRequest mock_diff_request;
+ expect_diff_send(mock_diff_request, {}, -EINVAL);
+ expect_get_image_size(mock_src_image_ctx, 2 * (1 << m_src_image_ctx->order));
+ expect_get_image_size(mock_src_image_ctx, 0);
+
+ librbd::deep_copy::NoOpHandler no_op;
+ C_SaferCond ctx;
+ auto request = new MockImageCopyRequest(&mock_src_image_ctx,
+ &mock_dst_image_ctx,
+ 0, snap_id_end, 0, false, boost::none,
+ m_snap_seqs, &no_op, &ctx);
+ request->cancel();
+ request->send();
+
+ ASSERT_EQ(-ECANCELED, ctx.wait());
+}
+
+TEST_F(TestMockDeepCopyImageCopyRequest, MissingSnap) {
+ librbd::MockTestImageCtx mock_src_image_ctx(*m_src_image_ctx);
+ librbd::MockTestImageCtx mock_dst_image_ctx(*m_dst_image_ctx);
+
+ librbd::deep_copy::NoOpHandler no_op;
+ C_SaferCond ctx;
+ auto request = new MockImageCopyRequest(&mock_src_image_ctx,
+ &mock_dst_image_ctx,
+ 0, 123, 0, false, boost::none,
+ m_snap_seqs, &no_op, &ctx);
+ request->send();
+ ASSERT_EQ(-EINVAL, ctx.wait());
+}
+
+TEST_F(TestMockDeepCopyImageCopyRequest, MissingFromSnap) {
+ librados::snap_t snap_id_end;
+ ASSERT_EQ(0, create_snap("copy", &snap_id_end));
+
+ librbd::MockTestImageCtx mock_src_image_ctx(*m_src_image_ctx);
+ librbd::MockTestImageCtx mock_dst_image_ctx(*m_dst_image_ctx);
+
+ librbd::deep_copy::NoOpHandler no_op;
+ C_SaferCond ctx;
+ auto request = new MockImageCopyRequest(&mock_src_image_ctx,
+ &mock_dst_image_ctx,
+ 123, snap_id_end, 0, false,
+ boost::none, m_snap_seqs, &no_op,
+ &ctx);
+ request->send();
+ ASSERT_EQ(-EINVAL, ctx.wait());
+}
+
+TEST_F(TestMockDeepCopyImageCopyRequest, EmptySnapMap) {
+ librados::snap_t snap_id_start;
+ librados::snap_t snap_id_end;
+ ASSERT_EQ(0, create_snap("snap1", &snap_id_start));
+ ASSERT_EQ(0, create_snap("copy", &snap_id_end));
+
+ librbd::MockTestImageCtx mock_src_image_ctx(*m_src_image_ctx);
+ librbd::MockTestImageCtx mock_dst_image_ctx(*m_dst_image_ctx);
+
+ librbd::deep_copy::NoOpHandler no_op;
+ C_SaferCond ctx;
+ auto request = new MockImageCopyRequest(&mock_src_image_ctx,
+ &mock_dst_image_ctx,
+ snap_id_start, snap_id_end, 0, false,
+ boost::none, {{0, 0}}, &no_op, &ctx);
+ request->send();
+ ASSERT_EQ(-EINVAL, ctx.wait());
+}
+
+TEST_F(TestMockDeepCopyImageCopyRequest, EmptySnapSeqs) {
+ librados::snap_t snap_id_start;
+ librados::snap_t snap_id_end;
+ ASSERT_EQ(0, create_snap("snap1", &snap_id_start));
+ ASSERT_EQ(0, create_snap("copy", &snap_id_end));
+
+ librbd::MockTestImageCtx mock_src_image_ctx(*m_src_image_ctx);
+ librbd::MockTestImageCtx mock_dst_image_ctx(*m_dst_image_ctx);
+
+ librbd::deep_copy::NoOpHandler no_op;
+ C_SaferCond ctx;
+ auto request = new MockImageCopyRequest(&mock_src_image_ctx,
+ &mock_dst_image_ctx,
+ snap_id_start, snap_id_end, 0, false,
+ boost::none, {}, &no_op, &ctx);
+ request->send();
+ ASSERT_EQ(-EINVAL, ctx.wait());
+}
+
+} // namespace deep_copy
+} // namespace librbd
diff --git a/src/test/librbd/deep_copy/test_mock_MetadataCopyRequest.cc b/src/test/librbd/deep_copy/test_mock_MetadataCopyRequest.cc
new file mode 100644
index 000000000..ba59e3cdb
--- /dev/null
+++ b/src/test/librbd/deep_copy/test_mock_MetadataCopyRequest.cc
@@ -0,0 +1,220 @@
+// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
+// vim: ts=8 sw=2 smarttab
+
+#include "test/librbd/test_mock_fixture.h"
+#include "include/rbd/librbd.hpp"
+#include "include/stringify.h"
+#include "librbd/AsioEngine.h"
+#include "librbd/ImageCtx.h"
+#include "librbd/deep_copy/MetadataCopyRequest.h"
+#include "librbd/image/GetMetadataRequest.h"
+#include "test/librados_test_stub/MockTestMemIoCtxImpl.h"
+#include "test/librbd/mock/MockImageCtx.h"
+#include "test/librbd/test_support.h"
+#include <map>
+
+namespace librbd {
+namespace {
+
+struct MockTestImageCtx : public librbd::MockImageCtx {
+ MockTestImageCtx(librbd::ImageCtx &image_ctx)
+ : librbd::MockImageCtx(image_ctx) {
+ }
+};
+
+} // anonymous namespace
+
+namespace image {
+
+template <>
+struct GetMetadataRequest<MockTestImageCtx> {
+ std::map<std::string, bufferlist>* pairs = nullptr;
+ Context* on_finish = nullptr;
+
+ static GetMetadataRequest* s_instance;
+ static GetMetadataRequest* create(librados::IoCtx&,
+ const std::string& oid,
+ bool filter_internal,
+ const std::string& filter_key_prefix,
+ const std::string& last_key,
+ uint32_t max_results,
+ std::map<std::string, bufferlist>* pairs,
+ Context* on_finish) {
+ ceph_assert(s_instance != nullptr);
+ s_instance->pairs = pairs;
+ s_instance->on_finish = on_finish;
+ return s_instance;
+ }
+
+ GetMetadataRequest() {
+ s_instance = this;
+ }
+
+ MOCK_METHOD0(send, void());
+};
+
+GetMetadataRequest<MockTestImageCtx>* GetMetadataRequest<MockTestImageCtx>::s_instance = nullptr;
+
+} // namspace image
+} // namespace librbd
+
+// template definitions
+#include "librbd/deep_copy/MetadataCopyRequest.cc"
+
+namespace librbd {
+namespace deep_copy {
+
+using ::testing::_;
+using ::testing::DoAll;
+using ::testing::InSequence;
+using ::testing::Invoke;
+using ::testing::Return;
+using ::testing::StrEq;
+using ::testing::WithArg;
+
+class TestMockDeepCopyMetadataCopyRequest : public TestMockFixture {
+public:
+ typedef MetadataCopyRequest<librbd::MockTestImageCtx> MockMetadataCopyRequest;
+ typedef image::GetMetadataRequest<MockTestImageCtx> MockGetMetadataRequest;
+ typedef std::map<std::string, bufferlist> Metadata;
+
+ librbd::ImageCtx *m_src_image_ctx;
+ librbd::ImageCtx *m_dst_image_ctx;
+
+ std::shared_ptr<librbd::AsioEngine> m_asio_engine;
+ asio::ContextWQ *m_work_queue;
+
+ void SetUp() override {
+ TestMockFixture::SetUp();
+
+ ASSERT_EQ(0, open_image(m_image_name, &m_src_image_ctx));
+
+ librbd::RBD rbd;
+ std::string dst_image_name = get_temp_image_name();
+ ASSERT_EQ(0, create_image_pp(rbd, m_ioctx, dst_image_name, m_image_size));
+ ASSERT_EQ(0, open_image(dst_image_name, &m_dst_image_ctx));
+
+ m_asio_engine = std::make_shared<librbd::AsioEngine>(
+ m_src_image_ctx->md_ctx);
+ m_work_queue = m_asio_engine->get_work_queue();
+ }
+
+ void expect_get_metadata(MockGetMetadataRequest& mock_request,
+ const Metadata& metadata, int r) {
+ EXPECT_CALL(mock_request, send())
+ .WillOnce(Invoke([this, &mock_request, metadata, r]() {
+ *mock_request.pairs = metadata;
+ m_work_queue->queue(mock_request.on_finish, r);
+ }));
+ }
+
+ void expect_metadata_set(librbd::MockTestImageCtx &mock_image_ctx,
+ const Metadata& metadata, int r) {
+ bufferlist in_bl;
+ encode(metadata, in_bl);
+
+ EXPECT_CALL(get_mock_io_ctx(mock_image_ctx.md_ctx),
+ exec(mock_image_ctx.header_oid, _, StrEq("rbd"),
+ StrEq("metadata_set"), ContentsEqual(in_bl), _, _, _))
+ .WillOnce(Return(r));
+ }
+};
+
+TEST_F(TestMockDeepCopyMetadataCopyRequest, Success) {
+ librbd::MockTestImageCtx mock_src_image_ctx(*m_src_image_ctx);
+ librbd::MockTestImageCtx mock_dst_image_ctx(*m_dst_image_ctx);
+
+ size_t idx = 1;
+ Metadata key_values_1;
+ for (; idx <= 128; ++idx) {
+ bufferlist bl;
+ bl.append("value" + stringify(idx));
+ key_values_1.emplace("key" + stringify(idx), bl);
+ }
+
+ Metadata key_values_2;
+ for (; idx <= 255; ++idx) {
+ bufferlist bl;
+ bl.append("value" + stringify(idx));
+ key_values_2.emplace("key" + stringify(idx), bl);
+ }
+
+ InSequence seq;
+ MockGetMetadataRequest mock_request;
+ expect_get_metadata(mock_request, key_values_1, 0);
+ expect_metadata_set(mock_dst_image_ctx, key_values_1, 0);
+ expect_get_metadata(mock_request, key_values_2, 0);
+ expect_metadata_set(mock_dst_image_ctx, key_values_2, 0);
+
+ C_SaferCond ctx;
+ auto request = MockMetadataCopyRequest::create(&mock_src_image_ctx,
+ &mock_dst_image_ctx,
+ &ctx);
+ request->send();
+
+ ASSERT_EQ(0, ctx.wait());
+}
+
+TEST_F(TestMockDeepCopyMetadataCopyRequest, Empty) {
+ librbd::MockTestImageCtx mock_src_image_ctx(*m_src_image_ctx);
+ librbd::MockTestImageCtx mock_dst_image_ctx(*m_dst_image_ctx);
+
+ Metadata key_values;
+
+ InSequence seq;
+ MockGetMetadataRequest mock_request;
+ expect_get_metadata(mock_request, key_values, 0);
+
+ C_SaferCond ctx;
+ auto request = MockMetadataCopyRequest::create(&mock_src_image_ctx,
+ &mock_dst_image_ctx,
+ &ctx);
+ request->send();
+
+ ASSERT_EQ(0, ctx.wait());
+}
+
+TEST_F(TestMockDeepCopyMetadataCopyRequest, MetadataListError) {
+ librbd::MockTestImageCtx mock_src_image_ctx(*m_src_image_ctx);
+ librbd::MockTestImageCtx mock_dst_image_ctx(*m_dst_image_ctx);
+
+ Metadata key_values;
+
+ InSequence seq;
+ MockGetMetadataRequest mock_request;
+ expect_get_metadata(mock_request, key_values, -EINVAL);
+
+ C_SaferCond ctx;
+ auto request = MockMetadataCopyRequest::create(&mock_src_image_ctx,
+ &mock_dst_image_ctx,
+ &ctx);
+ request->send();
+
+ ASSERT_EQ(-EINVAL, ctx.wait());
+}
+
+TEST_F(TestMockDeepCopyMetadataCopyRequest, MetadataSetError) {
+ librbd::MockTestImageCtx mock_src_image_ctx(*m_src_image_ctx);
+ librbd::MockTestImageCtx mock_dst_image_ctx(*m_dst_image_ctx);
+
+ Metadata key_values;
+ bufferlist bl;
+ bl.append("value");
+ key_values.emplace("key", bl);
+
+ InSequence seq;
+ MockGetMetadataRequest mock_request;
+ expect_get_metadata(mock_request, key_values, 0);
+ expect_metadata_set(mock_dst_image_ctx, key_values, -EINVAL);
+
+ C_SaferCond ctx;
+ auto request = MockMetadataCopyRequest::create(&mock_src_image_ctx,
+ &mock_dst_image_ctx,
+ &ctx);
+ request->send();
+
+ ASSERT_EQ(-EINVAL, ctx.wait());
+}
+
+} // namespace deep_sync
+} // namespace librbd
diff --git a/src/test/librbd/deep_copy/test_mock_ObjectCopyRequest.cc b/src/test/librbd/deep_copy/test_mock_ObjectCopyRequest.cc
new file mode 100644
index 000000000..d8bb0678a
--- /dev/null
+++ b/src/test/librbd/deep_copy/test_mock_ObjectCopyRequest.cc
@@ -0,0 +1,1103 @@
+// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
+// vim: ts=8 sw=2 smarttab
+
+#include "test/librbd/test_mock_fixture.h"
+#include "include/interval_set.h"
+#include "include/neorados/RADOS.hpp"
+#include "include/rbd/librbd.hpp"
+#include "include/rbd/object_map_types.h"
+#include "librbd/AsioEngine.h"
+#include "librbd/ImageCtx.h"
+#include "librbd/ImageState.h"
+#include "librbd/internal.h"
+#include "librbd/Operations.h"
+#include "librbd/api/Image.h"
+#include "librbd/api/Io.h"
+#include "librbd/deep_copy/ObjectCopyRequest.h"
+#include "librbd/deep_copy/Utils.h"
+#include "librbd/io/ReadResult.h"
+#include "librbd/io/Utils.h"
+#include "test/librados_test_stub/MockTestMemIoCtxImpl.h"
+#include "test/librbd/mock/MockImageCtx.h"
+#include "test/librbd/test_support.h"
+
+namespace librbd {
+namespace {
+
+struct MockTestImageCtx : public librbd::MockImageCtx {
+ explicit MockTestImageCtx(librbd::ImageCtx &image_ctx)
+ : librbd::MockImageCtx(image_ctx) {
+ }
+
+ MockTestImageCtx *parent = nullptr;
+};
+
+} // anonymous namespace
+
+namespace util {
+
+inline ImageCtx* get_image_ctx(MockTestImageCtx* image_ctx) {
+ return image_ctx->image_ctx;
+}
+
+} // namespace util
+
+namespace io {
+namespace util {
+
+template <> void file_to_extents(
+ MockTestImageCtx* image_ctx, uint64_t offset, uint64_t length,
+ uint64_t buffer_offset,
+ striper::LightweightObjectExtents* object_extents) {
+ Striper::file_to_extents(image_ctx->cct, &image_ctx->layout, offset, length,
+ 0, buffer_offset, object_extents);
+}
+
+template <> void extent_to_file(
+ MockTestImageCtx* image_ctx, uint64_t object_no, uint64_t offset,
+ uint64_t length,
+ std::vector<std::pair<uint64_t, uint64_t> >& extents) {
+ Striper::extent_to_file(image_ctx->cct, &image_ctx->layout, object_no,
+ offset, length, extents);
+}
+
+} // namespace util
+} // namespace io
+
+} // namespace librbd
+
+// template definitions
+#include "librbd/deep_copy/ObjectCopyRequest.cc"
+template class librbd::deep_copy::ObjectCopyRequest<librbd::MockTestImageCtx>;
+
+static bool operator==(const SnapContext& rhs, const SnapContext& lhs) {
+ return (rhs.seq == lhs.seq && rhs.snaps == lhs.snaps);
+}
+
+namespace librbd {
+namespace deep_copy {
+
+using ::testing::_;
+using ::testing::DoAll;
+using ::testing::DoDefault;
+using ::testing::InSequence;
+using ::testing::Invoke;
+using ::testing::Return;
+using ::testing::ReturnNew;
+using ::testing::WithArg;
+
+namespace {
+
+void scribble(librbd::ImageCtx *image_ctx, int num_ops, size_t max_size,
+ interval_set<uint64_t> *what)
+{
+ uint64_t object_size = 1 << image_ctx->order;
+ for (int i = 0; i < num_ops; i++) {
+ uint64_t off = rand() % (object_size - max_size + 1);
+ uint64_t len = 1 + rand() % max_size;
+ std::cout << __func__ << ": off=" << off << ", len=" << len << std::endl;
+
+ bufferlist bl;
+ bl.append(std::string(len, '1'));
+
+ int r = api::Io<>::write(*image_ctx, off, len, std::move(bl), 0);
+ ASSERT_EQ(static_cast<int>(len), r);
+
+ interval_set<uint64_t> w;
+ w.insert(off, len);
+ what->union_of(w);
+ }
+ std::cout << " wrote " << *what << std::endl;
+}
+
+} // anonymous namespace
+
+
+MATCHER(IsListSnaps, "") {
+ auto req = boost::get<io::ImageDispatchSpec::ListSnaps>(&arg->request);
+ return (req != nullptr);
+}
+
+MATCHER_P2(IsRead, snap_id, image_interval, "") {
+ auto req = boost::get<io::ImageDispatchSpec::Read>(&arg->request);
+ if (req == nullptr ||
+ arg->io_context->read_snap().value_or(CEPH_NOSNAP) != snap_id) {
+ return false;
+ }
+
+ // ensure the read request encloses the full snapshot delta
+ interval_set<uint64_t> expected_interval(image_interval);
+ interval_set<uint64_t> read_interval;
+ for (auto &image_extent : arg->image_extents) {
+ read_interval.insert(image_extent.first, image_extent.second);
+ }
+
+ interval_set<uint64_t> intersection;
+ intersection.intersection_of(expected_interval, read_interval);
+ expected_interval.subtract(intersection);
+ return expected_interval.empty();
+}
+
+class TestMockDeepCopyObjectCopyRequest : public TestMockFixture {
+public:
+ typedef ObjectCopyRequest<librbd::MockTestImageCtx> MockObjectCopyRequest;
+
+ librbd::ImageCtx *m_src_image_ctx;
+ librbd::ImageCtx *m_dst_image_ctx;
+
+ std::shared_ptr<librbd::AsioEngine> m_asio_engine;
+ asio::ContextWQ *m_work_queue;
+
+ SnapMap m_snap_map;
+ SnapSeqs m_snap_seqs;
+ std::vector<librados::snap_t> m_src_snap_ids;
+ std::vector<librados::snap_t> m_dst_snap_ids;
+
+ void SetUp() override {
+ TestMockFixture::SetUp();
+
+ ASSERT_EQ(0, open_image(m_image_name, &m_src_image_ctx));
+
+ librbd::NoOpProgressContext no_op;
+ m_image_size = 1 << m_src_image_ctx->order;
+ ASSERT_EQ(0, m_src_image_ctx->operations->resize(m_image_size, true, no_op));
+
+ librbd::RBD rbd;
+ std::string dst_image_name = get_temp_image_name();
+ ASSERT_EQ(0, create_image_pp(rbd, m_ioctx, dst_image_name, m_image_size));
+ ASSERT_EQ(0, open_image(dst_image_name, &m_dst_image_ctx));
+
+ m_asio_engine = std::make_shared<librbd::AsioEngine>(
+ m_src_image_ctx->md_ctx);
+ m_work_queue = m_asio_engine->get_work_queue();
+ }
+
+ bool is_fast_diff(librbd::MockImageCtx &mock_image_ctx) {
+ return (mock_image_ctx.features & RBD_FEATURE_FAST_DIFF) != 0;
+ }
+
+ void prepare_exclusive_lock(librbd::MockImageCtx &mock_image_ctx,
+ librbd::MockExclusiveLock &mock_exclusive_lock) {
+ if ((mock_image_ctx.features & RBD_FEATURE_EXCLUSIVE_LOCK) == 0) {
+ return;
+ }
+ mock_image_ctx.exclusive_lock = &mock_exclusive_lock;
+ }
+
+ void expect_get_object_count(librbd::MockImageCtx& mock_image_ctx) {
+ EXPECT_CALL(mock_image_ctx, get_object_count(_))
+ .WillRepeatedly(Invoke([&mock_image_ctx](librados::snap_t snap_id) {
+ return mock_image_ctx.image_ctx->get_object_count(snap_id);
+ }));
+ }
+
+ void expect_test_features(librbd::MockImageCtx &mock_image_ctx) {
+ EXPECT_CALL(mock_image_ctx, test_features(_))
+ .WillRepeatedly(WithArg<0>(Invoke([&mock_image_ctx](uint64_t features) {
+ return (mock_image_ctx.features & features) != 0;
+ })));
+ }
+
+ void expect_start_op(librbd::MockExclusiveLock &mock_exclusive_lock) {
+ if ((m_src_image_ctx->features & RBD_FEATURE_EXCLUSIVE_LOCK) == 0) {
+ return;
+ }
+ EXPECT_CALL(mock_exclusive_lock, start_op(_)).WillOnce(Return(new LambdaContext([](int){})));
+ }
+
+ void expect_list_snaps(librbd::MockTestImageCtx &mock_image_ctx, int r) {
+ EXPECT_CALL(*mock_image_ctx.io_image_dispatcher, send(IsListSnaps()))
+ .WillOnce(Invoke(
+ [&mock_image_ctx, r](io::ImageDispatchSpec* spec) {
+ if (r < 0) {
+ spec->fail(r);
+ return;
+ }
+
+ spec->image_dispatcher =
+ mock_image_ctx.image_ctx->io_image_dispatcher;
+ mock_image_ctx.image_ctx->io_image_dispatcher->send(spec);
+ }));
+ }
+
+ void expect_get_object_name(librbd::MockTestImageCtx &mock_image_ctx) {
+ EXPECT_CALL(mock_image_ctx, get_object_name(0))
+ .WillOnce(Return(mock_image_ctx.image_ctx->get_object_name(0)));
+ }
+
+ MockObjectCopyRequest *create_request(
+ librbd::MockTestImageCtx &mock_src_image_ctx,
+ librbd::MockTestImageCtx &mock_dst_image_ctx,
+ librados::snap_t src_snap_id_start,
+ librados::snap_t src_snap_id_end,
+ librados::snap_t dst_snap_id_start,
+ uint32_t flags, Context *on_finish) {
+ SnapMap snap_map;
+ util::compute_snap_map(mock_dst_image_ctx.cct, src_snap_id_start,
+ src_snap_id_end, m_dst_snap_ids, m_snap_seqs,
+ &snap_map);
+
+ expect_get_object_name(mock_dst_image_ctx);
+ return new MockObjectCopyRequest(&mock_src_image_ctx, &mock_dst_image_ctx,
+ src_snap_id_start, dst_snap_id_start,
+ snap_map, 0, flags, nullptr, on_finish);
+ }
+
+ void expect_read(librbd::MockTestImageCtx& mock_image_ctx,
+ uint64_t snap_id, uint64_t offset, uint64_t length, int r) {
+ interval_set<uint64_t> extents;
+ extents.insert(offset, length);
+ expect_read(mock_image_ctx, snap_id, extents, r);
+ }
+
+ void expect_read(librbd::MockTestImageCtx& mock_image_ctx, uint64_t snap_id,
+ const interval_set<uint64_t> &extents, int r) {
+ EXPECT_CALL(*mock_image_ctx.io_image_dispatcher,
+ send(IsRead(snap_id, extents)))
+ .WillOnce(Invoke(
+ [&mock_image_ctx, r](io::ImageDispatchSpec* spec) {
+ if (r < 0) {
+ spec->fail(r);
+ return;
+ }
+
+ spec->image_dispatcher =
+ mock_image_ctx.image_ctx->io_image_dispatcher;
+ mock_image_ctx.image_ctx->io_image_dispatcher->send(spec);
+ }));
+ }
+
+ void expect_write(librados::MockTestMemIoCtxImpl &mock_io_ctx,
+ uint64_t offset, uint64_t length,
+ const SnapContext &snapc, int r) {
+ auto &expect = EXPECT_CALL(mock_io_ctx, write(_, _, length, offset, snapc));
+ if (r < 0) {
+ expect.WillOnce(Return(r));
+ } else {
+ expect.WillOnce(DoDefault());
+ }
+ }
+
+ void expect_write(librados::MockTestMemIoCtxImpl &mock_io_ctx,
+ const interval_set<uint64_t> &extents,
+ const SnapContext &snapc, int r) {
+ for (auto extent : extents) {
+ expect_write(mock_io_ctx, extent.first, extent.second, snapc, r);
+ if (r < 0) {
+ break;
+ }
+ }
+ }
+
+ void expect_truncate(librados::MockTestMemIoCtxImpl &mock_io_ctx,
+ uint64_t offset, int r) {
+ auto &expect = EXPECT_CALL(mock_io_ctx, truncate(_, offset, _));
+ if (r < 0) {
+ expect.WillOnce(Return(r));
+ } else {
+ expect.WillOnce(DoDefault());
+ }
+ }
+
+ void expect_remove(librados::MockTestMemIoCtxImpl &mock_io_ctx, int r) {
+ auto &expect = EXPECT_CALL(mock_io_ctx, remove(_, _));
+ if (r < 0) {
+ expect.WillOnce(Return(r));
+ } else {
+ expect.WillOnce(DoDefault());
+ }
+ }
+
+ void expect_update_object_map(librbd::MockTestImageCtx &mock_image_ctx,
+ librbd::MockObjectMap &mock_object_map,
+ librados::snap_t snap_id, uint8_t state,
+ int r) {
+ if (mock_image_ctx.image_ctx->object_map != nullptr) {
+ auto &expect = EXPECT_CALL(mock_object_map, aio_update(snap_id, 0, 1, state, _, _, false, _));
+ if (r < 0) {
+ expect.WillOnce(DoAll(WithArg<7>(Invoke([this, r](Context *ctx) {
+ m_work_queue->queue(ctx, r);
+ })),
+ Return(true)));
+ } else {
+ expect.WillOnce(DoAll(WithArg<7>(Invoke([&mock_image_ctx, snap_id, state](Context *ctx) {
+ ceph_assert(ceph_mutex_is_locked(mock_image_ctx.image_ctx->image_lock));
+ mock_image_ctx.image_ctx->object_map->aio_update<Context>(
+ snap_id, 0, 1, state, boost::none, {}, false, ctx);
+ })),
+ Return(true)));
+ }
+ }
+ }
+
+ void expect_prepare_copyup(MockTestImageCtx& mock_image_ctx, int r = 0) {
+ EXPECT_CALL(*mock_image_ctx.io_object_dispatcher,
+ prepare_copyup(_, _)).WillOnce(Return(r));
+ }
+
+ int create_snap(librbd::ImageCtx *image_ctx, const char* snap_name,
+ librados::snap_t *snap_id) {
+ NoOpProgressContext prog_ctx;
+ int r = image_ctx->operations->snap_create(
+ cls::rbd::UserSnapshotNamespace(), snap_name, 0, prog_ctx);
+ if (r < 0) {
+ return r;
+ }
+
+ r = image_ctx->state->refresh();
+ if (r < 0) {
+ return r;
+ }
+
+ if (image_ctx->snap_ids.count({cls::rbd::UserSnapshotNamespace(),
+ snap_name}) == 0) {
+ return -ENOENT;
+ }
+
+ if (snap_id != nullptr) {
+ *snap_id = image_ctx->snap_ids[{cls::rbd::UserSnapshotNamespace(),
+ snap_name}];
+ }
+ return 0;
+ }
+
+ int create_snap(const char* snap_name) {
+ librados::snap_t src_snap_id;
+ int r = create_snap(m_src_image_ctx, snap_name, &src_snap_id);
+ if (r < 0) {
+ return r;
+ }
+
+ librados::snap_t dst_snap_id;
+ r = create_snap(m_dst_image_ctx, snap_name, &dst_snap_id);
+ if (r < 0) {
+ return r;
+ }
+
+ // collection of all existing snaps in dst image
+ SnapIds dst_snap_ids({dst_snap_id});
+ if (!m_snap_map.empty()) {
+ dst_snap_ids.insert(dst_snap_ids.end(),
+ m_snap_map.rbegin()->second.begin(),
+ m_snap_map.rbegin()->second.end());
+ }
+ m_snap_map[src_snap_id] = dst_snap_ids;
+ m_snap_seqs[src_snap_id] = dst_snap_id;
+ m_src_snap_ids.push_back(src_snap_id);
+ m_dst_snap_ids.push_back(dst_snap_id);
+
+ return 0;
+ }
+
+ std::string get_snap_name(librbd::ImageCtx *image_ctx,
+ librados::snap_t snap_id) {
+ auto it = std::find_if(image_ctx->snap_ids.begin(),
+ image_ctx->snap_ids.end(),
+ [snap_id](const std::pair<std::pair<cls::rbd::SnapshotNamespace,
+ std::string>,
+ librados::snap_t> &pair) {
+ return (pair.second == snap_id);
+ });
+ if (it == image_ctx->snap_ids.end()) {
+ return "";
+ }
+ return it->first.second;
+ }
+
+ int copy_objects() {
+ int r;
+ uint64_t object_size = 1 << m_src_image_ctx->order;
+
+ bufferlist bl;
+ bl.append(std::string(object_size, '1'));
+ r = api::Io<>::read(*m_src_image_ctx, 0, object_size,
+ librbd::io::ReadResult{&bl}, 0);
+ if (r < 0) {
+ return r;
+ }
+
+ r = api::Io<>::write(*m_dst_image_ctx, 0, object_size, std::move(bl), 0);
+ if (r < 0) {
+ return r;
+ }
+
+ return 0;
+ }
+
+ int compare_objects() {
+ SnapMap snap_map(m_snap_map);
+ if (snap_map.empty()) {
+ return -ENOENT;
+ }
+
+ int r;
+ uint64_t object_size = 1 << m_src_image_ctx->order;
+ while (!snap_map.empty()) {
+ librados::snap_t src_snap_id = snap_map.begin()->first;
+ librados::snap_t dst_snap_id = *snap_map.begin()->second.begin();
+ snap_map.erase(snap_map.begin());
+
+ std::string snap_name = get_snap_name(m_src_image_ctx, src_snap_id);
+ if (snap_name.empty()) {
+ return -ENOENT;
+ }
+
+ std::cout << "comparing '" << snap_name << " (" << src_snap_id
+ << " to " << dst_snap_id << ")" << std::endl;
+
+ r = librbd::api::Image<>::snap_set(m_src_image_ctx,
+ cls::rbd::UserSnapshotNamespace(),
+ snap_name.c_str());
+ if (r < 0) {
+ return r;
+ }
+
+ r = librbd::api::Image<>::snap_set(m_dst_image_ctx,
+ cls::rbd::UserSnapshotNamespace(),
+ snap_name.c_str());
+ if (r < 0) {
+ return r;
+ }
+
+ bufferlist src_bl;
+ src_bl.append(std::string(object_size, '1'));
+ r = api::Io<>::read(
+ *m_src_image_ctx, 0, object_size, librbd::io::ReadResult{&src_bl}, 0);
+ if (r < 0) {
+ return r;
+ }
+
+ bufferlist dst_bl;
+ dst_bl.append(std::string(object_size, '1'));
+ r = api::Io<>::read(
+ *m_dst_image_ctx, 0, object_size, librbd::io::ReadResult{&dst_bl}, 0);
+ if (r < 0) {
+ return r;
+ }
+
+ if (!src_bl.contents_equal(dst_bl)) {
+ std::cout << "src block: " << std::endl; src_bl.hexdump(std::cout);
+ std::cout << "dst block: " << std::endl; dst_bl.hexdump(std::cout);
+ return -EBADMSG;
+ }
+ }
+
+ r = librbd::api::Image<>::snap_set(m_src_image_ctx,
+ cls::rbd::UserSnapshotNamespace(),
+ nullptr);
+ if (r < 0) {
+ return r;
+ }
+ r = librbd::api::Image<>::snap_set(m_dst_image_ctx,
+ cls::rbd::UserSnapshotNamespace(),
+ nullptr);
+ if (r < 0) {
+ return r;
+ }
+
+ return 0;
+ }
+};
+
+TEST_F(TestMockDeepCopyObjectCopyRequest, DNE) {
+ ASSERT_EQ(0, create_snap("copy"));
+ librbd::MockTestImageCtx mock_src_image_ctx(*m_src_image_ctx);
+ librbd::MockTestImageCtx mock_dst_image_ctx(*m_dst_image_ctx);
+
+ librbd::MockExclusiveLock mock_exclusive_lock;
+ prepare_exclusive_lock(mock_dst_image_ctx, mock_exclusive_lock);
+
+ librbd::MockObjectMap mock_object_map;
+ mock_dst_image_ctx.object_map = &mock_object_map;
+ expect_op_work_queue(mock_src_image_ctx);
+ expect_test_features(mock_dst_image_ctx);
+ expect_get_object_count(mock_dst_image_ctx);
+
+ C_SaferCond ctx;
+ MockObjectCopyRequest *request = create_request(mock_src_image_ctx,
+ mock_dst_image_ctx, 0,
+ CEPH_NOSNAP, 0, 0, &ctx);
+
+ InSequence seq;
+ expect_list_snaps(mock_src_image_ctx, -ENOENT);
+
+ request->send();
+ ASSERT_EQ(-ENOENT, ctx.wait());
+}
+
+TEST_F(TestMockDeepCopyObjectCopyRequest, Write) {
+ // scribble some data
+ interval_set<uint64_t> one;
+ scribble(m_src_image_ctx, 10, 102400, &one);
+
+ ASSERT_EQ(0, create_snap("copy"));
+ librbd::MockTestImageCtx mock_src_image_ctx(*m_src_image_ctx);
+ librbd::MockTestImageCtx mock_dst_image_ctx(*m_dst_image_ctx);
+
+ librbd::MockExclusiveLock mock_exclusive_lock;
+ prepare_exclusive_lock(mock_dst_image_ctx, mock_exclusive_lock);
+
+ librbd::MockObjectMap mock_object_map;
+ mock_dst_image_ctx.object_map = &mock_object_map;
+
+ expect_op_work_queue(mock_src_image_ctx);
+ expect_test_features(mock_dst_image_ctx);
+ expect_get_object_count(mock_dst_image_ctx);
+
+ C_SaferCond ctx;
+ MockObjectCopyRequest *request = create_request(mock_src_image_ctx,
+ mock_dst_image_ctx, 0,
+ CEPH_NOSNAP, 0, 0, &ctx);
+
+ librados::MockTestMemIoCtxImpl &mock_dst_io_ctx(get_mock_io_ctx(
+ request->get_dst_io_ctx()));
+
+ InSequence seq;
+ expect_list_snaps(mock_src_image_ctx, 0);
+ expect_read(mock_src_image_ctx, m_src_snap_ids[0], 0, one.range_end(), 0);
+ expect_start_op(mock_exclusive_lock);
+ expect_update_object_map(mock_dst_image_ctx, mock_object_map,
+ m_dst_snap_ids[0], OBJECT_EXISTS, 0);
+ expect_prepare_copyup(mock_dst_image_ctx);
+ expect_start_op(mock_exclusive_lock);
+ expect_write(mock_dst_io_ctx, 0, one.range_end(), {0, {}}, 0);
+
+ request->send();
+ ASSERT_EQ(0, ctx.wait());
+ ASSERT_EQ(0, compare_objects());
+}
+
+TEST_F(TestMockDeepCopyObjectCopyRequest, ReadError) {
+ // scribble some data
+ interval_set<uint64_t> one;
+ scribble(m_src_image_ctx, 10, 102400, &one);
+
+ ASSERT_EQ(0, create_snap("copy"));
+ librbd::MockTestImageCtx mock_src_image_ctx(*m_src_image_ctx);
+ librbd::MockTestImageCtx mock_dst_image_ctx(*m_dst_image_ctx);
+
+ librbd::MockExclusiveLock mock_exclusive_lock;
+ prepare_exclusive_lock(mock_dst_image_ctx, mock_exclusive_lock);
+
+ librbd::MockObjectMap mock_object_map;
+ mock_dst_image_ctx.object_map = &mock_object_map;
+
+ expect_op_work_queue(mock_src_image_ctx);
+ expect_test_features(mock_dst_image_ctx);
+ expect_get_object_count(mock_dst_image_ctx);
+
+ C_SaferCond ctx;
+ MockObjectCopyRequest *request = create_request(mock_src_image_ctx,
+ mock_dst_image_ctx, 0,
+ CEPH_NOSNAP, 0, 0, &ctx);
+
+ InSequence seq;
+ expect_list_snaps(mock_src_image_ctx, 0);
+ expect_read(mock_src_image_ctx, m_src_snap_ids[0], 0, one.range_end(),
+ -EINVAL);
+
+ request->send();
+ ASSERT_EQ(-EINVAL, ctx.wait());
+}
+
+TEST_F(TestMockDeepCopyObjectCopyRequest, WriteError) {
+ // scribble some data
+ interval_set<uint64_t> one;
+ scribble(m_src_image_ctx, 10, 102400, &one);
+
+ ASSERT_EQ(0, create_snap("copy"));
+ librbd::MockTestImageCtx mock_src_image_ctx(*m_src_image_ctx);
+ librbd::MockTestImageCtx mock_dst_image_ctx(*m_dst_image_ctx);
+
+ librbd::MockExclusiveLock mock_exclusive_lock;
+ prepare_exclusive_lock(mock_dst_image_ctx, mock_exclusive_lock);
+
+ librbd::MockObjectMap mock_object_map;
+ mock_dst_image_ctx.object_map = &mock_object_map;
+
+ expect_op_work_queue(mock_src_image_ctx);
+ expect_test_features(mock_dst_image_ctx);
+ expect_get_object_count(mock_dst_image_ctx);
+
+ C_SaferCond ctx;
+ MockObjectCopyRequest *request = create_request(mock_src_image_ctx,
+ mock_dst_image_ctx, 0,
+ CEPH_NOSNAP, 0, 0, &ctx);
+
+ librados::MockTestMemIoCtxImpl &mock_dst_io_ctx(get_mock_io_ctx(
+ request->get_dst_io_ctx()));
+
+ InSequence seq;
+ expect_list_snaps(mock_src_image_ctx, 0);
+ expect_read(mock_src_image_ctx, m_src_snap_ids[0], 0, one.range_end(), 0);
+
+ expect_start_op(mock_exclusive_lock);
+ expect_update_object_map(mock_dst_image_ctx, mock_object_map,
+ m_dst_snap_ids[0], OBJECT_EXISTS, 0);
+
+ expect_prepare_copyup(mock_dst_image_ctx);
+ expect_start_op(mock_exclusive_lock);
+ expect_write(mock_dst_io_ctx, 0, one.range_end(), {0, {}}, -EINVAL);
+
+ request->send();
+ ASSERT_EQ(-EINVAL, ctx.wait());
+}
+
+TEST_F(TestMockDeepCopyObjectCopyRequest, WriteSnaps) {
+ // scribble some data
+ interval_set<uint64_t> one;
+ scribble(m_src_image_ctx, 10, 102400, &one);
+ ASSERT_EQ(0, create_snap("one"));
+
+ interval_set<uint64_t> two;
+ scribble(m_src_image_ctx, 10, 102400, &two);
+ ASSERT_EQ(0, create_snap("two"));
+
+ if (one.range_end() < two.range_end()) {
+ interval_set<uint64_t> resize_diff;
+ resize_diff.insert(one.range_end(), two.range_end() - one.range_end());
+ two.union_of(resize_diff);
+ }
+
+ ASSERT_EQ(0, create_snap("copy"));
+ librbd::MockTestImageCtx mock_src_image_ctx(*m_src_image_ctx);
+ librbd::MockTestImageCtx mock_dst_image_ctx(*m_dst_image_ctx);
+
+ librbd::MockExclusiveLock mock_exclusive_lock;
+ prepare_exclusive_lock(mock_dst_image_ctx, mock_exclusive_lock);
+
+ librbd::MockObjectMap mock_object_map;
+ mock_dst_image_ctx.object_map = &mock_object_map;
+
+ expect_op_work_queue(mock_src_image_ctx);
+ expect_test_features(mock_dst_image_ctx);
+ expect_get_object_count(mock_dst_image_ctx);
+
+ C_SaferCond ctx;
+ MockObjectCopyRequest *request = create_request(mock_src_image_ctx,
+ mock_dst_image_ctx, 0,
+ CEPH_NOSNAP, 0, 0, &ctx);
+
+ librados::MockTestMemIoCtxImpl &mock_dst_io_ctx(get_mock_io_ctx(
+ request->get_dst_io_ctx()));
+
+ InSequence seq;
+ expect_list_snaps(mock_src_image_ctx, 0);
+ expect_read(mock_src_image_ctx, m_src_snap_ids[0], 0, one.range_end(), 0);
+ expect_read(mock_src_image_ctx, m_src_snap_ids[2], two, 0);
+ expect_start_op(mock_exclusive_lock);
+ expect_update_object_map(mock_dst_image_ctx, mock_object_map,
+ m_dst_snap_ids[0], OBJECT_EXISTS, 0);
+ expect_start_op(mock_exclusive_lock);
+ expect_update_object_map(mock_dst_image_ctx, mock_object_map,
+ m_dst_snap_ids[1], OBJECT_EXISTS, 0);
+ expect_start_op(mock_exclusive_lock);
+ expect_update_object_map(mock_dst_image_ctx, mock_object_map,
+ m_dst_snap_ids[2], is_fast_diff(mock_dst_image_ctx) ?
+ OBJECT_EXISTS_CLEAN : OBJECT_EXISTS, 0);
+ expect_prepare_copyup(mock_dst_image_ctx);
+ expect_start_op(mock_exclusive_lock);
+ expect_write(mock_dst_io_ctx, 0, one.range_end(), {0, {}}, 0);
+ expect_start_op(mock_exclusive_lock);
+ expect_write(mock_dst_io_ctx, two,
+ {m_dst_snap_ids[0], {m_dst_snap_ids[0]}}, 0);
+
+ request->send();
+ ASSERT_EQ(0, ctx.wait());
+ ASSERT_EQ(0, compare_objects());
+}
+
+TEST_F(TestMockDeepCopyObjectCopyRequest, Trim) {
+ ASSERT_EQ(0, m_src_image_ctx->operations->metadata_set(
+ "conf_rbd_skip_partial_discard", "false"));
+ m_src_image_ctx->discard_granularity_bytes = 0;
+
+ // scribble some data
+ interval_set<uint64_t> one;
+ scribble(m_src_image_ctx, 10, 102400, &one);
+ ASSERT_EQ(0, create_snap("one"));
+
+ // trim the object
+ uint64_t trim_offset = rand() % one.range_end();
+ ASSERT_LE(0, api::Io<>::discard(
+ *m_src_image_ctx, trim_offset, one.range_end() - trim_offset,
+ m_src_image_ctx->discard_granularity_bytes));
+ ASSERT_EQ(0, create_snap("copy"));
+
+ librbd::MockTestImageCtx mock_src_image_ctx(*m_src_image_ctx);
+ librbd::MockTestImageCtx mock_dst_image_ctx(*m_dst_image_ctx);
+
+ librbd::MockExclusiveLock mock_exclusive_lock;
+ prepare_exclusive_lock(mock_dst_image_ctx, mock_exclusive_lock);
+
+ librbd::MockObjectMap mock_object_map;
+ mock_dst_image_ctx.object_map = &mock_object_map;
+
+ expect_op_work_queue(mock_src_image_ctx);
+ expect_test_features(mock_dst_image_ctx);
+ expect_get_object_count(mock_dst_image_ctx);
+
+ C_SaferCond ctx;
+ MockObjectCopyRequest *request = create_request(mock_src_image_ctx,
+ mock_dst_image_ctx, 0,
+ CEPH_NOSNAP, 0, 0, &ctx);
+
+ librados::MockTestMemIoCtxImpl &mock_dst_io_ctx(get_mock_io_ctx(
+ request->get_dst_io_ctx()));
+
+ InSequence seq;
+ expect_list_snaps(mock_src_image_ctx, 0);
+ expect_read(mock_src_image_ctx, m_src_snap_ids[0], 0, one.range_end(), 0);
+ expect_start_op(mock_exclusive_lock);
+ expect_update_object_map(mock_dst_image_ctx, mock_object_map,
+ m_dst_snap_ids[0], OBJECT_EXISTS, 0);
+ expect_start_op(mock_exclusive_lock);
+ expect_update_object_map(mock_dst_image_ctx, mock_object_map,
+ m_dst_snap_ids[1], OBJECT_EXISTS, 0);
+ expect_prepare_copyup(mock_dst_image_ctx);
+ expect_start_op(mock_exclusive_lock);
+ expect_write(mock_dst_io_ctx, 0, one.range_end(), {0, {}}, 0);
+ expect_start_op(mock_exclusive_lock);
+ expect_truncate(mock_dst_io_ctx, trim_offset, 0);
+
+ request->send();
+ ASSERT_EQ(0, ctx.wait());
+ ASSERT_EQ(0, compare_objects());
+}
+
+TEST_F(TestMockDeepCopyObjectCopyRequest, Remove) {
+ // scribble some data
+ interval_set<uint64_t> one;
+ scribble(m_src_image_ctx, 10, 102400, &one);
+ ASSERT_EQ(0, create_snap("one"));
+ ASSERT_EQ(0, create_snap("two"));
+
+ // remove the object
+ uint64_t object_size = 1 << m_src_image_ctx->order;
+ ASSERT_LE(0, api::Io<>::discard(
+ *m_src_image_ctx, 0, object_size,
+ m_src_image_ctx->discard_granularity_bytes));
+ ASSERT_EQ(0, create_snap("copy"));
+ librbd::MockTestImageCtx mock_src_image_ctx(*m_src_image_ctx);
+ librbd::MockTestImageCtx mock_dst_image_ctx(*m_dst_image_ctx);
+
+ librbd::MockExclusiveLock mock_exclusive_lock;
+ prepare_exclusive_lock(mock_dst_image_ctx, mock_exclusive_lock);
+
+ librbd::MockObjectMap mock_object_map;
+ mock_dst_image_ctx.object_map = &mock_object_map;
+
+ expect_op_work_queue(mock_src_image_ctx);
+ expect_test_features(mock_dst_image_ctx);
+ expect_get_object_count(mock_dst_image_ctx);
+
+ C_SaferCond ctx;
+ MockObjectCopyRequest *request = create_request(mock_src_image_ctx,
+ mock_dst_image_ctx, 0,
+ CEPH_NOSNAP, 0, 0, &ctx);
+
+ librados::MockTestMemIoCtxImpl &mock_dst_io_ctx(get_mock_io_ctx(
+ request->get_dst_io_ctx()));
+
+ InSequence seq;
+ expect_list_snaps(mock_src_image_ctx, 0);
+ expect_read(mock_src_image_ctx, m_src_snap_ids[1], 0, one.range_end(), 0);
+
+ expect_start_op(mock_exclusive_lock);
+ uint8_t state = OBJECT_EXISTS;
+ expect_update_object_map(mock_dst_image_ctx, mock_object_map,
+ m_dst_snap_ids[0], state, 0);
+ expect_start_op(mock_exclusive_lock);
+ expect_update_object_map(mock_dst_image_ctx, mock_object_map,
+ m_dst_snap_ids[1], is_fast_diff(mock_dst_image_ctx) ?
+ OBJECT_EXISTS_CLEAN : OBJECT_EXISTS, 0);
+
+ expect_prepare_copyup(mock_dst_image_ctx);
+ expect_start_op(mock_exclusive_lock);
+ expect_write(mock_dst_io_ctx, 0, one.range_end(), {0, {}}, 0);
+ expect_start_op(mock_exclusive_lock);
+ expect_remove(mock_dst_io_ctx, 0);
+
+ request->send();
+ ASSERT_EQ(0, ctx.wait());
+ ASSERT_EQ(0, compare_objects());
+}
+
+TEST_F(TestMockDeepCopyObjectCopyRequest, ObjectMapUpdateError) {
+ REQUIRE_FEATURE(RBD_FEATURE_OBJECT_MAP);
+
+ // scribble some data
+ interval_set<uint64_t> one;
+ scribble(m_src_image_ctx, 10, 102400, &one);
+
+ ASSERT_EQ(0, create_snap("copy"));
+ librbd::MockTestImageCtx mock_src_image_ctx(*m_src_image_ctx);
+ librbd::MockTestImageCtx mock_dst_image_ctx(*m_dst_image_ctx);
+
+ librbd::MockExclusiveLock mock_exclusive_lock;
+ prepare_exclusive_lock(mock_dst_image_ctx, mock_exclusive_lock);
+
+ librbd::MockObjectMap mock_object_map;
+ mock_dst_image_ctx.object_map = &mock_object_map;
+
+ expect_op_work_queue(mock_src_image_ctx);
+ expect_test_features(mock_dst_image_ctx);
+ expect_get_object_count(mock_dst_image_ctx);
+
+ C_SaferCond ctx;
+ MockObjectCopyRequest *request = create_request(mock_src_image_ctx,
+ mock_dst_image_ctx, 0,
+ CEPH_NOSNAP, 0, 0, &ctx);
+
+ InSequence seq;
+ expect_list_snaps(mock_src_image_ctx, 0);
+ expect_read(mock_src_image_ctx, m_src_snap_ids[0], 0, one.range_end(), 0);
+ expect_start_op(mock_exclusive_lock);
+ expect_update_object_map(mock_dst_image_ctx, mock_object_map,
+ m_dst_snap_ids[0], OBJECT_EXISTS, -EBLOCKLISTED);
+
+ request->send();
+ ASSERT_EQ(-EBLOCKLISTED, ctx.wait());
+}
+
+TEST_F(TestMockDeepCopyObjectCopyRequest, PrepareCopyupError) {
+ // scribble some data
+ interval_set<uint64_t> one;
+ scribble(m_src_image_ctx, 10, 102400, &one);
+
+ ASSERT_EQ(0, create_snap("copy"));
+ librbd::MockTestImageCtx mock_src_image_ctx(*m_src_image_ctx);
+ librbd::MockTestImageCtx mock_dst_image_ctx(*m_dst_image_ctx);
+
+ librbd::MockExclusiveLock mock_exclusive_lock;
+ prepare_exclusive_lock(mock_dst_image_ctx, mock_exclusive_lock);
+
+ librbd::MockObjectMap mock_object_map;
+ mock_dst_image_ctx.object_map = &mock_object_map;
+
+ expect_op_work_queue(mock_src_image_ctx);
+ expect_test_features(mock_dst_image_ctx);
+ expect_get_object_count(mock_dst_image_ctx);
+
+ C_SaferCond ctx;
+ MockObjectCopyRequest *request = create_request(mock_src_image_ctx,
+ mock_dst_image_ctx, 0,
+ CEPH_NOSNAP, 0, 0, &ctx);
+
+ InSequence seq;
+ expect_list_snaps(mock_src_image_ctx, 0);
+ expect_read(mock_src_image_ctx, m_src_snap_ids[0], 0, one.range_end(), 0);
+
+ expect_start_op(mock_exclusive_lock);
+ expect_update_object_map(mock_dst_image_ctx, mock_object_map,
+ m_dst_snap_ids[0], OBJECT_EXISTS, 0);
+
+ expect_prepare_copyup(mock_dst_image_ctx, -EIO);
+
+ request->send();
+ ASSERT_EQ(-EIO, ctx.wait());
+}
+
+TEST_F(TestMockDeepCopyObjectCopyRequest, WriteSnapsStart) {
+ // scribble some data
+ interval_set<uint64_t> one;
+ scribble(m_src_image_ctx, 10, 102400, &one);
+ ASSERT_EQ(0, copy_objects());
+ ASSERT_EQ(0, create_snap("one"));
+
+ auto src_snap_id_start = m_src_image_ctx->snaps[0];
+ auto dst_snap_id_start = m_dst_image_ctx->snaps[0];
+
+ interval_set<uint64_t> two;
+ scribble(m_src_image_ctx, 10, 102400, &two);
+ ASSERT_EQ(0, create_snap("two"));
+
+ interval_set<uint64_t> three;
+ scribble(m_src_image_ctx, 10, 102400, &three);
+ ASSERT_EQ(0, create_snap("three"));
+
+ auto max_extent = one.range_end();
+ if (max_extent < two.range_end()) {
+ interval_set<uint64_t> resize_diff;
+ resize_diff.insert(max_extent, two.range_end() - max_extent);
+ two.union_of(resize_diff);
+ }
+
+ max_extent = std::max(max_extent, two.range_end());
+ if (max_extent < three.range_end()) {
+ interval_set<uint64_t> resize_diff;
+ resize_diff.insert(max_extent, three.range_end() - max_extent);
+ three.union_of(resize_diff);
+ }
+
+ interval_set<uint64_t> four;
+ scribble(m_src_image_ctx, 10, 102400, &four);
+
+ // map should begin after src start and src end's dst snap seqs should
+ // point to HEAD revision
+ m_snap_seqs.rbegin()->second = CEPH_NOSNAP;
+ m_dst_snap_ids.pop_back();
+
+ librbd::MockTestImageCtx mock_src_image_ctx(*m_src_image_ctx);
+ librbd::MockTestImageCtx mock_dst_image_ctx(*m_dst_image_ctx);
+
+ librbd::MockExclusiveLock mock_exclusive_lock;
+ prepare_exclusive_lock(mock_dst_image_ctx, mock_exclusive_lock);
+
+ librbd::MockObjectMap mock_object_map;
+ mock_dst_image_ctx.object_map = &mock_object_map;
+
+ expect_op_work_queue(mock_src_image_ctx);
+ expect_test_features(mock_dst_image_ctx);
+ expect_get_object_count(mock_dst_image_ctx);
+
+ C_SaferCond ctx;
+ MockObjectCopyRequest *request = create_request(mock_src_image_ctx,
+ mock_dst_image_ctx,
+ src_snap_id_start,
+ CEPH_NOSNAP,
+ dst_snap_id_start,
+ 0, &ctx);
+
+ librados::MockTestMemIoCtxImpl &mock_dst_io_ctx(get_mock_io_ctx(
+ request->get_dst_io_ctx()));
+
+ InSequence seq;
+ expect_list_snaps(mock_src_image_ctx, 0);
+
+ expect_read(mock_src_image_ctx, m_src_snap_ids[1], two, 0);
+ expect_read(mock_src_image_ctx, m_src_snap_ids[2], three, 0);
+
+ expect_start_op(mock_exclusive_lock);
+ expect_update_object_map(mock_dst_image_ctx, mock_object_map,
+ m_dst_snap_ids[1], OBJECT_EXISTS, 0);
+
+ expect_start_op(mock_exclusive_lock);
+ expect_update_object_map(mock_dst_image_ctx, mock_object_map,
+ CEPH_NOSNAP, OBJECT_EXISTS, 0);
+
+ expect_prepare_copyup(mock_dst_image_ctx);
+ expect_start_op(mock_exclusive_lock);
+ expect_write(mock_dst_io_ctx, two,
+ {m_dst_snap_ids[0], {m_dst_snap_ids[0]}}, 0);
+
+ expect_start_op(mock_exclusive_lock);
+ expect_write(mock_dst_io_ctx, three,
+ {m_dst_snap_ids[1], {m_dst_snap_ids[1], m_dst_snap_ids[0]}}, 0);
+
+ request->send();
+ ASSERT_EQ(0, ctx.wait());
+ ASSERT_EQ(0, compare_objects());
+}
+
+TEST_F(TestMockDeepCopyObjectCopyRequest, Incremental) {
+ librbd::MockTestImageCtx mock_src_image_ctx(*m_src_image_ctx);
+ librbd::MockTestImageCtx mock_dst_image_ctx(*m_dst_image_ctx);
+
+ librbd::MockExclusiveLock mock_exclusive_lock;
+ prepare_exclusive_lock(mock_dst_image_ctx, mock_exclusive_lock);
+
+ librbd::MockObjectMap mock_object_map;
+ mock_dst_image_ctx.object_map = &mock_object_map;
+
+ expect_op_work_queue(mock_src_image_ctx);
+ expect_test_features(mock_dst_image_ctx);
+ expect_get_object_count(mock_dst_image_ctx);
+
+ // scribble some data
+ interval_set<uint64_t> one;
+ scribble(m_src_image_ctx, 10, 102400, &one);
+ ASSERT_EQ(0, create_snap("snap1"));
+ mock_dst_image_ctx.snaps = m_dst_image_ctx->snaps;
+
+ InSequence seq;
+
+ C_SaferCond ctx1;
+ auto request1 = create_request(mock_src_image_ctx, mock_dst_image_ctx,
+ 0, m_src_snap_ids[0], 0, 0, &ctx1);
+
+ expect_list_snaps(mock_src_image_ctx, 0);
+
+ expect_read(mock_src_image_ctx, m_src_snap_ids[0], 0, one.range_end(), 0);
+
+ expect_start_op(mock_exclusive_lock);
+ expect_update_object_map(mock_dst_image_ctx, mock_object_map,
+ m_dst_snap_ids[0], OBJECT_EXISTS, 0);
+
+ librados::MockTestMemIoCtxImpl &mock_dst_io_ctx(get_mock_io_ctx(
+ request1->get_dst_io_ctx()));
+ expect_prepare_copyup(mock_dst_image_ctx);
+ expect_start_op(mock_exclusive_lock);
+ expect_write(mock_dst_io_ctx, 0, one.range_end(), {0, {}}, 0);
+
+ request1->send();
+ ASSERT_EQ(0, ctx1.wait());
+
+ // clean (no-updates) snapshots
+ ASSERT_EQ(0, create_snap("snap2"));
+ ASSERT_EQ(0, create_snap("snap3"));
+ mock_dst_image_ctx.snaps = m_dst_image_ctx->snaps;
+
+ C_SaferCond ctx2;
+ auto request2 = create_request(mock_src_image_ctx, mock_dst_image_ctx,
+ m_src_snap_ids[0], m_src_snap_ids[2],
+ m_dst_snap_ids[0], 0, &ctx2);
+
+ expect_list_snaps(mock_src_image_ctx, 0);
+ expect_start_op(mock_exclusive_lock);
+ expect_update_object_map(mock_dst_image_ctx, mock_object_map,
+ m_dst_snap_ids[1],
+ is_fast_diff(mock_dst_image_ctx) ?
+ OBJECT_EXISTS_CLEAN : OBJECT_EXISTS, 0);
+ expect_start_op(mock_exclusive_lock);
+ expect_update_object_map(mock_dst_image_ctx, mock_object_map,
+ m_dst_snap_ids[2],
+ is_fast_diff(mock_dst_image_ctx) ?
+ OBJECT_EXISTS_CLEAN : OBJECT_EXISTS, 0);
+
+ request2->send();
+ ASSERT_EQ(0, ctx2.wait());
+ ASSERT_EQ(0, compare_objects());
+}
+
+TEST_F(TestMockDeepCopyObjectCopyRequest, SkipSnapList) {
+ librbd::MockTestImageCtx mock_src_image_ctx(*m_src_image_ctx);
+ librbd::MockTestImageCtx mock_dst_image_ctx(*m_dst_image_ctx);
+
+ librbd::MockExclusiveLock mock_exclusive_lock;
+ prepare_exclusive_lock(mock_dst_image_ctx, mock_exclusive_lock);
+
+ librbd::MockObjectMap mock_object_map;
+ mock_dst_image_ctx.object_map = &mock_object_map;
+
+ expect_op_work_queue(mock_src_image_ctx);
+ expect_test_features(mock_dst_image_ctx);
+ expect_get_object_count(mock_dst_image_ctx);
+
+ ASSERT_EQ(0, create_snap("snap1"));
+ mock_dst_image_ctx.snaps = m_dst_image_ctx->snaps;
+
+ InSequence seq;
+
+ // clean (no-updates) snapshots
+ ASSERT_EQ(0, create_snap("snap2"));
+ mock_dst_image_ctx.snaps = m_dst_image_ctx->snaps;
+
+ C_SaferCond ctx;
+ auto request = create_request(mock_src_image_ctx, mock_dst_image_ctx,
+ m_src_snap_ids[0], m_src_snap_ids[1],
+ m_dst_snap_ids[0],
+ OBJECT_COPY_REQUEST_FLAG_EXISTS_CLEAN, &ctx);
+
+ expect_start_op(mock_exclusive_lock);
+ expect_update_object_map(mock_dst_image_ctx, mock_object_map,
+ m_dst_snap_ids[1],
+ is_fast_diff(mock_dst_image_ctx) ?
+ OBJECT_EXISTS_CLEAN : OBJECT_EXISTS, 0);
+
+ request->send();
+ ASSERT_EQ(0, ctx.wait());
+}
+
+} // namespace deep_copy
+} // namespace librbd
diff --git a/src/test/librbd/deep_copy/test_mock_SetHeadRequest.cc b/src/test/librbd/deep_copy/test_mock_SetHeadRequest.cc
new file mode 100644
index 000000000..209339973
--- /dev/null
+++ b/src/test/librbd/deep_copy/test_mock_SetHeadRequest.cc
@@ -0,0 +1,296 @@
+// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
+// vim: ts=8 sw=2 smarttab
+
+#include "test/librbd/test_mock_fixture.h"
+#include "test/librados_test_stub/LibradosTestStub.h"
+#include "include/rbd/librbd.hpp"
+#include "librbd/AsioEngine.h"
+#include "librbd/ImageCtx.h"
+#include "librbd/ImageState.h"
+#include "osdc/Striper.h"
+#include "test/librados_test_stub/MockTestMemIoCtxImpl.h"
+#include "test/librbd/mock/MockImageCtx.h"
+#include "librbd/deep_copy/SetHeadRequest.h"
+#include "librbd/image/AttachParentRequest.h"
+#include "librbd/image/DetachParentRequest.h"
+
+namespace librbd {
+namespace {
+
+struct MockTestImageCtx : public librbd::MockImageCtx {
+ explicit MockTestImageCtx(librbd::ImageCtx &image_ctx)
+ : librbd::MockImageCtx(image_ctx) {
+ }
+};
+
+} // anonymous namespace
+
+namespace image {
+
+template <>
+struct AttachParentRequest<MockTestImageCtx> {
+ Context* on_finish = nullptr;
+ static AttachParentRequest* s_instance;
+ static AttachParentRequest* create(MockTestImageCtx&,
+ const cls::rbd::ParentImageSpec& pspec,
+ uint64_t parent_overlap, bool reattach,
+ Context *on_finish) {
+ ceph_assert(s_instance != nullptr);
+ s_instance->on_finish = on_finish;
+ return s_instance;
+ }
+
+ MOCK_METHOD0(send, void());
+
+ AttachParentRequest() {
+ s_instance = this;
+ }
+};
+
+AttachParentRequest<MockTestImageCtx>* AttachParentRequest<MockTestImageCtx>::s_instance = nullptr;
+
+template <>
+class DetachParentRequest<MockTestImageCtx> {
+public:
+ static DetachParentRequest *s_instance;
+ static DetachParentRequest *create(MockTestImageCtx &image_ctx,
+ Context *on_finish) {
+ ceph_assert(s_instance != nullptr);
+ s_instance->on_finish = on_finish;
+ return s_instance;
+ }
+
+ Context *on_finish = nullptr;
+
+ DetachParentRequest() {
+ s_instance = this;
+ }
+
+ MOCK_METHOD0(send, void());
+};
+
+DetachParentRequest<MockTestImageCtx> *DetachParentRequest<MockTestImageCtx>::s_instance;
+
+} // namespace image
+} // namespace librbd
+
+// template definitions
+#include "librbd/deep_copy/SetHeadRequest.cc"
+template class librbd::deep_copy::SetHeadRequest<librbd::MockTestImageCtx>;
+
+namespace librbd {
+namespace deep_copy {
+
+using ::testing::_;
+using ::testing::DoAll;
+using ::testing::InSequence;
+using ::testing::Invoke;
+using ::testing::InvokeWithoutArgs;
+using ::testing::Return;
+using ::testing::ReturnNew;
+using ::testing::StrEq;
+using ::testing::WithArg;
+
+class TestMockDeepCopySetHeadRequest : public TestMockFixture {
+public:
+ typedef SetHeadRequest<librbd::MockTestImageCtx> MockSetHeadRequest;
+ typedef image::AttachParentRequest<MockTestImageCtx> MockAttachParentRequest;
+ typedef image::DetachParentRequest<MockTestImageCtx> MockDetachParentRequest;
+
+ librbd::ImageCtx *m_image_ctx;
+
+ std::shared_ptr<librbd::AsioEngine> m_asio_engine;
+ asio::ContextWQ *m_work_queue;
+
+ void SetUp() override {
+ TestMockFixture::SetUp();
+
+ ASSERT_EQ(0, open_image(m_image_name, &m_image_ctx));
+
+ m_asio_engine = std::make_shared<librbd::AsioEngine>(
+ m_image_ctx->md_ctx);
+ m_work_queue = m_asio_engine->get_work_queue();
+ }
+
+ void expect_start_op(librbd::MockExclusiveLock &mock_exclusive_lock) {
+ EXPECT_CALL(mock_exclusive_lock, start_op(_)).WillOnce(Return(new LambdaContext([](int){})));
+ }
+
+ void expect_test_features(librbd::MockTestImageCtx &mock_image_ctx,
+ uint64_t features, bool enabled) {
+ EXPECT_CALL(mock_image_ctx, test_features(features))
+ .WillOnce(Return(enabled));
+ }
+
+ void expect_set_size(librbd::MockTestImageCtx &mock_image_ctx, int r) {
+ EXPECT_CALL(get_mock_io_ctx(mock_image_ctx.md_ctx),
+ exec(mock_image_ctx.header_oid, _, StrEq("rbd"),
+ StrEq("set_size"), _, _, _, _))
+ .WillOnce(Return(r));
+ }
+
+ void expect_detach_parent(MockImageCtx &mock_image_ctx,
+ MockDetachParentRequest& mock_request, int r) {
+ EXPECT_CALL(mock_request, send())
+ .WillOnce(FinishRequest(&mock_request, r, &mock_image_ctx));
+ }
+
+ void expect_attach_parent(MockImageCtx &mock_image_ctx,
+ MockAttachParentRequest& mock_request, int r) {
+ EXPECT_CALL(mock_request, send())
+ .WillOnce(FinishRequest(&mock_request, r, &mock_image_ctx));
+ }
+
+ MockSetHeadRequest *create_request(
+ librbd::MockTestImageCtx &mock_local_image_ctx, uint64_t size,
+ const cls::rbd::ParentImageSpec &parent_spec, uint64_t parent_overlap,
+ Context *on_finish) {
+ return new MockSetHeadRequest(&mock_local_image_ctx, size, parent_spec,
+ parent_overlap, on_finish);
+ }
+};
+
+TEST_F(TestMockDeepCopySetHeadRequest, Resize) {
+ librbd::MockTestImageCtx mock_image_ctx(*m_image_ctx);
+ librbd::MockExclusiveLock mock_exclusive_lock;
+ mock_image_ctx.exclusive_lock = &mock_exclusive_lock;
+
+ InSequence seq;
+ expect_start_op(mock_exclusive_lock);
+ expect_set_size(mock_image_ctx, 0);
+
+ C_SaferCond ctx;
+ auto request = create_request(mock_image_ctx, 123, {}, 0, &ctx);
+ request->send();
+ ASSERT_EQ(0, ctx.wait());
+}
+
+TEST_F(TestMockDeepCopySetHeadRequest, ResizeError) {
+ librbd::MockTestImageCtx mock_image_ctx(*m_image_ctx);
+ librbd::MockExclusiveLock mock_exclusive_lock;
+ mock_image_ctx.exclusive_lock = &mock_exclusive_lock;
+
+ InSequence seq;
+ expect_start_op(mock_exclusive_lock);
+ expect_set_size(mock_image_ctx, -EINVAL);
+
+ C_SaferCond ctx;
+ auto request = create_request(mock_image_ctx, 123, {}, 0, &ctx);
+ request->send();
+ ASSERT_EQ(-EINVAL, ctx.wait());
+}
+
+TEST_F(TestMockDeepCopySetHeadRequest, RemoveParent) {
+ librbd::MockTestImageCtx mock_image_ctx(*m_image_ctx);
+ librbd::MockExclusiveLock mock_exclusive_lock;
+ mock_image_ctx.exclusive_lock = &mock_exclusive_lock;
+
+ mock_image_ctx.parent_md.spec.pool_id = 213;
+
+ InSequence seq;
+ expect_start_op(mock_exclusive_lock);
+ MockDetachParentRequest mock_detach_parent;
+ expect_detach_parent(mock_image_ctx, mock_detach_parent, 0);
+
+ C_SaferCond ctx;
+ auto request = create_request(mock_image_ctx, m_image_ctx->size, {}, 0, &ctx);
+ request->send();
+ ASSERT_EQ(0, ctx.wait());
+}
+
+TEST_F(TestMockDeepCopySetHeadRequest, RemoveParentError) {
+ librbd::MockTestImageCtx mock_image_ctx(*m_image_ctx);
+ librbd::MockExclusiveLock mock_exclusive_lock;
+ mock_image_ctx.exclusive_lock = &mock_exclusive_lock;
+
+ mock_image_ctx.parent_md.spec.pool_id = 213;
+
+ InSequence seq;
+ expect_start_op(mock_exclusive_lock);
+ MockDetachParentRequest mock_detach_parent;
+ expect_detach_parent(mock_image_ctx, mock_detach_parent, -EINVAL);
+
+ C_SaferCond ctx;
+ auto request = create_request(mock_image_ctx, m_image_ctx->size, {}, 0, &ctx);
+ request->send();
+ ASSERT_EQ(-EINVAL, ctx.wait());
+}
+
+TEST_F(TestMockDeepCopySetHeadRequest, RemoveSetParent) {
+ librbd::MockTestImageCtx mock_image_ctx(*m_image_ctx);
+ librbd::MockExclusiveLock mock_exclusive_lock;
+ mock_image_ctx.exclusive_lock = &mock_exclusive_lock;
+
+ mock_image_ctx.parent_md.spec.pool_id = 213;
+
+ InSequence seq;
+ expect_start_op(mock_exclusive_lock);
+ MockDetachParentRequest mock_detach_parent;
+ expect_detach_parent(mock_image_ctx, mock_detach_parent, 0);
+ expect_start_op(mock_exclusive_lock);
+ MockAttachParentRequest mock_attach_parent;
+ expect_attach_parent(mock_image_ctx, mock_attach_parent, 0);
+
+ C_SaferCond ctx;
+ auto request = create_request(mock_image_ctx, m_image_ctx->size,
+ {123, "", "test", 0}, 0, &ctx);
+ request->send();
+ ASSERT_EQ(0, ctx.wait());
+}
+
+TEST_F(TestMockDeepCopySetHeadRequest, SetParentSpec) {
+ librbd::MockTestImageCtx mock_image_ctx(*m_image_ctx);
+ librbd::MockExclusiveLock mock_exclusive_lock;
+ mock_image_ctx.exclusive_lock = &mock_exclusive_lock;
+
+ InSequence seq;
+ expect_start_op(mock_exclusive_lock);
+ MockAttachParentRequest mock_attach_parent;
+ expect_attach_parent(mock_image_ctx, mock_attach_parent, 0);
+
+ C_SaferCond ctx;
+ auto request = create_request(mock_image_ctx, m_image_ctx->size,
+ {123, "", "test", 0}, 0, &ctx);
+ request->send();
+ ASSERT_EQ(0, ctx.wait());
+}
+
+TEST_F(TestMockDeepCopySetHeadRequest, SetParentOverlap) {
+ librbd::MockTestImageCtx mock_image_ctx(*m_image_ctx);
+ librbd::MockExclusiveLock mock_exclusive_lock;
+ mock_image_ctx.exclusive_lock = &mock_exclusive_lock;
+
+ mock_image_ctx.parent_md.spec = {123, "", "test", 0};
+ mock_image_ctx.parent_md.overlap = m_image_ctx->size;
+
+ InSequence seq;
+ expect_start_op(mock_exclusive_lock);
+ expect_set_size(mock_image_ctx, 0);
+
+ C_SaferCond ctx;
+ auto request = create_request(mock_image_ctx, 123,
+ mock_image_ctx.parent_md.spec, 123, &ctx);
+ request->send();
+ ASSERT_EQ(0, ctx.wait());
+ ASSERT_EQ(123U, mock_image_ctx.parent_md.overlap);
+}
+
+TEST_F(TestMockDeepCopySetHeadRequest, SetParentError) {
+ librbd::MockTestImageCtx mock_image_ctx(*m_image_ctx);
+ librbd::MockExclusiveLock mock_exclusive_lock;
+ mock_image_ctx.exclusive_lock = &mock_exclusive_lock;
+
+ InSequence seq;
+ expect_start_op(mock_exclusive_lock);
+ MockAttachParentRequest mock_attach_parent;
+ expect_attach_parent(mock_image_ctx, mock_attach_parent, -ESTALE);
+
+ C_SaferCond ctx;
+ auto request = create_request(mock_image_ctx, m_image_ctx->size,
+ {123, "", "test", 0}, 0, &ctx);
+ request->send();
+ ASSERT_EQ(-ESTALE, ctx.wait());
+}
+
+} // namespace deep_copy
+} // namespace librbd
diff --git a/src/test/librbd/deep_copy/test_mock_SnapshotCopyRequest.cc b/src/test/librbd/deep_copy/test_mock_SnapshotCopyRequest.cc
new file mode 100644
index 000000000..6a5289822
--- /dev/null
+++ b/src/test/librbd/deep_copy/test_mock_SnapshotCopyRequest.cc
@@ -0,0 +1,923 @@
+// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
+// vim: ts=8 sw=2 smarttab
+
+#include "test/librbd/test_mock_fixture.h"
+#include "include/rbd/librbd.hpp"
+#include "librbd/AsioEngine.h"
+#include "librbd/ImageCtx.h"
+#include "librbd/ImageState.h"
+#include "librbd/Operations.h"
+#include "librbd/deep_copy/SetHeadRequest.h"
+#include "librbd/deep_copy/SnapshotCopyRequest.h"
+#include "librbd/deep_copy/SnapshotCreateRequest.h"
+#include "test/librados_test_stub/MockTestMemIoCtxImpl.h"
+#include "test/librbd/mock/MockImageCtx.h"
+#include "test/librbd/test_support.h"
+
+namespace librbd {
+
+namespace {
+
+struct MockTestImageCtx : public librbd::MockImageCtx {
+ explicit MockTestImageCtx(librbd::ImageCtx &image_ctx)
+ : librbd::MockImageCtx(image_ctx) {
+ }
+};
+
+} // anonymous namespace
+
+namespace deep_copy {
+
+template <>
+class SetHeadRequest<librbd::MockTestImageCtx> {
+public:
+ static SetHeadRequest* s_instance;
+ Context *on_finish;
+
+ static SetHeadRequest* create(librbd::MockTestImageCtx *image_ctx,
+ uint64_t size,
+ const cls::rbd::ParentImageSpec &parent_spec,
+ uint64_t parent_overlap, Context *on_finish) {
+ ceph_assert(s_instance != nullptr);
+ s_instance->on_finish = on_finish;
+ return s_instance;
+ }
+
+ SetHeadRequest() {
+ s_instance = this;
+ }
+
+ MOCK_METHOD0(send, void());
+};
+
+template <>
+struct SnapshotCreateRequest<librbd::MockTestImageCtx> {
+ static SnapshotCreateRequest* s_instance;
+ static SnapshotCreateRequest* create(librbd::MockTestImageCtx* image_ctx,
+ const std::string &snap_name,
+ const cls::rbd::SnapshotNamespace &snap_namespace,
+ uint64_t size,
+ const cls::rbd::ParentImageSpec &parent_spec,
+ uint64_t parent_overlap,
+ Context *on_finish) {
+ ceph_assert(s_instance != nullptr);
+ s_instance->on_finish = on_finish;
+ return s_instance;
+ }
+
+ Context *on_finish = nullptr;
+
+ SnapshotCreateRequest() {
+ s_instance = this;
+ }
+
+ MOCK_METHOD0(send, void());
+};
+
+SetHeadRequest<librbd::MockTestImageCtx>* SetHeadRequest<librbd::MockTestImageCtx>::s_instance = nullptr;
+SnapshotCreateRequest<librbd::MockTestImageCtx>* SnapshotCreateRequest<librbd::MockTestImageCtx>::s_instance = nullptr;
+
+} // namespace deep_copy
+} // namespace librbd
+
+// template definitions
+#include "librbd/deep_copy/SnapshotCopyRequest.cc"
+template class librbd::deep_copy::SnapshotCopyRequest<librbd::MockTestImageCtx>;
+
+namespace librbd {
+namespace deep_copy {
+
+using ::testing::_;
+using ::testing::DoAll;
+using ::testing::DoDefault;
+using ::testing::InSequence;
+using ::testing::Invoke;
+using ::testing::InvokeWithoutArgs;
+using ::testing::Return;
+using ::testing::ReturnNew;
+using ::testing::SetArgPointee;
+using ::testing::StrEq;
+using ::testing::WithArg;
+
+class TestMockDeepCopySnapshotCopyRequest : public TestMockFixture {
+public:
+ typedef SetHeadRequest<librbd::MockTestImageCtx> MockSetHeadRequest;
+ typedef SnapshotCopyRequest<librbd::MockTestImageCtx> MockSnapshotCopyRequest;
+ typedef SnapshotCreateRequest<librbd::MockTestImageCtx> MockSnapshotCreateRequest;
+
+ librbd::ImageCtx *m_src_image_ctx;
+ librbd::ImageCtx *m_dst_image_ctx;
+
+ std::shared_ptr<librbd::AsioEngine> m_asio_engine;
+ asio::ContextWQ *m_work_queue;
+
+ librbd::SnapSeqs m_snap_seqs;
+
+ void SetUp() override {
+ TestMockFixture::SetUp();
+
+ ASSERT_EQ(0, open_image(m_image_name, &m_src_image_ctx));
+
+ librbd::RBD rbd;
+ std::string dst_image_name = get_temp_image_name();
+ ASSERT_EQ(0, create_image_pp(rbd, m_ioctx, dst_image_name, m_image_size));
+ ASSERT_EQ(0, open_image(dst_image_name, &m_dst_image_ctx));
+
+ m_asio_engine = std::make_shared<librbd::AsioEngine>(
+ m_src_image_ctx->md_ctx);
+ m_work_queue = m_asio_engine->get_work_queue();
+ }
+
+ void prepare_exclusive_lock(librbd::MockImageCtx &mock_image_ctx,
+ librbd::MockExclusiveLock &mock_exclusive_lock) {
+ if ((mock_image_ctx.features & RBD_FEATURE_EXCLUSIVE_LOCK) == 0) {
+ return;
+ }
+ mock_image_ctx.exclusive_lock = &mock_exclusive_lock;
+ }
+
+ void expect_test_features(librbd::MockImageCtx &mock_image_ctx) {
+ EXPECT_CALL(mock_image_ctx, test_features(_, _))
+ .WillRepeatedly(WithArg<0>(Invoke([&mock_image_ctx](uint64_t features) {
+ return (mock_image_ctx.features & features) != 0;
+ })));
+ EXPECT_CALL(mock_image_ctx, test_features(_))
+ .WillRepeatedly(WithArg<0>(Invoke([&mock_image_ctx](uint64_t features) {
+ return (mock_image_ctx.features & features) != 0;
+ })));
+ }
+
+ void expect_start_op(librbd::MockExclusiveLock &mock_exclusive_lock) {
+ if ((m_src_image_ctx->features & RBD_FEATURE_EXCLUSIVE_LOCK) == 0) {
+ return;
+ }
+ EXPECT_CALL(mock_exclusive_lock, start_op(_)).WillOnce(Return(new LambdaContext([](int){})));
+ }
+
+ void expect_get_snap_namespace(librbd::MockTestImageCtx &mock_image_ctx,
+ uint64_t snap_id) {
+ EXPECT_CALL(mock_image_ctx, get_snap_namespace(snap_id, _))
+ .WillOnce(Invoke([&mock_image_ctx](uint64_t snap_id,
+ cls::rbd::SnapshotNamespace* snap_ns) {
+ auto it = mock_image_ctx.snap_info.find(snap_id);
+ *snap_ns = it->second.snap_namespace;
+ return 0;
+ }));
+ }
+
+ void expect_snap_create(librbd::MockTestImageCtx &mock_image_ctx,
+ MockSnapshotCreateRequest &mock_snapshot_create_request,
+ const std::string &snap_name, uint64_t snap_id, int r) {
+ EXPECT_CALL(mock_snapshot_create_request, send())
+ .WillOnce(DoAll(Invoke([&mock_image_ctx, snap_id, snap_name]() {
+ inject_snap(mock_image_ctx, snap_id, snap_name);
+ }),
+ Invoke([this, &mock_snapshot_create_request, r]() {
+ m_work_queue->queue(mock_snapshot_create_request.on_finish, r);
+ })));
+ }
+
+ void expect_snap_remove(librbd::MockTestImageCtx &mock_image_ctx,
+ const std::string &snap_name, int r) {
+ EXPECT_CALL(*mock_image_ctx.operations, execute_snap_remove(_, StrEq(snap_name), _))
+ .WillOnce(WithArg<2>(Invoke([this, r](Context *ctx) {
+ m_work_queue->queue(ctx, r);
+ })));
+ }
+
+ void expect_snap_protect(librbd::MockTestImageCtx &mock_image_ctx,
+ const std::string &snap_name, int r) {
+ EXPECT_CALL(*mock_image_ctx.operations, execute_snap_protect(_, StrEq(snap_name), _))
+ .WillOnce(WithArg<2>(Invoke([this, r](Context *ctx) {
+ m_work_queue->queue(ctx, r);
+ })));
+ }
+
+ void expect_snap_unprotect(librbd::MockTestImageCtx &mock_image_ctx,
+ const std::string &snap_name, int r) {
+ EXPECT_CALL(*mock_image_ctx.operations, execute_snap_unprotect(_, StrEq(snap_name), _))
+ .WillOnce(WithArg<2>(Invoke([this, r](Context *ctx) {
+ m_work_queue->queue(ctx, r);
+ })));
+ }
+
+ void expect_snap_is_protected(librbd::MockTestImageCtx &mock_image_ctx,
+ uint64_t snap_id, bool is_protected, int r) {
+ EXPECT_CALL(mock_image_ctx, is_snap_protected(snap_id, _))
+ .WillOnce(DoAll(SetArgPointee<1>(is_protected),
+ Return(r)));
+ }
+
+ void expect_snap_is_unprotected(librbd::MockTestImageCtx &mock_image_ctx,
+ uint64_t snap_id, bool is_unprotected, int r) {
+ EXPECT_CALL(mock_image_ctx, is_snap_unprotected(snap_id, _))
+ .WillOnce(DoAll(SetArgPointee<1>(is_unprotected),
+ Return(r)));
+ }
+
+ void expect_set_head(MockSetHeadRequest &mock_set_head_request, int r) {
+ EXPECT_CALL(mock_set_head_request, send())
+ .WillOnce(Invoke([&mock_set_head_request, r]() {
+ mock_set_head_request.on_finish->complete(r);
+ }));
+ }
+
+ static void inject_snap(librbd::MockTestImageCtx &mock_image_ctx,
+ uint64_t snap_id, const std::string &snap_name) {
+ mock_image_ctx.snap_ids[{cls::rbd::UserSnapshotNamespace(),
+ snap_name}] = snap_id;
+ }
+
+ MockSnapshotCopyRequest *create_request(
+ librbd::MockTestImageCtx &mock_src_image_ctx,
+ librbd::MockTestImageCtx &mock_dst_image_ctx,
+ librados::snap_t src_snap_id_start,
+ librados::snap_t src_snap_id_end,
+ librados::snap_t dst_snap_id_start,
+ Context *on_finish) {
+ return new MockSnapshotCopyRequest(&mock_src_image_ctx, &mock_dst_image_ctx,
+ src_snap_id_start, src_snap_id_end,
+ dst_snap_id_start, false, m_work_queue,
+ &m_snap_seqs, on_finish);
+ }
+
+ int create_snap(librbd::ImageCtx *image_ctx,
+ const cls::rbd::SnapshotNamespace& snap_ns,
+ const std::string &snap_name, bool protect) {
+ NoOpProgressContext prog_ctx;
+ int r = image_ctx->operations->snap_create(snap_ns, snap_name.c_str(), 0,
+ prog_ctx);
+ if (r < 0) {
+ return r;
+ }
+
+ if (protect) {
+ EXPECT_TRUE(boost::get<cls::rbd::UserSnapshotNamespace>(&snap_ns) !=
+ nullptr);
+ r = image_ctx->operations->snap_protect(snap_ns, snap_name.c_str());
+ if (r < 0) {
+ return r;
+ }
+ }
+
+ r = image_ctx->state->refresh();
+ if (r < 0) {
+ return r;
+ }
+ return 0;
+ }
+
+ int create_snap(librbd::ImageCtx *image_ctx, const std::string &snap_name,
+ bool protect = false) {
+ return create_snap(image_ctx, cls::rbd::UserSnapshotNamespace{}, snap_name,
+ protect);
+ }
+
+ void validate_snap_seqs(const librbd::SnapSeqs &snap_seqs) {
+ ASSERT_EQ(snap_seqs, m_snap_seqs);
+ }
+};
+
+TEST_F(TestMockDeepCopySnapshotCopyRequest, Empty) {
+ librbd::MockTestImageCtx mock_src_image_ctx(*m_src_image_ctx);
+ librbd::MockTestImageCtx mock_dst_image_ctx(*m_dst_image_ctx);
+ MockSetHeadRequest mock_set_head_request;
+
+ librbd::MockExclusiveLock mock_exclusive_lock;
+ prepare_exclusive_lock(mock_dst_image_ctx, mock_exclusive_lock);
+
+ expect_test_features(mock_dst_image_ctx);
+
+ InSequence seq;
+ expect_set_head(mock_set_head_request, 0);
+
+ C_SaferCond ctx;
+ MockSnapshotCopyRequest *request = create_request(mock_src_image_ctx,
+ mock_dst_image_ctx, 0,
+ CEPH_NOSNAP, 0, &ctx);
+ request->send();
+ ASSERT_EQ(0, ctx.wait());
+
+ validate_snap_seqs({});
+}
+
+TEST_F(TestMockDeepCopySnapshotCopyRequest, SnapCreate) {
+ ASSERT_EQ(0, create_snap(m_src_image_ctx, "snap1"));
+ ASSERT_EQ(0, create_snap(m_src_image_ctx, "snap2"));
+
+ uint64_t src_snap_id1 = m_src_image_ctx->snap_ids[
+ {cls::rbd::UserSnapshotNamespace(), "snap1"}];
+ uint64_t src_snap_id2 = m_src_image_ctx->snap_ids[
+ {cls::rbd::UserSnapshotNamespace(), "snap2"}];
+
+ librbd::MockTestImageCtx mock_src_image_ctx(*m_src_image_ctx);
+ librbd::MockTestImageCtx mock_dst_image_ctx(*m_dst_image_ctx);
+ MockSnapshotCreateRequest mock_snapshot_create_request;
+ MockSetHeadRequest mock_set_head_request;
+
+ librbd::MockExclusiveLock mock_exclusive_lock;
+ prepare_exclusive_lock(mock_dst_image_ctx, mock_exclusive_lock);
+
+ expect_test_features(mock_dst_image_ctx);
+
+ InSequence seq;
+ expect_get_snap_namespace(mock_src_image_ctx, src_snap_id1);
+ expect_start_op(mock_exclusive_lock);
+ expect_snap_create(mock_dst_image_ctx, mock_snapshot_create_request, "snap1", 12, 0);
+ expect_get_snap_namespace(mock_src_image_ctx, src_snap_id2);
+ expect_start_op(mock_exclusive_lock);
+ expect_snap_create(mock_dst_image_ctx, mock_snapshot_create_request, "snap2", 14, 0);
+ expect_snap_is_protected(mock_src_image_ctx, src_snap_id1, false, 0);
+ expect_snap_is_protected(mock_src_image_ctx, src_snap_id2, false, 0);
+ expect_set_head(mock_set_head_request, 0);
+
+ C_SaferCond ctx;
+ MockSnapshotCopyRequest *request = create_request(mock_src_image_ctx,
+ mock_dst_image_ctx, 0,
+ CEPH_NOSNAP, 0, &ctx);
+ request->send();
+ ASSERT_EQ(0, ctx.wait());
+
+ validate_snap_seqs({{src_snap_id1, 12}, {src_snap_id2, 14}});
+}
+
+TEST_F(TestMockDeepCopySnapshotCopyRequest, SnapCreateError) {
+ ASSERT_EQ(0, create_snap(m_src_image_ctx, "snap1"));
+
+ librbd::MockTestImageCtx mock_src_image_ctx(*m_src_image_ctx);
+ librbd::MockTestImageCtx mock_dst_image_ctx(*m_dst_image_ctx);
+ MockSnapshotCreateRequest mock_snapshot_create_request;
+
+ librbd::MockExclusiveLock mock_exclusive_lock;
+ prepare_exclusive_lock(mock_dst_image_ctx, mock_exclusive_lock);
+
+ uint64_t src_snap_id1 = mock_src_image_ctx.snap_ids[
+ {cls::rbd::UserSnapshotNamespace(), "snap1"}];
+
+ expect_test_features(mock_dst_image_ctx);
+
+ InSequence seq;
+ expect_get_snap_namespace(mock_src_image_ctx, src_snap_id1);
+ expect_start_op(mock_exclusive_lock);
+ expect_snap_create(mock_dst_image_ctx, mock_snapshot_create_request, "snap1",
+ 12, -EINVAL);
+
+ C_SaferCond ctx;
+ MockSnapshotCopyRequest *request = create_request(mock_src_image_ctx,
+ mock_dst_image_ctx, 0,
+ CEPH_NOSNAP, 0, &ctx);
+ request->send();
+ ASSERT_EQ(-EINVAL, ctx.wait());
+}
+
+TEST_F(TestMockDeepCopySnapshotCopyRequest, SnapCreateCancel) {
+ ASSERT_EQ(0, create_snap(m_src_image_ctx, "snap1"));
+
+ librbd::MockTestImageCtx mock_src_image_ctx(*m_src_image_ctx);
+ librbd::MockTestImageCtx mock_dst_image_ctx(*m_dst_image_ctx);
+ MockSnapshotCreateRequest mock_snapshot_create_request;
+
+ librbd::MockExclusiveLock mock_exclusive_lock;
+ prepare_exclusive_lock(mock_dst_image_ctx, mock_exclusive_lock);
+
+ uint64_t src_snap_id1 = m_src_image_ctx->snap_ids[
+ {cls::rbd::UserSnapshotNamespace(), "snap1"}];
+ expect_get_snap_namespace(mock_src_image_ctx, src_snap_id1);
+
+ C_SaferCond ctx;
+ MockSnapshotCopyRequest *request = create_request(mock_src_image_ctx,
+ mock_dst_image_ctx, 0,
+ CEPH_NOSNAP, 0, &ctx);
+ expect_test_features(mock_dst_image_ctx);
+
+ InSequence seq;
+ expect_start_op(mock_exclusive_lock);
+ EXPECT_CALL(mock_snapshot_create_request, send())
+ .WillOnce(DoAll(InvokeWithoutArgs([request]() {
+ request->cancel();
+ }),
+ Invoke([this, &mock_snapshot_create_request]() {
+ m_work_queue->queue(mock_snapshot_create_request.on_finish, 0);
+ })));
+
+ request->send();
+ ASSERT_EQ(-ECANCELED, ctx.wait());
+}
+
+TEST_F(TestMockDeepCopySnapshotCopyRequest, SnapRemoveAndCreate) {
+ ASSERT_EQ(0, create_snap(m_src_image_ctx, "snap1"));
+ ASSERT_EQ(0, create_snap(m_dst_image_ctx, "snap1"));
+
+ uint64_t src_snap_id1 = m_src_image_ctx->snap_ids[
+ {cls::rbd::UserSnapshotNamespace(), "snap1"}];
+ uint64_t dst_snap_id1 = m_dst_image_ctx->snap_ids[
+ {cls::rbd::UserSnapshotNamespace(), "snap1"}];
+
+ librbd::MockTestImageCtx mock_src_image_ctx(*m_src_image_ctx);
+ librbd::MockTestImageCtx mock_dst_image_ctx(*m_dst_image_ctx);
+ MockSnapshotCreateRequest mock_snapshot_create_request;
+ MockSetHeadRequest mock_set_head_request;
+
+ librbd::MockExclusiveLock mock_exclusive_lock;
+ prepare_exclusive_lock(mock_dst_image_ctx, mock_exclusive_lock);
+
+ expect_test_features(mock_dst_image_ctx);
+
+ InSequence seq;
+ expect_snap_is_unprotected(mock_dst_image_ctx,
+ m_dst_image_ctx->snap_ids[
+ {cls::rbd::UserSnapshotNamespace(), "snap1"}],
+ true, 0);
+ expect_get_snap_namespace(mock_dst_image_ctx, dst_snap_id1);
+ expect_start_op(mock_exclusive_lock);
+ expect_snap_remove(mock_dst_image_ctx, "snap1", 0);
+ expect_get_snap_namespace(mock_src_image_ctx, src_snap_id1);
+ expect_start_op(mock_exclusive_lock);
+ expect_snap_create(mock_dst_image_ctx, mock_snapshot_create_request, "snap1", 12, 0);
+ expect_snap_is_protected(mock_src_image_ctx, src_snap_id1, false, 0);
+ expect_set_head(mock_set_head_request, 0);
+
+ C_SaferCond ctx;
+ MockSnapshotCopyRequest *request = create_request(mock_src_image_ctx,
+ mock_dst_image_ctx, 0,
+ CEPH_NOSNAP, 0, &ctx);
+ request->send();
+ ASSERT_EQ(0, ctx.wait());
+
+ validate_snap_seqs({{src_snap_id1, 12}});
+}
+
+TEST_F(TestMockDeepCopySnapshotCopyRequest, SnapRemoveError) {
+ ASSERT_EQ(0, create_snap(m_dst_image_ctx, "snap1"));
+
+ librbd::MockTestImageCtx mock_src_image_ctx(*m_src_image_ctx);
+ librbd::MockTestImageCtx mock_dst_image_ctx(*m_dst_image_ctx);
+
+ librbd::MockExclusiveLock mock_exclusive_lock;
+ prepare_exclusive_lock(mock_dst_image_ctx, mock_exclusive_lock);
+
+ uint64_t dst_snap_id1 = m_dst_image_ctx->snap_ids[
+ {cls::rbd::UserSnapshotNamespace(), "snap1"}];
+
+ expect_test_features(mock_dst_image_ctx);
+
+ InSequence seq;
+ expect_snap_is_unprotected(mock_dst_image_ctx,
+ m_dst_image_ctx->snap_ids[
+ {cls::rbd::UserSnapshotNamespace(), "snap1"}],
+ true, 0);
+ expect_get_snap_namespace(mock_dst_image_ctx, dst_snap_id1);
+ expect_start_op(mock_exclusive_lock);
+ expect_snap_remove(mock_dst_image_ctx, "snap1", -EINVAL);
+
+ C_SaferCond ctx;
+ MockSnapshotCopyRequest *request = create_request(mock_src_image_ctx,
+ mock_dst_image_ctx, 0,
+ CEPH_NOSNAP, 0, &ctx);
+ request->send();
+ ASSERT_EQ(-EINVAL, ctx.wait());
+}
+
+TEST_F(TestMockDeepCopySnapshotCopyRequest, SnapUnprotect) {
+ REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
+
+ ASSERT_EQ(0, create_snap(m_src_image_ctx, "snap1", true));
+ ASSERT_EQ(0, create_snap(m_dst_image_ctx, "snap1", true));
+
+ uint64_t src_snap_id1 = m_src_image_ctx->snap_ids[
+ {cls::rbd::UserSnapshotNamespace(), "snap1"}];
+ uint64_t dst_snap_id1 = m_dst_image_ctx->snap_ids[
+ {cls::rbd::UserSnapshotNamespace(), "snap1"}];
+ m_snap_seqs[src_snap_id1] = dst_snap_id1;
+
+ librbd::MockTestImageCtx mock_src_image_ctx(*m_src_image_ctx);
+ librbd::MockTestImageCtx mock_dst_image_ctx(*m_dst_image_ctx);
+ MockSetHeadRequest mock_set_head_request;
+
+ librbd::MockExclusiveLock mock_exclusive_lock;
+ prepare_exclusive_lock(mock_dst_image_ctx, mock_exclusive_lock);
+
+ expect_test_features(mock_dst_image_ctx);
+
+ InSequence seq;
+ expect_snap_is_unprotected(mock_dst_image_ctx, dst_snap_id1, false, 0);
+ expect_snap_is_unprotected(mock_src_image_ctx, src_snap_id1, true, 0);
+ expect_start_op(mock_exclusive_lock);
+ expect_snap_unprotect(mock_dst_image_ctx, "snap1", 0);
+ expect_get_snap_namespace(mock_dst_image_ctx, dst_snap_id1);
+ expect_get_snap_namespace(mock_src_image_ctx, src_snap_id1);
+ expect_snap_is_protected(mock_src_image_ctx, src_snap_id1, false, 0);
+ expect_set_head(mock_set_head_request, 0);
+
+ C_SaferCond ctx;
+ MockSnapshotCopyRequest *request = create_request(mock_src_image_ctx,
+ mock_dst_image_ctx, 0,
+ CEPH_NOSNAP, 0, &ctx);
+ request->send();
+ ASSERT_EQ(0, ctx.wait());
+
+ validate_snap_seqs({{src_snap_id1, dst_snap_id1}});
+}
+
+TEST_F(TestMockDeepCopySnapshotCopyRequest, SnapUnprotectError) {
+ REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
+
+ ASSERT_EQ(0, create_snap(m_src_image_ctx, "snap1", true));
+ ASSERT_EQ(0, create_snap(m_dst_image_ctx, "snap1", true));
+
+ uint64_t src_snap_id1 = m_src_image_ctx->snap_ids[
+ {cls::rbd::UserSnapshotNamespace(), "snap1"}];
+ uint64_t dst_snap_id1 = m_dst_image_ctx->snap_ids[
+ {cls::rbd::UserSnapshotNamespace(), "snap1"}];
+ m_snap_seqs[src_snap_id1] = dst_snap_id1;
+
+ librbd::MockTestImageCtx mock_src_image_ctx(*m_src_image_ctx);
+ librbd::MockTestImageCtx mock_dst_image_ctx(*m_dst_image_ctx);
+
+ librbd::MockExclusiveLock mock_exclusive_lock;
+ prepare_exclusive_lock(mock_dst_image_ctx, mock_exclusive_lock);
+
+ expect_test_features(mock_dst_image_ctx);
+
+ InSequence seq;
+ expect_snap_is_unprotected(mock_dst_image_ctx, dst_snap_id1, false, 0);
+ expect_snap_is_unprotected(mock_src_image_ctx, src_snap_id1, true, 0);
+ expect_start_op(mock_exclusive_lock);
+ expect_snap_unprotect(mock_dst_image_ctx, "snap1", -EBUSY);
+
+ C_SaferCond ctx;
+ MockSnapshotCopyRequest *request = create_request(mock_src_image_ctx,
+ mock_dst_image_ctx, 0,
+ CEPH_NOSNAP, 0, &ctx);
+ request->send();
+ ASSERT_EQ(-EBUSY, ctx.wait());
+}
+
+TEST_F(TestMockDeepCopySnapshotCopyRequest, SnapUnprotectCancel) {
+ REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
+
+ ASSERT_EQ(0, create_snap(m_src_image_ctx, "snap1", true));
+ ASSERT_EQ(0, create_snap(m_dst_image_ctx, "snap1", true));
+
+ uint64_t src_snap_id1 = m_src_image_ctx->snap_ids[
+ {cls::rbd::UserSnapshotNamespace(), "snap1"}];
+ uint64_t dst_snap_id1 = m_dst_image_ctx->snap_ids[
+ {cls::rbd::UserSnapshotNamespace(), "snap1"}];
+ m_snap_seqs[src_snap_id1] = dst_snap_id1;
+
+ librbd::MockTestImageCtx mock_src_image_ctx(*m_src_image_ctx);
+ librbd::MockTestImageCtx mock_dst_image_ctx(*m_dst_image_ctx);
+
+ librbd::MockExclusiveLock mock_exclusive_lock;
+ prepare_exclusive_lock(mock_dst_image_ctx, mock_exclusive_lock);
+
+ C_SaferCond ctx;
+ MockSnapshotCopyRequest *request = create_request(mock_src_image_ctx,
+ mock_dst_image_ctx, 0,
+ CEPH_NOSNAP, 0, &ctx);
+ expect_test_features(mock_dst_image_ctx);
+
+ InSequence seq;
+ expect_snap_is_unprotected(mock_dst_image_ctx, dst_snap_id1, false, 0);
+ expect_snap_is_unprotected(mock_src_image_ctx, src_snap_id1, true, 0);
+ expect_start_op(mock_exclusive_lock);
+ EXPECT_CALL(*mock_dst_image_ctx.operations,
+ execute_snap_unprotect(_, StrEq("snap1"), _))
+ .WillOnce(DoAll(InvokeWithoutArgs([request]() {
+ request->cancel();
+ }),
+ WithArg<2>(Invoke([this](Context *ctx) {
+ m_work_queue->queue(ctx, 0);
+ }))));
+
+ request->send();
+ ASSERT_EQ(-ECANCELED, ctx.wait());
+}
+
+TEST_F(TestMockDeepCopySnapshotCopyRequest, SnapUnprotectRemove) {
+ REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
+
+ ASSERT_EQ(0, create_snap(m_src_image_ctx, "snap1", true));
+ ASSERT_EQ(0, create_snap(m_dst_image_ctx, "snap1", true));
+
+ uint64_t src_snap_id1 = m_src_image_ctx->snap_ids[
+ {cls::rbd::UserSnapshotNamespace(), "snap1"}];
+ uint64_t dst_snap_id1 = m_dst_image_ctx->snap_ids[
+ {cls::rbd::UserSnapshotNamespace(), "snap1"}];
+
+ librbd::MockTestImageCtx mock_src_image_ctx(*m_src_image_ctx);
+ librbd::MockTestImageCtx mock_dst_image_ctx(*m_dst_image_ctx);
+ MockSnapshotCreateRequest mock_snapshot_create_request;
+ MockSetHeadRequest mock_set_head_request;
+
+ librbd::MockExclusiveLock mock_exclusive_lock;
+ prepare_exclusive_lock(mock_dst_image_ctx, mock_exclusive_lock);
+
+ expect_test_features(mock_dst_image_ctx);
+
+ InSequence seq;
+ expect_snap_is_unprotected(mock_dst_image_ctx,
+ m_dst_image_ctx->snap_ids[
+ {cls::rbd::UserSnapshotNamespace(), "snap1"}],
+ false, 0);
+ expect_start_op(mock_exclusive_lock);
+ expect_snap_unprotect(mock_dst_image_ctx, "snap1", 0);
+ expect_get_snap_namespace(mock_dst_image_ctx, dst_snap_id1);
+ expect_start_op(mock_exclusive_lock);
+ expect_snap_remove(mock_dst_image_ctx, "snap1", 0);
+ expect_get_snap_namespace(mock_src_image_ctx, src_snap_id1);
+ expect_start_op(mock_exclusive_lock);
+ expect_snap_create(mock_dst_image_ctx, mock_snapshot_create_request, "snap1",
+ 12, 0);
+ expect_snap_is_protected(mock_src_image_ctx, src_snap_id1, false, 0);
+ expect_set_head(mock_set_head_request, 0);
+
+ C_SaferCond ctx;
+ MockSnapshotCopyRequest *request = create_request(mock_src_image_ctx,
+ mock_dst_image_ctx, 0,
+ CEPH_NOSNAP, 0, &ctx);
+ request->send();
+ ASSERT_EQ(0, ctx.wait());
+
+ validate_snap_seqs({{src_snap_id1, 12}});
+}
+
+TEST_F(TestMockDeepCopySnapshotCopyRequest, SnapCreateProtect) {
+ REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
+
+ ASSERT_EQ(0, create_snap(m_src_image_ctx, "snap1", true));
+
+ uint64_t src_snap_id1 = m_src_image_ctx->snap_ids[
+ {cls::rbd::UserSnapshotNamespace(), "snap1"}];
+
+ librbd::MockTestImageCtx mock_src_image_ctx(*m_src_image_ctx);
+ librbd::MockTestImageCtx mock_dst_image_ctx(*m_dst_image_ctx);
+ MockSnapshotCreateRequest mock_snapshot_create_request;
+ MockSetHeadRequest mock_set_head_request;
+
+ librbd::MockExclusiveLock mock_exclusive_lock;
+ prepare_exclusive_lock(mock_dst_image_ctx, mock_exclusive_lock);
+
+ expect_test_features(mock_dst_image_ctx);
+
+ InSequence seq;
+ expect_get_snap_namespace(mock_src_image_ctx, src_snap_id1);
+ expect_start_op(mock_exclusive_lock);
+ expect_snap_create(mock_dst_image_ctx, mock_snapshot_create_request, "snap1",
+ 12, 0);
+ expect_snap_is_protected(mock_src_image_ctx, src_snap_id1, true, 0);
+ expect_snap_is_protected(mock_dst_image_ctx, 12, false, 0);
+ expect_start_op(mock_exclusive_lock);
+ expect_snap_protect(mock_dst_image_ctx, "snap1", 0);
+ expect_set_head(mock_set_head_request, 0);
+
+ C_SaferCond ctx;
+ MockSnapshotCopyRequest *request = create_request(mock_src_image_ctx,
+ mock_dst_image_ctx, 0,
+ CEPH_NOSNAP, 0, &ctx);
+ request->send();
+ ASSERT_EQ(0, ctx.wait());
+
+ validate_snap_seqs({{src_snap_id1, 12}});
+}
+
+TEST_F(TestMockDeepCopySnapshotCopyRequest, SnapProtect) {
+ REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
+
+ ASSERT_EQ(0, create_snap(m_src_image_ctx, "snap1", true));
+ ASSERT_EQ(0, create_snap(m_dst_image_ctx, "snap1", true));
+
+ uint64_t src_snap_id1 = m_src_image_ctx->snap_ids[
+ {cls::rbd::UserSnapshotNamespace(), "snap1"}];
+ uint64_t dst_snap_id1 = m_dst_image_ctx->snap_ids[
+ {cls::rbd::UserSnapshotNamespace(), "snap1"}];
+ m_snap_seqs[src_snap_id1] = dst_snap_id1;
+
+ librbd::MockTestImageCtx mock_src_image_ctx(*m_src_image_ctx);
+ librbd::MockTestImageCtx mock_dst_image_ctx(*m_dst_image_ctx);
+ MockSetHeadRequest mock_set_head_request;
+
+ librbd::MockExclusiveLock mock_exclusive_lock;
+ prepare_exclusive_lock(mock_dst_image_ctx, mock_exclusive_lock);
+
+ expect_test_features(mock_dst_image_ctx);
+
+ InSequence seq;
+ expect_snap_is_unprotected(mock_dst_image_ctx, dst_snap_id1, true, 0);
+ expect_get_snap_namespace(mock_dst_image_ctx, dst_snap_id1);
+ expect_get_snap_namespace(mock_src_image_ctx, src_snap_id1);
+ expect_snap_is_protected(mock_src_image_ctx, src_snap_id1, true, 0);
+ expect_snap_is_protected(mock_dst_image_ctx, dst_snap_id1, false, 0);
+ expect_start_op(mock_exclusive_lock);
+ expect_snap_protect(mock_dst_image_ctx, "snap1", 0);
+ expect_set_head(mock_set_head_request, 0);
+
+ C_SaferCond ctx;
+ MockSnapshotCopyRequest *request = create_request(mock_src_image_ctx,
+ mock_dst_image_ctx, 0,
+ CEPH_NOSNAP, 0, &ctx);
+ request->send();
+ ASSERT_EQ(0, ctx.wait());
+
+ validate_snap_seqs({{src_snap_id1, dst_snap_id1}});
+}
+
+TEST_F(TestMockDeepCopySnapshotCopyRequest, SnapProtectError) {
+ REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
+
+ ASSERT_EQ(0, create_snap(m_src_image_ctx, "snap1", true));
+ ASSERT_EQ(0, create_snap(m_dst_image_ctx, "snap1", true));
+
+ uint64_t src_snap_id1 = m_src_image_ctx->snap_ids[
+ {cls::rbd::UserSnapshotNamespace(), "snap1"}];
+ uint64_t dst_snap_id1 = m_dst_image_ctx->snap_ids[
+ {cls::rbd::UserSnapshotNamespace(), "snap1"}];
+ m_snap_seqs[src_snap_id1] = dst_snap_id1;
+
+ librbd::MockTestImageCtx mock_src_image_ctx(*m_src_image_ctx);
+ librbd::MockTestImageCtx mock_dst_image_ctx(*m_dst_image_ctx);
+
+ librbd::MockExclusiveLock mock_exclusive_lock;
+ prepare_exclusive_lock(mock_dst_image_ctx, mock_exclusive_lock);
+
+ expect_test_features(mock_dst_image_ctx);
+
+ InSequence seq;
+ expect_snap_is_unprotected(mock_dst_image_ctx, dst_snap_id1, true, 0);
+ expect_get_snap_namespace(mock_dst_image_ctx, dst_snap_id1);
+ expect_get_snap_namespace(mock_src_image_ctx, src_snap_id1);
+ expect_snap_is_protected(mock_src_image_ctx, src_snap_id1, true, 0);
+ expect_snap_is_protected(mock_dst_image_ctx, dst_snap_id1, false, 0);
+ expect_start_op(mock_exclusive_lock);
+ expect_snap_protect(mock_dst_image_ctx, "snap1", -EINVAL);
+
+ C_SaferCond ctx;
+ MockSnapshotCopyRequest *request = create_request(mock_src_image_ctx,
+ mock_dst_image_ctx, 0,
+ CEPH_NOSNAP, 0, &ctx);
+ request->send();
+ ASSERT_EQ(-EINVAL, ctx.wait());
+}
+
+TEST_F(TestMockDeepCopySnapshotCopyRequest, SnapProtectCancel) {
+ REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
+
+ ASSERT_EQ(0, create_snap(m_src_image_ctx, "snap1", true));
+ ASSERT_EQ(0, create_snap(m_dst_image_ctx, "snap1", true));
+
+ uint64_t src_snap_id1 = m_src_image_ctx->snap_ids[
+ {cls::rbd::UserSnapshotNamespace(), "snap1"}];
+ uint64_t dst_snap_id1 = m_dst_image_ctx->snap_ids[
+ {cls::rbd::UserSnapshotNamespace(), "snap1"}];
+ m_snap_seqs[src_snap_id1] = dst_snap_id1;
+
+ librbd::MockTestImageCtx mock_src_image_ctx(*m_src_image_ctx);
+ librbd::MockTestImageCtx mock_dst_image_ctx(*m_dst_image_ctx);
+
+ librbd::MockExclusiveLock mock_exclusive_lock;
+ prepare_exclusive_lock(mock_dst_image_ctx, mock_exclusive_lock);
+
+ C_SaferCond ctx;
+ MockSnapshotCopyRequest *request = create_request(mock_src_image_ctx,
+ mock_dst_image_ctx, 0,
+ CEPH_NOSNAP, 0, &ctx);
+ expect_test_features(mock_dst_image_ctx);
+
+ InSequence seq;
+ expect_snap_is_unprotected(mock_dst_image_ctx, dst_snap_id1, true, 0);
+ expect_get_snap_namespace(mock_dst_image_ctx, dst_snap_id1);
+ expect_get_snap_namespace(mock_src_image_ctx, src_snap_id1);
+ expect_snap_is_protected(mock_src_image_ctx, src_snap_id1, true, 0);
+ expect_snap_is_protected(mock_dst_image_ctx, dst_snap_id1, false, 0);
+ expect_start_op(mock_exclusive_lock);
+ EXPECT_CALL(*mock_dst_image_ctx.operations,
+ execute_snap_protect(_, StrEq("snap1"), _))
+ .WillOnce(DoAll(InvokeWithoutArgs([request]() {
+ request->cancel();
+ }),
+ WithArg<2>(Invoke([this](Context *ctx) {
+ m_work_queue->queue(ctx, 0);
+ }))));
+
+ request->send();
+ ASSERT_EQ(-ECANCELED, ctx.wait());
+}
+
+TEST_F(TestMockDeepCopySnapshotCopyRequest, SetHeadError) {
+ librbd::MockTestImageCtx mock_src_image_ctx(*m_src_image_ctx);
+ librbd::MockTestImageCtx mock_dst_image_ctx(*m_dst_image_ctx);
+ MockSetHeadRequest mock_set_head_request;
+
+ librbd::MockExclusiveLock mock_exclusive_lock;
+ prepare_exclusive_lock(mock_dst_image_ctx, mock_exclusive_lock);
+
+ expect_test_features(mock_dst_image_ctx);
+
+ InSequence seq;
+ expect_set_head(mock_set_head_request, -EINVAL);
+
+ C_SaferCond ctx;
+ MockSnapshotCopyRequest *request = create_request(mock_src_image_ctx,
+ mock_dst_image_ctx, 0,
+ CEPH_NOSNAP, 0, &ctx);
+ request->send();
+ ASSERT_EQ(-EINVAL, ctx.wait());
+}
+
+TEST_F(TestMockDeepCopySnapshotCopyRequest, NoSetHead) {
+ REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
+
+ ASSERT_EQ(0, create_snap(m_src_image_ctx, "snap1", true));
+
+ uint64_t src_snap_id1 = m_src_image_ctx->snap_ids[
+ {cls::rbd::UserSnapshotNamespace(), "snap1"}];
+
+ librbd::MockTestImageCtx mock_src_image_ctx(*m_src_image_ctx);
+ librbd::MockTestImageCtx mock_dst_image_ctx(*m_dst_image_ctx);
+ MockSnapshotCreateRequest mock_snapshot_create_request;
+
+ librbd::MockExclusiveLock mock_exclusive_lock;
+ prepare_exclusive_lock(mock_dst_image_ctx, mock_exclusive_lock);
+
+ expect_test_features(mock_dst_image_ctx);
+
+ InSequence seq;
+ expect_get_snap_namespace(mock_src_image_ctx, src_snap_id1);
+ expect_start_op(mock_exclusive_lock);
+ expect_snap_create(mock_dst_image_ctx, mock_snapshot_create_request, "snap1",
+ 12, 0);
+ expect_snap_is_protected(mock_src_image_ctx, src_snap_id1, false, 0);
+
+ C_SaferCond ctx;
+ MockSnapshotCopyRequest *request = create_request(mock_src_image_ctx,
+ mock_dst_image_ctx,0,
+ src_snap_id1, 0, &ctx);
+ request->send();
+ ASSERT_EQ(0, ctx.wait());
+
+ validate_snap_seqs({{src_snap_id1, 12}});
+}
+
+TEST_F(TestMockDeepCopySnapshotCopyRequest, StartEndLimit) {
+ REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
+
+ ASSERT_EQ(0, create_snap(m_src_image_ctx, "snap1", false));
+ ASSERT_EQ(0, create_snap(m_src_image_ctx, "snap2", false));
+ ASSERT_EQ(0, create_snap(m_src_image_ctx,
+ {cls::rbd::MirrorSnapshotNamespace{
+ cls::rbd::MIRROR_SNAPSHOT_STATE_PRIMARY,
+ {"peer uuid1"}, "", CEPH_NOSNAP}},
+ "snap3", false));
+ auto src_snap_id1 = m_src_image_ctx->snaps[2];
+ auto src_snap_id2 = m_src_image_ctx->snaps[1];
+ auto src_snap_id3 = m_src_image_ctx->snaps[0];
+
+ ASSERT_EQ(0, create_snap(m_dst_image_ctx, "snap0", true));
+ ASSERT_EQ(0, create_snap(m_dst_image_ctx, "snap1", false));
+ ASSERT_EQ(0, create_snap(m_dst_image_ctx, "snap3", false));
+ auto dst_snap_id1 = m_dst_image_ctx->snaps[1];
+ auto dst_snap_id3 = m_dst_image_ctx->snaps[0];
+
+ librbd::MockTestImageCtx mock_src_image_ctx(*m_src_image_ctx);
+ librbd::MockTestImageCtx mock_dst_image_ctx(*m_dst_image_ctx);
+ MockSnapshotCreateRequest mock_snapshot_create_request;
+
+ librbd::MockExclusiveLock mock_exclusive_lock;
+ prepare_exclusive_lock(mock_dst_image_ctx, mock_exclusive_lock);
+
+ expect_test_features(mock_dst_image_ctx);
+
+ InSequence seq;
+ expect_snap_is_unprotected(mock_dst_image_ctx, dst_snap_id3,
+ true, 0);
+
+ expect_get_snap_namespace(mock_dst_image_ctx, dst_snap_id3);
+ expect_start_op(mock_exclusive_lock);
+ expect_snap_remove(mock_dst_image_ctx, "snap3", 0);
+
+ expect_get_snap_namespace(mock_src_image_ctx, src_snap_id2);
+ expect_start_op(mock_exclusive_lock);
+ expect_snap_create(mock_dst_image_ctx, mock_snapshot_create_request, "snap2",
+ 12, 0);
+ expect_get_snap_namespace(mock_src_image_ctx, src_snap_id3);
+
+ expect_snap_is_protected(mock_src_image_ctx, src_snap_id2, false, 0);
+ expect_snap_is_protected(mock_src_image_ctx, src_snap_id3, false, 0);
+
+ MockSetHeadRequest mock_set_head_request;
+ expect_set_head(mock_set_head_request, 0);
+
+ C_SaferCond ctx;
+ MockSnapshotCopyRequest *request = create_request(mock_src_image_ctx,
+ mock_dst_image_ctx,
+ src_snap_id1,
+ src_snap_id3,
+ dst_snap_id1, &ctx);
+ request->send();
+ ASSERT_EQ(0, ctx.wait());
+
+ validate_snap_seqs({{src_snap_id2, 12}, {src_snap_id3, CEPH_NOSNAP}});
+}
+
+} // namespace deep_copy
+} // namespace librbd
diff --git a/src/test/librbd/deep_copy/test_mock_SnapshotCreateRequest.cc b/src/test/librbd/deep_copy/test_mock_SnapshotCreateRequest.cc
new file mode 100644
index 000000000..af24f5a0c
--- /dev/null
+++ b/src/test/librbd/deep_copy/test_mock_SnapshotCreateRequest.cc
@@ -0,0 +1,262 @@
+// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
+// vim: ts=8 sw=2 smarttab
+
+#include "test/librbd/test_mock_fixture.h"
+#include "test/librados_test_stub/LibradosTestStub.h"
+#include "include/rbd/librbd.hpp"
+#include "librbd/AsioEngine.h"
+#include "librbd/ImageCtx.h"
+#include "librbd/ImageState.h"
+#include "librbd/Operations.h"
+#include "osdc/Striper.h"
+#include "test/librados_test_stub/MockTestMemIoCtxImpl.h"
+#include "test/librbd/mock/MockImageCtx.h"
+#include "librbd/deep_copy/SetHeadRequest.h"
+#include "librbd/deep_copy/SnapshotCreateRequest.h"
+
+namespace librbd {
+namespace {
+
+struct MockTestImageCtx : public librbd::MockImageCtx {
+ explicit MockTestImageCtx(librbd::ImageCtx &image_ctx)
+ : librbd::MockImageCtx(image_ctx) {
+ }
+};
+
+} // anonymous namespace
+
+namespace deep_copy {
+
+template <>
+class SetHeadRequest<librbd::MockTestImageCtx> {
+public:
+ static SetHeadRequest* s_instance;
+ Context *on_finish;
+
+ static SetHeadRequest* create(librbd::MockTestImageCtx *image_ctx,
+ uint64_t size,
+ const cls::rbd::ParentImageSpec &parent_spec,
+ uint64_t parent_overlap, Context *on_finish) {
+ ceph_assert(s_instance != nullptr);
+ s_instance->on_finish = on_finish;
+ return s_instance;
+ }
+
+ SetHeadRequest() {
+ s_instance = this;
+ }
+
+ MOCK_METHOD0(send, void());
+};
+
+SetHeadRequest<librbd::MockTestImageCtx>* SetHeadRequest<librbd::MockTestImageCtx>::s_instance = nullptr;
+
+} // namespace deep_copy
+} // namespace librbd
+
+// template definitions
+#include "librbd/deep_copy/SnapshotCreateRequest.cc"
+template class librbd::deep_copy::SnapshotCreateRequest<librbd::MockTestImageCtx>;
+
+namespace librbd {
+namespace deep_copy {
+
+using ::testing::_;
+using ::testing::DoAll;
+using ::testing::InSequence;
+using ::testing::Invoke;
+using ::testing::InvokeWithoutArgs;
+using ::testing::Return;
+using ::testing::ReturnNew;
+using ::testing::StrEq;
+using ::testing::WithArg;
+
+class TestMockDeepCopySnapshotCreateRequest : public TestMockFixture {
+public:
+ typedef SetHeadRequest<librbd::MockTestImageCtx> MockSetHeadRequest;
+ typedef SnapshotCreateRequest<librbd::MockTestImageCtx> MockSnapshotCreateRequest;
+
+ librbd::ImageCtx *m_image_ctx;
+
+ std::shared_ptr<librbd::AsioEngine> m_asio_engine;
+ asio::ContextWQ *m_work_queue;
+
+ void SetUp() override {
+ TestMockFixture::SetUp();
+
+ ASSERT_EQ(0, open_image(m_image_name, &m_image_ctx));
+
+ m_asio_engine = std::make_shared<librbd::AsioEngine>(
+ m_image_ctx->md_ctx);
+ m_work_queue = m_asio_engine->get_work_queue();
+ }
+
+ void expect_start_op(librbd::MockExclusiveLock &mock_exclusive_lock) {
+ EXPECT_CALL(mock_exclusive_lock, start_op(_)).WillOnce(Return(new LambdaContext([](int){})));
+ }
+
+ void expect_test_features(librbd::MockTestImageCtx &mock_image_ctx,
+ uint64_t features, bool enabled) {
+ EXPECT_CALL(mock_image_ctx, test_features(features))
+ .WillOnce(Return(enabled));
+ }
+
+ void expect_set_head(MockSetHeadRequest &mock_set_head_request, int r) {
+ EXPECT_CALL(mock_set_head_request, send())
+ .WillOnce(Invoke([&mock_set_head_request, r]() {
+ mock_set_head_request.on_finish->complete(r);
+ }));
+ }
+
+ void expect_snap_create(librbd::MockTestImageCtx &mock_image_ctx,
+ const std::string &snap_name, uint64_t snap_id, int r) {
+ uint64_t flags = SNAP_CREATE_FLAG_SKIP_OBJECT_MAP |
+ SNAP_CREATE_FLAG_SKIP_NOTIFY_QUIESCE;
+ EXPECT_CALL(*mock_image_ctx.operations,
+ execute_snap_create(_, StrEq(snap_name), _, 0, flags, _))
+ .WillOnce(DoAll(InvokeWithoutArgs([&mock_image_ctx, snap_id, snap_name]() {
+ inject_snap(mock_image_ctx, snap_id, snap_name);
+ }),
+ WithArg<2>(Invoke([this, r](Context *ctx) {
+ m_work_queue->queue(ctx, r);
+ }))));
+ }
+
+ void expect_object_map_resize(librbd::MockTestImageCtx &mock_image_ctx,
+ librados::snap_t snap_id, int r) {
+ std::string oid(librbd::ObjectMap<>::object_map_name(mock_image_ctx.id,
+ snap_id));
+ EXPECT_CALL(get_mock_io_ctx(mock_image_ctx.md_ctx),
+ exec(oid, _, StrEq("rbd"), StrEq("object_map_resize"), _, _, _,
+ _))
+ .WillOnce(Return(r));
+ }
+
+ static void inject_snap(librbd::MockTestImageCtx &mock_image_ctx,
+ uint64_t snap_id, const std::string &snap_name) {
+ mock_image_ctx.snap_ids[{cls::rbd::UserSnapshotNamespace(),
+ snap_name}] = snap_id;
+ }
+
+ MockSnapshotCreateRequest *create_request(librbd::MockTestImageCtx &mock_local_image_ctx,
+ const std::string &snap_name,
+ const cls::rbd::SnapshotNamespace &snap_namespace,
+ uint64_t size,
+ const cls::rbd::ParentImageSpec &spec,
+ uint64_t parent_overlap,
+ Context *on_finish) {
+ return new MockSnapshotCreateRequest(&mock_local_image_ctx, snap_name, snap_namespace, size,
+ spec, parent_overlap, on_finish);
+ }
+};
+
+TEST_F(TestMockDeepCopySnapshotCreateRequest, SnapCreate) {
+ librbd::MockTestImageCtx mock_image_ctx(*m_image_ctx);
+ librbd::MockExclusiveLock mock_exclusive_lock;
+ mock_image_ctx.exclusive_lock = &mock_exclusive_lock;
+ MockSetHeadRequest mock_set_head_request;
+
+ InSequence seq;
+ expect_set_head(mock_set_head_request, 0);
+ expect_start_op(mock_exclusive_lock);
+ expect_snap_create(mock_image_ctx, "snap1", 10, 0);
+ expect_test_features(mock_image_ctx, RBD_FEATURE_OBJECT_MAP, false);
+
+ C_SaferCond ctx;
+ MockSnapshotCreateRequest *request = create_request(mock_image_ctx,
+ "snap1",
+ cls::rbd::UserSnapshotNamespace(),
+ m_image_ctx->size,
+ {}, 0, &ctx);
+ request->send();
+ ASSERT_EQ(0, ctx.wait());
+}
+
+TEST_F(TestMockDeepCopySnapshotCreateRequest, SetHeadError) {
+ librbd::MockTestImageCtx mock_image_ctx(*m_image_ctx);
+ MockSetHeadRequest mock_set_head_request;
+
+ InSequence seq;
+ expect_set_head(mock_set_head_request, -EINVAL);
+
+ C_SaferCond ctx;
+ MockSnapshotCreateRequest *request = create_request(mock_image_ctx,
+ "snap1",
+ cls::rbd::UserSnapshotNamespace(),
+ 123, {}, 0,
+ &ctx);
+ request->send();
+ ASSERT_EQ(-EINVAL, ctx.wait());
+}
+
+TEST_F(TestMockDeepCopySnapshotCreateRequest, SnapCreateError) {
+ librbd::MockTestImageCtx mock_image_ctx(*m_image_ctx);
+ librbd::MockExclusiveLock mock_exclusive_lock;
+ mock_image_ctx.exclusive_lock = &mock_exclusive_lock;
+ MockSetHeadRequest mock_set_head_request;
+
+ InSequence seq;
+ expect_set_head(mock_set_head_request, 0);
+ expect_start_op(mock_exclusive_lock);
+ expect_snap_create(mock_image_ctx, "snap1", 10, -EINVAL);
+
+ C_SaferCond ctx;
+ MockSnapshotCreateRequest *request = create_request(mock_image_ctx,
+ "snap1",
+ cls::rbd::UserSnapshotNamespace(),
+ m_image_ctx->size,
+ {}, 0, &ctx);
+ request->send();
+ ASSERT_EQ(-EINVAL, ctx.wait());
+}
+
+TEST_F(TestMockDeepCopySnapshotCreateRequest, ResizeObjectMap) {
+ librbd::MockTestImageCtx mock_image_ctx(*m_image_ctx);
+ librbd::MockExclusiveLock mock_exclusive_lock;
+ mock_image_ctx.exclusive_lock = &mock_exclusive_lock;
+ MockSetHeadRequest mock_set_head_request;
+
+ InSequence seq;
+ expect_set_head(mock_set_head_request, 0);
+ expect_start_op(mock_exclusive_lock);
+ expect_snap_create(mock_image_ctx, "snap1", 10, 0);
+ expect_test_features(mock_image_ctx, RBD_FEATURE_OBJECT_MAP, true);
+ expect_start_op(mock_exclusive_lock);
+ expect_object_map_resize(mock_image_ctx, 10, 0);
+
+ C_SaferCond ctx;
+ MockSnapshotCreateRequest *request = create_request(mock_image_ctx,
+ "snap1",
+ cls::rbd::UserSnapshotNamespace(),
+ m_image_ctx->size,
+ {}, 0, &ctx);
+ request->send();
+ ASSERT_EQ(0, ctx.wait());
+}
+
+TEST_F(TestMockDeepCopySnapshotCreateRequest, ResizeObjectMapError) {
+ librbd::MockTestImageCtx mock_image_ctx(*m_image_ctx);
+ librbd::MockExclusiveLock mock_exclusive_lock;
+ mock_image_ctx.exclusive_lock = &mock_exclusive_lock;
+ MockSetHeadRequest mock_set_head_request;
+
+ InSequence seq;
+ expect_set_head(mock_set_head_request, 0);
+ expect_start_op(mock_exclusive_lock);
+ expect_snap_create(mock_image_ctx, "snap1", 10, 0);
+ expect_test_features(mock_image_ctx, RBD_FEATURE_OBJECT_MAP, true);
+ expect_start_op(mock_exclusive_lock);
+ expect_object_map_resize(mock_image_ctx, 10, -EINVAL);
+
+ C_SaferCond ctx;
+ MockSnapshotCreateRequest *request = create_request(mock_image_ctx,
+ "snap1",
+ cls::rbd::UserSnapshotNamespace(),
+ m_image_ctx->size,
+ {}, 0, &ctx);
+ request->send();
+ ASSERT_EQ(-EINVAL, ctx.wait());
+}
+
+} // namespace deep_copy
+} // namespace librbd