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
|
// -*- 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("debug", "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("debug", &tmp, sizeof(buf));
ASSERT_EQ(ret, 0);
ASSERT_EQ(string("21"), string(buf));
}
TEST(DaemonConfig, ArgV) {
ASSERT_EQ(0, g_ceph_context->_conf->set_val("internal_safe_to_start_threads",
"false"));
int ret;
const char *argv[] = { "foo", "--debug", "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("debug", &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::ostringstream chat;
std::string injection("--debug 56 --debug-mds 42");
ret = g_ceph_context->_conf->injectargs(injection, &chat);
ASSERT_EQ(ret, 0);
char buf[128];
char *tmp = buf;
memset(buf, 0, sizeof(buf));
ret = g_ceph_context->_conf->get_val("debug_mds", &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("debug", &tmp, sizeof(buf));
ASSERT_EQ(ret, 0);
ASSERT_EQ(string("56"), string(buf));
injection = "--debug 57";
ret = g_ceph_context->_conf->injectargs(injection, &chat);
ASSERT_EQ(ret, 0);
ret = g_ceph_context->_conf->get_val("debug", &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::ostringstream chat;
std::string injection("--random-garbage-in-injectargs 26 --debug 28");
ret = g_ceph_context->_conf->injectargs(injection, &chat);
ASSERT_EQ(ret, -EINVAL);
// But, debug should still be set...
memset(buf, 0, sizeof(buf));
ret = g_ceph_context->_conf->get_val("debug", &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 --debug 4");
ret = g_ceph_context->_conf->injectargs(injection2, &chat);
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::ostringstream chat;
std::string injection("--log_to_syslog --debug 28");
ret = g_ceph_context->_conf->injectargs(injection, &chat);
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
std::ostringstream chat2;
injection = "--log_to_syslog=false --debug 28";
ret = g_ceph_context->_conf->injectargs(injection, &chat2);
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
std::ostringstream chat3;
injection = "--debug 1 --log_to_syslog=true --debug-ms 40";
ret = g_ceph_context->_conf->injectargs(injection, &chat3);
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
std::ostringstream chat4;
injection = "--debug 1 --log_to_syslog=falsey --debug-ms 42";
ret = g_ceph_context->_conf->injectargs(injection, &chat3);
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("debug_ms", &tmp, sizeof(buf));
ASSERT_EQ(ret, 0);
ASSERT_EQ(string("42"), string(buf));
}
TEST(DaemonConfig, InjectArgsLogfile) {
int ret;
std::ostringstream chat;
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, &chat);
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);
}
|