]>
git.proxmox.com Git - ceph.git/blob - ceph/src/test/librados/test.cc
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*
2 // vim: ts=8 sw=2 smarttab
4 #include "include/rados/librados.h"
5 #include "include/rados/librados.hpp"
6 #include "test/librados/test.h"
8 #include "include/stringify.h"
9 #include "common/ceph_context.h"
10 #include "common/config.h"
19 #include "gtest/gtest.h"
21 std::string
create_one_pool(
22 const std::string
&pool_name
, rados_t
*cluster
, uint32_t pg_num
)
24 std::string err_str
= connect_cluster(cluster
);
28 int ret
= rados_pool_create(*cluster
, pool_name
.c_str());
30 rados_shutdown(*cluster
);
31 std::ostringstream oss
;
32 oss
<< "create_one_pool(" << pool_name
<< ") failed with error " << ret
;
37 ret
= rados_ioctx_create(*cluster
, pool_name
.c_str(), &ioctx
);
39 rados_shutdown(*cluster
);
40 std::ostringstream oss
;
41 oss
<< "rados_ioctx_create(" << pool_name
<< ") failed with error " << ret
;
45 rados_application_enable(ioctx
, "rados", 1);
46 rados_ioctx_destroy(ioctx
);
50 int destroy_ec_profile(rados_t
*cluster
,
51 const std::string
& pool_name
,
55 snprintf(buf
, sizeof(buf
),
56 "{\"prefix\": \"osd erasure-code-profile rm\", \"name\": \"testprofile-%s\"}",
61 int ret
= rados_mon_command(*cluster
, (const char **)cmd
, 1, "", 0, NULL
,
64 oss
<< "rados_mon_command: erasure-code-profile rm testprofile-"
65 << pool_name
<< " failed with error " << ret
;
69 int destroy_rule(rados_t
*cluster
,
70 const std::string
&rule
,
74 std::string tmp
= ("{\"prefix\": \"osd crush rule rm\", \"name\":\"" +
76 cmd
[0] = (char*)tmp
.c_str();
78 int ret
= rados_mon_command(*cluster
, (const char **)cmd
, 1, "", 0, NULL
, 0, NULL
, 0);
80 oss
<< "rados_mon_command: osd crush rule rm " + rule
+ " failed with error " << ret
;
84 int destroy_ec_profile_and_rule(rados_t
*cluster
,
85 const std::string
&rule
,
89 ret
= destroy_ec_profile(cluster
, rule
, oss
);
92 return destroy_rule(cluster
, rule
, oss
);
96 std::string
create_one_ec_pool(const std::string
&pool_name
, rados_t
*cluster
)
98 std::string err
= connect_cluster(cluster
);
102 std::ostringstream oss
;
103 int ret
= destroy_ec_profile_and_rule(cluster
, pool_name
, oss
);
105 rados_shutdown(*cluster
);
112 std::string profile_create
= "{\"prefix\": \"osd erasure-code-profile set\", \"name\": \"testprofile-" + pool_name
+ "\", \"profile\": [ \"k=2\", \"m=1\", \"crush-failure-domain=osd\"]}";
113 cmd
[0] = (char *)profile_create
.c_str();
114 ret
= rados_mon_command(*cluster
, (const char **)cmd
, 1, "", 0, NULL
, 0, NULL
, 0);
116 rados_shutdown(*cluster
);
117 oss
<< "rados_mon_command erasure-code-profile set name:testprofile-" << pool_name
<< " failed with error " << ret
;
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);
126 destroy_ec_profile(cluster
, pool_name
, oss
);
127 rados_shutdown(*cluster
);
128 oss
<< "rados_mon_command osd pool create failed with error " << ret
;
132 rados_wait_for_latest_osdmap(*cluster
);
136 std::string
connect_cluster(rados_t
*cluster
)
138 char *id
= getenv("CEPH_CLIENT_ID");
139 if (id
) std::cerr
<< "Client id is: " << id
<< std::endl
;
142 ret
= rados_create(cluster
, NULL
);
144 std::ostringstream oss
;
145 oss
<< "rados_create failed with error " << ret
;
148 ret
= rados_conf_read_file(*cluster
, NULL
);
150 rados_shutdown(*cluster
);
151 std::ostringstream oss
;
152 oss
<< "rados_conf_read_file failed with error " << ret
;
155 rados_conf_parse_env(*cluster
, NULL
);
156 ret
= rados_connect(*cluster
);
158 rados_shutdown(*cluster
);
159 std::ostringstream oss
;
160 oss
<< "rados_connect failed with error " << ret
;
166 int destroy_one_pool(const std::string
&pool_name
, rados_t
*cluster
)
168 int ret
= rados_pool_delete(*cluster
, pool_name
.c_str());
170 rados_shutdown(*cluster
);
173 rados_shutdown(*cluster
);
177 int destroy_one_ec_pool(const std::string
&pool_name
, rados_t
*cluster
)
179 int ret
= rados_pool_delete(*cluster
, pool_name
.c_str());
181 rados_shutdown(*cluster
);
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
;
188 ret
= destroy_ec_profile_and_rule(cluster
, pool_name
, oss
);
190 rados_shutdown(*cluster
);
195 rados_wait_for_latest_osdmap(*cluster
);
196 rados_shutdown(*cluster
);