]> git.proxmox.com Git - ceph.git/blame - ceph/src/test/rbd_mirror/test_fixture.cc
bump version to 12.1.1-pve1 while rebasing patches
[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"
11#include "test/librados/test.h"
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
34 _remote_pool_name = get_temp_pool_name("test-rbd-mirror-");
35 ASSERT_EQ(0, _rados->pool_create(_remote_pool_name.c_str()));
36
37 ASSERT_EQ(0, create_image_data_pool(_data_pool));
38 if (!_data_pool.empty()) {
39 printf("using image data pool: %s\n", _data_pool.c_str());
40 }
41}
42
43void TestFixture::TearDownTestCase() {
44 if (!_data_pool.empty()) {
45 ASSERT_EQ(0, _rados->pool_delete(_data_pool.c_str()));
46 }
47
48 ASSERT_EQ(0, _rados->pool_delete(_remote_pool_name.c_str()));
49 ASSERT_EQ(0, _rados->pool_delete(_local_pool_name.c_str()));
50 _rados->shutdown();
51}
52
53void TestFixture::SetUp() {
54 static bool seeded = false;
55 if (!seeded) {
56 seeded = true;
57 int seed = getpid();
58 cout << "seed " << seed << std::endl;
59 srand(seed);
60 }
61
62 ASSERT_EQ(0, _rados->ioctx_create(_local_pool_name.c_str(), m_local_io_ctx));
63 ASSERT_EQ(0, _rados->ioctx_create(_remote_pool_name.c_str(), m_remote_io_ctx));
64 m_image_name = get_temp_image_name();
65
66 m_threads = new rbd::mirror::Threads<>(reinterpret_cast<CephContext*>(
67 m_local_io_ctx.cct()));
68}
69
70void TestFixture::TearDown() {
71 for (auto image_ctx : m_image_ctxs) {
72 image_ctx->state->close();
73 }
74
75 m_remote_io_ctx.close();
76 m_local_io_ctx.close();
77
78 delete m_threads;
79}
80
81int TestFixture::create_image(librbd::RBD &rbd, librados::IoCtx &ioctx,
82 const std::string &name, uint64_t size) {
83 int order = 18;
84 return rbd.create2(ioctx, name.c_str(), size, RBD_FEATURES_ALL, &order);
85}
86
87int TestFixture::open_image(librados::IoCtx &io_ctx,
88 const std::string &image_name,
89 librbd::ImageCtx **image_ctx) {
90 *image_ctx = new librbd::ImageCtx(image_name.c_str(), "", NULL, io_ctx,
91 false);
92 m_image_ctxs.insert(*image_ctx);
93 return (*image_ctx)->state->open(false);
94}
95
96int TestFixture::create_snap(librbd::ImageCtx *image_ctx, const char* snap_name,
97 librados::snap_t *snap_id) {
98 int r = image_ctx->operations->snap_create(cls::rbd::UserSnapshotNamespace(),
99 snap_name);
100 if (r < 0) {
101 return r;
102 }
103
104 r = image_ctx->state->refresh();
105 if (r < 0) {
106 return r;
107 }
108
109 if (image_ctx->snap_ids.count({cls::rbd::UserSnapshotNamespace(),
110 snap_name}) == 0) {
111 return -ENOENT;
112 }
113
114 if (snap_id != nullptr) {
115 *snap_id = image_ctx->snap_ids[{cls::rbd::UserSnapshotNamespace(),
116 snap_name}];
117 }
118 return 0;
119}
120
121std::string TestFixture::get_temp_image_name() {
122 ++_image_number;
123 return "image" + stringify(_image_number);
124}
125
126int TestFixture::create_image_data_pool(std::string &data_pool) {
127 std::string pool;
128 int r = _rados->conf_get("rbd_default_data_pool", pool);
129 if (r != 0) {
130 return r;
131 } else if (pool.empty()) {
132 return 0;
133 }
134
135 r = _rados->pool_create(pool.c_str());
136 if (r == 0) {
137 data_pool = pool;
138 return 0;
139 }
140
141 return r;
142}
143
144} // namespace mirror
145} // namespace rbd