]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
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" | |
11fdf7f2 | 16 | #include "test/librados/test_cxx.h" |
7c673cae FG |
17 | #include <iostream> |
18 | #include <sstream> | |
19 | #include <stdlib.h> | |
20 | ||
21 | std::string TestFixture::_pool_name; | |
22 | librados::Rados TestFixture::_rados; | |
23 | rados_t TestFixture::_cluster; | |
24 | uint64_t TestFixture::_image_number = 0; | |
25 | std::string TestFixture::_data_pool; | |
26 | ||
27 | TestFixture::TestFixture() : m_image_size(0) { | |
28 | } | |
29 | ||
30 | void TestFixture::SetUpTestCase() { | |
31 | ASSERT_EQ("", connect_cluster(&_cluster)); | |
32 | _pool_name = get_temp_pool_name("test-librbd-"); | |
33 | ASSERT_EQ("", create_one_pool_pp(_pool_name, _rados)); | |
34 | ||
35 | bool created = false; | |
36 | ASSERT_EQ(0, create_image_data_pool(_rados, _data_pool, &created)); | |
37 | if (!_data_pool.empty()) { | |
38 | printf("using image data pool: %s\n", _data_pool.c_str()); | |
39 | if (!created) { | |
40 | _data_pool.clear(); | |
41 | } | |
42 | } | |
43 | } | |
44 | ||
45 | void TestFixture::TearDownTestCase() { | |
46 | rados_shutdown(_cluster); | |
47 | if (!_data_pool.empty()) { | |
48 | ASSERT_EQ(0, _rados.pool_delete(_data_pool.c_str())); | |
49 | } | |
50 | ||
51 | ASSERT_EQ(0, destroy_one_pool_pp(_pool_name, _rados)); | |
52 | } | |
53 | ||
54 | std::string TestFixture::get_temp_image_name() { | |
55 | ++_image_number; | |
56 | return "image" + stringify(_image_number); | |
57 | } | |
58 | ||
59 | void TestFixture::SetUp() { | |
60 | ASSERT_EQ(0, _rados.ioctx_create(_pool_name.c_str(), m_ioctx)); | |
11fdf7f2 | 61 | m_cct = reinterpret_cast<CephContext*>(m_ioctx.cct()); |
7c673cae FG |
62 | |
63 | m_image_name = get_temp_image_name(); | |
64 | m_image_size = 2 << 20; | |
65 | ASSERT_EQ(0, create_image_pp(m_rbd, m_ioctx, m_image_name, m_image_size)); | |
66 | } | |
67 | ||
68 | void TestFixture::TearDown() { | |
69 | unlock_image(); | |
70 | for (std::set<librbd::ImageCtx *>::iterator iter = m_ictxs.begin(); | |
71 | iter != m_ictxs.end(); ++iter) { | |
72 | (*iter)->state->close(); | |
73 | } | |
74 | ||
75 | m_ioctx.close(); | |
76 | } | |
77 | ||
78 | int TestFixture::open_image(const std::string &image_name, | |
79 | librbd::ImageCtx **ictx) { | |
11fdf7f2 | 80 | *ictx = new librbd::ImageCtx(image_name.c_str(), "", nullptr, m_ioctx, false); |
7c673cae FG |
81 | m_ictxs.insert(*ictx); |
82 | ||
11fdf7f2 | 83 | return (*ictx)->state->open(0); |
7c673cae FG |
84 | } |
85 | ||
86 | int TestFixture::snap_create(librbd::ImageCtx &ictx, | |
87 | const std::string &snap_name) { | |
88 | return ictx.operations->snap_create(cls::rbd::UserSnapshotNamespace(), | |
89 | snap_name.c_str()); | |
90 | } | |
91 | ||
92 | int TestFixture::snap_protect(librbd::ImageCtx &ictx, | |
93 | const std::string &snap_name) { | |
94 | return ictx.operations->snap_protect(cls::rbd::UserSnapshotNamespace(), | |
95 | snap_name.c_str()); | |
96 | } | |
97 | ||
98 | int TestFixture::flatten(librbd::ImageCtx &ictx, | |
99 | librbd::ProgressContext &prog_ctx) { | |
100 | return ictx.operations->flatten(prog_ctx); | |
101 | } | |
102 | ||
b32b8144 FG |
103 | int TestFixture::resize(librbd::ImageCtx *ictx, uint64_t size){ |
104 | librbd::NoOpProgressContext prog_ctx; | |
105 | return ictx->operations->resize(size, true, prog_ctx); | |
106 | } | |
107 | ||
7c673cae FG |
108 | void TestFixture::close_image(librbd::ImageCtx *ictx) { |
109 | m_ictxs.erase(ictx); | |
110 | ||
111 | ictx->state->close(); | |
112 | } | |
113 | ||
114 | int TestFixture::lock_image(librbd::ImageCtx &ictx, ClsLockType lock_type, | |
115 | const std::string &cookie) { | |
116 | int r = rados::cls::lock::lock(&ictx.md_ctx, ictx.header_oid, RBD_LOCK_NAME, | |
117 | lock_type, cookie, "internal", "", utime_t(), | |
118 | 0); | |
119 | if (r == 0) { | |
120 | m_lock_object = ictx.header_oid; | |
121 | m_lock_cookie = cookie; | |
122 | } | |
123 | return r; | |
124 | } | |
125 | ||
126 | int TestFixture::unlock_image() { | |
127 | int r = 0; | |
128 | if (!m_lock_cookie.empty()) { | |
129 | r = rados::cls::lock::unlock(&m_ioctx, m_lock_object, RBD_LOCK_NAME, | |
130 | m_lock_cookie); | |
131 | m_lock_cookie = ""; | |
132 | } | |
133 | return r; | |
134 | } | |
135 | ||
136 | int TestFixture::acquire_exclusive_lock(librbd::ImageCtx &ictx) { | |
137 | int r = ictx.io_work_queue->write(0, 0, {}, 0); | |
138 | if (r != 0) { | |
139 | return r; | |
140 | } | |
141 | ||
9f95a23c | 142 | std::shared_lock owner_locker{ictx.owner_lock}; |
11fdf7f2 | 143 | ceph_assert(ictx.exclusive_lock != nullptr); |
7c673cae FG |
144 | return ictx.exclusive_lock->is_lock_owner() ? 0 : -EINVAL; |
145 | } |