summaryrefslogtreecommitdiffstats
path: root/src/test/rbd_mirror/test_fixture.cc
blob: 7abf0e3e2a5c26b4e19cd3bf104863b435301dbb (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
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab

#include "cls/rbd/cls_rbd_types.h"
#include "test/rbd_mirror/test_fixture.h"
#include "include/stringify.h"
#include "include/rbd/librbd.hpp"
#include "librbd/ImageCtx.h"
#include "librbd/ImageState.h"
#include "librbd/Operations.h"
#include "librbd/internal.h"
#include "test/librados/test_cxx.h"
#include "tools/rbd_mirror/Threads.h"

namespace rbd {
namespace mirror {

std::string TestFixture::_local_pool_name;
std::string TestFixture::_remote_pool_name;
std::shared_ptr<librados::Rados> TestFixture::_rados;
uint64_t TestFixture::_image_number = 0;
std::string TestFixture::_data_pool;

TestFixture::TestFixture() {
}

void TestFixture::SetUpTestCase() {
  _rados = std::shared_ptr<librados::Rados>(new librados::Rados());
  ASSERT_EQ("", connect_cluster_pp(*_rados.get()));
  ASSERT_EQ(0, _rados->conf_set("rbd_cache", "false"));

  _local_pool_name = get_temp_pool_name("test-rbd-mirror-");
  ASSERT_EQ(0, _rados->pool_create(_local_pool_name.c_str()));

  librados::IoCtx local_ioctx;
  ASSERT_EQ(0, _rados->ioctx_create(_local_pool_name.c_str(), local_ioctx));
  local_ioctx.application_enable("rbd", true);

  _remote_pool_name = get_temp_pool_name("test-rbd-mirror-");
  ASSERT_EQ(0, _rados->pool_create(_remote_pool_name.c_str()));

  librados::IoCtx remote_ioctx;
  ASSERT_EQ(0, _rados->ioctx_create(_remote_pool_name.c_str(), remote_ioctx));
  remote_ioctx.application_enable("rbd", true);

  ASSERT_EQ(0, create_image_data_pool(_data_pool));
  if (!_data_pool.empty()) {
    printf("using image data pool: %s\n", _data_pool.c_str());
  }
}

void TestFixture::TearDownTestCase() {
  if (!_data_pool.empty()) {
    ASSERT_EQ(0, _rados->pool_delete(_data_pool.c_str()));
  }

  ASSERT_EQ(0, _rados->pool_delete(_remote_pool_name.c_str()));
  ASSERT_EQ(0, _rados->pool_delete(_local_pool_name.c_str()));
  _rados->shutdown();
}

void TestFixture::SetUp() {
  static bool seeded = false;
  if (!seeded) {
    seeded = true;
    int seed = getpid();
    cout << "seed " << seed << std::endl;
    srand(seed);
  }

  ASSERT_EQ(0, _rados->ioctx_create(_local_pool_name.c_str(), m_local_io_ctx));
  ASSERT_EQ(0, _rados->ioctx_create(_remote_pool_name.c_str(), m_remote_io_ctx));
  m_image_name = get_temp_image_name();

  m_threads = new rbd::mirror::Threads<>(_rados);
}

void TestFixture::TearDown() {
  for (auto image_ctx : m_image_ctxs) {
    image_ctx->state->close();
  }

  m_remote_io_ctx.close();
  m_local_io_ctx.close();

  delete m_threads;
}

int TestFixture::create_image(librbd::RBD &rbd, librados::IoCtx &ioctx,
                              const std::string &name, uint64_t size) {
  int order = 18;
  return rbd.create2(ioctx, name.c_str(), size, RBD_FEATURES_ALL, &order);
}

int TestFixture::open_image(librados::IoCtx &io_ctx,
                            const std::string &image_name,
                            librbd::ImageCtx **image_ctx) {
  *image_ctx = new librbd::ImageCtx(image_name.c_str(), "", nullptr, io_ctx,
                                    false);
  m_image_ctxs.insert(*image_ctx);
  return (*image_ctx)->state->open(0);
}

int TestFixture::create_snap(librbd::ImageCtx *image_ctx, const char* snap_name,
                             librados::snap_t *snap_id) {
  librbd::NoOpProgressContext prog_ctx;
  int r = image_ctx->operations->snap_create(cls::rbd::UserSnapshotNamespace(),
					     snap_name, 0, prog_ctx);
  if (r < 0) {
    return r;
  }

  r = image_ctx->state->refresh();
  if (r < 0) {
    return r;
  }

  if (image_ctx->snap_ids.count({cls::rbd::UserSnapshotNamespace(),
				 snap_name}) == 0) {
    return -ENOENT;
  }

  if (snap_id != nullptr) {
    *snap_id = image_ctx->snap_ids[{cls::rbd::UserSnapshotNamespace(),
				    snap_name}];
  }
  return 0;
}

std::string TestFixture::get_temp_image_name() {
  ++_image_number;
  return "image" + stringify(_image_number);
}

int TestFixture::create_image_data_pool(std::string &data_pool) {
  std::string pool;
  int r = _rados->conf_get("rbd_default_data_pool", pool);
  if (r != 0) {
    return r;
  } else if (pool.empty()) {
    return 0;
  }

  r = _rados->pool_create(pool.c_str());
  if (r < 0) {
    return r;
  }

  librados::IoCtx data_ioctx;
  r = _rados->ioctx_create(pool.c_str(), data_ioctx);
  if (r < 0) {
    return r;
  }

  data_ioctx.application_enable("rbd", true);
  data_pool = pool;
  return 0;
}

} // namespace mirror
} // namespace rbd