]>
git.proxmox.com Git - ceph.git/blob - 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
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"
17 std::string
TestFixture::_local_pool_name
;
18 std::string
TestFixture::_remote_pool_name
;
19 std::shared_ptr
<librados::Rados
> TestFixture::_rados
;
20 uint64_t TestFixture::_image_number
= 0;
21 std::string
TestFixture::_data_pool
;
23 TestFixture::TestFixture() {
26 void 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"));
31 _local_pool_name
= get_temp_pool_name("test-rbd-mirror-");
32 ASSERT_EQ(0, _rados
->pool_create(_local_pool_name
.c_str()));
34 _remote_pool_name
= get_temp_pool_name("test-rbd-mirror-");
35 ASSERT_EQ(0, _rados
->pool_create(_remote_pool_name
.c_str()));
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());
43 void TestFixture::TearDownTestCase() {
44 if (!_data_pool
.empty()) {
45 ASSERT_EQ(0, _rados
->pool_delete(_data_pool
.c_str()));
48 ASSERT_EQ(0, _rados
->pool_delete(_remote_pool_name
.c_str()));
49 ASSERT_EQ(0, _rados
->pool_delete(_local_pool_name
.c_str()));
53 void TestFixture::SetUp() {
54 static bool seeded
= false;
58 cout
<< "seed " << seed
<< std::endl
;
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();
66 m_threads
= new rbd::mirror::Threads
<>(reinterpret_cast<CephContext
*>(
67 m_local_io_ctx
.cct()));
70 void TestFixture::TearDown() {
71 for (auto image_ctx
: m_image_ctxs
) {
72 image_ctx
->state
->close();
75 m_remote_io_ctx
.close();
76 m_local_io_ctx
.close();
81 int TestFixture::create_image(librbd::RBD
&rbd
, librados::IoCtx
&ioctx
,
82 const std::string
&name
, uint64_t size
) {
84 return rbd
.create2(ioctx
, name
.c_str(), size
, RBD_FEATURES_ALL
, &order
);
87 int 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
,
92 m_image_ctxs
.insert(*image_ctx
);
93 return (*image_ctx
)->state
->open(false);
96 int 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(),
104 r
= image_ctx
->state
->refresh();
109 if (image_ctx
->snap_ids
.count({cls::rbd::UserSnapshotNamespace(),
114 if (snap_id
!= nullptr) {
115 *snap_id
= image_ctx
->snap_ids
[{cls::rbd::UserSnapshotNamespace(),
121 std::string
TestFixture::get_temp_image_name() {
123 return "image" + stringify(_image_number
);
126 int TestFixture::create_image_data_pool(std::string
&data_pool
) {
128 int r
= _rados
->conf_get("rbd_default_data_pool", pool
);
131 } else if (pool
.empty()) {
135 r
= _rados
->pool_create(pool
.c_str());
144 } // namespace mirror