]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
rbd-mirror: tweaks to support creating mock test cases
authorJason Dillaman <dillaman@redhat.com>
Wed, 23 Mar 2016 21:28:22 +0000 (17:28 -0400)
committerJason Dillaman <dillaman@redhat.com>
Tue, 29 Mar 2016 19:12:29 +0000 (15:12 -0400)
Signed-off-by: Jason Dillaman <dillaman@redhat.com>
src/librbd/ImageCtx.h
src/test/Makefile-client.am
src/test/librbd/mock/MockImageCtx.cc [new file with mode: 0644]
src/test/librbd/mock/MockImageCtx.h
src/test/librbd/mock/MockOperations.h
src/tools/rbd_mirror/ImageSync.h
src/tools/rbd_mirror/image_replayer/BootstrapRequest.cc
src/tools/rbd_mirror/image_replayer/OpenLocalImageRequest.cc

index 551c4bfc22addbc1f2b8458a34d6fefc1e68b701..3b58c6699ff765d8d4967c866593def2eb3cb934 100644 (file)
@@ -194,6 +194,16 @@ namespace librbd {
     static bool _filter_metadata_confs(const string &prefix, std::map<string, bool> &configs,
                                        map<string, bufferlist> &pairs, map<string, bufferlist> *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,
index 90d1875eeb691966db95363c69d3005ed49c698e..c35c57f8d7490446b91577a0f89e128b5a20cc48 100644 (file)
@@ -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 (file)
index 0000000..b207eab
--- /dev/null
@@ -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
index 06168965c60bb9a2c8af70b1a5e5d1c1df273075..170e9a141f004232b0a2843632eb8f6d0062ac61 100644 (file)
@@ -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 <string>
 
 namespace librbd {
 
@@ -25,6 +27,15 @@ template <typename> 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),
index 61501256501caff9a06da674377a2a1baeb5908e..c92578a36f1ad2337774664704434576dfcb6b73 100644 (file)
@@ -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;
index a9e0d517e7a8c0b6255106d519ea176b6605058b..1ed22568f52b70bdcfbc78bb08d378fccea0ab96 100644 (file)
@@ -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,
index 77fea4fef63965f59765d236d3e7b496b92823b9..18f9bf4718d31ee9b6e5bd06df6fdc48305d3c24 100644 (file)
@@ -153,10 +153,8 @@ template <typename I>
 void BootstrapRequest<I>::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<I>, &BootstrapRequest<I>::handle_open_remote_image>(
       this);
@@ -304,14 +302,13 @@ template <typename I>
 void BootstrapRequest<I>::image_sync() {
   dout(20) << dendl;
 
-  // TODO: need factory method to support mocking
   Context *ctx = create_context_callback<
     BootstrapRequest<I>, &BootstrapRequest<I>::handle_image_sync>(
       this);
-  ImageSync<I> *request = new ImageSync<I>(*m_local_image_ctx,
-                                           m_remote_image_ctx, m_timer,
-                                           m_timer_lock, m_mirror_uuid,
-                                           m_journaler, m_client_meta, ctx);
+  ImageSync<I> *request = ImageSync<I>::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();
 }
 
index 0d2b2871e54b632591370d959bf64b0a31c3afad..3f4f483707e12a95bb222ec57edbc60026f91a85 100644 (file)
@@ -77,9 +77,8 @@ template <typename I>
 void OpenLocalImageRequest<I>::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(