]>
git.proxmox.com Git - ceph.git/blob - ceph/src/test/librados/service.cc
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"
8 #include <sys/resource.h>
12 #include <condition_variable>
16 #include "gtest/gtest.h"
17 #include "test/unit.cc"
20 using namespace librados
;
22 TEST(LibRadosService
, RegisterEarly
) {
24 ASSERT_EQ(0, rados_create(&cluster
, "admin"));
25 ASSERT_EQ(0, rados_conf_read_file(cluster
, NULL
));
26 ASSERT_EQ(0, rados_conf_parse_env(cluster
, NULL
));
28 string name
= string("pid") + stringify(getpid());
29 ASSERT_EQ(0, rados_service_register(cluster
, "laundry", name
.c_str(),
30 "foo\0bar\0this\0that\0"));
31 ASSERT_EQ(-EEXIST
, rados_service_register(cluster
, "laundry", name
.c_str(),
32 "foo\0bar\0this\0that\0"));
34 ASSERT_EQ(0, rados_connect(cluster
));
36 rados_shutdown(cluster
);
39 TEST(LibRadosService
, RegisterLate
) {
41 ASSERT_EQ(0, rados_create(&cluster
, "admin"));
42 ASSERT_EQ(0, rados_conf_read_file(cluster
, NULL
));
43 ASSERT_EQ(0, rados_conf_parse_env(cluster
, NULL
));
44 ASSERT_EQ(0, rados_connect(cluster
));
46 string name
= string("pid") + stringify(getpid());
47 ASSERT_EQ(0, rados_service_register(cluster
, "laundry", name
.c_str(),
48 "foo\0bar\0this\0that\0"));
49 ASSERT_EQ(-EEXIST
, rados_service_register(cluster
, "laundry", name
.c_str(),
50 "foo\0bar\0this\0that\0"));
51 rados_shutdown(cluster
);
54 static void status_format_func(const int i
, std::mutex
&lock
,
55 std::condition_variable
&cond
,
56 int &threads_started
, bool &stopped
)
59 char metadata_buf
[4096];
61 ASSERT_EQ(0, rados_create(&cluster
, "admin"));
62 ASSERT_EQ(0, rados_conf_read_file(cluster
, NULL
));
63 ASSERT_EQ(0, rados_conf_parse_env(cluster
, NULL
));
65 ASSERT_EQ(0, rados_connect(cluster
));
67 ASSERT_LT(0, sprintf(metadata_buf
, "%s%c%s%c",
68 "foo", '\0', "bar", '\0'));
70 ASSERT_LT(0, sprintf(metadata_buf
, "%s%c%s%c",
71 "daemon_type", '\0', "portal", '\0'));
73 ASSERT_LT(0, sprintf(metadata_buf
, "%s%c%s%c",
74 "daemon_prefix", '\0', "gateway", '\0'));
76 string prefix
= string("gw") + stringify(i
% 4);
77 string zone
= string("z") + stringify(i
% 3);
78 ASSERT_LT(0, sprintf(metadata_buf
, "%s%c%s%c%s%c%s%c%s%c%s%c%s%c%s%c",
79 "daemon_type", '\0', "portal", '\0',
80 "daemon_prefix", '\0', prefix
.c_str(), '\0',
81 "hostname", '\0', prefix
.c_str(), '\0',
82 "zone_id", '\0', zone
.c_str(), '\0'));
84 string name
= string("rbd/image") + stringify(i
);
85 ASSERT_EQ(0, rados_service_register(cluster
, "foo", name
.c_str(),
88 std::unique_lock
<std::mutex
> l(lock
);
91 cond
.wait(l
, [&stopped
] {
95 rados_shutdown(cluster
);
98 TEST(LibRadosService
, StatusFormat
) {
99 const int nthreads
= 16;
100 std::thread threads
[nthreads
];
102 std::condition_variable cond
;
103 bool stopped
= false;
104 int threads_started
= 0;
106 // no rlimits on Windows
108 // Need a bunch of fd's for this test
109 struct rlimit rold
, rnew
;
110 ASSERT_EQ(getrlimit(RLIMIT_NOFILE
, &rold
), 0);
112 rnew
.rlim_cur
= rnew
.rlim_max
;
113 ASSERT_EQ(setrlimit(RLIMIT_NOFILE
, &rnew
), 0);
116 for (int i
= 0; i
< nthreads
; ++i
)
117 threads
[i
] = std::thread(status_format_func
, i
, std::ref(lock
),
118 std::ref(cond
), std::ref(threads_started
),
122 std::unique_lock
<std::mutex
> l(lock
);
123 cond
.wait(l
, [&threads_started
] {
124 return nthreads
== threads_started
;
128 int retry
= 60; // mon thrashing may make this take a long time
132 ASSERT_EQ(0, rados_create(&cluster
, "admin"));
133 ASSERT_EQ(0, rados_conf_read_file(cluster
, NULL
));
134 ASSERT_EQ(0, rados_conf_parse_env(cluster
, NULL
));
136 ASSERT_EQ(0, rados_connect(cluster
));
138 cmd_f
.open_object_section("command");
139 cmd_f
.dump_string("prefix", "status");
140 cmd_f
.close_section();
141 std::ostringstream cmd_stream
;
142 cmd_f
.flush(cmd_stream
);
143 const std::string serialized_cmd
= cmd_stream
.str();
146 cmd
[0] = serialized_cmd
.c_str();
149 ASSERT_EQ(0, rados_mon_command(cluster
, (const char **)cmd
, 1, "", 0,
150 &outbuf
, &outlen
, NULL
, NULL
));
151 std::string
out(outbuf
, outlen
);
152 cout
<< out
<< std::endl
;
153 bool success
= false;
154 auto r1
= out
.find("16 portals active (1 hosts, 3 zones)");
155 if (std::string::npos
!= r1
) {
158 rados_buffer_free(outbuf
);
159 rados_shutdown(cluster
);
161 if (success
|| !retry
) {
165 // wait for 2 seconds to make sure all the
166 // services have been successfully updated
167 // to ceph mon, then retry it.
173 std::scoped_lock
<std::mutex
> l(lock
);
177 for (int i
= 0; i
< nthreads
; ++i
)
182 ASSERT_EQ(setrlimit(RLIMIT_NOFILE
, &rold
), 0);
186 TEST(LibRadosService
, Status
) {
188 ASSERT_EQ(0, rados_create(&cluster
, "admin"));
189 ASSERT_EQ(0, rados_conf_read_file(cluster
, NULL
));
190 ASSERT_EQ(0, rados_conf_parse_env(cluster
, NULL
));
192 ASSERT_EQ(-ENOTCONN
, rados_service_update_status(cluster
,
193 "testing\0testing\0"));
195 ASSERT_EQ(0, rados_connect(cluster
));
196 string name
= string("pid") + stringify(getpid());
197 ASSERT_EQ(0, rados_service_register(cluster
, "laundry", name
.c_str(),
198 "foo\0bar\0this\0that\0"));
200 for (int i
=0; i
<20; ++i
) {
202 snprintf(buffer
, sizeof(buffer
), "%s%c%s%c%s%c%d%c",
203 "testing", '\0', "testing", '\0',
204 "count", '\0', i
, '\0');
205 ASSERT_EQ(0, rados_service_update_status(cluster
, buffer
));
208 rados_shutdown(cluster
);