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