1 /*
2 * SysDB - t/unit/core/store_json_test.c
3 * Copyright (C) 2014 Sebastian 'tokkee' Harl <sh@tokkee.org>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
19 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
28 #include "core/store.h"
29 #include "libsysdb_test.h"
31 #include <assert.h>
33 #include <check.h>
34 #include <stdlib.h>
36 static void
37 populate(void)
38 {
39 sdb_data_t datum;
41 sdb_store_host("h1", 1);
42 sdb_store_host("h2", 3);
44 datum.type = SDB_TYPE_STRING;
45 datum.data.string = "v1";
46 sdb_store_attribute("h1", "k1", &datum, 1);
47 datum.data.string = "v2";
48 sdb_store_attribute("h1", "k2", &datum, 2);
49 datum.data.string = "v3";
50 sdb_store_attribute("h1", "k3", &datum, 2);
52 /* make sure that older updates don't overwrite existing values */
53 datum.data.string = "fail";
54 sdb_store_attribute("h1", "k2", &datum, 1);
55 sdb_store_attribute("h1", "k3", &datum, 2);
57 sdb_store_metric("h1", "m1", /* store */ NULL, 2);
58 sdb_store_metric("h1", "m2", /* store */ NULL, 1);
59 sdb_store_metric("h2", "m1", /* store */ NULL, 1);
61 sdb_store_service("h2", "s1", 1);
62 sdb_store_service("h2", "s2", 2);
64 datum.type = SDB_TYPE_INTEGER;
65 datum.data.integer = 42;
66 sdb_store_metric_attr("h1", "m1", "k3", &datum, 2);
68 datum.data.integer = 123;
69 sdb_store_service_attr("h2", "s2", "k1", &datum, 2);
70 datum.data.integer = 4711;
71 sdb_store_service_attr("h2", "s2", "k2", &datum, 1);
73 /* don't overwrite k1 */
74 datum.data.integer = 666;
75 sdb_store_service_attr("h2", "s2", "k1", &datum, 2);
76 } /* populate */
78 static int
79 scan_tojson(sdb_store_obj_t *obj,
80 sdb_store_matcher_t __attribute__((unused)) *filter,
81 void *user_data)
82 {
83 sdb_store_json_formatter_t *f = user_data;
84 return sdb_store_json_emit(f, obj);
85 } /* scan_tojson */
87 static int
88 scan_tojson_full(sdb_store_obj_t *obj, sdb_store_matcher_t *filter,
89 void *user_data)
90 {
91 sdb_store_json_formatter_t *f = user_data;
92 return sdb_store_json_emit_full(f, obj, filter);
93 } /* scan_tojson_full */
95 static void
96 verify_json_output(sdb_strbuf_t *buf, const char *expected)
97 {
98 const char *got = sdb_strbuf_string(buf);
99 size_t len1 = strlen(got);
100 size_t len2 = strlen(expected);
102 size_t i;
103 int pos = -1;
105 if (len1 != len2)
106 pos = (int)SDB_MIN(len1, len2);
108 for (i = 0; i < SDB_MIN(len1, len2); ++i) {
109 if (got[i] != expected[i]) {
110 pos = (int)i;
111 break;
112 }
113 }
115 fail_unless(pos == -1,
116 "Serializing hosts to JSON returned unexpected result\n"
117 " got: %s\n %*s\n expected: %s",
118 got, pos + 1, "^", expected);
119 } /* verify_json_output */
121 START_TEST(test_store_tojson)
122 {
123 sdb_strbuf_t *buf;
124 size_t i;
126 struct {
127 struct {
128 sdb_store_matcher_t *(*m)(sdb_store_expr_t *,
129 sdb_store_expr_t *);
130 int field;
131 sdb_data_t value;
132 } filter;
133 int type;
134 int (*f)(sdb_store_obj_t *, sdb_store_matcher_t *, void *);
135 const char *expected;
136 } golden_data[] = {
137 { { NULL, 0, SDB_DATA_INIT },
138 SDB_HOST, scan_tojson_full,
139 "["
140 "{\"name\": \"h1\", \"last_update\": \"1970-01-01 00:00:00 +0000\", "
141 "\"update_interval\": \"0s\", \"backends\": [], "
142 "\"attributes\": ["
143 "{\"name\": \"k1\", \"value\": \"v1\", "
144 "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
145 "\"update_interval\": \"0s\", \"backends\": []},"
146 "{\"name\": \"k2\", \"value\": \"v2\", "
147 "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
148 "\"update_interval\": \"0s\", \"backends\": []},"
149 "{\"name\": \"k3\", \"value\": \"v3\", "
150 "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
151 "\"update_interval\": \"0s\", \"backends\": []}"
152 "], "
153 "\"metrics\": ["
154 "{\"name\": \"m1\", "
155 "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
156 "\"update_interval\": \"0s\", \"backends\": [], "
157 "\"attributes\": ["
158 "{\"name\": \"k3\", \"value\": 42, "
159 "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
160 "\"update_interval\": \"0s\", \"backends\": []}"
161 "]},"
162 "{\"name\": \"m2\", "
163 "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
164 "\"update_interval\": \"0s\", \"backends\": []}"
165 "]},"
166 "{\"name\": \"h2\", \"last_update\": \"1970-01-01 00:00:00 +0000\", "
167 "\"update_interval\": \"0s\", \"backends\": [], "
168 "\"metrics\": ["
169 "{\"name\": \"m1\", "
170 "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
171 "\"update_interval\": \"0s\", \"backends\": []}"
172 "], "
173 "\"services\": ["
174 "{\"name\": \"s1\", "
175 "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
176 "\"update_interval\": \"0s\", \"backends\": []},"
177 "{\"name\": \"s2\", "
178 "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
179 "\"update_interval\": \"0s\", \"backends\": [], "
180 "\"attributes\": ["
181 "{\"name\": \"k1\", \"value\": 123, "
182 "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
183 "\"update_interval\": \"0s\", \"backends\": []},"
184 "{\"name\": \"k2\", \"value\": 4711, "
185 "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
186 "\"update_interval\": \"0s\", \"backends\": []}"
187 "]}"
188 "]}"
189 "]" },
190 { { NULL, 0, SDB_DATA_INIT },
191 SDB_HOST, scan_tojson,
192 "["
193 "{\"name\": \"h1\", \"last_update\": \"1970-01-01 00:00:00 +0000\", "
194 "\"update_interval\": \"0s\", \"backends\": []},"
195 "{\"name\": \"h2\", \"last_update\": \"1970-01-01 00:00:00 +0000\", "
196 "\"update_interval\": \"0s\", \"backends\": []}"
197 "]" },
198 { { sdb_store_eq_matcher, SDB_FIELD_NAME,
199 { SDB_TYPE_STRING, { .string = "h1" } } },
200 SDB_HOST, scan_tojson_full,
201 "["
202 "{\"name\": \"h1\", \"last_update\": \"1970-01-01 00:00:00 +0000\", "
203 "\"update_interval\": \"0s\", \"backends\": []}"
204 "]" },
205 { { sdb_store_gt_matcher, SDB_FIELD_LAST_UPDATE,
206 { SDB_TYPE_DATETIME, { .datetime = 1 } } },
207 SDB_HOST, scan_tojson_full,
208 "["
209 "{\"name\": \"h2\", \"last_update\": \"1970-01-01 00:00:00 +0000\", "
210 "\"update_interval\": \"0s\", \"backends\": [], "
211 "\"services\": ["
212 "{\"name\": \"s2\", "
213 "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
214 "\"update_interval\": \"0s\", \"backends\": [], "
215 "\"attributes\": ["
216 "{\"name\": \"k1\", \"value\": 123, "
217 "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
218 "\"update_interval\": \"0s\", \"backends\": []}"
219 "]}"
220 "]}"
221 "]" },
222 { { sdb_store_le_matcher, SDB_FIELD_LAST_UPDATE,
223 { SDB_TYPE_DATETIME, { .datetime = 1 } } },
224 SDB_HOST, scan_tojson_full,
225 "["
226 "{\"name\": \"h1\", \"last_update\": \"1970-01-01 00:00:00 +0000\", "
227 "\"update_interval\": \"0s\", \"backends\": [], "
228 "\"attributes\": ["
229 "{\"name\": \"k1\", \"value\": \"v1\", "
230 "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
231 "\"update_interval\": \"0s\", \"backends\": []}"
232 "], "
233 "\"metrics\": ["
234 "{\"name\": \"m2\", "
235 "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
236 "\"update_interval\": \"0s\", \"backends\": []}"
237 "]}"
238 "]" },
239 { { sdb_store_ge_matcher, SDB_FIELD_LAST_UPDATE,
240 { SDB_TYPE_DATETIME, { .datetime = 3 } } },
241 SDB_HOST, scan_tojson_full,
242 "["
243 "{\"name\": \"h2\", \"last_update\": \"1970-01-01 00:00:00 +0000\", "
244 "\"update_interval\": \"0s\", \"backends\": []}"
245 "]" },
246 { { sdb_store_lt_matcher, SDB_FIELD_LAST_UPDATE,
247 { SDB_TYPE_DATETIME, { .datetime = 0 } } },
248 SDB_HOST, scan_tojson_full,
249 "[]" },
251 { { NULL, 0, SDB_DATA_INIT },
252 SDB_SERVICE, scan_tojson_full,
253 "["
254 "{\"name\": \"h2\", \"last_update\": \"1970-01-01 00:00:00 +0000\", "
255 "\"update_interval\": \"0s\", \"backends\": [], "
256 "\"services\": ["
257 "{\"name\": \"s1\", "
258 "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
259 "\"update_interval\": \"0s\", \"backends\": []},"
260 "{\"name\": \"s2\", "
261 "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
262 "\"update_interval\": \"0s\", \"backends\": [], "
263 "\"attributes\": ["
264 "{\"name\": \"k1\", \"value\": 123, "
265 "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
266 "\"update_interval\": \"0s\", \"backends\": []},"
267 "{\"name\": \"k2\", \"value\": 4711, "
268 "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
269 "\"update_interval\": \"0s\", \"backends\": []}"
270 "]}"
271 "]}"
272 "]" },
273 { { NULL, 0, SDB_DATA_INIT },
274 SDB_SERVICE, scan_tojson,
275 "["
276 "{\"name\": \"h2\", \"last_update\": \"1970-01-01 00:00:00 +0000\", "
277 "\"update_interval\": \"0s\", \"backends\": [], "
278 "\"services\": ["
279 "{\"name\": \"s1\", "
280 "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
281 "\"update_interval\": \"0s\", \"backends\": []},"
282 "{\"name\": \"s2\", "
283 "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
284 "\"update_interval\": \"0s\", \"backends\": []}"
285 "]}"
286 "]" },
287 { { sdb_store_gt_matcher, SDB_FIELD_LAST_UPDATE,
288 { SDB_TYPE_DATETIME, { .datetime = 1 } } },
289 SDB_SERVICE, scan_tojson_full,
290 "["
291 "{\"name\": \"h2\", \"last_update\": \"1970-01-01 00:00:00 +0000\", "
292 "\"update_interval\": \"0s\", \"backends\": [], "
293 "\"services\": ["
294 "{\"name\": \"s2\", "
295 "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
296 "\"update_interval\": \"0s\", \"backends\": [], "
297 "\"attributes\": ["
298 "{\"name\": \"k1\", \"value\": 123, "
299 "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
300 "\"update_interval\": \"0s\", \"backends\": []}"
301 "]}"
302 "]}"
303 "]" },
304 { { sdb_store_lt_matcher, SDB_FIELD_LAST_UPDATE,
305 { SDB_TYPE_DATETIME, { .datetime = 0 } } },
306 SDB_SERVICE, scan_tojson_full,
307 "[]" },
308 { { NULL, 0, SDB_DATA_INIT },
309 SDB_METRIC, scan_tojson_full,
310 "["
311 "{\"name\": \"h1\", \"last_update\": \"1970-01-01 00:00:00 +0000\", "
312 "\"update_interval\": \"0s\", \"backends\": [], "
313 "\"metrics\": ["
314 "{\"name\": \"m1\", "
315 "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
316 "\"update_interval\": \"0s\", \"backends\": [], "
317 "\"attributes\": ["
318 "{\"name\": \"k3\", \"value\": 42, "
319 "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
320 "\"update_interval\": \"0s\", \"backends\": []}"
321 "]},"
322 "{\"name\": \"m2\", "
323 "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
324 "\"update_interval\": \"0s\", \"backends\": []}"
325 "]},"
326 "{\"name\": \"h2\", \"last_update\": \"1970-01-01 00:00:00 +0000\", "
327 "\"update_interval\": \"0s\", \"backends\": [], "
328 "\"metrics\": ["
329 "{\"name\": \"m1\", "
330 "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
331 "\"update_interval\": \"0s\", \"backends\": []}"
332 "]}"
333 "]" },
334 { { NULL, 0, SDB_DATA_INIT },
335 SDB_METRIC, scan_tojson,
336 "["
337 "{\"name\": \"h1\", \"last_update\": \"1970-01-01 00:00:00 +0000\", "
338 "\"update_interval\": \"0s\", \"backends\": [], "
339 "\"metrics\": ["
340 "{\"name\": \"m1\", "
341 "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
342 "\"update_interval\": \"0s\", \"backends\": []},"
343 "{\"name\": \"m2\", "
344 "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
345 "\"update_interval\": \"0s\", \"backends\": []}"
346 "]},"
347 "{\"name\": \"h2\", \"last_update\": \"1970-01-01 00:00:00 +0000\", "
348 "\"update_interval\": \"0s\", \"backends\": [], "
349 "\"metrics\": ["
350 "{\"name\": \"m1\", "
351 "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
352 "\"update_interval\": \"0s\", \"backends\": []}"
353 "]}"
354 "]" },
355 { { sdb_store_le_matcher, SDB_FIELD_LAST_UPDATE,
356 { SDB_TYPE_DATETIME, { .datetime = 1 } } },
357 SDB_METRIC, scan_tojson_full,
358 "["
359 "{\"name\": \"h1\", \"last_update\": \"1970-01-01 00:00:00 +0000\", "
360 "\"update_interval\": \"0s\", \"backends\": [], "
361 "\"metrics\": ["
362 "{\"name\": \"m2\", "
363 "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
364 "\"update_interval\": \"0s\", \"backends\": []}"
365 "]}"
366 "]" },
367 { { sdb_store_lt_matcher, SDB_FIELD_LAST_UPDATE,
368 { SDB_TYPE_DATETIME, { .datetime = 0 } } },
369 SDB_METRIC, scan_tojson_full,
370 "[]" },
371 };
373 buf = sdb_strbuf_create(0);
374 fail_unless(buf != NULL, "INTERNAL ERROR: failed to create string buffer");
375 populate();
377 for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
378 sdb_store_matcher_t *filter = NULL;
379 sdb_store_json_formatter_t *f;
380 int status;
382 sdb_strbuf_clear(buf);
384 if (golden_data[i].filter.m) {
385 sdb_store_expr_t *field;
386 sdb_store_expr_t *value;
388 field = sdb_store_expr_fieldvalue(golden_data[i].filter.field);
389 fail_unless(field != NULL,
390 "INTERNAL ERROR: sdb_store_expr_fieldvalue() = NULL");
391 value = sdb_store_expr_constvalue(&golden_data[i].filter.value);
392 fail_unless(value != NULL,
393 "INTERNAL ERROR: sdb_store_expr_constvalue() = NULL");
395 filter = golden_data[i].filter.m(field, value);
396 fail_unless(filter != NULL,
397 "INTERNAL ERROR: sdb_store_*_matcher() = NULL");
399 sdb_object_deref(SDB_OBJ(field));
400 sdb_object_deref(SDB_OBJ(value));
401 }
403 sdb_strbuf_clear(buf);
404 f = sdb_store_json_formatter(buf,
405 golden_data[i].type, SDB_WANT_ARRAY);
406 assert(f);
408 status = sdb_store_scan(golden_data[i].type, /* m = */ NULL, filter,
409 golden_data[i].f, f);
410 fail_unless(status == 0,
411 "sdb_store_scan(HOST, ..., tojson) = %d; expected: 0",
412 status);
413 sdb_store_json_finish(f);
415 verify_json_output(buf, golden_data[i].expected);
416 free(f);
417 sdb_object_deref(SDB_OBJ(filter));
418 }
419 sdb_strbuf_destroy(buf);
420 }
421 END_TEST
423 Suite *
424 core_store_json_suite(void)
425 {
426 Suite *s = suite_create("core::store_json");
427 TCase *tc;
429 tc = tcase_create("core");
430 tcase_add_test(tc, test_store_tojson);
431 tcase_add_unchecked_fixture(tc, NULL, sdb_store_clear);
432 suite_add_tcase(s, tc);
434 return s;
435 } /* core_store_json_suite */
437 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */