Skip to content

Commit

Permalink
rbd-mirror: fix the unittest for new CloneRequest
Browse files Browse the repository at this point in the history
Signed-off-by: Dongsheng Yang <dongsheng.yang@easystack.cn>
  • Loading branch information
yangdongsheng committed Dec 7, 2016
1 parent b903423 commit 86f372f
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 90 deletions.
102 changes: 47 additions & 55 deletions src/test/rbd_mirror/image_replayer/test_mock_CreateImageRequest.cc
Expand Up @@ -13,7 +13,7 @@
#include "tools/rbd_mirror/image_replayer/OpenImageRequest.h"
#include "tools/rbd_mirror/image_replayer/OpenLocalImageRequest.h"
#include "librbd/image/CreateRequest.h"
#include "tools/rbd_mirror/image_replayer/Utils.h"
#include "librbd/image/CloneRequest.h"
#include "tools/rbd_mirror/Threads.h"

namespace librbd {
Expand Down Expand Up @@ -63,50 +63,44 @@ struct CreateRequest<librbd::MockTestImageCtx> {
CreateRequest<librbd::MockTestImageCtx>*
CreateRequest<librbd::MockTestImageCtx>::s_instance = nullptr;

} // namespace image
} // namespace librbd

namespace rbd {
namespace mirror {
namespace image_replayer {
template<>
struct CloneRequest<librbd::MockTestImageCtx> {
static CloneRequest *s_instance;
Context *on_finish = nullptr;

struct CreateCloneImage {
static CreateCloneImage *s_instance;
static CreateCloneImage *get_instance() {
static CloneRequest *create(librbd::MockTestImageCtx *p_imctx,
IoCtx &c_ioctx, const std::string &c_name,
ImageOptions c_options,
const std::string &non_primary_global_image_id,
const std::string &primary_mirror_uuid,
MockContextWQ *op_work_queue, Context *on_finish) {
assert(s_instance != nullptr);
s_instance->on_finish = on_finish;
s_instance->construct(p_imctx);
return s_instance;
}

CreateCloneImage() {
assert(s_instance == nullptr);
CloneRequest() {
s_instance = this;
}
~CreateCloneImage() {

~CloneRequest() {
s_instance = nullptr;
}

MOCK_METHOD3(clone, int(const std::string &image_name,
const std::string &non_primary_global_image_id,
const std::string &primary_mirror_uuid));
MOCK_METHOD0(send, void());
MOCK_METHOD1(construct, void(librbd::MockTestImageCtx *p_imctx));
};

CreateCloneImage *CreateCloneImage::s_instance = nullptr;

namespace utils {
CloneRequest<librbd::MockTestImageCtx>*
CloneRequest<librbd::MockTestImageCtx>::s_instance = nullptr;

template <>
int clone_image<librbd::MockTestImageCtx>(librbd::MockTestImageCtx *p_imctx,
librados::IoCtx& c_ioctx,
const char *c_name,
librbd::ImageOptions& c_opts,
const std::string &non_primary_global_image_id,
const std::string &remote_mirror_uuid) {
return CreateCloneImage::get_instance()->clone(c_name,
non_primary_global_image_id,
remote_mirror_uuid);
}
} // namespace image
} // namespace librbd

} // namespace utils
namespace rbd {
namespace mirror {
namespace image_replayer {

template<>
struct CloseImageRequest<librbd::MockTestImageCtx> {
Expand Down Expand Up @@ -197,6 +191,7 @@ MATCHER_P(IsSameIoCtx, io_ctx, "") {
class TestMockImageReplayerCreateImageRequest : public TestMockFixture {
public:
typedef librbd::image::CreateRequest<librbd::MockTestImageCtx> MockCreateRequest;
typedef librbd::image::CloneRequest<librbd::MockTestImageCtx> MockCloneRequest;
typedef CreateImageRequest<librbd::MockTestImageCtx> MockCreateImageRequest;
typedef OpenImageRequest<librbd::MockTestImageCtx> MockOpenImageRequest;
typedef CloseImageRequest<librbd::MockTestImageCtx> MockCloseImageRequest;
Expand Down Expand Up @@ -293,13 +288,14 @@ class TestMockImageReplayerCreateImageRequest : public TestMockFixture {
})));
}

void expect_clone_image(CreateCloneImage &create_clone_image,
const std::string &local_image_name,
const std::string &global_image_id,
const std::string &remote_mirror_uuid, int r) {
EXPECT_CALL(create_clone_image, clone(local_image_name, global_image_id,
remote_mirror_uuid))
.WillOnce(Return(r));
void expect_clone_image(MockCloneRequest &mock_clone_request,
librbd::MockTestImageCtx *mock_parent_imctx,
int r) {
EXPECT_CALL(mock_clone_request, construct(mock_parent_imctx));
EXPECT_CALL(mock_clone_request, send())
.WillOnce(Invoke([this, &mock_clone_request, r]() {
m_threads->work_queue->queue(mock_clone_request.on_finish, r);
}));
}

void expect_close_image(MockCloseImageRequest &mock_close_image_request,
Expand Down Expand Up @@ -371,7 +367,7 @@ TEST_F(TestMockImageReplayerCreateImageRequest, Clone) {
librbd::MockTestImageCtx mock_remote_parent_image_ctx(*m_remote_image_ctx);
librbd::MockTestImageCtx mock_local_parent_image_ctx(*local_image_ctx);
librbd::MockTestImageCtx mock_remote_clone_image_ctx(*remote_clone_image_ctx);
CreateCloneImage create_clone_image;
MockCloneRequest mock_clone_request;
MockOpenImageRequest mock_open_image_request;
MockCloseImageRequest mock_close_image_request;

Expand All @@ -386,8 +382,7 @@ TEST_F(TestMockImageReplayerCreateImageRequest, Clone) {
expect_open_image(mock_open_image_request, m_local_io_ctx,
"local parent id", mock_local_parent_image_ctx, 0);
expect_snap_set(mock_local_parent_image_ctx, "snap", 0);
expect_clone_image(create_clone_image, "image name", "global uuid",
"remote uuid", 0);
expect_clone_image(mock_clone_request, &mock_local_parent_image_ctx, 0);
expect_close_image(mock_close_image_request, mock_local_parent_image_ctx, 0);
expect_close_image(mock_close_image_request, mock_remote_parent_image_ctx, 0);

Expand All @@ -409,7 +404,7 @@ TEST_F(TestMockImageReplayerCreateImageRequest, CloneGetGlobalImageIdError) {
&remote_clone_image_ctx));

librbd::MockTestImageCtx mock_remote_clone_image_ctx(*remote_clone_image_ctx);
CreateCloneImage create_clone_image;
MockCloneRequest mock_clone_request;

InSequence seq;
expect_ioctx_create(m_remote_io_ctx);
Expand All @@ -434,7 +429,7 @@ TEST_F(TestMockImageReplayerCreateImageRequest, CloneGetLocalParentImageIdError)
&remote_clone_image_ctx));

librbd::MockTestImageCtx mock_remote_clone_image_ctx(*remote_clone_image_ctx);
CreateCloneImage create_clone_image;
MockCloneRequest mock_clone_request;

InSequence seq;
expect_ioctx_create(m_remote_io_ctx);
Expand All @@ -461,7 +456,7 @@ TEST_F(TestMockImageReplayerCreateImageRequest, CloneOpenRemoteParentError) {

librbd::MockTestImageCtx mock_remote_parent_image_ctx(*m_remote_image_ctx);
librbd::MockTestImageCtx mock_remote_clone_image_ctx(*remote_clone_image_ctx);
CreateCloneImage create_clone_image;
MockCloneRequest mock_clone_request;
MockOpenImageRequest mock_open_image_request;

InSequence seq;
Expand Down Expand Up @@ -498,7 +493,7 @@ TEST_F(TestMockImageReplayerCreateImageRequest, CloneOpenLocalParentError) {
librbd::MockTestImageCtx mock_remote_parent_image_ctx(*m_remote_image_ctx);
librbd::MockTestImageCtx mock_local_parent_image_ctx(*local_image_ctx);
librbd::MockTestImageCtx mock_remote_clone_image_ctx(*remote_clone_image_ctx);
CreateCloneImage create_clone_image;
MockCloneRequest mock_clone_request;
MockOpenImageRequest mock_open_image_request;
MockCloseImageRequest mock_close_image_request;

Expand Down Expand Up @@ -539,7 +534,7 @@ TEST_F(TestMockImageReplayerCreateImageRequest, CloneSnapSetError) {
librbd::MockTestImageCtx mock_remote_parent_image_ctx(*m_remote_image_ctx);
librbd::MockTestImageCtx mock_local_parent_image_ctx(*local_image_ctx);
librbd::MockTestImageCtx mock_remote_clone_image_ctx(*remote_clone_image_ctx);
CreateCloneImage create_clone_image;
MockCloneRequest mock_clone_request;
MockOpenImageRequest mock_open_image_request;
MockCloseImageRequest mock_close_image_request;

Expand Down Expand Up @@ -582,7 +577,7 @@ TEST_F(TestMockImageReplayerCreateImageRequest, CloneError) {
librbd::MockTestImageCtx mock_remote_parent_image_ctx(*m_remote_image_ctx);
librbd::MockTestImageCtx mock_local_parent_image_ctx(*local_image_ctx);
librbd::MockTestImageCtx mock_remote_clone_image_ctx(*remote_clone_image_ctx);
CreateCloneImage create_clone_image;
MockCloneRequest mock_clone_request;
MockOpenImageRequest mock_open_image_request;
MockCloseImageRequest mock_close_image_request;

Expand All @@ -597,8 +592,7 @@ TEST_F(TestMockImageReplayerCreateImageRequest, CloneError) {
expect_open_image(mock_open_image_request, m_local_io_ctx,
"local parent id", mock_local_parent_image_ctx, 0);
expect_snap_set(mock_local_parent_image_ctx, "snap", 0);
expect_clone_image(create_clone_image, "image name", "global uuid",
"remote uuid", -EINVAL);
expect_clone_image(mock_clone_request, &mock_local_parent_image_ctx, -EINVAL);
expect_close_image(mock_close_image_request, mock_local_parent_image_ctx, 0);
expect_close_image(mock_close_image_request, mock_remote_parent_image_ctx, 0);

Expand Down Expand Up @@ -627,7 +621,7 @@ TEST_F(TestMockImageReplayerCreateImageRequest, CloneLocalParentCloseError) {
librbd::MockTestImageCtx mock_remote_parent_image_ctx(*m_remote_image_ctx);
librbd::MockTestImageCtx mock_local_parent_image_ctx(*local_image_ctx);
librbd::MockTestImageCtx mock_remote_clone_image_ctx(*remote_clone_image_ctx);
CreateCloneImage create_clone_image;
MockCloneRequest mock_clone_request;
MockOpenImageRequest mock_open_image_request;
MockCloseImageRequest mock_close_image_request;

Expand All @@ -642,8 +636,7 @@ TEST_F(TestMockImageReplayerCreateImageRequest, CloneLocalParentCloseError) {
expect_open_image(mock_open_image_request, m_local_io_ctx,
"local parent id", mock_local_parent_image_ctx, 0);
expect_snap_set(mock_local_parent_image_ctx, "snap", 0);
expect_clone_image(create_clone_image, "image name", "global uuid",
"remote uuid", 0);
expect_clone_image(mock_clone_request, &mock_local_parent_image_ctx, 0);
expect_close_image(mock_close_image_request, mock_local_parent_image_ctx, -EINVAL);
expect_close_image(mock_close_image_request, mock_remote_parent_image_ctx, 0);

Expand Down Expand Up @@ -672,7 +665,7 @@ TEST_F(TestMockImageReplayerCreateImageRequest, CloneRemoteParentCloseError) {
librbd::MockTestImageCtx mock_remote_parent_image_ctx(*m_remote_image_ctx);
librbd::MockTestImageCtx mock_local_parent_image_ctx(*local_image_ctx);
librbd::MockTestImageCtx mock_remote_clone_image_ctx(*remote_clone_image_ctx);
CreateCloneImage create_clone_image;
MockCloneRequest mock_clone_request;
MockOpenImageRequest mock_open_image_request;
MockCloseImageRequest mock_close_image_request;

Expand All @@ -687,8 +680,7 @@ TEST_F(TestMockImageReplayerCreateImageRequest, CloneRemoteParentCloseError) {
expect_open_image(mock_open_image_request, m_local_io_ctx,
"local parent id", mock_local_parent_image_ctx, 0);
expect_snap_set(mock_local_parent_image_ctx, "snap", 0);
expect_clone_image(create_clone_image, "image name", "global uuid",
"remote uuid", 0);
expect_clone_image(mock_clone_request, &mock_local_parent_image_ctx, 0);
expect_close_image(mock_close_image_request, mock_local_parent_image_ctx, 0);
expect_close_image(mock_close_image_request, mock_remote_parent_image_ctx, -EINVAL);

Expand Down
35 changes: 0 additions & 35 deletions src/tools/rbd_mirror/image_replayer/Utils.h

This file was deleted.

0 comments on commit 86f372f

Please sign in to comment.