Code

store_json: Moved special casing of parent objects into the caller.
[sysdb.git] / t / unit / core / store_json_test.c
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 #if HAVE_CONFIG_H
29 #       include "config.h"
30 #endif
32 #include "core/store.h"
33 #include "testutils.h"
35 #include <check.h>
36 #include <stdlib.h>
38 /* Make SDB_INTERVAL_SECOND a constant initializer. */
39 #undef SDB_INTERVAL_SECOND
40 #define SDB_INTERVAL_SECOND 1000000000L
42 static sdb_store_t *store;
44 static void
45 populate(void)
46 {
47         sdb_data_t datum;
49         store = sdb_store_create();
50         ck_assert(store != NULL);
52         sdb_store_host(store, "h1", 1 * SDB_INTERVAL_SECOND);
53         sdb_store_host(store, "h2", 3 * SDB_INTERVAL_SECOND);
55         datum.type = SDB_TYPE_STRING;
56         datum.data.string = "v1";
57         sdb_store_attribute(store, "h1", "k1", &datum, 1 * SDB_INTERVAL_SECOND);
58         datum.data.string = "v2";
59         sdb_store_attribute(store, "h1", "k2", &datum, 2 * SDB_INTERVAL_SECOND);
60         datum.data.string = "v3";
61         sdb_store_attribute(store, "h1", "k3", &datum, 2 * SDB_INTERVAL_SECOND);
63         /* make sure that older updates don't overwrite existing values */
64         datum.data.string = "fail";
65         sdb_store_attribute(store, "h1", "k2", &datum, 1 * SDB_INTERVAL_SECOND);
66         sdb_store_attribute(store, "h1", "k3", &datum, 2 * SDB_INTERVAL_SECOND);
68         sdb_store_metric(store, "h1", "m1", /* store */ NULL, 2 * SDB_INTERVAL_SECOND);
69         sdb_store_metric(store, "h1", "m2", /* store */ NULL, 1 * SDB_INTERVAL_SECOND);
70         sdb_store_metric(store, "h2", "m1", /* store */ NULL, 1 * SDB_INTERVAL_SECOND);
72         sdb_store_service(store, "h2", "s1", 1 * SDB_INTERVAL_SECOND);
73         sdb_store_service(store, "h2", "s2", 2 * SDB_INTERVAL_SECOND);
75         datum.type = SDB_TYPE_INTEGER;
76         datum.data.integer = 42;
77         sdb_store_metric_attr(store, "h1", "m1", "k3",
78                         &datum, 2 * SDB_INTERVAL_SECOND);
80         datum.data.integer = 123;
81         sdb_store_service_attr(store, "h2", "s2", "k1",
82                         &datum, 2 * SDB_INTERVAL_SECOND);
83         datum.data.integer = 4711;
84         sdb_store_service_attr(store, "h2", "s2", "k2",
85                         &datum, 1 * SDB_INTERVAL_SECOND);
87         /* don't overwrite k1 */
88         datum.data.integer = 666;
89         sdb_store_service_attr(store, "h2", "s2", "k1",
90                         &datum, 2 * SDB_INTERVAL_SECOND);
91 } /* populate */
93 static void
94 turndown(void)
95 {
96         sdb_object_deref(SDB_OBJ(store));
97         store = NULL;
98 } /* turndown */
100 static int
101 scan_tojson(sdb_store_obj_t *obj,
102                 sdb_store_matcher_t __attribute__((unused)) *filter,
103                 void *user_data)
105         sdb_store_json_formatter_t *f = user_data;
106         return sdb_store_json_emit(f, obj);
107 } /* scan_tojson */
109 static int
110 scan_tojson_full(sdb_store_obj_t *obj, sdb_store_matcher_t *filter,
111                 void *user_data)
113         sdb_store_json_formatter_t *f = user_data;
114         return sdb_store_json_emit_full(f, obj, filter);
115 } /* scan_tojson_full */
117 static void
118 verify_json_output(sdb_strbuf_t *buf, const char *expected)
120         const char *got = sdb_strbuf_string(buf);
121         size_t len1 = strlen(got);
122         size_t len2 = strlen(expected);
124         size_t i;
125         int pos = -1;
127         if (len1 != len2)
128                 pos = (int)SDB_MIN(len1, len2);
130         for (i = 0; i < SDB_MIN(len1, len2); ++i) {
131                 if (got[i] != expected[i]) {
132                         pos = (int)i;
133                         break;
134                 }
135         }
137         fail_unless(pos == -1,
138                         "Serializing hosts to JSON returned unexpected result\n"
139                         "         got: %s\n              %*s\n    expected: %s",
140                         got, pos + 1, "^", expected);
141 } /* verify_json_output */
143 struct {
144         struct {
145                 sdb_store_matcher_t *(*m)(sdb_store_expr_t *, sdb_store_expr_t *);
146                 int field;
147                 sdb_data_t value;
148         } filter;
149         int type;
150         int (*f)(sdb_store_obj_t *, sdb_store_matcher_t *, void *);
151         const char *expected;
152 } store_tojson_data[] = {
153         { { NULL, 0, SDB_DATA_INIT },
154                 SDB_HOST, scan_tojson_full,
155                 "["
156                         "{\"name\": \"h1\", \"last_update\": \"1970-01-01 00:00:01 +0000\", "
157                                 "\"update_interval\": \"0s\", \"backends\": [], "
158                                 "\"attributes\": ["
159                                         "{\"name\": \"k1\", \"value\": \"v1\", "
160                                                 "\"last_update\": \"1970-01-01 00:00:01 +0000\", "
161                                                 "\"update_interval\": \"0s\", \"backends\": []},"
162                                         "{\"name\": \"k2\", \"value\": \"v2\", "
163                                                 "\"last_update\": \"1970-01-01 00:00:02 +0000\", "
164                                                 "\"update_interval\": \"0s\", \"backends\": []},"
165                                         "{\"name\": \"k3\", \"value\": \"v3\", "
166                                                 "\"last_update\": \"1970-01-01 00:00:02 +0000\", "
167                                                 "\"update_interval\": \"0s\", \"backends\": []}"
168                                 "], "
169                                 "\"metrics\": ["
170                                         "{\"name\": \"m1\", "
171                                                 "\"timeseries\": false, "
172                                                 "\"last_update\": \"1970-01-01 00:00:02 +0000\", "
173                                                 "\"update_interval\": \"0s\", \"backends\": [], "
174                                                 "\"attributes\": ["
175                                                         "{\"name\": \"k3\", \"value\": 42, "
176                                                                 "\"last_update\": \"1970-01-01 00:00:02 +0000\", "
177                                                                 "\"update_interval\": \"0s\", \"backends\": []}"
178                                                 "]},"
179                                         "{\"name\": \"m2\", "
180                                                 "\"timeseries\": false, "
181                                                 "\"last_update\": \"1970-01-01 00:00:01 +0000\", "
182                                                 "\"update_interval\": \"0s\", \"backends\": []}"
183                                 "]},"
184                         "{\"name\": \"h2\", \"last_update\": \"1970-01-01 00:00:03 +0000\", "
185                                 "\"update_interval\": \"0s\", \"backends\": [], "
186                                 "\"metrics\": ["
187                                         "{\"name\": \"m1\", "
188                                                 "\"timeseries\": false, "
189                                                 "\"last_update\": \"1970-01-01 00:00:01 +0000\", "
190                                                 "\"update_interval\": \"0s\", \"backends\": []}"
191                                 "], "
192                                 "\"services\": ["
193                                         "{\"name\": \"s1\", "
194                                                 "\"last_update\": \"1970-01-01 00:00:01 +0000\", "
195                                                 "\"update_interval\": \"0s\", \"backends\": []},"
196                                         "{\"name\": \"s2\", "
197                                                 "\"last_update\": \"1970-01-01 00:00:02 +0000\", "
198                                                 "\"update_interval\": \"0s\", \"backends\": [], "
199                                                 "\"attributes\": ["
200                                                         "{\"name\": \"k1\", \"value\": 123, "
201                                                                 "\"last_update\": \"1970-01-01 00:00:02 +0000\", "
202                                                                 "\"update_interval\": \"0s\", \"backends\": []},"
203                                                         "{\"name\": \"k2\", \"value\": 4711, "
204                                                                 "\"last_update\": \"1970-01-01 00:00:01 +0000\", "
205                                                                 "\"update_interval\": \"0s\", \"backends\": []}"
206                                                 "]}"
207                                 "]}"
208                 "]" },
209         { { NULL, 0, SDB_DATA_INIT },
210                 SDB_HOST, scan_tojson,
211                 "["
212                         "{\"name\": \"h1\", \"last_update\": \"1970-01-01 00:00:01 +0000\", "
213                                 "\"update_interval\": \"0s\", \"backends\": []},"
214                         "{\"name\": \"h2\", \"last_update\": \"1970-01-01 00:00:03 +0000\", "
215                                 "\"update_interval\": \"0s\", \"backends\": []}"
216                 "]" },
217         { { sdb_store_eq_matcher, SDB_FIELD_NAME,
218                         { SDB_TYPE_STRING, { .string = "h1" } } },
219                 SDB_HOST, scan_tojson_full,
220                 "["
221                         "{\"name\": \"h1\", \"last_update\": \"1970-01-01 00:00:01 +0000\", "
222                                 "\"update_interval\": \"0s\", \"backends\": []}"
223                 "]" },
224         { { sdb_store_gt_matcher, SDB_FIELD_LAST_UPDATE,
225                         { SDB_TYPE_DATETIME, { .datetime = 1 * SDB_INTERVAL_SECOND } } },
226                 SDB_HOST, scan_tojson_full,
227                 "["
228                         "{\"name\": \"h2\", \"last_update\": \"1970-01-01 00:00:03 +0000\", "
229                                 "\"update_interval\": \"0s\", \"backends\": [], "
230                                 "\"services\": ["
231                                         "{\"name\": \"s2\", "
232                                                 "\"last_update\": \"1970-01-01 00:00:02 +0000\", "
233                                                 "\"update_interval\": \"0s\", \"backends\": [], "
234                                                 "\"attributes\": ["
235                                                         "{\"name\": \"k1\", \"value\": 123, "
236                                                                 "\"last_update\": \"1970-01-01 00:00:02 +0000\", "
237                                                                 "\"update_interval\": \"0s\", \"backends\": []}"
238                                                 "]}"
239                                 "]}"
240                 "]" },
241         { { sdb_store_le_matcher, SDB_FIELD_LAST_UPDATE,
242                         { SDB_TYPE_DATETIME, { .datetime = 1 * SDB_INTERVAL_SECOND } } },
243                 SDB_HOST, scan_tojson_full,
244                 "["
245                         "{\"name\": \"h1\", \"last_update\": \"1970-01-01 00:00:01 +0000\", "
246                                 "\"update_interval\": \"0s\", \"backends\": [], "
247                                 "\"attributes\": ["
248                                         "{\"name\": \"k1\", \"value\": \"v1\", "
249                                                 "\"last_update\": \"1970-01-01 00:00:01 +0000\", "
250                                                 "\"update_interval\": \"0s\", \"backends\": []}"
251                                 "], "
252                                 "\"metrics\": ["
253                                         "{\"name\": \"m2\", "
254                                                 "\"timeseries\": false, "
255                                                 "\"last_update\": \"1970-01-01 00:00:01 +0000\", "
256                                                 "\"update_interval\": \"0s\", \"backends\": []}"
257                                 "]}"
258                 "]" },
259         { { sdb_store_ge_matcher, SDB_FIELD_LAST_UPDATE,
260                         { SDB_TYPE_DATETIME, { .datetime = 3 * SDB_INTERVAL_SECOND } } },
261                 SDB_HOST, scan_tojson_full,
262                 "["
263                         "{\"name\": \"h2\", \"last_update\": \"1970-01-01 00:00:03 +0000\", "
264                                 "\"update_interval\": \"0s\", \"backends\": []}"
265                 "]" },
266         { { sdb_store_lt_matcher, SDB_FIELD_LAST_UPDATE,
267                         { SDB_TYPE_DATETIME, { .datetime = 0 } } },
268                 SDB_HOST, scan_tojson_full,
269                 "[]" },
271         { { NULL, 0, SDB_DATA_INIT },
272                 SDB_SERVICE, scan_tojson_full,
273                 "["
274                         "{\"name\": \"s1\", "
275                                 "\"last_update\": \"1970-01-01 00:00:01 +0000\", "
276                                 "\"update_interval\": \"0s\", \"backends\": []},"
277                         "{\"name\": \"s2\", "
278                                 "\"last_update\": \"1970-01-01 00:00:02 +0000\", "
279                                 "\"update_interval\": \"0s\", \"backends\": [], "
280                                 "\"attributes\": ["
281                                         "{\"name\": \"k1\", \"value\": 123, "
282                                                 "\"last_update\": \"1970-01-01 00:00:02 +0000\", "
283                                                 "\"update_interval\": \"0s\", \"backends\": []},"
284                                         "{\"name\": \"k2\", \"value\": 4711, "
285                                                 "\"last_update\": \"1970-01-01 00:00:01 +0000\", "
286                                                 "\"update_interval\": \"0s\", \"backends\": []}"
287                                 "]}"
288                 "]" },
289         { { NULL, 0, SDB_DATA_INIT },
290                 SDB_SERVICE, scan_tojson,
291                 "["
292                         "{\"name\": \"s1\", "
293                                 "\"last_update\": \"1970-01-01 00:00:01 +0000\", "
294                                 "\"update_interval\": \"0s\", \"backends\": []},"
295                         "{\"name\": \"s2\", "
296                                 "\"last_update\": \"1970-01-01 00:00:02 +0000\", "
297                                 "\"update_interval\": \"0s\", \"backends\": []}"
298                 "]" },
299         { { sdb_store_gt_matcher, SDB_FIELD_LAST_UPDATE,
300                         { SDB_TYPE_DATETIME, { .datetime = 1 * SDB_INTERVAL_SECOND } } },
301                 SDB_SERVICE, scan_tojson_full,
302                 "["
303                         "{\"name\": \"s2\", "
304                                 "\"last_update\": \"1970-01-01 00:00:02 +0000\", "
305                                 "\"update_interval\": \"0s\", \"backends\": [], "
306                                 "\"attributes\": ["
307                                         "{\"name\": \"k1\", \"value\": 123, "
308                                                 "\"last_update\": \"1970-01-01 00:00:02 +0000\", "
309                                                 "\"update_interval\": \"0s\", \"backends\": []}"
310                                 "]}"
311                 "]" },
312         { { sdb_store_lt_matcher, SDB_FIELD_LAST_UPDATE,
313                         { SDB_TYPE_DATETIME, { .datetime = 0 } } },
314                 SDB_SERVICE, scan_tojson_full,
315                 "[]" },
316         { { NULL, 0, SDB_DATA_INIT },
317                 SDB_METRIC, scan_tojson_full,
318                 "["
319                         "{\"name\": \"m1\", "
320                                 "\"timeseries\": false, "
321                                 "\"last_update\": \"1970-01-01 00:00:02 +0000\", "
322                                 "\"update_interval\": \"0s\", \"backends\": [], "
323                                 "\"attributes\": ["
324                                         "{\"name\": \"k3\", \"value\": 42, "
325                                                 "\"last_update\": \"1970-01-01 00:00:02 +0000\", "
326                                                 "\"update_interval\": \"0s\", \"backends\": []}"
327                                 "]},"
328                         "{\"name\": \"m2\", "
329                                 "\"timeseries\": false, "
330                                 "\"last_update\": \"1970-01-01 00:00:01 +0000\", "
331                                 "\"update_interval\": \"0s\", \"backends\": []},"
332                         "{\"name\": \"m1\", "
333                                 "\"timeseries\": false, "
334                                 "\"last_update\": \"1970-01-01 00:00:01 +0000\", "
335                                 "\"update_interval\": \"0s\", \"backends\": []}"
336                 "]" },
337         { { NULL, 0, SDB_DATA_INIT },
338                 SDB_METRIC, scan_tojson,
339                 "["
340                         "{\"name\": \"m1\", "
341                                 "\"timeseries\": false, "
342                                 "\"last_update\": \"1970-01-01 00:00:02 +0000\", "
343                                 "\"update_interval\": \"0s\", \"backends\": []},"
344                         "{\"name\": \"m2\", "
345                                 "\"timeseries\": false, "
346                                 "\"last_update\": \"1970-01-01 00:00:01 +0000\", "
347                                 "\"update_interval\": \"0s\", \"backends\": []},"
348                         "{\"name\": \"m1\", "
349                                 "\"timeseries\": false, "
350                                 "\"last_update\": \"1970-01-01 00:00:01 +0000\", "
351                                 "\"update_interval\": \"0s\", \"backends\": []}"
352                 "]" },
353         { { sdb_store_le_matcher, SDB_FIELD_LAST_UPDATE,
354                         { SDB_TYPE_DATETIME, { .datetime = 1 * SDB_INTERVAL_SECOND } } },
355                 SDB_METRIC, scan_tojson_full,
356                 "["
357                         "{\"name\": \"m2\", "
358                                 "\"timeseries\": false, "
359                                 "\"last_update\": \"1970-01-01 00:00:01 +0000\", "
360                                 "\"update_interval\": \"0s\", \"backends\": []}"
361                 "]" },
362         { { sdb_store_lt_matcher, SDB_FIELD_LAST_UPDATE,
363                         { SDB_TYPE_DATETIME, { .datetime = 0 } } },
364                 SDB_METRIC, scan_tojson_full,
365                 "[]" },
366 };
368 START_TEST(test_store_tojson)
370         sdb_strbuf_t *buf = sdb_strbuf_create(0);
371         sdb_store_matcher_t *filter = NULL;
372         sdb_store_json_formatter_t *f;
373         int status;
375         if (store_tojson_data[_i].filter.m) {
376                 sdb_store_expr_t *field;
377                 sdb_store_expr_t *value;
379                 field = sdb_store_expr_fieldvalue(store_tojson_data[_i].filter.field);
380                 fail_unless(field != NULL,
381                                 "INTERNAL ERROR: sdb_store_expr_fieldvalue() = NULL");
382                 value = sdb_store_expr_constvalue(&store_tojson_data[_i].filter.value);
383                 fail_unless(value != NULL,
384                                 "INTERNAL ERROR: sdb_store_expr_constvalue() = NULL");
386                 filter = store_tojson_data[_i].filter.m(field, value);
387                 fail_unless(filter != NULL,
388                                 "INTERNAL ERROR: sdb_store_*_matcher() = NULL");
390                 sdb_object_deref(SDB_OBJ(field));
391                 sdb_object_deref(SDB_OBJ(value));
392         }
394         sdb_strbuf_clear(buf);
395         f = sdb_store_json_formatter(buf,
396                         store_tojson_data[_i].type, SDB_WANT_ARRAY);
397         ck_assert(f != NULL);
399         status = sdb_store_scan(store, store_tojson_data[_i].type,
400                         /* m = */ NULL, filter, store_tojson_data[_i].f, f);
401         fail_unless(status == 0,
402                         "sdb_store_scan(HOST, ..., tojson) = %d; expected: 0",
403                         status);
404         sdb_store_json_finish(f);
406         verify_json_output(buf, store_tojson_data[_i].expected);
408         sdb_object_deref(SDB_OBJ(filter));
409         sdb_object_deref(SDB_OBJ(f));
410         sdb_strbuf_destroy(buf);
412 END_TEST
414 TEST_MAIN("core::store_json")
416         TCase *tc = tcase_create("core");
417         tcase_add_unchecked_fixture(tc, populate, turndown);
418         TC_ADD_LOOP_TEST(tc, store_tojson);
419         ADD_TCASE(tc);
421 TEST_MAIN_END
423 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */