]> git.proxmox.com Git - ceph.git/blame - ceph/src/test/rbd_mirror/test_fixture.cc
import 15.2.4
[ceph.git] / ceph / src / test / rbd_mirror / test_fixture.cc
CommitLineData
7c673cae
FG
1// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2// vim: ts=8 sw=2 smarttab
3
4#include "cls/rbd/cls_rbd_types.h"
5#include "test/rbd_mirror/test_fixture.h"
6#include "include/stringify.h"
7#include "include/rbd/librbd.hpp"
8#include "librbd/ImageCtx.h"
9#include "librbd/ImageState.h"
10#include "librbd/Operations.h"
11fdf7f2 11#include "test/librados/test_cxx.h"
7c673cae
FG
12#include "tools/rbd_mirror/Threads.h"
13
14namespace rbd {
15namespace mirror {
16
17std::string TestFixture::_local_pool_name;
18std::string TestFixture::_remote_pool_name;
19std::shared_ptr<librados::Rados> TestFixture::_rados;
20uint64_t TestFixture::_image_number = 0;
21std::string TestFixture::_data_pool;
22
23TestFixture::TestFixture() {
24}
25
26void TestFixture::SetUpTestCase() {
27 _rados = std::shared_ptr<librados::Rados>(new librados::Rados());
28 ASSERT_EQ("", connect_cluster_pp(*_rados.get()));
29 ASSERT_EQ(0, _rados->conf_set("rbd_cache", "false"));
30
31 _local_pool_name = get_temp_pool_name("test-rbd-mirror-");
32 ASSERT_EQ(0, _rados->pool_create(_local_pool_name.c_str()));
33
c07f9fc5
FG
34 librados::IoCtx local_ioctx;
35 ASSERT_EQ(0, _rados->ioctx_create(_local_pool_name.c_str(), local_ioctx));
36 local_ioctx.application_enable("rbd", true);
37
7c673cae
FG
38 _remote_pool_name = get_temp_pool_name("test-rbd-mirror-");
39 ASSERT_EQ(0, _rados->pool_create(_remote_pool_name.c_str()));
40
c07f9fc5
FG
41 librados::IoCtx remote_ioctx;
42 ASSERT_EQ(0, _rados->ioctx_create(_remote_pool_name.c_str(), remote_ioctx));
43 remote_ioctx.application_enable("rbd", true);
44
7c673cae
FG
45 ASSERT_EQ(0, create_image_data_pool(_data_pool));
46 if (!_data_pool.empty()) {
47 printf("using image data pool: %s\n", _data_pool.c_str());
48 }
49}
50
51void TestFixture::TearDownTestCase() {
52 if (!_data_pool.empty()) {
53 ASSERT_EQ(0, _rados->pool_delete(_data_pool.c_str()));
54 }
55
56 ASSERT_EQ(0, _rados->pool_delete(_remote_pool_name.c_str()));
57 ASSERT_EQ(0, _rados->pool_delete(_local_pool_name.c_str()));
58 _rados->shutdown();
59}
60
61void TestFixture::SetUp() {
62 static bool seeded = false;
63 if (!seeded) {
64 seeded = true;
65 int seed = getpid();
66 cout << "seed " << seed << std::endl;
67 srand(seed);
68 }
69
70 ASSERT_EQ(0, _rados->ioctx_create(_local_pool_name.c_str(), m_local_io_ctx));
71 ASSERT_EQ(0, _rados->ioctx_create(_remote_pool_name.c_str(), m_remote_io_ctx));
72 m_image_name = get_temp_image_name();
73
74 m_threads = new rbd::mirror::Threads<>(reinterpret_cast<CephContext*>(
75 m_local_io_ctx.cct()));
76}
77
78void TestFixture::TearDown() {
79 for (auto image_ctx : m_image_ctxs) {
80 image_ctx->state->close();
81 }
82
83 m_remote_io_ctx.close();
84 m_local_io_ctx.close();
85
86 delete m_threads;
87}
88
89int TestFixture::create_image(librbd::RBD &rbd, librados::IoCtx &ioctx,
90 const std::string &name, uint64_t size) {
91 int order = 18;
11fdf7f2
TL
92 return rbd.create2(ioctx, name.c_str(), size,
93 (RBD_FEATURES_ALL & ~RBD_FEATURES_IMPLICIT_ENABLE),
94 &order);
7c673cae
FG
95}
96
97int TestFixture::open_image(librados::IoCtx &io_ctx,
98 const std::string &image_name,
99 librbd::ImageCtx **image_ctx) {
11fdf7f2 100 *image_ctx = new librbd::ImageCtx(image_name.c_str(), "", nullptr, io_ctx,
7c673cae
FG
101 false);
102 m_image_ctxs.insert(*image_ctx);
11fdf7f2 103 return (*image_ctx)->state->open(0);
7c673cae
FG
104}
105
106int TestFixture::create_snap(librbd::ImageCtx *image_ctx, const char* snap_name,
107 librados::snap_t *snap_id) {
108 int r = image_ctx->operations->snap_create(cls::rbd::UserSnapshotNamespace(),
109 snap_name);
110 if (r < 0) {
111 return r;
112 }
113
114 r = image_ctx->state->refresh();
115 if (r < 0) {
116 return r;
117 }
118
119 if (image_ctx->snap_ids.count({cls::rbd::UserSnapshotNamespace(),
120 snap_name}) == 0) {
121 return -ENOENT;
122 }
123
124 if (snap_id != nullptr) {
125 *snap_id = image_ctx->snap_ids[{cls::rbd::UserSnapshotNamespace(),
126 snap_name}];
127 }
128 return 0;
129}
130
131std::string TestFixture::get_temp_image_name() {
132 ++_image_number;
133 return "image" + stringify(_image_number);
134}
135
136int TestFixture::create_image_data_pool(std::string &data_pool) {
137 std::string pool;
138 int r = _rados->conf_get("rbd_default_data_pool", pool);
139 if (r != 0) {
140 return r;
141 } else if (pool.empty()) {
142 return 0;
143 }
144
145 r = _rados->pool_create(pool.c_str());
d2e6a577
FG
146 if (r < 0) {
147 return r;
148 }
149
150 librados::IoCtx data_ioctx;
151 r = _rados->ioctx_create(pool.c_str(), data_ioctx);
152 if (r < 0) {
153 return r;
7c673cae
FG
154 }
155
d2e6a577
FG
156 data_ioctx.application_enable("rbd", true);
157 data_pool = pool;
158 return 0;
7c673cae
FG
159}
160
161} // namespace mirror
162} // namespace rbd