summaryrefslogtreecommitdiff
path: root/src/test/daemon_config.cc
blob: d643a6f49677dd4d34db8ea238b3fb17f5cdaab5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
/*
 * Ceph - scalable distributed file system
 *
 * Copyright (C) 2011 New Dream Network
 *
 * This is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License version 2.1, as published by the Free Software
 * Foundation.  See file COPYING.
 *
 */

#include "common/ceph_argparse.h"
#include "common/config.h"
#include "include/cephfs/libcephfs.h"
#include "include/rados/librados.h"
#include "test/unit.h"

#include <errno.h>
#include <sstream>
#include <string>
#include <string.h>

using std::string;

TEST(DaemonConfig, SimpleSet) {
  int ret;
  ret = g_ceph_context->_conf->set_val("num_client", "21");
  ASSERT_EQ(ret, 0);
  g_ceph_context->_conf->apply_changes(NULL);
  char buf[128];
  memset(buf, 0, sizeof(buf));
  char *tmp = buf;
  ret = g_ceph_context->_conf->get_val("num_client", &tmp, sizeof(buf));
  ASSERT_EQ(ret, 0);
  ASSERT_EQ(string("21"), string(buf));
}

TEST(DaemonConfig, Substitution) {
  int ret;
  ret = g_ceph_context->_conf->set_val("internal_safe_to_start_threads", "false");
  ret = g_ceph_context->_conf->set_val("host", "foo");
  ASSERT_EQ(ret, 0);
  ret = g_ceph_context->_conf->set_val("public_network", "bar$host.baz", false);
  ASSERT_EQ(ret, 0);
  g_ceph_context->_conf->apply_changes(NULL);
  char buf[128];
  memset(buf, 0, sizeof(buf));
  char *tmp = buf;
  ret = g_ceph_context->_conf->get_val("public_network", &tmp, sizeof(buf));
  ASSERT_EQ(ret, 0);
  ASSERT_EQ(string("barfoo.baz"), string(buf));
}

TEST(DaemonConfig, SubstitutionTrailing) {
  int ret;
  ret = g_ceph_context->_conf->set_val("internal_safe_to_start_threads", "false");
  ret = g_ceph_context->_conf->set_val("host", "foo");
  ASSERT_EQ(ret, 0);
  ret = g_ceph_context->_conf->set_val("public_network", "bar$host", false);
  ASSERT_EQ(ret, 0);
  g_ceph_context->_conf->apply_changes(NULL);
  char buf[128];
  memset(buf, 0, sizeof(buf));
  char *tmp = buf;
  ret = g_ceph_context->_conf->get_val("public_network", &tmp, sizeof(buf));
  ASSERT_EQ(ret, 0);
  ASSERT_EQ(string("barfoo"), string(buf));
}

TEST(DaemonConfig, SubstitutionBraces) {
  int ret;
  ret = g_ceph_context->_conf->set_val("internal_safe_to_start_threads", "false");
  ret = g_ceph_context->_conf->set_val("host", "foo");
  ASSERT_EQ(ret, 0);
  ret = g_ceph_context->_conf->set_val("public_network", "bar${host}baz", false);
  ASSERT_EQ(ret, 0);
  g_ceph_context->_conf->apply_changes(NULL);
  char buf[128];
  memset(buf, 0, sizeof(buf));
  char *tmp = buf;
  ret = g_ceph_context->_conf->get_val("public_network", &tmp, sizeof(buf));
  ASSERT_EQ(ret, 0);
  ASSERT_EQ(string("barfoobaz"), string(buf));
}
TEST(DaemonConfig, SubstitutionBracesTrailing) {
  int ret;
  ret = g_ceph_context->_conf->set_val("internal_safe_to_start_threads", "false");
  ret = g_ceph_context->_conf->set_val("host", "foo");
  ASSERT_EQ(ret, 0);
  ret = g_ceph_context->_conf->set_val("public_network", "bar${host}", false);
  ASSERT_EQ(ret, 0);
  g_ceph_context->_conf->apply_changes(NULL);
  char buf[128];
  memset(buf, 0, sizeof(buf));
  char *tmp = buf;
  ret = g_ceph_context->_conf->get_val("public_network", &tmp, sizeof(buf));
  ASSERT_EQ(ret, 0);
  ASSERT_EQ(string("barfoo"), string(buf));
}

TEST(DaemonConfig, SubstitutionLoop) {
  int ret;
  ret = g_ceph_context->_conf->set_val("internal_safe_to_start_threads", "false");
  ret = g_ceph_context->_conf->set_val("host", "foo$public_network", false);
  ASSERT_EQ(ret, 0);
  ret = g_ceph_context->_conf->set_val("public_network", "bar$host", false);
  ASSERT_EQ(ret, 0);
  g_ceph_context->_conf->apply_changes(NULL);
  char buf[128], buf2[128];
  memset(buf, 0, sizeof(buf));
  memset(buf2, 0, sizeof(buf2));
  char *tmp = buf;
  char *tmp2 = buf;
  ret = g_ceph_context->_conf->get_val("host", &tmp, sizeof(buf));
  ASSERT_EQ(ret, 0);
  ret = g_ceph_context->_conf->get_val("public_network", &tmp2, sizeof(buf));
  ASSERT_EQ(ret, 0);
  ASSERT_TRUE(strchr(buf, '$') || strchr(buf2, '$'));
}

TEST(DaemonConfig, ArgV) {
  ASSERT_EQ(0, g_ceph_context->_conf->set_val("internal_safe_to_start_threads",
				       "false"));

  int ret;
  const char *argv[] = { "foo", "--num-client", "22",
			 "--keyfile", "/tmp/my-keyfile", NULL };
  size_t argc = (sizeof(argv) / sizeof(argv[0])) - 1;
  vector<const char*> args;
  argv_to_vec(argc, argv, args);
  g_ceph_context->_conf->parse_argv(args);
  g_ceph_context->_conf->apply_changes(NULL);

  char buf[128];
  char *tmp = buf;
  memset(buf, 0, sizeof(buf));
  ret = g_ceph_context->_conf->get_val("keyfile", &tmp, sizeof(buf));
  ASSERT_EQ(ret, 0);
  ASSERT_EQ(string("/tmp/my-keyfile"), string(buf));

  memset(buf, 0, sizeof(buf));
  ret = g_ceph_context->_conf->get_val("num_client", &tmp, sizeof(buf));
  ASSERT_EQ(ret, 0);
  ASSERT_EQ(string("22"), string(buf));

  ASSERT_EQ(0, g_ceph_context->_conf->set_val("internal_safe_to_start_threads",
				       "true"));
}

TEST(DaemonConfig, InjectArgs) {
  int ret;
  std::string injection("--num-client 56 --max-open-files 42");
  ret = g_ceph_context->_conf->injectargs(injection, &cout);
  ASSERT_EQ(ret, 0);

  char buf[128];
  char *tmp = buf;
  memset(buf, 0, sizeof(buf));
  ret = g_ceph_context->_conf->get_val("max_open_files", &tmp, sizeof(buf));
  ASSERT_EQ(ret, 0);
  ASSERT_EQ(string("42"), string(buf));

  memset(buf, 0, sizeof(buf));
  ret = g_ceph_context->_conf->get_val("num_client", &tmp, sizeof(buf));
  ASSERT_EQ(ret, 0);
  ASSERT_EQ(string("56"), string(buf));

  injection = "--num-client 57";
  ret = g_ceph_context->_conf->injectargs(injection, &cout);
  ASSERT_EQ(ret, 0);
  ret = g_ceph_context->_conf->get_val("num_client", &tmp, sizeof(buf));
  ASSERT_EQ(ret, 0);
  ASSERT_EQ(string("57"), string(buf));
}

TEST(DaemonConfig, InjectArgsReject) {
  int ret;
  char buf[128];
  char *tmp = buf;
  char buf2[128];
  char *tmp2 = buf2;

  // We should complain about the garbage in the input
  std::string injection("--random-garbage-in-injectargs 26 --num-client 28");
  ret = g_ceph_context->_conf->injectargs(injection, &cout);
  ASSERT_EQ(ret, -EINVAL); 

  // But, debug should still be set...
  memset(buf, 0, sizeof(buf));
  ret = g_ceph_context->_conf->get_val("num_client", &tmp, sizeof(buf));
  ASSERT_EQ(ret, 0);
  ASSERT_EQ(string("28"), string(buf));

  // What's the current value of osd_data?
  memset(buf, 0, sizeof(buf));
  ret = g_ceph_context->_conf->get_val("osd_data", &tmp, sizeof(buf));
  ASSERT_EQ(ret, 0);

  // Injectargs shouldn't let us change this, since it is a string-valued
  // variable and there isn't an observer for it.
  std::string injection2("--osd_data /tmp/some-other-directory --num-client 4");
  ret = g_ceph_context->_conf->injectargs(injection2, &cout);
  ASSERT_EQ(ret, -ENOSYS); 

  // It should be unchanged.
  memset(buf2, 0, sizeof(buf2));
  ret = g_ceph_context->_conf->get_val("osd_data", &tmp2, sizeof(buf2));
  ASSERT_EQ(ret, 0);
  ASSERT_EQ(string(buf), string(buf2));
}

TEST(DaemonConfig, InjectArgsBooleans) {
  int ret;
  char buf[128];
  char *tmp = buf;

  // Change log_to_syslog
  std::string injection("--log_to_syslog --num-client 28");
  ret = g_ceph_context->_conf->injectargs(injection, &cout);
  ASSERT_EQ(ret, 0);

  // log_to_syslog should be set...
  memset(buf, 0, sizeof(buf));
  ret = g_ceph_context->_conf->get_val("log_to_syslog", &tmp, sizeof(buf));
  ASSERT_EQ(ret, 0);
  ASSERT_EQ(string("true"), string(buf));

  // Turn off log_to_syslog
  injection = "--log_to_syslog=false --num-client 28";
  ret = g_ceph_context->_conf->injectargs(injection, &cout);
  ASSERT_EQ(ret, 0);

  // log_to_syslog should be cleared...
  memset(buf, 0, sizeof(buf));
  ret = g_ceph_context->_conf->get_val("log_to_syslog", &tmp, sizeof(buf));
  ASSERT_EQ(ret, 0);
  ASSERT_EQ(string("false"), string(buf));

  // Turn on log_to_syslog
  injection = "--num-client 1 --log_to_syslog=true --max-open-files 40";
  ret = g_ceph_context->_conf->injectargs(injection, &cout);
  ASSERT_EQ(ret, 0);

  // log_to_syslog should be set...
  memset(buf, 0, sizeof(buf));
  ret = g_ceph_context->_conf->get_val("log_to_syslog", &tmp, sizeof(buf));
  ASSERT_EQ(ret, 0);
  ASSERT_EQ(string("true"), string(buf));

  // parse error
  injection = "--num-client 1 --log_to_syslog=falsey --max-open-files 42";
  ret = g_ceph_context->_conf->injectargs(injection, &cout);
  ASSERT_EQ(ret, -EINVAL);

  // log_to_syslog should still be set...
  memset(buf, 0, sizeof(buf));
  ret = g_ceph_context->_conf->get_val("log_to_syslog", &tmp, sizeof(buf));
  ASSERT_EQ(ret, 0);
  ASSERT_EQ(string("true"), string(buf));

  // debug-ms should still become 42...
  memset(buf, 0, sizeof(buf));
  ret = g_ceph_context->_conf->get_val("max_open_files", &tmp, sizeof(buf));
  ASSERT_EQ(ret, 0);
  ASSERT_EQ(string("42"), string(buf));
}

TEST(DaemonConfig, InjectArgsLogfile) {
  int ret;
  char tmpfile[PATH_MAX];
  const char *tmpdir = getenv("TMPDIR");
  if (!tmpdir)
    tmpdir = "/tmp";
  snprintf(tmpfile, sizeof(tmpfile), "%s/daemon_config_test.%d",
	   tmpdir, getpid());
  std::string injection("--log_file ");
  injection += tmpfile;
  // We're allowed to change log_file because there is an observer.
  ret = g_ceph_context->_conf->injectargs(injection, &cout);
  ASSERT_EQ(ret, 0);

  // It should have taken effect.
  char buf[128];
  char *tmp = buf;
  memset(buf, 0, sizeof(buf));
  ret = g_ceph_context->_conf->get_val("log_file", &tmp, sizeof(buf));
  ASSERT_EQ(ret, 0);
  ASSERT_EQ(string(buf), string(tmpfile));

  // The logfile should exist.
  ASSERT_EQ(access(tmpfile, R_OK), 0);

  // Let's turn off the logfile.
  ret = g_ceph_context->_conf->set_val("log_file", "");
  ASSERT_EQ(ret, 0);
  g_ceph_context->_conf->apply_changes(NULL);
  ret = g_ceph_context->_conf->get_val("log_file", &tmp, sizeof(buf));
  ASSERT_EQ(ret, 0);
  ASSERT_EQ(string(""), string(buf));

  // Clean up the garbage
  unlink(tmpfile);
}

TEST(DaemonConfig, ThreadSafety1) {
  int ret;
  // Verify that we can't change this, since internal_safe_to_start_threads has
  // been set.
  ret = g_ceph_context->_conf->set_val("osd_data", "");
  ASSERT_EQ(ret, -ENOSYS);

  ASSERT_EQ(0, g_ceph_context->_conf->set_val("internal_safe_to_start_threads",
				       "false"));

  // Ok, now we can change this. Since this is just a test, and there are no
  // OSD threads running, we know changing osd_data won't actually blow up the
  // world.
  ret = g_ceph_context->_conf->set_val("osd_data", "/tmp/crazydata");
  ASSERT_EQ(ret, 0);

  char buf[128];
  char *tmp = buf;
  memset(buf, 0, sizeof(buf));
  ret = g_ceph_context->_conf->get_val("osd_data", &tmp, sizeof(buf));
  ASSERT_EQ(ret, 0);
  ASSERT_EQ(string("/tmp/crazydata"), string(buf));

  ASSERT_EQ(0, g_ceph_context->_conf->set_val("internal_safe_to_start_threads",
				       "false"));
  ASSERT_EQ(ret, 0);
}