Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / test / librbd / 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 #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"
16 #include <iostream>
17 #include <sstream>
18 #include <stdlib.h>
19
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;
25
26 TestFixture::TestFixture() : m_image_size(0) {
27 }
28
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));
33
34   bool created = false;
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());
38     if (!created) {
39       _data_pool.clear();
40     }
41   }
42 }
43
44 void TestFixture::TearDownTestCase() {
45   rados_shutdown(_cluster);
46   if (!_data_pool.empty()) {
47     ASSERT_EQ(0, _rados.pool_delete(_data_pool.c_str()));
48   }
49
50   ASSERT_EQ(0, destroy_one_pool_pp(_pool_name, _rados));
51 }
52
53 std::string TestFixture::get_temp_image_name() {
54   ++_image_number;
55   return "image" + stringify(_image_number);
56 }
57
58 void TestFixture::SetUp() {
59   ASSERT_EQ(0, _rados.ioctx_create(_pool_name.c_str(), m_ioctx));
60
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));
64 }
65
66 void TestFixture::TearDown() {
67   unlock_image();
68   for (std::set<librbd::ImageCtx *>::iterator iter = m_ictxs.begin();
69        iter != m_ictxs.end(); ++iter) {
70     (*iter)->state->close();
71   }
72
73   m_ioctx.close();
74 }
75
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);
80
81   return (*ictx)->state->open(false);
82 }
83
84 int TestFixture::snap_create(librbd::ImageCtx &ictx,
85                              const std::string &snap_name) {
86   return ictx.operations->snap_create(cls::rbd::UserSnapshotNamespace(),
87                                       snap_name.c_str());
88 }
89
90 int TestFixture::snap_protect(librbd::ImageCtx &ictx,
91                               const std::string &snap_name) {
92   return ictx.operations->snap_protect(cls::rbd::UserSnapshotNamespace(),
93                                        snap_name.c_str());
94 }
95
96 int TestFixture::flatten(librbd::ImageCtx &ictx,
97                          librbd::ProgressContext &prog_ctx) {
98   return ictx.operations->flatten(prog_ctx);
99 }
100
101 void TestFixture::close_image(librbd::ImageCtx *ictx) {
102   m_ictxs.erase(ictx);
103
104   ictx->state->close();
105 }
106
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(),
111                            0);
112   if (r == 0) {
113     m_lock_object = ictx.header_oid;
114     m_lock_cookie = cookie;
115   }
116   return r;
117 }
118
119 int TestFixture::unlock_image() {
120   int r = 0;
121   if (!m_lock_cookie.empty()) {
122     r = rados::cls::lock::unlock(&m_ioctx, m_lock_object, RBD_LOCK_NAME,
123                            m_lock_cookie);
124     m_lock_cookie = "";
125   }
126   return r;
127 }
128
129 int TestFixture::acquire_exclusive_lock(librbd::ImageCtx &ictx) {
130   int r = ictx.io_work_queue->write(0, 0, {}, 0);
131   if (r != 0) {
132     return r;
133   }
134
135   RWLock::RLocker owner_locker(ictx.owner_lock);
136   assert(ictx.exclusive_lock != nullptr);
137   return ictx.exclusive_lock->is_lock_owner() ? 0 : -EINVAL;
138 }