]> git.proxmox.com Git - ceph.git/blob - ceph/src/test/common/test_config.cc
update sources to 12.2.10
[ceph.git] / ceph / src / test / common / test_config.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 Cloudwatt <libre.licensing@cloudwatt.com>
7 *
8 * Author: Loic Dachary <loic@dachary.org>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Library Public License as published by
12 * the Free Software Foundation; either version 2, or (at your option)
13 * any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Library Public License for more details.
19 *
20 *
21 */
22 #include "common/config.h"
23 #include "common/errno.h"
24 #include "gtest/gtest.h"
25 #include "common/hostname.h"
26
27 extern std::string exec(const char* cmd); // defined in test_hostname.cc
28
29 class test_md_config_t : public md_config_t, public ::testing::Test {
30 public:
31
32 test_md_config_t()
33 : md_config_t(true), Test()
34 {}
35
36 void test_expand_meta() {
37 Mutex::Locker l(lock);
38 // successfull meta expansion $run_dir and ${run_dir}
39 {
40 ostringstream oss;
41 std::string before = " BEFORE ";
42 std::string after = " AFTER ";
43
44 std::string val(before + "$run_dir${run_dir}" + after);
45 EXPECT_TRUE(expand_meta(val, &oss));
46 EXPECT_EQ(before + "/var/run/ceph/var/run/ceph" + after, val);
47 EXPECT_EQ("", oss.str());
48 }
49 {
50 ostringstream oss;
51 std::string before = " BEFORE ";
52 std::string after = " AFTER ";
53 std::string val(before + "$host${host}" + after);
54 EXPECT_TRUE(expand_meta(val, &oss));
55 std::string hostname = ceph_get_short_hostname();
56 EXPECT_EQ(before + hostname + hostname + after, val);
57 EXPECT_EQ("", oss.str());
58 }
59 // no meta expansion if variables are unknown
60 {
61 ostringstream oss;
62 std::string expected = "expect $foo and ${bar} to not expand";
63 std::string val = expected;
64 EXPECT_FALSE(expand_meta(val, &oss));
65 EXPECT_EQ(expected, val);
66 EXPECT_EQ("", oss.str());
67 }
68 // recursive variable expansion
69 {
70 std::string host = "localhost";
71 EXPECT_EQ(0, set_val("host", host.c_str(), false));
72
73 std::string mon_host = "$cluster_network";
74 EXPECT_EQ(0, set_val("mon_host", mon_host.c_str(), false));
75
76 std::string lockdep = "true";
77 EXPECT_EQ(0, set_val("lockdep", lockdep.c_str(), false));
78
79 std::string cluster_network = "$public_network $public_network $lockdep $host";
80 EXPECT_EQ(0, set_val("cluster_network", cluster_network.c_str(), false));
81
82 std::string public_network = "NETWORK";
83 EXPECT_EQ(0, set_val("public_network", public_network.c_str(), false));
84
85 ostringstream oss;
86 std::string val = "$mon_host";
87 EXPECT_TRUE(expand_meta(val, &oss));
88 EXPECT_EQ(public_network + " " +
89 public_network + " " +
90 lockdep + " " +
91 "localhost", val);
92 EXPECT_EQ("", oss.str());
93 }
94 // variable expansion loops are non fatal
95 {
96 std::string mon_host = "$cluster_network";
97 EXPECT_EQ(0, set_val("mon_host", mon_host.c_str(), false));
98
99 std::string cluster_network = "$public_network";
100 EXPECT_EQ(0, set_val("cluster_network", cluster_network.c_str(), false));
101
102 std::string public_network = "$mon_host";
103 EXPECT_EQ(0, set_val("public_network", public_network.c_str(), false));
104
105 ostringstream oss;
106 std::string val = "$mon_host";
107 EXPECT_TRUE(expand_meta(val, &oss));
108 EXPECT_EQ("$cluster_network", val);
109 const char *expected_oss =
110 "variable expansion loop at mon_host=$cluster_network\n"
111 "expansion stack: \n"
112 "public_network=$mon_host\n"
113 "cluster_network=$public_network\n"
114 "mon_host=$cluster_network\n";
115 EXPECT_EQ(expected_oss, oss.str());
116 }
117 }
118
119 void test_expand_all_meta() {
120 Mutex::Locker l(lock);
121 int before_count = 0, data_dir = 0;
122 for (const auto &i : schema) {
123 const Option &opt = i.second;
124 if (opt.type == Option::TYPE_STR) {
125 const auto &str = boost::get<std::string>(opt.value);
126 if (str.find("$") != string::npos)
127 before_count++;
128 if (str.find("$data_dir") != string::npos)
129 data_dir++;
130 }
131 }
132
133 // if there are no meta variables in the default configuration,
134 // something must be done to check the expected side effect
135 // of expand_all_meta
136 ASSERT_LT(0, before_count);
137 expand_all_meta();
138 int after_count = 0;
139 for (auto& i : values) {
140 const auto &opt = schema.at(i.first);
141 if (opt.type == Option::TYPE_STR) {
142 const std::string &str = boost::get<std::string>(i.second);
143 size_t pos = 0;
144 while ((pos = str.find("$", pos)) != string::npos) {
145 if (str.substr(pos, 8) != "$channel") {
146 std::cout << "unexpected meta-variable found at pos " << pos
147 << " of '" << str << "'" << std::endl;
148 after_count++;
149 }
150 pos++;
151 }
152 }
153 }
154 ASSERT_EQ(data_dir, after_count);
155 }
156 };
157
158 TEST_F(test_md_config_t, expand_meta)
159 {
160 test_expand_meta();
161 }
162
163 TEST_F(test_md_config_t, expand_all_meta)
164 {
165 test_expand_all_meta();
166 }
167
168 TEST(md_config_t, set_val)
169 {
170 int buf_size = 1024;
171 md_config_t conf;
172 // disable meta variable expansion
173 {
174 char *buf = (char*)malloc(buf_size);
175 std::string expected = "$host";
176 EXPECT_EQ(0, conf.set_val("mon_host", expected.c_str(), false));
177 EXPECT_EQ(0, conf.get_val("mon_host", &buf, buf_size));
178 EXPECT_EQ(expected, buf);
179 free(buf);
180 }
181 // meta variable expansion is enabled by default
182 {
183 char *run_dir = (char*)malloc(buf_size);
184 EXPECT_EQ(0, conf.get_val("run_dir", &run_dir, buf_size));
185 EXPECT_EQ(0, conf.set_val("admin_socket", "$run_dir"));
186 char *admin_socket = (char*)malloc(buf_size);
187 EXPECT_EQ(0, conf.get_val("admin_socket", &admin_socket, buf_size));
188 EXPECT_EQ(std::string(run_dir), std::string(admin_socket));
189 free(run_dir);
190 free(admin_socket);
191 }
192 }
193
194 TEST(Option, validation)
195 {
196 Option opt_int("foo", Option::TYPE_INT, Option::LEVEL_BASIC);
197 opt_int.set_min_max(5, 10);
198
199 std::string msg;
200 EXPECT_EQ(-EINVAL, opt_int.validate(Option::value_t(int64_t(4)), &msg));
201 EXPECT_EQ(-EINVAL, opt_int.validate(Option::value_t(int64_t(11)), &msg));
202 EXPECT_EQ(0, opt_int.validate(Option::value_t(int64_t(7)), &msg));
203
204 Option opt_enum("foo", Option::TYPE_STR, Option::LEVEL_BASIC);
205 opt_enum.set_enum_allowed({"red", "blue"});
206 EXPECT_EQ(0, opt_enum.validate(Option::value_t(std::string("red")), &msg));
207 EXPECT_EQ(0, opt_enum.validate(Option::value_t(std::string("blue")), &msg));
208 EXPECT_EQ(-EINVAL, opt_enum.validate(Option::value_t(std::string("green")), &msg));
209
210 Option opt_validator("foo", Option::TYPE_INT, Option::LEVEL_BASIC);
211 opt_validator.set_validator([](std::string *value, std::string *error_message){
212 if (*value == std::string("one")) {
213 *value = "1";
214 return 0;
215 } else if (*value == std::string("666")) {
216 return -EINVAL;
217 } else {
218 return 0;
219 }
220 });
221
222 std::string input = "666"; // An explicitly forbidden value
223 EXPECT_EQ(-EINVAL, opt_validator.pre_validate(&input, &msg));
224 EXPECT_EQ(input, "666");
225
226 input = "123"; // A permitted value with no special behaviour
227 EXPECT_EQ(0, opt_validator.pre_validate(&input, &msg));
228 EXPECT_EQ(input, "123");
229
230 input = "one"; // A value that has a magic conversion
231 EXPECT_EQ(0, opt_validator.pre_validate(&input, &msg));
232 EXPECT_EQ(input, "1");
233 }
234
235 /*
236 * Local Variables:
237 * compile-command: "cd ../.. ;
238 * make unittest_config &&
239 * valgrind \
240 * --max-stackframe=20000000 --tool=memcheck \
241 * ./unittest_config # --gtest_filter=md_config_t.set_val
242 * "
243 * End:
244 */