From 511039eca9b6b66f16598217c1ad8d9ae5365c0c Mon Sep 17 00:00:00 2001 From: Jason Dillaman Date: Wed, 23 Mar 2016 17:28:22 -0400 Subject: [PATCH] rbd-mirror: tweaks to support creating mock test cases Signed-off-by: Jason Dillaman --- src/librbd/ImageCtx.h | 10 ++++++++++ src/test/Makefile-client.am | 9 ++++++++- src/test/librbd/mock/MockImageCtx.cc | 10 ++++++++++ src/test/librbd/mock/MockImageCtx.h | 11 +++++++++++ src/test/librbd/mock/MockOperations.h | 1 + src/tools/rbd_mirror/ImageSync.h | 10 ++++++++++ .../rbd_mirror/image_replayer/BootstrapRequest.cc | 15 ++++++--------- .../image_replayer/OpenLocalImageRequest.cc | 5 ++--- 8 files changed, 58 insertions(+), 13 deletions(-) create mode 100644 src/test/librbd/mock/MockImageCtx.cc diff --git a/src/librbd/ImageCtx.h b/src/librbd/ImageCtx.h index 551c4bfc22add..3b58c6699ff76 100644 --- a/src/librbd/ImageCtx.h +++ b/src/librbd/ImageCtx.h @@ -194,6 +194,16 @@ namespace librbd { static bool _filter_metadata_confs(const string &prefix, std::map &configs, map &pairs, map *res); + // unit test mock helpers + static ImageCtx* create(const std::string &image_name, + const std::string &image_id, + const char *snap, IoCtx& p, bool read_only) { + return new ImageCtx(image_name, image_id, snap, p, read_only); + } + void destroy() { + delete this; + } + /** * Either image_name or image_id must be set. * If id is not known, pass the empty std::string, diff --git a/src/test/Makefile-client.am b/src/test/Makefile-client.am index 90d1875eeb691..c35c57f8d7490 100644 --- a/src/test/Makefile-client.am +++ b/src/test/Makefile-client.am @@ -368,6 +368,11 @@ librbd_test_la_SOURCES = \ librbd_test_la_CXXFLAGS = $(UNITTEST_CXXFLAGS) noinst_LTLIBRARIES += librbd_test.la +librbd_test_mock_la_SOURCES = \ + test/librbd/mock/MockImageCtx.cc +librbd_test_mock_la_CXXFLAGS = $(UNITTEST_CXXFLAGS) +noinst_LTLIBRARIES += librbd_test_mock.la + unittest_librbd_SOURCES = \ test/librbd/test_main.cc \ test/librbd/test_mock_fixture.cc \ @@ -394,7 +399,8 @@ unittest_librbd_SOURCES = \ test/librbd/operation/test_mock_SnapshotUnprotectRequest.cc unittest_librbd_CXXFLAGS = $(UNITTEST_CXXFLAGS) -DTEST_LIBRBD_INTERNALS unittest_librbd_LDADD = \ - librbd_test.la librbd_api.la librbd_internal.la $(LIBRBD_TYPES) \ + librbd_test.la librbd_test_mock.la \ + librbd_api.la librbd_internal.la $(LIBRBD_TYPES) \ libcls_rbd_client.la libcls_lock_client.la \ libjournal.la libcls_journal_client.la \ librados_test_stub.la librados_internal.la \ @@ -469,6 +475,7 @@ unittest_rbd_mirror_SOURCES = \ unittest_rbd_mirror_CXXFLAGS = $(UNITTEST_CXXFLAGS) unittest_rbd_mirror_LDADD = \ librbd_mirror_test.la \ + librbd_test_mock.la \ librados_test_stub.la \ librbd_mirror_internal.la \ librbd_internal.la \ diff --git a/src/test/librbd/mock/MockImageCtx.cc b/src/test/librbd/mock/MockImageCtx.cc new file mode 100644 index 0000000000000..b207eab0dedf5 --- /dev/null +++ b/src/test/librbd/mock/MockImageCtx.cc @@ -0,0 +1,10 @@ +// -*- mode:C; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- +// vim: ts=8 sw=2 smarttab + +#include "test/librbd/mock/MockImageCtx.h" + +namespace librbd { + +MockImageCtx* MockImageCtx::s_instance = nullptr; + +} // namespace librbd diff --git a/src/test/librbd/mock/MockImageCtx.h b/src/test/librbd/mock/MockImageCtx.h index 06168965c60bb..170e9a141f004 100644 --- a/src/test/librbd/mock/MockImageCtx.h +++ b/src/test/librbd/mock/MockImageCtx.h @@ -4,6 +4,7 @@ #ifndef CEPH_TEST_LIBRBD_MOCK_IMAGE_CTX_H #define CEPH_TEST_LIBRBD_MOCK_IMAGE_CTX_H +#include "include/rados/librados.hpp" #include "test/librbd/mock/MockAioImageRequestWQ.h" #include "test/librbd/mock/MockContextWQ.h" #include "test/librbd/mock/MockExclusiveLock.h" @@ -17,6 +18,7 @@ #include "common/WorkQueue.h" #include "librbd/ImageCtx.h" #include "gmock/gmock.h" +#include namespace librbd { @@ -25,6 +27,15 @@ template class ResizeRequest; } struct MockImageCtx { + static MockImageCtx *s_instance; + static MockImageCtx *create(const std::string &image_name, + const std::string &image_id, + const char *snap, librados::IoCtx& p, + bool read_only) { + assert(s_instance != nullptr); + return s_instance; + } + MockImageCtx(librbd::ImageCtx &image_ctx) : image_ctx(&image_ctx), cct(image_ctx.cct), diff --git a/src/test/librbd/mock/MockOperations.h b/src/test/librbd/mock/MockOperations.h index 61501256501ca..c92578a36f1ad 100644 --- a/src/test/librbd/mock/MockOperations.h +++ b/src/test/librbd/mock/MockOperations.h @@ -5,6 +5,7 @@ #define CEPH_TEST_LIBRBD_MOCK_OPERATIONS_H #include "include/int_types.h" +#include "include/rbd/librbd.hpp" #include "gmock/gmock.h" class Context; diff --git a/src/tools/rbd_mirror/ImageSync.h b/src/tools/rbd_mirror/ImageSync.h index a9e0d517e7a8c..1ed22568f52b7 100644 --- a/src/tools/rbd_mirror/ImageSync.h +++ b/src/tools/rbd_mirror/ImageSync.h @@ -29,6 +29,16 @@ public: typedef typename TypeTraits::Journaler Journaler; typedef librbd::journal::MirrorPeerClientMeta MirrorPeerClientMeta; + static ImageSync* create(ImageCtxT *local_image_ctx, + ImageCtxT *remote_image_ctx, SafeTimer *timer, + Mutex *timer_lock, const std::string &mirror_uuid, + Journaler *journaler, + MirrorPeerClientMeta *client_meta, + Context *on_finish) { + return new ImageSync(local_image_ctx, remote_image_ctx, timer, timer_lock, + mirror_uuid, journaler, client_meta, on_finish); + } + ImageSync(ImageCtxT *local_image_ctx, ImageCtxT *remote_image_ctx, SafeTimer *timer, Mutex *timer_lock, const std::string &mirror_uuid, Journaler *journaler, MirrorPeerClientMeta *client_meta, diff --git a/src/tools/rbd_mirror/image_replayer/BootstrapRequest.cc b/src/tools/rbd_mirror/image_replayer/BootstrapRequest.cc index 77fea4fef6396..18f9bf4718d31 100644 --- a/src/tools/rbd_mirror/image_replayer/BootstrapRequest.cc +++ b/src/tools/rbd_mirror/image_replayer/BootstrapRequest.cc @@ -153,10 +153,8 @@ template void BootstrapRequest::open_remote_image() { dout(20) << dendl; - // TODO: need factory method to support mocking - m_remote_image_ctx = new I("", m_remote_image_id, nullptr, m_remote_io_ctx, - false); - + m_remote_image_ctx = I::create("", m_remote_image_id, nullptr, + m_remote_io_ctx, false); Context *ctx = create_context_callback< BootstrapRequest, &BootstrapRequest::handle_open_remote_image>( this); @@ -304,14 +302,13 @@ template void BootstrapRequest::image_sync() { dout(20) << dendl; - // TODO: need factory method to support mocking Context *ctx = create_context_callback< BootstrapRequest, &BootstrapRequest::handle_image_sync>( this); - ImageSync *request = new ImageSync(*m_local_image_ctx, - m_remote_image_ctx, m_timer, - m_timer_lock, m_mirror_uuid, - m_journaler, m_client_meta, ctx); + ImageSync *request = ImageSync::create(*m_local_image_ctx, + m_remote_image_ctx, m_timer, + m_timer_lock, m_mirror_uuid, + m_journaler, m_client_meta, ctx); request->start(); } diff --git a/src/tools/rbd_mirror/image_replayer/OpenLocalImageRequest.cc b/src/tools/rbd_mirror/image_replayer/OpenLocalImageRequest.cc index 0d2b2871e54b6..3f4f483707e12 100644 --- a/src/tools/rbd_mirror/image_replayer/OpenLocalImageRequest.cc +++ b/src/tools/rbd_mirror/image_replayer/OpenLocalImageRequest.cc @@ -77,9 +77,8 @@ template void OpenLocalImageRequest::send_open_image() { dout(20) << dendl; - *m_local_image_ctx = new librbd::ImageCtx(m_local_image_name, - m_local_image_id, nullptr, - m_local_io_ctx, false); + *m_local_image_ctx = I::create(m_local_image_name, m_local_image_id, nullptr, + m_local_io_ctx, false); (*m_local_image_ctx)->set_exclusive_lock_policy( new MirrorExclusiveLockPolicy()); (*m_local_image_ctx)->set_journal_policy( -- 2.39.5