]>
Commit | Line | Data |
---|---|---|
224ce89b WB |
1 | #include "include/rados/librados.h" |
2 | #include "include/rados/librados.hpp" | |
11fdf7f2 TL |
3 | #include "include/stringify.h" |
4 | #include "common/config_proxy.h" | |
224ce89b WB |
5 | #include "test/librados/test.h" |
6 | #include "test/librados/TestCase.h" | |
f67539c2 | 7 | #include <sys/resource.h> |
224ce89b | 8 | |
f67539c2 TL |
9 | #include <mutex> |
10 | #include <condition_variable> | |
224ce89b | 11 | #include <algorithm> |
11fdf7f2 | 12 | #include <thread> |
224ce89b WB |
13 | #include <errno.h> |
14 | #include "gtest/gtest.h" | |
15 | #include "test/unit.cc" | |
16 | ||
17 | using namespace librados; | |
18 | ||
c07f9fc5 FG |
19 | TEST(LibRadosService, RegisterEarly) { |
20 | rados_t cluster; | |
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)); | |
24 | ||
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")); | |
30 | ||
31 | ASSERT_EQ(0, rados_connect(cluster)); | |
32 | sleep(5); | |
33 | rados_shutdown(cluster); | |
34 | } | |
35 | ||
36 | TEST(LibRadosService, RegisterLate) { | |
37 | rados_t cluster; | |
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)); | |
42 | ||
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); | |
49 | } | |
50 | ||
f67539c2 TL |
51 | static void status_format_func(const int i, std::mutex &lock, |
52 | std::condition_variable &cond, | |
53 | int &threads_started, bool &stopped) | |
54 | { | |
55 | rados_t cluster; | |
56 | char *metadata_buf = NULL; | |
57 | ||
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)); | |
61 | ||
62 | ASSERT_EQ(0, rados_connect(cluster)); | |
63 | if (i == 0) { | |
64 | ASSERT_NE(-1, asprintf(&metadata_buf, "%s%c%s%c", | |
65 | "foo", '\0', "bar", '\0')); | |
66 | } else if (i == 1) { | |
67 | ASSERT_NE(-1, asprintf(&metadata_buf, "%s%c%s%c", | |
68 | "daemon_type", '\0', "portal", '\0')); | |
69 | } else if (i == 2) { | |
70 | ASSERT_NE(-1, asprintf(&metadata_buf, "%s%c%s%c", | |
71 | "daemon_prefix", '\0', "gateway", '\0')); | |
72 | } else { | |
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' | |
80 | )); | |
81 | } | |
82 | string name = string("rbd/image") + stringify(i); | |
83 | ASSERT_EQ(0, rados_service_register(cluster, "foo", name.c_str(), | |
84 | metadata_buf)); | |
85 | ||
86 | std::unique_lock<std::mutex> l(lock); | |
87 | threads_started++; | |
88 | cond.notify_all(); | |
89 | cond.wait(l, [&stopped] { | |
90 | return stopped; | |
91 | }); | |
92 | ||
93 | rados_shutdown(cluster); | |
94 | } | |
95 | ||
96 | TEST(LibRadosService, StatusFormat) { | |
97 | const int nthreads = 16; | |
98 | std::thread threads[nthreads]; | |
99 | std::mutex lock; | |
100 | std::condition_variable cond; | |
101 | bool stopped = false; | |
102 | int threads_started = 0; | |
103 | ||
104 | // Need a bunch of fd's for this test | |
105 | struct rlimit rold, rnew; | |
106 | ASSERT_EQ(getrlimit(RLIMIT_NOFILE, &rold), 0); | |
107 | rnew = rold; | |
108 | rnew.rlim_cur = rnew.rlim_max; | |
109 | ASSERT_EQ(setrlimit(RLIMIT_NOFILE, &rnew), 0); | |
110 | ||
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), | |
114 | std::ref(stopped)); | |
115 | ||
116 | { | |
117 | std::unique_lock<std::mutex> l(lock); | |
118 | cond.wait(l, [nthreads, &threads_started] { | |
119 | return nthreads == threads_started; | |
120 | }); | |
121 | } | |
122 | ||
123 | int retry = 5; | |
124 | while (retry) { | |
125 | rados_t cluster; | |
126 | ||
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)); | |
130 | ||
131 | ASSERT_EQ(0, rados_connect(cluster)); | |
132 | JSONFormatter cmd_f; | |
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(); | |
139 | const char *cmd[2]; | |
140 | cmd[1] = NULL; | |
141 | cmd[0] = serialized_cmd.c_str(); | |
142 | char *outbuf = NULL; | |
143 | size_t outlen = 0; | |
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) { | |
151 | success = true; | |
152 | } | |
153 | rados_buffer_free(outbuf); | |
154 | rados_shutdown(cluster); | |
155 | ||
156 | if (success || !retry) { | |
157 | break; | |
158 | } | |
159 | ||
160 | // wait for 2 seconds to make sure all the | |
161 | // services have been successfully updated | |
162 | // to ceph mon, then retry it. | |
163 | sleep(2); | |
164 | retry--; | |
165 | } | |
166 | ASSERT_NE(0, retry); | |
167 | ||
168 | { | |
169 | std::scoped_lock<std::mutex> l(lock); | |
170 | stopped = true; | |
171 | cond.notify_all(); | |
172 | } | |
173 | for (int i = 0; i < nthreads; ++i) | |
174 | threads[i].join(); | |
175 | ||
176 | ASSERT_EQ(setrlimit(RLIMIT_NOFILE, &rold), 0); | |
177 | } | |
178 | ||
c07f9fc5 FG |
179 | TEST(LibRadosService, Status) { |
180 | rados_t cluster; | |
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)); | |
184 | ||
185 | ASSERT_EQ(-ENOTCONN, rados_service_update_status(cluster, | |
186 | "testing\0testing\0")); | |
187 | ||
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")); | |
192 | ||
193 | for (int i=0; i<20; ++i) { | |
194 | char buffer[1024]; | |
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)); | |
199 | sleep(1); | |
200 | } | |
201 | rados_shutdown(cluster); | |
202 | } |