]> git.proxmox.com Git - ceph.git/blame - ceph/src/test/librados/test.cc
bump version to 18.2.2-pve1
[ceph.git] / ceph / src / test / librados / test.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
4#include "include/rados/librados.h"
5#include "include/rados/librados.hpp"
6#include "test/librados/test.h"
7
8#include "include/stringify.h"
7c673cae
FG
9#include "common/ceph_context.h"
10#include "common/config.h"
11
12#include <errno.h>
13#include <sstream>
14#include <stdlib.h>
15#include <string>
16#include <time.h>
17#include <unistd.h>
18#include <iostream>
19#include "gtest/gtest.h"
20
7c673cae
FG
21std::string create_one_pool(
22 const std::string &pool_name, rados_t *cluster, uint32_t pg_num)
23{
24 std::string err_str = connect_cluster(cluster);
25 if (err_str.length())
26 return err_str;
27
28 int ret = rados_pool_create(*cluster, pool_name.c_str());
29 if (ret) {
30 rados_shutdown(*cluster);
31 std::ostringstream oss;
32 oss << "create_one_pool(" << pool_name << ") failed with error " << ret;
33 return oss.str();
34 }
35
c07f9fc5
FG
36 rados_ioctx_t ioctx;
37 ret = rados_ioctx_create(*cluster, pool_name.c_str(), &ioctx);
38 if (ret < 0) {
39 rados_shutdown(*cluster);
40 std::ostringstream oss;
41 oss << "rados_ioctx_create(" << pool_name << ") failed with error " << ret;
42 return oss.str();
43 }
44
45 rados_application_enable(ioctx, "rados", 1);
46 rados_ioctx_destroy(ioctx);
7c673cae
FG
47 return "";
48}
49
50int destroy_ec_profile(rados_t *cluster,
51 const std::string& pool_name,
52 std::ostream &oss)
53{
54 char buf[1000];
55 snprintf(buf, sizeof(buf),
56 "{\"prefix\": \"osd erasure-code-profile rm\", \"name\": \"testprofile-%s\"}",
57 pool_name.c_str());
58 char *cmd[2];
59 cmd[0] = buf;
60 cmd[1] = NULL;
61 int ret = rados_mon_command(*cluster, (const char **)cmd, 1, "", 0, NULL,
62 0, NULL, 0);
63 if (ret)
64 oss << "rados_mon_command: erasure-code-profile rm testprofile-"
65 << pool_name << " failed with error " << ret;
66 return ret;
67}
68
20effc67
TL
69int destroy_rule(rados_t *cluster,
70 const std::string &rule,
71 std::ostream &oss)
7c673cae
FG
72{
73 char *cmd[2];
74 std::string tmp = ("{\"prefix\": \"osd crush rule rm\", \"name\":\"" +
20effc67 75 rule + "\"}");
7c673cae
FG
76 cmd[0] = (char*)tmp.c_str();
77 cmd[1] = NULL;
78 int ret = rados_mon_command(*cluster, (const char **)cmd, 1, "", 0, NULL, 0, NULL, 0);
79 if (ret)
20effc67 80 oss << "rados_mon_command: osd crush rule rm " + rule + " failed with error " << ret;
7c673cae
FG
81 return ret;
82}
83
20effc67
TL
84int destroy_ec_profile_and_rule(rados_t *cluster,
85 const std::string &rule,
86 std::ostream &oss)
7c673cae
FG
87{
88 int ret;
20effc67 89 ret = destroy_ec_profile(cluster, rule, oss);
7c673cae
FG
90 if (ret)
91 return ret;
20effc67 92 return destroy_rule(cluster, rule, oss);
7c673cae
FG
93}
94
7c673cae
FG
95
96std::string create_one_ec_pool(const std::string &pool_name, rados_t *cluster)
97{
98 std::string err = connect_cluster(cluster);
99 if (err.length())
100 return err;
101
102 std::ostringstream oss;
20effc67 103 int ret = destroy_ec_profile_and_rule(cluster, pool_name, oss);
7c673cae
FG
104 if (ret) {
105 rados_shutdown(*cluster);
106 return oss.str();
107 }
108
109 char *cmd[2];
110 cmd[1] = NULL;
111
224ce89b 112 std::string profile_create = "{\"prefix\": \"osd erasure-code-profile set\", \"name\": \"testprofile-" + pool_name + "\", \"profile\": [ \"k=2\", \"m=1\", \"crush-failure-domain=osd\"]}";
7c673cae
FG
113 cmd[0] = (char *)profile_create.c_str();
114 ret = rados_mon_command(*cluster, (const char **)cmd, 1, "", 0, NULL, 0, NULL, 0);
115 if (ret) {
116 rados_shutdown(*cluster);
117 oss << "rados_mon_command erasure-code-profile set name:testprofile-" << pool_name << " failed with error " << ret;
118 return oss.str();
119 }
120
121 std::string cmdstr = "{\"prefix\": \"osd pool create\", \"pool\": \"" +
122 pool_name + "\", \"pool_type\":\"erasure\", \"pg_num\":8, \"pgp_num\":8, \"erasure_code_profile\":\"testprofile-" + pool_name + "\"}";
123 cmd[0] = (char *)cmdstr.c_str();
124 ret = rados_mon_command(*cluster, (const char **)cmd, 1, "", 0, NULL, 0, NULL, 0);
125 if (ret) {
126 destroy_ec_profile(cluster, pool_name, oss);
127 rados_shutdown(*cluster);
128 oss << "rados_mon_command osd pool create failed with error " << ret;
129 return oss.str();
130 }
131
132 rados_wait_for_latest_osdmap(*cluster);
133 return "";
134}
135
7c673cae
FG
136std::string connect_cluster(rados_t *cluster)
137{
138 char *id = getenv("CEPH_CLIENT_ID");
139 if (id) std::cerr << "Client id is: " << id << std::endl;
140
141 int ret;
142 ret = rados_create(cluster, NULL);
143 if (ret) {
144 std::ostringstream oss;
145 oss << "rados_create failed with error " << ret;
146 return oss.str();
147 }
148 ret = rados_conf_read_file(*cluster, NULL);
149 if (ret) {
150 rados_shutdown(*cluster);
151 std::ostringstream oss;
152 oss << "rados_conf_read_file failed with error " << ret;
153 return oss.str();
154 }
155 rados_conf_parse_env(*cluster, NULL);
156 ret = rados_connect(*cluster);
157 if (ret) {
158 rados_shutdown(*cluster);
159 std::ostringstream oss;
160 oss << "rados_connect failed with error " << ret;
161 return oss.str();
162 }
163 return "";
164}
165
7c673cae
FG
166int destroy_one_pool(const std::string &pool_name, rados_t *cluster)
167{
168 int ret = rados_pool_delete(*cluster, pool_name.c_str());
169 if (ret) {
170 rados_shutdown(*cluster);
171 return ret;
172 }
173 rados_shutdown(*cluster);
174 return 0;
175}
176
177int destroy_one_ec_pool(const std::string &pool_name, rados_t *cluster)
178{
179 int ret = rados_pool_delete(*cluster, pool_name.c_str());
180 if (ret) {
181 rados_shutdown(*cluster);
182 return ret;
183 }
184
185 CephContext *cct = static_cast<CephContext*>(rados_cct(*cluster));
186 if (!cct->_conf->mon_fake_pool_delete) { // hope this is in [global]
187 std::ostringstream oss;
20effc67 188 ret = destroy_ec_profile_and_rule(cluster, pool_name, oss);
7c673cae
FG
189 if (ret) {
190 rados_shutdown(*cluster);
191 return ret;
192 }
193 }
194
195 rados_wait_for_latest_osdmap(*cluster);
196 rados_shutdown(*cluster);
197 return ret;
198}