]> git.proxmox.com Git - ceph.git/blame - ceph/src/test/librbd/test_support.cc
update sources to ceph Nautilus 14.2.1
[ceph.git] / ceph / src / test / librbd / test_support.cc
CommitLineData
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_support.h"
4#include "include/rbd_types.h"
11fdf7f2 5#include "gtest/gtest.h"
7c673cae
FG
6#include <sstream>
7
8bool get_features(uint64_t *features) {
9 const char *c = getenv("RBD_FEATURES");
10 if (c == NULL) {
11 return false;
12 }
13
14 std::stringstream ss(c);
15 if (!(ss >> *features)) {
16 return false;
17 }
18 return true;
19}
20
21bool is_feature_enabled(uint64_t feature) {
22 uint64_t features;
23 return (get_features(&features) && (features & feature) == feature);
24}
25
26int create_image_full_pp(librbd::RBD &rbd, librados::IoCtx &ioctx,
27 const std::string &name, uint64_t size,
28 uint64_t features, bool old_format, int *order)
29{
30 if (old_format) {
31 librados::Rados rados(ioctx);
32 int r = rados.conf_set("rbd_default_format", "1");
33 if (r < 0) {
34 return r;
35 }
36 return rbd.create(ioctx, name.c_str(), size, order);
37 } else if ((features & RBD_FEATURE_STRIPINGV2) != 0) {
38 uint64_t stripe_unit = IMAGE_STRIPE_UNIT;
39 if (*order) {
40 // use a conservative stripe_unit for non default order
41 stripe_unit = (1ull << (*order-1));
42 }
43
44 printf("creating image with stripe unit: %" PRIu64 ", stripe count: %" PRIu64 "\n",
45 stripe_unit, IMAGE_STRIPE_COUNT);
46 return rbd.create3(ioctx, name.c_str(), size, features, order, stripe_unit,
47 IMAGE_STRIPE_COUNT);
48 } else {
49 return rbd.create2(ioctx, name.c_str(), size, features, order);
50 }
51}
52
53int create_image_pp(librbd::RBD &rbd, librados::IoCtx &ioctx,
54 const std::string &name, uint64_t size) {
55 int order = 0;
56 uint64_t features = 0;
57 if (!get_features(&features)) {
58 // ensure old-format tests actually use the old format
59 librados::Rados rados(ioctx);
60 int r = rados.conf_set("rbd_default_format", "1");
61 if (r < 0) {
62 return r;
63 }
64 return rbd.create(ioctx, name.c_str(), size, &order);
65 } else {
66 return rbd.create2(ioctx, name.c_str(), size, features, &order);
67 }
68}
69
70int clone_image_pp(librbd::RBD &rbd, librbd::Image &p_image, librados::IoCtx &p_ioctx,
71 const char *p_name, const char *p_snap_name, librados::IoCtx &c_ioctx,
72 const char *c_name, uint64_t features)
73{
74 uint64_t stripe_unit = p_image.get_stripe_unit();
75 uint64_t stripe_count = p_image.get_stripe_count();
76
77 librbd::image_info_t p_info;
78 int r = p_image.stat(p_info, sizeof(p_info));
79 if (r < 0) {
80 return r;
81 }
82
83 int c_order = p_info.order;
84 return rbd.clone2(p_ioctx, p_name, p_snap_name, c_ioctx, c_name,
85 features, &c_order, stripe_unit, stripe_count);
86}
87
88int get_image_id(librbd::Image &image, std::string *image_id)
89{
90 int r = image.get_id(image_id);
91 if (r < 0) {
92 return r;
93 }
94 return 0;
95}
96
97int create_image_data_pool(librados::Rados &rados, std::string &data_pool, bool *created) {
98 std::string pool;
99 int r = rados.conf_get("rbd_default_data_pool", pool);
100 if (r != 0) {
101 return r;
102 } else if (pool.empty()) {
103 return 0;
104 }
105
106 r = rados.pool_create(pool.c_str());
107 if ((r == 0) || (r == -EEXIST)) {
108 data_pool = pool;
109 *created = (r == 0);
110 return 0;
111 }
112
c07f9fc5
FG
113 librados::IoCtx ioctx;
114 r = rados.ioctx_create(pool.c_str(), ioctx);
115 if (r < 0) {
116 return r;
117 }
c07f9fc5 118
11fdf7f2
TL
119 librbd::RBD rbd;
120 return rbd.pool_init(ioctx, true);
7c673cae 121}
11fdf7f2
TL
122
123bool is_librados_test_stub(librados::Rados &rados) {
124 std::string fsid;
125 EXPECT_EQ(0, rados.cluster_fsid(&fsid));
126 return fsid == "00000000-1111-2222-3333-444444444444";
127}
128