-
Notifications
You must be signed in to change notification settings - Fork 5
/
etcd_test.cpp
297 lines (216 loc) · 9.89 KB
/
etcd_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
// MIT License
// Copyright (c) 2018 Gang Liao <gangliao@cs.umd.edu>
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include <b64.h>
#include <curl/curl.h>
#include <glog/logging.h>
#include <gtest/gtest.h>
#include <json/json.h>
#include <unordered_map>
size_t process_data_v2(void *buffer, size_t size, size_t nmemb, void *user_p)
{
Json::Value root;
Json::Value node;
Json::Reader reader;
Json::FastWriter writer;
std::string json = (char *)buffer;
EXPECT_TRUE(reader.parse(json, root));
std::string nodeString = writer.write(root["node"]);
EXPECT_TRUE(reader.parse(nodeString, node));
*(std::string *)user_p = writer.write(node["value"]);
return size * nmemb;
}
TEST(ETCDv2, GetValue)
{
static const char *pCertFile = "/etc/etcd/ssl/etcd.pem";
static const char *pCACertFile = "/etc/kubernetes/ssl/ca.pem";
static const char *pKeyName = "/etc/etcd/ssl/etcd-key.pem";
static const char *pKeyType = "PEM";
CURLcode return_code;
return_code = curl_global_init(CURL_GLOBAL_SSL);
EXPECT_EQ(CURLE_OK, return_code) << "init libcurl failed.";
CURL *easy_handle = curl_easy_init();
CHECK_NOTNULL(easy_handle);
std::string buff_p;
curl_easy_setopt(easy_handle, CURLOPT_URL, "https://10.142.104.73/v2/keys/Microsoft");
curl_easy_setopt(easy_handle, CURLOPT_PORT, 2379);
/* since PEM is default, we needn't set it for PEM */
curl_easy_setopt(easy_handle, CURLOPT_SSLCERTTYPE, "PEM");
/* set the cert for client authentication */
curl_easy_setopt(easy_handle, CURLOPT_SSLCERT, pCertFile);
/* set the private key (file or ID in engine) */
curl_easy_setopt(easy_handle, CURLOPT_SSLKEYTYPE, pKeyType);
curl_easy_setopt(easy_handle, CURLOPT_SSLKEY, pKeyName);
/* set the file with the certs vaildating the server */
curl_easy_setopt(easy_handle, CURLOPT_CAINFO, pCACertFile);
/* disconnect if we can't validate server's cert */
curl_easy_setopt(easy_handle, CURLOPT_SSL_VERIFYPEER, 1L);
curl_easy_setopt(easy_handle, CURLOPT_WRITEFUNCTION, &process_data_v2);
curl_easy_setopt(easy_handle, CURLOPT_WRITEDATA, &buff_p);
CURLcode res = curl_easy_perform(easy_handle);
EXPECT_EQ(res, CURLE_OK) << curl_easy_strerror(res);
std::cout << buff_p << std::endl;
curl_easy_cleanup(easy_handle);
curl_global_cleanup();
}
struct MemoryStruct
{
char *memory;
size_t size;
};
size_t process_range_v3(void *buffer, size_t size, size_t nmemb, void *user_p)
{
size_t realsize = size * nmemb;
struct MemoryStruct *mem = (struct MemoryStruct *)user_p;
mem->memory = (char *)realloc(mem->memory, mem->size + realsize + 1);
if (mem->memory == NULL)
{
/* out of memory! */
printf("not enough memory (realloc returned NULL)\n");
return 0;
}
memcpy(&(mem->memory[mem->size]), buffer, realsize);
mem->size += realsize;
mem->memory[mem->size] = 0;
return realsize;
}
TEST(ETCDv3, GetKVUnderDirectory)
{
static const char *pCACertFile = "/etc/kubernetes/ssl/ca.pem";
CURLcode return_code;
return_code = curl_global_init(CURL_GLOBAL_SSL);
EXPECT_EQ(CURLE_OK, return_code) << "init libcurl failed.";
CURL *easy_handle = curl_easy_init();
CHECK_NOTNULL(easy_handle);
curl_easy_setopt(easy_handle, CURLOPT_URL, "https://10.142.104.73/v3alpha/kv/range");
curl_easy_setopt(easy_handle, CURLOPT_PORT, 2379);
struct curl_slist *headers = NULL;
headers = curl_slist_append(headers, "Content-Type: application/json");
curl_easy_setopt(easy_handle, CURLOPT_HTTPHEADER, headers);
std::string key = "Microsoft/FreeFlow";
char *encoded_key = b64_encode((const unsigned char *)key.c_str(), key.length());
char *encoded_end_key = b64_encode((const unsigned char *)key.c_str(), key.length());
LOG(INFO) << "base64 encoding [Microsoft] to [" << encoded_key << "]";
// If range_end is key plus one (e.g., "aa"+1 == "ab", "a\xff"+1 == "b"), then the
// range represents all keys prefixed with key.
size_t n = strlen(encoded_end_key);
encoded_end_key[n - 1] = encoded_key[n - 1] + 1;
char post_fields[1024];
sprintf(post_fields, "{\"key\": \"%s\", \"range_end\": \"%s\"}", encoded_key, encoded_end_key);
curl_easy_setopt(easy_handle, CURLOPT_CUSTOMREQUEST, "POST");
curl_easy_setopt(easy_handle, CURLOPT_POSTFIELDS, post_fields);
/* set the file with the certs vaildating the server */
curl_easy_setopt(easy_handle, CURLOPT_CAINFO, pCACertFile);
/* disconnect if we can't validate server's cert */
curl_easy_setopt(easy_handle, CURLOPT_SSL_VERIFYPEER, 1L);
curl_easy_setopt(easy_handle, CURLOPT_WRITEFUNCTION, &process_range_v3);
struct MemoryStruct chunk;
chunk.memory = (char *)malloc(1); /* will be grown as needed by the realloc above */
chunk.size = 0; /* no data at this point */
curl_easy_setopt(easy_handle, CURLOPT_WRITEDATA, (void *)&chunk);
CURLcode res = curl_easy_perform(easy_handle);
EXPECT_EQ(res, CURLE_OK) << curl_easy_strerror(res);
Json::Value root;
Json::Value kv;
Json::Reader reader;
std::string json = chunk.memory;
EXPECT_TRUE(reader.parse(json, root));
kv = root["kvs"];
if (!kv.empty())
{
for (int i = 0; i < kv.size(); ++i)
{
std::string key = kv[i]["key"].asString();
std::string val = kv[i]["value"].asString();
key = (char *)b64_decode(key.c_str(), key.length());
val = (char *)b64_decode(val.c_str(), val.length());
LOG(INFO) << key << "\t" << val;
}
}
free(chunk.memory);
free(encoded_key);
free(encoded_end_key);
curl_easy_cleanup(easy_handle);
curl_global_cleanup();
}
static std::unordered_map<std::string, std::string> g_kv_map;
size_t process_watch_v3(void *buffer, size_t size, size_t nmemb, void *user_p)
{
Json::Value root;
Json::Value kv;
Json::Reader reader;
std::string json = (char *)buffer;
LOG(INFO) << "parsing json: " << json << std::endl;
EXPECT_TRUE(reader.parse(json, root));
kv = root["result"]["events"];
if (!kv.empty())
{
std::string encode_key = kv[0]["kv"]["key"].asString();
std::string decode_key = (char *)b64_decode(encode_key.c_str(), encode_key.length());
if (kv[0]["type"].asString() != "DELETE")
{
std::string encode_val = kv[0]["kv"]["value"].asString();
std::string decode_val = (char *)b64_decode(encode_val.c_str(), encode_val.length());
LOG(INFO) << "Add or update: (" << decode_key << "\t, " << decode_val << ")";
g_kv_map[decode_key] = decode_val;
}
else
{
LOG(INFO) << "Delete key: " << decode_key;
g_kv_map.erase(decode_key);
}
}
return size * nmemb;
}
TEST(ETCDv3, WatchValueChange)
{
static const char *pCACertFile = "/etc/kubernetes/ssl/ca.pem";
CURLcode return_code;
return_code = curl_global_init(CURL_GLOBAL_SSL);
EXPECT_EQ(CURLE_OK, return_code) << "init libcurl failed.";
CURL *easy_handle = curl_easy_init();
CHECK_NOTNULL(easy_handle);
curl_easy_setopt(easy_handle, CURLOPT_URL, "https://10.142.104.73/v3alpha/watch");
curl_easy_setopt(easy_handle, CURLOPT_PORT, 2379);
struct curl_slist *headers = NULL;
headers = curl_slist_append(headers, "Content-Type: application/json");
curl_easy_setopt(easy_handle, CURLOPT_HTTPHEADER, headers);
std::string key = "Microsoft/FreeFlow";
char *encoded_key = b64_encode((const unsigned char *)key.c_str(), key.length());
char *encoded_end_key = b64_encode((const unsigned char *)key.c_str(), key.length());
LOG(INFO) << "base64 encoding [Microsoft] to [" << encoded_key << "]";
// If range_end is key plus one (e.g., "aa"+1 == "ab", "a\xff"+1 == "b"), then the
// range represents all keys prefixed with key.
size_t n = strlen(encoded_end_key);
encoded_end_key[n - 1] = encoded_key[n - 1] + 1;
char post_fields[1024];
sprintf(post_fields, "{\"create_request\": {\"key\": \"%s\", \"range_end\": \"%s\"}}", encoded_key, encoded_end_key);
curl_easy_setopt(easy_handle, CURLOPT_CUSTOMREQUEST, "POST");
curl_easy_setopt(easy_handle, CURLOPT_POSTFIELDS, post_fields);
/* set the file with the certs vaildating the server */
curl_easy_setopt(easy_handle, CURLOPT_CAINFO, pCACertFile);
/* disconnect if we can't validate server's cert */
curl_easy_setopt(easy_handle, CURLOPT_SSL_VERIFYPEER, 1L);
curl_easy_setopt(easy_handle, CURLOPT_WRITEFUNCTION, &process_watch_v3);
curl_easy_setopt(easy_handle, CURLOPT_WRITEDATA, NULL);
CURLcode res = curl_easy_perform(easy_handle);
EXPECT_EQ(res, CURLE_OK) << curl_easy_strerror(res);
free(encoded_key);
free(encoded_end_key);
curl_easy_cleanup(easy_handle);
curl_global_cleanup();
}