1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3 #include "test/librbd/test_fixture.h"
4 #include "test/librbd/test_support.h"
5 #include "include/stringify.h"
6 #include "librbd/ExclusiveLock.h"
7 #include "librbd/ImageState.h"
8 #include "librbd/ImageWatcher.h"
9 #include "librbd/Operations.h"
10 #include "librbd/io/ImageRequestWQ.h"
11 #include "cls/lock/cls_lock_client.h"
12 #include "cls/lock/cls_lock_types.h"
13 #include "cls/rbd/cls_rbd_types.h"
14 #include "librbd/internal.h"
15 #include "test/librados/test.h"
20 std::string TestFixture::_pool_name;
21 librados::Rados TestFixture::_rados;
22 rados_t TestFixture::_cluster;
23 uint64_t TestFixture::_image_number = 0;
24 std::string TestFixture::_data_pool;
26 TestFixture::TestFixture() : m_image_size(0) {
29 void TestFixture::SetUpTestCase() {
30 ASSERT_EQ("", connect_cluster(&_cluster));
31 _pool_name = get_temp_pool_name("test-librbd-");
32 ASSERT_EQ("", create_one_pool_pp(_pool_name, _rados));
35 ASSERT_EQ(0, create_image_data_pool(_rados, _data_pool, &created));
36 if (!_data_pool.empty()) {
37 printf("using image data pool: %s\n", _data_pool.c_str());
44 void TestFixture::TearDownTestCase() {
45 rados_shutdown(_cluster);
46 if (!_data_pool.empty()) {
47 ASSERT_EQ(0, _rados.pool_delete(_data_pool.c_str()));
50 ASSERT_EQ(0, destroy_one_pool_pp(_pool_name, _rados));
53 std::string TestFixture::get_temp_image_name() {
55 return "image" + stringify(_image_number);
58 void TestFixture::SetUp() {
59 ASSERT_EQ(0, _rados.ioctx_create(_pool_name.c_str(), m_ioctx));
61 m_image_name = get_temp_image_name();
62 m_image_size = 2 << 20;
63 ASSERT_EQ(0, create_image_pp(m_rbd, m_ioctx, m_image_name, m_image_size));
66 void TestFixture::TearDown() {
68 for (std::set<librbd::ImageCtx *>::iterator iter = m_ictxs.begin();
69 iter != m_ictxs.end(); ++iter) {
70 (*iter)->state->close();
76 int TestFixture::open_image(const std::string &image_name,
77 librbd::ImageCtx **ictx) {
78 *ictx = new librbd::ImageCtx(image_name.c_str(), "", NULL, m_ioctx, false);
79 m_ictxs.insert(*ictx);
81 return (*ictx)->state->open(false);
84 int TestFixture::snap_create(librbd::ImageCtx &ictx,
85 const std::string &snap_name) {
86 return ictx.operations->snap_create(cls::rbd::UserSnapshotNamespace(),
90 int TestFixture::snap_protect(librbd::ImageCtx &ictx,
91 const std::string &snap_name) {
92 return ictx.operations->snap_protect(cls::rbd::UserSnapshotNamespace(),
96 int TestFixture::flatten(librbd::ImageCtx &ictx,
97 librbd::ProgressContext &prog_ctx) {
98 return ictx.operations->flatten(prog_ctx);
101 void TestFixture::close_image(librbd::ImageCtx *ictx) {
104 ictx->state->close();
107 int TestFixture::lock_image(librbd::ImageCtx &ictx, ClsLockType lock_type,
108 const std::string &cookie) {
109 int r = rados::cls::lock::lock(&ictx.md_ctx, ictx.header_oid, RBD_LOCK_NAME,
110 lock_type, cookie, "internal", "", utime_t(),
113 m_lock_object = ictx.header_oid;
114 m_lock_cookie = cookie;
119 int TestFixture::unlock_image() {
121 if (!m_lock_cookie.empty()) {
122 r = rados::cls::lock::unlock(&m_ioctx, m_lock_object, RBD_LOCK_NAME,
129 int TestFixture::acquire_exclusive_lock(librbd::ImageCtx &ictx) {
130 int r = ictx.io_work_queue->write(0, 0, {}, 0);
135 RWLock::RLocker owner_locker(ictx.owner_lock);
136 assert(ictx.exclusive_lock != nullptr);
137 return ictx.exclusive_lock->is_lock_owner() ? 0 : -EINVAL;