]> git.proxmox.com Git - ceph.git/blob - ceph/src/test/mon/test-mon-msg.cc
a372ff602794050ab3d12186f09ca6aa0364e966
[ceph.git] / ceph / src / test / mon / test-mon-msg.cc
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3 /*
4 * Ceph - scalable distributed file system
5 *
6 * Copyright (C) 2014 Red Hat
7 *
8 * This is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License version 2.1, as published by the Free Software
11 * Foundation. See file COPYING.
12 */
13 #include <stdio.h>
14 #include <string.h>
15 #include <iostream>
16 #include <sstream>
17 #include <time.h>
18 #include <stdlib.h>
19 #include <map>
20
21 #include "global/global_init.h"
22 #include "global/global_context.h"
23 #include "common/ceph_argparse.h"
24 #include "common/dout.h"
25 #include "common/debug.h"
26 #include "common/Cond.h"
27 #include "common/Mutex.h"
28 #include "common/Timer.h"
29 #include "common/errno.h"
30 #include "mon/MonClient.h"
31 #include "msg/Dispatcher.h"
32 #include "include/err.h"
33 #include <boost/scoped_ptr.hpp>
34
35 #include "gtest/gtest.h"
36
37 #include "common/config.h"
38 #include "include/assert.h"
39
40 #include "messages/MMonProbe.h"
41 #include "messages/MRoute.h"
42 #include "messages/MGenericMessage.h"
43 #include "messages/MMonJoin.h"
44
45 #define dout_context g_ceph_context
46 #define dout_subsys ceph_subsys_
47 #undef dout_prefix
48 #define dout_prefix *_dout << "test-mon-msg "
49
50 class MonClientHelper : public Dispatcher
51 {
52 protected:
53 CephContext *cct;
54 Messenger *msg;
55 MonClient monc;
56
57 Mutex lock;
58
59 set<int> wanted;
60
61 public:
62
63 explicit MonClientHelper(CephContext *cct_)
64 : Dispatcher(cct_),
65 cct(cct_),
66 monc(cct_),
67 lock("mon-msg-test::lock")
68 { }
69
70
71 int post_init() {
72 dout(1) << __func__ << dendl;
73 if (!msg)
74 return -EINVAL;
75 msg->add_dispatcher_tail(this);
76 return 0;
77 }
78
79 int init_messenger() {
80 dout(1) << __func__ << dendl;
81
82 std::string public_msgr_type = cct->_conf->ms_public_type.empty() ? cct->_conf->get_val<std::string>("ms_type") : cct->_conf->ms_public_type;
83 msg = Messenger::create(cct, public_msgr_type, entity_name_t::CLIENT(-1),
84 "test-mon-msg", 0, 0);
85 assert(msg != NULL);
86 msg->set_default_policy(Messenger::Policy::lossy_client(0));
87 dout(0) << __func__ << " starting messenger at "
88 << msg->get_myaddr() << dendl;
89 msg->start();
90 return 0;
91 }
92
93 int init_monc() {
94 dout(1) << __func__ << dendl;
95 assert(msg != NULL);
96 int err = monc.build_initial_monmap();
97 if (err < 0) {
98 derr << __func__ << " error building monmap: "
99 << cpp_strerror(err) << dendl;
100 return err;
101 }
102
103 monc.set_messenger(msg);
104 msg->add_dispatcher_head(&monc);
105
106 monc.set_want_keys(CEPH_ENTITY_TYPE_MON);
107 err = monc.init();
108 if (err < 0) {
109 derr << __func__ << " monc init failed: "
110 << cpp_strerror(err) << dendl;
111 goto fail;
112 }
113
114 err = monc.authenticate();
115 if (err < 0) {
116 derr << __func__ << " monc auth failed: "
117 << cpp_strerror(err) << dendl;
118 goto fail_monc;
119 }
120 monc.wait_auth_rotating(30.0);
121 monc.renew_subs();
122 dout(0) << __func__ << " finished" << dendl;
123 return 0;
124
125 fail_monc:
126 derr << __func__ << " failing monc" << dendl;
127 monc.shutdown();
128 fail:
129 return err;
130 }
131
132 void shutdown_messenger() {
133 dout(0) << __func__ << dendl;
134 msg->shutdown();
135 msg->wait();
136 }
137
138 void shutdown_monc() {
139 dout(0) << __func__ << dendl;
140 monc.shutdown();
141 }
142
143 void shutdown() {
144 dout(0) << __func__ << dendl;
145 shutdown_monc();
146 shutdown_messenger();
147 }
148
149 MonMap *get_monmap() {
150 return &monc.monmap;
151 }
152
153 int init() {
154 int err = init_messenger();
155 if (err < 0)
156 goto fail;
157 err = init_monc();
158 if (err < 0)
159 goto fail_msgr;
160 err = post_init();
161 if (err < 0)
162 goto fail_monc;
163 return 0;
164 fail_monc:
165 shutdown_monc();
166 fail_msgr:
167 shutdown_messenger();
168 fail:
169 return err;
170 }
171
172 virtual void handle_wanted(Message *m) { }
173
174 bool handle_message(Message *m) {
175 dout(1) << __func__ << " " << *m << dendl;
176 if (!is_wanted(m)) {
177 dout(10) << __func__ << " not wanted" << dendl;
178 return false;
179 }
180 handle_wanted(m);
181 m->put();
182
183 return true;
184 }
185
186 bool ms_dispatch(Message *m) override {
187 return handle_message(m);
188 }
189 void ms_handle_connect(Connection *con) override { }
190 void ms_handle_remote_reset(Connection *con) override { }
191 bool ms_handle_reset(Connection *con) override { return false; }
192 bool ms_handle_refused(Connection *con) override { return false; }
193
194 bool is_wanted(Message *m) {
195 dout(20) << __func__ << " " << *m << " type " << m->get_type() << dendl;
196 return (wanted.find(m->get_type()) != wanted.end());
197 }
198
199 void add_wanted(int t) {
200 dout(20) << __func__ << " type " << t << dendl;
201 wanted.insert(t);
202 }
203
204 void rm_wanted(int t) {
205 dout(20) << __func__ << " type " << t << dendl;
206 wanted.erase(t);
207 }
208
209 void send_message(Message *m) {
210 dout(15) << __func__ << " " << *m << dendl;
211 monc.send_mon_message(m);
212 }
213
214 void wait() { msg->wait(); }
215 };
216
217 class MonMsgTest : public MonClientHelper,
218 public ::testing::Test
219 {
220 protected:
221 int reply_type;
222 Message *reply_msg = nullptr;
223 Mutex lock;
224 Cond cond;
225
226 MonMsgTest() :
227 MonClientHelper(g_ceph_context),
228 lock("lock") { }
229
230 public:
231 void SetUp() override {
232 reply_type = -1;
233 if (reply_msg) {
234 reply_msg->put();
235 reply_msg = nullptr;
236 }
237 ASSERT_EQ(init(), 0);
238 }
239
240 void TearDown() override {
241 shutdown();
242 if (reply_msg) {
243 reply_msg->put();
244 reply_msg = nullptr;
245 }
246 }
247
248 void handle_wanted(Message *m) override {
249 lock.Lock();
250 // caller will put() after they call us, so hold on to a ref
251 m->get();
252 reply_msg = m;
253 cond.Signal();
254 lock.Unlock();
255 }
256
257 Message *send_wait_reply(Message *m, int t, double timeout=30.0) {
258 lock.Lock();
259 reply_type = t;
260 add_wanted(t);
261 send_message(m);
262
263 int err = 0;
264 if (timeout > 0) {
265 utime_t cond_timeout;
266 cond_timeout.set_from_double(timeout);
267 utime_t s = ceph_clock_now();
268 err = cond.WaitInterval(lock, cond_timeout);
269 utime_t e = ceph_clock_now();
270 dout(20) << __func__ << " took " << (e-s) << " seconds" << dendl;
271 } else {
272 err = cond.Wait(lock);
273 }
274 rm_wanted(t);
275 lock.Unlock();
276 if (err > 0) {
277 dout(20) << __func__ << " error: " << cpp_strerror(err) << dendl;
278 return (Message*)((long)-err);
279 }
280
281 if (!reply_msg)
282 dout(20) << __func__ << " reply_msg is nullptr" << dendl;
283 else
284 dout(20) << __func__ << " reply_msg " << *reply_msg << dendl;
285 return reply_msg;
286 }
287 };
288
289 TEST_F(MonMsgTest, MMonProbeTest)
290 {
291 Message *m = new MMonProbe(get_monmap()->fsid,
292 MMonProbe::OP_PROBE, "b", false);
293 Message *r = send_wait_reply(m, MSG_MON_PROBE);
294 ASSERT_NE(IS_ERR(r), 0);
295 ASSERT_EQ(PTR_ERR(r), -ETIMEDOUT);
296 }
297
298 TEST_F(MonMsgTest, MRouteTest)
299 {
300 Message *payload = new MGenericMessage(CEPH_MSG_SHUTDOWN);
301 MRoute *m = new MRoute;
302 m->msg = payload;
303 m->dest = msg->get_myinst();
304 Message *r = send_wait_reply(m, CEPH_MSG_SHUTDOWN);
305 // we want an error
306 ASSERT_NE(IS_ERR(r), 0);
307 ASSERT_EQ(PTR_ERR(r), -ETIMEDOUT);
308 }
309
310 /* MMonScrub and MMonSync have other safeguards in place that prevent
311 * us from actually receiving a reply even if the message is handled
312 * by the monitor due to lack of cap checking.
313 */
314 TEST_F(MonMsgTest, MMonJoin)
315 {
316 Message *m = new MMonJoin(get_monmap()->fsid, string("client"),
317 msg->get_myaddr());
318 send_wait_reply(m, MSG_MON_PAXOS, 10.0);
319
320 int r = monc.get_monmap();
321 ASSERT_EQ(r, 0);
322 ASSERT_FALSE(monc.monmap.contains("client"));
323 }
324
325 int main(int argc, char *argv[])
326 {
327 vector<const char*> args;
328 argv_to_vec(argc, (const char **)argv, args);
329
330 auto cct = global_init(nullptr, args,
331 CEPH_ENTITY_TYPE_CLIENT, CODE_ENVIRONMENT_UTILITY,
332 0);
333 common_init_finish(g_ceph_context);
334 g_ceph_context->_conf->apply_changes(NULL);
335 ::testing::InitGoogleTest(&argc, argv);
336
337 return RUN_ALL_TESTS();
338 }
339