summaryrefslogtreecommitdiffstats
path: root/src/test/librbd/deep_copy/test_mock_MetadataCopyRequest.cc
blob: 8717f684a8ed2fe21efc46e8376673207e98913d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// -*- 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/ImageCtx.h"
#include "librbd/deep_copy/MetadataCopyRequest.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 librbd

// template definitions
#include "librbd/deep_copy/MetadataCopyRequest.cc"

namespace librbd {
namespace deep_copy {

using ::testing::_;
using ::testing::DoAll;
using ::testing::InSequence;
using ::testing::Return;
using ::testing::StrEq;
using ::testing::WithArg;

class TestMockDeepCopyMetadataCopyRequest : public TestMockFixture {
public:
  typedef MetadataCopyRequest<librbd::MockTestImageCtx> MockMetadataCopyRequest;
  typedef std::map<std::string, bufferlist> Metadata;

  librbd::ImageCtx *m_src_image_ctx;
  librbd::ImageCtx *m_dst_image_ctx;
  ThreadPool *m_thread_pool;
  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));

    librbd::ImageCtx::get_thread_pool_instance(m_src_image_ctx->cct,
                                               &m_thread_pool, &m_work_queue);
  }

  void expect_metadata_list(librbd::MockTestImageCtx &mock_image_ctx,
                            const Metadata& metadata, int r) {
    bufferlist out_bl;
    encode(metadata, out_bl);

    EXPECT_CALL(get_mock_io_ctx(mock_image_ctx.md_ctx),
                exec(mock_image_ctx.header_oid, _, StrEq("rbd"),
                     StrEq("metadata_list"), _, _, _))
                  .WillOnce(DoAll(WithArg<5>(CopyInBufferlist(out_bl)),
                                  Return(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;
  expect_metadata_list(mock_src_image_ctx, key_values_1, 0);
  expect_metadata_set(mock_dst_image_ctx, key_values_1, 0);
  expect_metadata_list(mock_src_image_ctx, 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;
  expect_metadata_list(mock_src_image_ctx, 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;
  expect_metadata_list(mock_src_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());
}

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;
  expect_metadata_list(mock_src_image_ctx, 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