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