]>
git.proxmox.com Git - ceph.git/blob - ceph/src/test/librados/service.cc
bc8e26efaf4d89aee3204af00728d0a9f71d7dea
1 #include "include/rados/librados.h"
2 #include "include/rados/librados.hpp"
3 #include "include/stringify.h"
4 #include "common/config_proxy.h"
5 #include "test/librados/test.h"
6 #include "test/librados/TestCase.h"
7 #include <sys/resource.h>
10 #include <condition_variable>
14 #include "gtest/gtest.h"
15 #include "test/unit.cc"
17 using namespace librados
;
19 TEST(LibRadosService
, RegisterEarly
) {
21 ASSERT_EQ(0, rados_create(&cluster
, "admin"));
22 ASSERT_EQ(0, rados_conf_read_file(cluster
, NULL
));
23 ASSERT_EQ(0, rados_conf_parse_env(cluster
, NULL
));
25 string name
= string("pid") + stringify(getpid());
26 ASSERT_EQ(0, rados_service_register(cluster
, "laundry", name
.c_str(),
27 "foo\0bar\0this\0that\0"));
28 ASSERT_EQ(-EEXIST
, rados_service_register(cluster
, "laundry", name
.c_str(),
29 "foo\0bar\0this\0that\0"));
31 ASSERT_EQ(0, rados_connect(cluster
));
33 rados_shutdown(cluster
);
36 TEST(LibRadosService
, RegisterLate
) {
38 ASSERT_EQ(0, rados_create(&cluster
, "admin"));
39 ASSERT_EQ(0, rados_conf_read_file(cluster
, NULL
));
40 ASSERT_EQ(0, rados_conf_parse_env(cluster
, NULL
));
41 ASSERT_EQ(0, rados_connect(cluster
));
43 string name
= string("pid") + stringify(getpid());
44 ASSERT_EQ(0, rados_service_register(cluster
, "laundry", name
.c_str(),
45 "foo\0bar\0this\0that\0"));
46 ASSERT_EQ(-EEXIST
, rados_service_register(cluster
, "laundry", name
.c_str(),
47 "foo\0bar\0this\0that\0"));
48 rados_shutdown(cluster
);
51 static void status_format_func(const int i
, std::mutex
&lock
,
52 std::condition_variable
&cond
,
53 int &threads_started
, bool &stopped
)
56 char *metadata_buf
= NULL
;
58 ASSERT_EQ(0, rados_create(&cluster
, "admin"));
59 ASSERT_EQ(0, rados_conf_read_file(cluster
, NULL
));
60 ASSERT_EQ(0, rados_conf_parse_env(cluster
, NULL
));
62 ASSERT_EQ(0, rados_connect(cluster
));
64 ASSERT_NE(-1, asprintf(&metadata_buf
, "%s%c%s%c",
65 "foo", '\0', "bar", '\0'));
67 ASSERT_NE(-1, asprintf(&metadata_buf
, "%s%c%s%c",
68 "daemon_type", '\0', "portal", '\0'));
70 ASSERT_NE(-1, asprintf(&metadata_buf
, "%s%c%s%c",
71 "daemon_prefix", '\0', "gateway", '\0'));
73 string prefix
= string("gw") + stringify(i
% 4);
74 string zone
= string("z") + stringify(i
% 3);
75 ASSERT_NE(-1, asprintf(&metadata_buf
, "%s%c%s%c%s%c%s%c%s%c%s%c%s%c%s%c",
76 "daemon_type", '\0', "portal", '\0',
77 "daemon_prefix", '\0', prefix
.c_str(), '\0',
78 "hostname", '\0', prefix
.c_str(), '\0',
79 "zone_id", '\0', zone
.c_str(), '\0'
82 string name
= string("rbd/image") + stringify(i
);
83 ASSERT_EQ(0, rados_service_register(cluster
, "foo", name
.c_str(),
86 std::unique_lock
<std::mutex
> l(lock
);
89 cond
.wait(l
, [&stopped
] {
93 rados_shutdown(cluster
);
96 TEST(LibRadosService
, StatusFormat
) {
97 const int nthreads
= 16;
98 std::thread threads
[nthreads
];
100 std::condition_variable cond
;
101 bool stopped
= false;
102 int threads_started
= 0;
104 // Need a bunch of fd's for this test
105 struct rlimit rold
, rnew
;
106 ASSERT_EQ(getrlimit(RLIMIT_NOFILE
, &rold
), 0);
108 rnew
.rlim_cur
= rnew
.rlim_max
;
109 ASSERT_EQ(setrlimit(RLIMIT_NOFILE
, &rnew
), 0);
111 for (int i
= 0; i
< nthreads
; ++i
)
112 threads
[i
] = std::thread(status_format_func
, i
, std::ref(lock
),
113 std::ref(cond
), std::ref(threads_started
),
117 std::unique_lock
<std::mutex
> l(lock
);
118 cond
.wait(l
, [nthreads
, &threads_started
] {
119 return nthreads
== threads_started
;
127 ASSERT_EQ(0, rados_create(&cluster
, "admin"));
128 ASSERT_EQ(0, rados_conf_read_file(cluster
, NULL
));
129 ASSERT_EQ(0, rados_conf_parse_env(cluster
, NULL
));
131 ASSERT_EQ(0, rados_connect(cluster
));
133 cmd_f
.open_object_section("command");
134 cmd_f
.dump_string("prefix", "status");
135 cmd_f
.close_section();
136 std::ostringstream cmd_stream
;
137 cmd_f
.flush(cmd_stream
);
138 const std::string serialized_cmd
= cmd_stream
.str();
141 cmd
[0] = serialized_cmd
.c_str();
144 ASSERT_EQ(0, rados_mon_command(cluster
, (const char **)cmd
, 1, "", 0,
145 &outbuf
, &outlen
, NULL
, NULL
));
146 std::string
out(outbuf
, outlen
);
147 cout
<< out
<< std::endl
;
148 bool success
= false;
149 auto r1
= out
.find("16 portals active (1 hosts, 3 zones)");
150 if (std::string::npos
!= r1
) {
153 rados_buffer_free(outbuf
);
154 rados_shutdown(cluster
);
156 if (success
|| !retry
) {
160 // wait for 2 seconds to make sure all the
161 // services have been successfully updated
162 // to ceph mon, then retry it.
169 std::scoped_lock
<std::mutex
> l(lock
);
173 for (int i
= 0; i
< nthreads
; ++i
)
176 ASSERT_EQ(setrlimit(RLIMIT_NOFILE
, &rold
), 0);
179 TEST(LibRadosService
, Status
) {
181 ASSERT_EQ(0, rados_create(&cluster
, "admin"));
182 ASSERT_EQ(0, rados_conf_read_file(cluster
, NULL
));
183 ASSERT_EQ(0, rados_conf_parse_env(cluster
, NULL
));
185 ASSERT_EQ(-ENOTCONN
, rados_service_update_status(cluster
,
186 "testing\0testing\0"));
188 ASSERT_EQ(0, rados_connect(cluster
));
189 string name
= string("pid") + stringify(getpid());
190 ASSERT_EQ(0, rados_service_register(cluster
, "laundry", name
.c_str(),
191 "foo\0bar\0this\0that\0"));
193 for (int i
=0; i
<20; ++i
) {
195 snprintf(buffer
, sizeof(buffer
), "%s%c%s%c%s%c%d%c",
196 "testing", '\0', "testing", '\0',
197 "count", '\0', i
, '\0');
198 ASSERT_EQ(0, rados_service_update_status(cluster
, buffer
));
201 rados_shutdown(cluster
);