forked from opencurve/curve
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfiguration_test.cpp
306 lines (240 loc) · 8.19 KB
/
configuration_test.cpp
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
/*
* Copyright (C) 2018 NetEase Inc. All rights reserved.
* Project: Curve
*
* History:
* 2018/11/23 Wenyu Zhou Initial version
*/
#include <gtest/gtest.h>
#include <gmock/gmock.h>
#include <string>
#include <iostream>
#include <fstream>
#include <sstream>
#include "src/common/configuration.h"
namespace curve {
namespace common {
class ConfigurationTest : public ::testing::Test {
public:
void SetUp() {
std::string confItem;
confFile_ = "curve.conf.test";
std::ofstream cFile(confFile_);
ASSERT_TRUE(cFile.is_open());
confItem = "test.str1=teststring\n";
cFile << confItem;
confItem = "test.int1=12345\n";
cFile << confItem;
confItem = "test.int2=-2345\n";
cFile << confItem;
confItem = "test.int3=0\n";
cFile << confItem;
confItem = "test.bool1=0\n";
cFile << confItem;
confItem = "test.bool2=1\n";
cFile << confItem;
confItem = "test.bool3=false\n";
cFile << confItem;
confItem = "test.bool4=true\n";
cFile << confItem;
confItem = "test.bool5=no\n";
cFile << confItem;
confItem = "test.bool6=yes\n";
cFile << confItem;
confItem = "test.double1=3.1415926\n";
cFile << confItem;
confItem = "test.double2=1\n";
cFile << confItem;
confItem = "test.double3=1.0\n";
cFile << confItem;
confItem = "test.double4=0.1\n";
cFile << confItem;
}
void TearDown() {
ASSERT_EQ(0, unlink(confFile_.c_str()));
}
std::string confFile_;
};
TEST_F(ConfigurationTest, SetAndGetConfigPath) {
Configuration conf;
conf.SetConfigPath(confFile_);
ASSERT_EQ(conf.GetConfigPath(), confFile_);
}
TEST_F(ConfigurationTest, LoadNonExistConfigFile) {
bool ret;
std::string confFile = "curve.conf.test.nonexist";
Configuration conf;
conf.SetConfigPath(confFile);
ret = conf.LoadConfig();
ASSERT_EQ(ret, false);
}
TEST_F(ConfigurationTest, LoadNormalConfigFile) {
bool ret;
Configuration conf;
conf.SetConfigPath(confFile_);
ret = conf.LoadConfig();
ASSERT_EQ(ret, true);
}
TEST_F(ConfigurationTest, ListConfig) {
Configuration conf;
conf.SetConfigPath(confFile_);
int ret = conf.LoadConfig();
ASSERT_EQ(ret, true);
std::map<std::string, std::string> configs;
configs = conf.ListConfig();
ASSERT_NE(0, configs.size());
// 抽几个key来校验以下
ASSERT_EQ(configs["test.int1"], "12345");
ASSERT_EQ(configs["test.bool1"], "0");
// 如果key不存在,返回为空
ASSERT_EQ(configs["xxx"], "");
}
// 覆盖原有配置
TEST_F(ConfigurationTest, SaveConfig) {
bool ret;
Configuration conf;
conf.SetConfigPath(confFile_);
// 自定义配置项并保存
conf.SetStringValue("test.str1", "new");
ret = conf.SaveConfig();
ASSERT_EQ(ret, true);
// 重新加载配置项
Configuration conf2;
conf2.SetConfigPath(confFile_);
ret = conf2.LoadConfig();
ASSERT_EQ(ret, true);
// 可以读取自定义配置项,原有配置项被覆盖,读取不到
ASSERT_EQ(conf2.GetValue("test.str1"), "new");
ASSERT_EQ(conf2.GetValue("test.int1"), "");
}
// 读取当前配置写到其他路径
TEST_F(ConfigurationTest, SaveConfigToFileNotExist) {
bool ret;
// 加载当前配置
Configuration conf;
conf.SetConfigPath(confFile_);
ret = conf.LoadConfig();
ASSERT_EQ(ret, true);
// 写配置到其他位置
std::string newFile("curve.conf.test2");
conf.SetConfigPath(newFile);
ret = conf.SaveConfig();
ASSERT_EQ(ret, true);
// 从新配置文件加载,并读取某项配置来进行校验
Configuration newConf;
newConf.SetConfigPath(newFile);
ret = newConf.LoadConfig();
ASSERT_EQ(ret, true);
ASSERT_EQ(newConf.GetValue("test.str1"), "teststring");
ASSERT_EQ(0, unlink(newFile.c_str()));
}
TEST_F(ConfigurationTest, GetSetValue) {
bool ret;
Configuration conf;
conf.SetConfigPath(confFile_);
ret = conf.LoadConfig();
ASSERT_EQ(ret, true);
ASSERT_EQ(conf.GetValue("test.str1"), "teststring");
ASSERT_EQ(conf.GetValue("test.int1"), "12345");
ASSERT_EQ(conf.GetValue("test.bool1"), "0");
ASSERT_EQ(conf.GetValue("test.str.nonexist"), "");
conf.SetValue("test.str1", "teststring2");
ASSERT_EQ(conf.GetValue("test.str1"), "teststring2");
std::string out;
ASSERT_FALSE(conf.GetValue("no.exist", &out));
conf.SetValue("put.in", "put.in");
ASSERT_TRUE(conf.GetValue("put.in", &out));
ASSERT_EQ("put.in", out);
}
TEST_F(ConfigurationTest, GetSetStringValue) {
bool ret;
Configuration conf;
conf.SetConfigPath(confFile_);
ret = conf.LoadConfig();
ASSERT_EQ(ret, true);
ASSERT_EQ(conf.GetStringValue("test.str1"), "teststring");
ASSERT_EQ(conf.GetStringValue("test.int1"), "12345");
ASSERT_EQ(conf.GetStringValue("test.bool1"), "0");
ASSERT_EQ(conf.GetStringValue("test.str.nonexist"), "");
conf.SetStringValue("test.str1", "teststring2");
ASSERT_EQ(conf.GetStringValue("test.str1"), "teststring2");
std::string out;
ASSERT_FALSE(conf.GetStringValue("no.exist", &out));
conf.SetStringValue("put.in", "put.in");
ASSERT_TRUE(conf.GetStringValue("put.in", &out));
ASSERT_EQ("put.in", out);
}
TEST_F(ConfigurationTest, GetSetIntValue) {
bool ret;
Configuration conf;
conf.SetConfigPath(confFile_);
ret = conf.LoadConfig();
ASSERT_EQ(ret, true);
ASSERT_EQ(conf.GetIntValue("test.int1"), 12345);
ASSERT_EQ(conf.GetIntValue("test.int2"), -2345);
ASSERT_EQ(conf.GetIntValue("test.int3"), 0);
ASSERT_EQ(conf.GetIntValue("test.int.nonexist"), 0);
conf.SetIntValue("test.int1", 123);
ASSERT_EQ(conf.GetIntValue("test.int1"), 123);
int out;
ASSERT_FALSE(conf.GetIntValue("no.exist", &out));
conf.SetIntValue("no.exist", 1);
ASSERT_TRUE(conf.GetIntValue("no.exist", &out));
ASSERT_EQ(1, out);
uint32_t outu32;
ASSERT_FALSE(conf.GetUInt32Value("no.exist.u32", &outu32));
conf.SetIntValue("no.exist.u32", 2);
ASSERT_TRUE(conf.GetUInt32Value("no.exist.u32", &outu32));
ASSERT_EQ(2, outu32);
uint64_t outu64;
ASSERT_FALSE(conf.GetUInt64Value("no.exist.u64", &outu64));
conf.SetIntValue("no.exist.u64", 3);
ASSERT_TRUE(conf.GetUInt64Value("no.exist.u64", &outu64));
ASSERT_EQ(3, outu64);
}
TEST_F(ConfigurationTest, GetSetBoolValue) {
bool ret;
Configuration conf;
conf.SetConfigPath(confFile_);
ret = conf.LoadConfig();
ASSERT_EQ(ret, true);
ASSERT_EQ(conf.GetBoolValue("test.bool1"), false);
ASSERT_EQ(conf.GetBoolValue("test.bool2"), true);
ASSERT_EQ(conf.GetBoolValue("test.bool3"), false);
ASSERT_EQ(conf.GetBoolValue("test.bool4"), true);
ASSERT_EQ(conf.GetBoolValue("test.bool5"), false);
ASSERT_EQ(conf.GetBoolValue("test.bool6"), true);
ASSERT_EQ(conf.GetBoolValue("test.bool.nonexist"), false);
conf.SetBoolValue("test.bool1", true);
ASSERT_EQ(conf.GetBoolValue("test.bool1"), true);
bool out;
ASSERT_FALSE(conf.GetBoolValue("no.exist", &out));
conf.SetIntValue("no.exist", false);
ASSERT_TRUE(conf.GetBoolValue("no.exist", &out));
ASSERT_FALSE(out);
}
TEST_F(ConfigurationTest, GetSetDoubleAndFloatValue) {
bool ret;
Configuration conf;
conf.SetConfigPath(confFile_);
ret = conf.LoadConfig();
ASSERT_EQ(ret, true);
ASSERT_EQ(conf.GetDoubleValue("test.double1"), 3.1415926);
ASSERT_EQ(conf.GetDoubleValue("test.double2"), 1);
ASSERT_EQ(conf.GetDoubleValue("test.double3"), 1.0);
ASSERT_EQ(conf.GetDoubleValue("test.double4"), 0.1);
ASSERT_EQ(conf.GetFloatValue("test.double4"), 0.1f);
conf.SetDoubleValue("test.double1", 100.0);
ASSERT_EQ(conf.GetDoubleValue("test.double1"), 100.0);
double out;
float outf;
ASSERT_FALSE(conf.GetDoubleValue("no.exist", &out));
ASSERT_FALSE(conf.GetFloatValue("no.exist", &outf));
conf.SetDoubleValue("no.exist", 0.009);
ASSERT_TRUE(conf.GetDoubleValue("no.exist", &out));
ASSERT_TRUE(conf.GetFloatValue("no.exist", &outf));
ASSERT_EQ(0.009, out);
ASSERT_EQ(0.009f, outf);
}
} // namespace common
} // namespace curve