1 /*
2 * SysDB - t/unit/core/store_lookup_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 "core/store-private.h"
30 #include "frontend/parser.h"
31 #include "libsysdb_test.h"
33 #include <assert.h>
35 #include <check.h>
36 #include <string.h>
38 static void
39 populate(void)
40 {
41 const char *hosts[] = { "a", "b", "c" };
43 struct {
44 const char *host;
45 const char *metric;
46 } metrics[] = {
47 { "a", "m1" },
48 { "b", "m1" },
49 { "b", "m2" },
50 };
52 struct {
53 const char *host;
54 const char *service;
55 } services[] = {
56 { "a", "s1" },
57 { "a", "s2" },
58 { "b", "s1" },
59 { "b", "s3" },
60 };
62 struct {
63 const char *host;
64 const char *name;
65 sdb_data_t value;
66 } attrs[] = {
67 { "a", "k1", { SDB_TYPE_STRING, { .string = "v1" } } },
68 { "a", "k2", { SDB_TYPE_INTEGER, { .integer = 123 } } },
69 { "b", "k1", { SDB_TYPE_STRING, { .string = "v2" } } },
70 };
72 size_t i;
74 for (i = 0; i < SDB_STATIC_ARRAY_LEN(hosts); ++i) {
75 int status = sdb_store_host(hosts[i], 1);
76 fail_unless(status == 0,
77 "sdb_store_host(%s, 1) = %d; expected: 0",
78 hosts[i], status);
79 }
81 for (i = 0; i < SDB_STATIC_ARRAY_LEN(metrics); ++i) {
82 int status = sdb_store_metric(metrics[i].host,
83 metrics[i].metric, /* store */ NULL, 1);
84 fail_unless(status == 0,
85 "sdb_store_metric(%s, %s, NULL, 1) = %d; expected: 0",
86 metrics[i].host, metrics[i].metric, status);
87 }
89 for (i = 0; i < SDB_STATIC_ARRAY_LEN(services); ++i) {
90 int status = sdb_store_service(services[i].host,
91 services[i].service, 1);
92 fail_unless(status == 0,
93 "sdb_store_service(%s, %s, 1) = %d; expected: 0",
94 services[i].host, services[i].service, status);
95 }
97 for (i = 0; i < SDB_STATIC_ARRAY_LEN(attrs); ++i) {
98 int status = sdb_store_attribute(attrs[i].host,
99 attrs[i].name, &attrs[i].value, 1);
100 fail_unless(status == 0,
101 "sdb_store_attribute(%s, %s, <val>, 1) = %d; expected: 0",
102 attrs[i].host, attrs[i].name, status);
103 }
104 } /* populate */
106 START_TEST(test_cmp_name)
107 {
108 sdb_store_obj_t *host;
110 struct {
111 int type;
112 char *name;
113 _Bool re;
115 int expected;
116 } golden_data[] = {
117 { SDB_HOST, "a", 0, 1 },
118 { SDB_HOST, "a", 1, 1 },
119 { SDB_HOST, "b", 0, 0 },
120 { SDB_HOST, "b", 1, 0 },
121 { SDB_HOST, "^a$", 1, 1 },
122 { SDB_HOST, "^b$", 1, 0 },
123 { SDB_HOST, "^a$", 0, 0 },
124 { SDB_HOST, "^b$", 0, 0 },
125 { SDB_METRIC, "m1", 0, 1 },
126 { SDB_METRIC, "m1", 1, 1 },
127 { SDB_METRIC, "^m1$", 1, 1 },
128 { SDB_METRIC, "m", 1, 1 },
129 { SDB_METRIC, "s", 1, 0 },
130 { SDB_METRIC, "m2", 0, 0 },
131 { SDB_METRIC, "x1", 0, 0 },
132 { SDB_METRIC, "x1", 1, 0 },
133 { SDB_SERVICE, "s1", 0, 1 },
134 { SDB_SERVICE, "s2", 0, 1 },
135 { SDB_SERVICE, "s3", 0, 0 },
136 { SDB_SERVICE, "s4", 0, 0 },
137 { SDB_SERVICE, "^s1$", 1, 1 },
138 { SDB_SERVICE, "^s1$", 0, 0 },
139 { SDB_SERVICE, "x1", 0, 0 },
140 { SDB_SERVICE, "x1", 1, 0 },
141 { SDB_SERVICE, "x", 1, 0 },
142 { SDB_ATTRIBUTE, "k1", 0, 1 },
143 { SDB_ATTRIBUTE, "k2", 0, 1 },
144 { SDB_ATTRIBUTE, "k3", 0, 0 },
145 { SDB_ATTRIBUTE, "k4", 0, 0 },
146 { SDB_ATTRIBUTE, "k", 1, 1 },
147 { SDB_ATTRIBUTE, "1", 1, 1 },
148 { SDB_ATTRIBUTE, "3", 1, 0 },
149 };
151 size_t i;
153 host = sdb_store_get_host("a");
154 fail_unless(host != NULL,
155 "sdb_store_get_host(a) = NULL; expected: <host>");
157 for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
158 sdb_data_t datum;
159 sdb_store_expr_t *obj, *value;
160 sdb_store_matcher_t *m, *n;
161 int status;
163 datum.type = SDB_TYPE_STRING;
164 datum.data.string = golden_data[i].name;
166 obj = sdb_store_expr_fieldvalue(SDB_FIELD_NAME);
167 fail_unless(obj != NULL,
168 "sdb_store_expr_fieldvalue(SDB_STORE_NAME) = NULL; "
169 "expected: <expr>");
170 value = sdb_store_expr_constvalue(&datum);
171 fail_unless(value != NULL,
172 "sdb_store_expr_constvalue(%s) = NULL; "
173 "expected: <expr>", golden_data[i].name);
175 if (golden_data[i].re)
176 m = sdb_store_regex_matcher(obj, value);
177 else
178 m = sdb_store_eq_matcher(obj, value);
179 if (golden_data[i].type != SDB_HOST) {
180 sdb_store_matcher_t *tmp;
181 tmp = sdb_store_child_matcher(golden_data[i].type, m);
182 sdb_object_deref(SDB_OBJ(m));
183 m = tmp;
184 }
185 sdb_object_deref(SDB_OBJ(obj));
186 sdb_object_deref(SDB_OBJ(value));
187 fail_unless(m != NULL,
188 "sdb_store_%s_matcher(%s, %s) = NULL; "
189 "expected: <matcher>",
190 golden_data[i].re ? "regex" : "eq",
191 SDB_STORE_TYPE_TO_NAME(golden_data[i].type),
192 golden_data[i].name);
194 status = sdb_store_matcher_matches(m, host, /* filter */ NULL);
195 fail_unless(status == golden_data[i].expected,
196 "sdb_store_matcher_matches(%s->%s, <host a>, NULL) = %d; "
197 "expected: %d", SDB_STORE_TYPE_TO_NAME(golden_data[i].type),
198 golden_data[i].name, status, golden_data[i].expected);
200 n = sdb_store_inv_matcher(m);
201 fail_unless(n != NULL,
202 "sdb_store_inv_matcher() = NULL; expected: <matcher>");
203 sdb_object_deref(SDB_OBJ(m));
205 /* now match the inverted set of objects */
206 status = sdb_store_matcher_matches(n, host, /* filter */ NULL);
207 fail_unless(status == !golden_data[i].expected,
208 "sdb_store_matcher_matches(%s->%s, <host a>, NULL) = %d; "
209 "expected: %d", SDB_STORE_TYPE_TO_NAME(golden_data[i].type),
210 golden_data[i].name, status, !golden_data[i].expected);
212 sdb_object_deref(SDB_OBJ(n));
213 }
215 sdb_object_deref(SDB_OBJ(host));
216 }
217 END_TEST
219 START_TEST(test_cmp_attr)
220 {
221 sdb_store_obj_t *host;
223 struct {
224 const char *attr;
225 const sdb_data_t value;
226 int expected_lt, expected_le, expected_eq, expected_ge, expected_gt;
227 } golden_data[] = {
228 { "k1", { SDB_TYPE_STRING, { .string = "v1" } }, 0, 1, 1, 1, 0 },
229 { "k1", { SDB_TYPE_STRING, { .string = "v2" } }, 1, 1, 0, 0, 0 },
230 { "k1", { SDB_TYPE_STRING, { .string = "v0" } }, 0, 0, 0, 1, 1 },
231 { "k1", { SDB_TYPE_STRING, { .string = "0" } }, 0, 0, 0, 1, 1 },
232 { "k2", { SDB_TYPE_INTEGER, { .integer = 123 } }, 0, 1, 1, 1, 0 },
233 { "k2", { SDB_TYPE_INTEGER, { .integer = 124 } }, 1, 1, 0, 0, 0 },
234 { "k2", { SDB_TYPE_INTEGER, { .integer = 122 } }, 0, 0, 0, 1, 1 },
235 /* key does not exist */
236 { "k3", { SDB_TYPE_STRING, { .string = "v1" } }, 0, 0, 0, 0, 0 },
237 { "k3", { SDB_TYPE_STRING, { .string = "123" } }, 0, 0, 0, 0, 0 },
238 { "k3", { SDB_TYPE_INTEGER, { .integer = 123 } }, 0, 0, 0, 0, 0 },
239 /* type mismatch */
240 { "k1", { SDB_TYPE_INTEGER, { .integer = 0 } }, 0, 0, 0, 1, 1 },
241 { "k2", { SDB_TYPE_STRING, { .string = "123" } }, 0, 1, 1, 1, 0 },
242 };
244 int status;
245 size_t i;
247 host = sdb_store_get_host("a");
248 fail_unless(host != NULL,
249 "sdb_store_get_host(a) = NULL; expected: <host>");
251 for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
252 sdb_store_expr_t *attr;
253 sdb_store_expr_t *value;
254 char value_str[1024];
255 size_t j;
257 struct {
258 sdb_store_matcher_t *(*matcher)(sdb_store_expr_t *,
259 sdb_store_expr_t *);
260 int expected;
261 } tests[] = {
262 { sdb_store_lt_matcher, golden_data[i].expected_lt },
263 { sdb_store_le_matcher, golden_data[i].expected_le },
264 { sdb_store_eq_matcher, golden_data[i].expected_eq },
265 { sdb_store_ge_matcher, golden_data[i].expected_ge },
266 { sdb_store_gt_matcher, golden_data[i].expected_gt },
267 };
269 sdb_data_format(&golden_data[i].value,
270 value_str, sizeof(value_str), SDB_UNQUOTED);
272 attr = sdb_store_expr_attrvalue(golden_data[i].attr);
273 fail_unless(attr != NULL,
274 "sdb_store_expr_attrvalue(%s) = NULL; expected: <expr>",
275 golden_data[i].attr);
277 value = sdb_store_expr_constvalue(&golden_data[i].value);
278 fail_unless(value != NULL,
279 "sdb_store_expr_constvalue(%s) = NULL; expected: <expr>",
280 value_str);
282 for (j = 0; j < SDB_STATIC_ARRAY_LEN(tests); ++j) {
283 sdb_store_matcher_t *m;
285 m = tests[j].matcher(attr, value);
286 fail_unless(m != NULL,
287 "sdb_store_<cond>_matcher() = NULL; expected: <matcher>");
289 status = sdb_store_matcher_matches(m, host, /* filter */ NULL);
290 fail_unless(status == tests[j].expected,
291 "sdb_store_matcher_matches(<m>, <host>, NULL) = %d; "
292 "expected: %d", status, tests[j].expected);
294 sdb_object_deref(SDB_OBJ(m));
295 }
297 sdb_object_deref(SDB_OBJ(attr));
298 sdb_object_deref(SDB_OBJ(value));
299 }
301 sdb_object_deref(SDB_OBJ(host));
302 }
303 END_TEST
305 START_TEST(test_cmp_obj)
306 {
307 struct {
308 const char *host;
309 int field;
310 const sdb_data_t value;
311 int expected_lt, expected_le, expected_eq, expected_ge, expected_gt;
312 } golden_data[] = {
313 { "b", SDB_FIELD_NAME,
314 { SDB_TYPE_STRING, { .string = "a" } }, 0, 0, 0, 1, 1 },
315 { "b", SDB_FIELD_NAME,
316 { SDB_TYPE_STRING, { .string = "b" } }, 0, 1, 1, 1, 0 },
317 { "b", SDB_FIELD_NAME,
318 { SDB_TYPE_STRING, { .string = "c" } }, 1, 1, 0, 0, 0 },
319 /* last-update = 1 for all objects */
320 { "a", SDB_FIELD_LAST_UPDATE,
321 { SDB_TYPE_DATETIME, { .datetime = 1 } }, 0, 1, 1, 1, 0 },
322 { "a", SDB_FIELD_LAST_UPDATE,
323 { SDB_TYPE_DATETIME, { .datetime = 2 } }, 1, 1, 0, 0, 0 },
324 { "a", SDB_FIELD_LAST_UPDATE,
325 { SDB_TYPE_DATETIME, { .datetime = 0 } }, 0, 0, 0, 1, 1 },
326 /* age > 0 for all objects */
327 { "a", SDB_FIELD_AGE,
328 { SDB_TYPE_DATETIME, { .datetime = 0 } }, 0, 0, 0, 1, 1 },
329 /* interval = 0 for all objects */
330 { "a", SDB_FIELD_INTERVAL,
331 { SDB_TYPE_DATETIME, { .datetime = 0 } }, 0, 1, 1, 1, 0 },
332 { "a", SDB_FIELD_INTERVAL,
333 { SDB_TYPE_DATETIME, { .datetime = 1 } }, 1, 1, 0, 0, 0 },
334 /* type mismatch */
335 /* TODO: let matchers only use data_strcmp for attributes
336 * everything else has a well-known type
337 { "a", SDB_FIELD_LAST_UPDATE,
338 { SDB_TYPE_INTEGER, { .integer = 0 } }, 0, 0, 0, 0, 0 },
339 { "a", SDB_FIELD_AGE,
340 { SDB_TYPE_INTEGER, { .integer = 0 } }, 0, 0, 0, 0, 0 },
341 { "a", SDB_FIELD_INTERVAL,
342 { SDB_TYPE_INTEGER, { .integer = 0 } }, 0, 0, 0, 0, 0 },
343 { "a", SDB_FIELD_BACKEND,
344 { SDB_TYPE_INTEGER, { .integer = 0 } }, 0, 0, 0, 0, 0 },
345 { "a", SDB_FIELD_BACKEND,
346 { SDB_TYPE_INTEGER, { .integer = 0 } }, 0, 0, 0, 0, 0 },
347 */
348 /* (64bit) integer value without zero-bytes */
349 /*
350 { "a", SDB_FIELD_BACKEND,
351 { SDB_TYPE_INTEGER, { .integer = 0xffffffffffffffffL } },
352 0, 0, 0, 0, 0 },
353 */
354 };
356 int status;
357 size_t i;
359 for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
360 sdb_store_obj_t *host;
361 sdb_store_expr_t *field;
362 sdb_store_expr_t *value;
363 char value_str[1024];
364 size_t j;
366 struct {
367 sdb_store_matcher_t *(*matcher)(sdb_store_expr_t *,
368 sdb_store_expr_t *);
369 int expected;
370 } tests[] = {
371 { sdb_store_lt_matcher, golden_data[i].expected_lt },
372 { sdb_store_le_matcher, golden_data[i].expected_le },
373 { sdb_store_eq_matcher, golden_data[i].expected_eq },
374 { sdb_store_ge_matcher, golden_data[i].expected_ge },
375 { sdb_store_gt_matcher, golden_data[i].expected_gt },
376 };
377 char *op_str[] = { "<", "<=", "=", ">=", ">" };
379 assert(SDB_STATIC_ARRAY_LEN(tests) == SDB_STATIC_ARRAY_LEN(op_str));
381 host = sdb_store_get_host(golden_data[i].host);
382 fail_unless(host != NULL,
383 "sdb_store_get_host(%s) = NULL; expected: <host>",
384 golden_data[i].host);
386 sdb_data_format(&golden_data[i].value,
387 value_str, sizeof(value_str), SDB_UNQUOTED);
389 field = sdb_store_expr_fieldvalue(golden_data[i].field);
390 fail_unless(field != NULL,
391 "sdb_store_expr_fieldvalue(%d) = NULL; "
392 "expected: <expr>", golden_data[i].field);
394 value = sdb_store_expr_constvalue(&golden_data[i].value);
395 fail_unless(value != NULL,
396 "sdb_store_expr_constvalue(%s) = NULL; "
397 "expected: <expr>", value_str);
399 for (j = 0; j < SDB_STATIC_ARRAY_LEN(tests); ++j) {
400 char m_str[1024];
401 sdb_store_matcher_t *m;
403 snprintf(m_str, sizeof(m_str), "%s %s %s",
404 SDB_FIELD_TO_NAME(golden_data[i].field),
405 op_str[j], value_str);
407 m = tests[j].matcher(field, value);
408 fail_unless(m != NULL,
409 "sdb_store_<cond>_matcher() = NULL; expected: <matcher>");
411 status = sdb_store_matcher_matches(m, host, /* filter */ NULL);
412 fail_unless(status == tests[j].expected,
413 "sdb_store_matcher_matches(<%s>, <host '%s'>, NULL) = %d; "
414 "expected: %d", m_str, golden_data[i].host, status,
415 tests[j].expected);
417 sdb_object_deref(SDB_OBJ(m));
418 }
420 sdb_object_deref(SDB_OBJ(field));
421 sdb_object_deref(SDB_OBJ(value));
422 sdb_object_deref(SDB_OBJ(host));
423 }
424 }
425 END_TEST
427 START_TEST(test_store_match_op)
428 {
429 sdb_store_obj_t *obj;
431 sdb_data_t d = { SDB_TYPE_STRING, { .string = "a" } };
432 sdb_store_expr_t *e = sdb_store_expr_constvalue(&d);
434 sdb_store_matcher_t *always = sdb_store_isnnull_matcher(e);
435 sdb_store_matcher_t *never = sdb_store_isnull_matcher(e);
437 struct {
438 const char *op;
439 sdb_store_matcher_t *left;
440 sdb_store_matcher_t *right;
441 int expected;
442 } golden_data[] = {
443 { "OR", always, always, 1 },
444 { "OR", always, never, 1 },
445 { "OR", never, always, 1 },
446 { "OR", never, never, 0 },
447 { "AND", always, always, 1 },
448 { "AND", always, never, 0 },
449 { "AND", never, always, 0 },
450 { "AND", never, never, 0 },
451 };
453 int status;
454 size_t i;
456 obj = sdb_store_get_host("a");
458 status = sdb_store_matcher_matches(always, obj, /* filter */ NULL);
459 fail_unless(status == 1,
460 "INTERNAL ERROR: 'always' did not match host");
461 status = sdb_store_matcher_matches(never, obj, /* filter */ NULL);
462 fail_unless(status == 0,
463 "INTERNAL ERROR: 'never' matches host");
465 for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
466 sdb_store_matcher_t *m;
468 if (! strcmp(golden_data[i].op, "OR"))
469 m = sdb_store_dis_matcher(golden_data[i].left,
470 golden_data[i].right);
471 else if (! strcmp(golden_data[i].op, "AND"))
472 m = sdb_store_con_matcher(golden_data[i].left,
473 golden_data[i].right);
474 else {
475 fail("INTERNAL ERROR: unexpected operator %s", golden_data[i].op);
476 continue;
477 }
479 #define TO_NAME(v) (((v) == always) ? "always" \
480 : ((v) == never) ? "never" : "<unknown>")
482 status = sdb_store_matcher_matches(m, obj, /* filter */ NULL);
483 fail_unless(status == golden_data[i].expected,
484 "%s(%s, %s, NULL) = %d; expected: %d", golden_data[i].op,
485 TO_NAME(golden_data[i].left), TO_NAME(golden_data[i].right),
486 status, golden_data[i].expected);
488 #undef TO_NAME
490 sdb_object_deref(SDB_OBJ(m));
491 }
493 sdb_object_deref(SDB_OBJ(always));
494 sdb_object_deref(SDB_OBJ(never));
495 sdb_object_deref(SDB_OBJ(e));
497 sdb_object_deref(SDB_OBJ(obj));
498 }
499 END_TEST
501 START_TEST(test_parse_cmp)
502 {
503 sdb_data_t hostname = { SDB_TYPE_STRING, { .string = "hostname" } };
504 sdb_data_t metricname = { SDB_TYPE_STRING, { .string = "metricname" } };
505 sdb_data_t srvname = { SDB_TYPE_STRING, { .string = "srvname" } };
506 sdb_data_t attrname = { SDB_TYPE_STRING, { .string = "attrname" } };
508 sdb_store_matcher_t *check;
510 size_t i;
512 struct {
513 const char *obj_type;
514 const char *op;
515 const sdb_data_t *value;
516 int expected;
517 } golden_data[] = {
518 { "host", "=", &hostname, MATCHER_NAME },
519 { "host", "!=", &hostname, MATCHER_NOT },
520 { "host", "=~", &hostname, MATCHER_NAME },
521 { "host", "!~", &hostname, MATCHER_NOT },
522 { "host", "&^", &hostname, -1 },
523 { "host", "<", &hostname, -1 },
524 { "host", "<=", &hostname, -1 },
525 { "host", ">=", &hostname, -1 },
526 { "host", ">", &hostname, -1 },
527 { "host", "=", NULL, -1 },
528 { "metric", "=", &metricname, MATCHER_NAME },
529 { "metric", "!=", &metricname, MATCHER_NOT },
530 { "metric", "=~", &metricname, MATCHER_NAME },
531 { "metric", "!~", &metricname, MATCHER_NOT },
532 { "metric", "&^", &metricname, -1 },
533 { "metric", "<", &metricname, -1 },
534 { "metric", "<=", &metricname, -1 },
535 { "metric", ">=", &metricname, -1 },
536 { "metric", ">", &metricname, -1 },
537 { "metric", "=", NULL, -1 },
538 { "service", "=", &srvname, MATCHER_NAME },
539 { "service", "!=", &srvname, MATCHER_NOT },
540 { "service", "=~", &srvname, MATCHER_NAME },
541 { "service", "!~", &srvname, MATCHER_NOT },
542 { "service", "&^", &srvname, -1 },
543 { "service", "<", &srvname, -1 },
544 { "service", "<=", &srvname, -1 },
545 { "service", ">=", &srvname, -1 },
546 { "service", ">", &srvname, -1 },
547 { "service", "=", NULL, -1 },
548 { "attribute", "=", &attrname, MATCHER_NAME },
549 { "attribute", "!=", &attrname, MATCHER_NOT },
550 { "attribute", "=~", &attrname, MATCHER_NAME },
551 { "attribute", "!~", &attrname, MATCHER_NOT },
552 { "attribute", "<", &attrname, -1 },
553 { "attribute", "<=", &attrname, -1 },
554 { "attribute", ">=", &attrname, -1 },
555 { "attribute", ">", &attrname, -1 },
556 { "attribute", "=", NULL, -1 },
557 { "foo", "=", &attrname, -1 },
558 };
560 for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
561 sdb_store_expr_t *expr;
562 char buf[1024];
564 if (sdb_data_format(golden_data[i].value,
565 buf, sizeof(buf), SDB_UNQUOTED) < 0)
566 snprintf(buf, sizeof(buf), "ERR");
568 expr = sdb_store_expr_constvalue(golden_data[i].value);
569 fail_unless(expr != NULL || golden_data[i].value == NULL,
570 "sdb_store_expr_constvalue(%s) = NULL; expected: <expr>",
571 buf);
573 check = sdb_store_matcher_parse_cmp(golden_data[i].obj_type,
574 golden_data[i].op, expr);
575 sdb_object_deref(SDB_OBJ(expr));
577 if (golden_data[i].expected == -1) {
578 fail_unless(check == NULL,
579 "sdb_store_matcher_parse_cmp(%s, %s, expr{%s}) = %p; "
580 "expected: NULL", golden_data[i].obj_type,
581 golden_data[i].op, buf, check);
582 continue;
583 }
585 fail_unless(check != NULL,
586 "sdb_store_matcher_parse_cmp(%s, %s, %s) = %p; "
587 "expected: <expr>", golden_data[i].obj_type,
588 golden_data[i].op, buf, check);
589 fail_unless(M(check)->type == golden_data[i].expected,
590 "sdb_store_matcher_parse_cmp(%s, %s, %s) returned matcher "
591 "of type %d; expected: %d", golden_data[i].obj_type,
592 golden_data[i].op, buf, M(check)->type, golden_data[i].expected);
594 sdb_object_deref(SDB_OBJ(check));
595 }
596 }
597 END_TEST
599 static int
600 scan_cb(sdb_store_obj_t *obj, void *user_data)
601 {
602 int *i = user_data;
604 fail_unless(obj != NULL,
605 "sdb_store_scan callback received NULL obj; expected: "
606 "<store base obj>");
607 fail_unless(i != NULL,
608 "sdb_store_scan callback received NULL user_data; "
609 "expected: <pointer to data>");
611 ++(*i);
612 return 0;
613 } /* scan_cb */
615 START_TEST(test_scan)
616 {
617 struct {
618 const char *query;
619 const char *filter;
620 int expected;
621 } golden_data[] = {
622 /* TODO: check the name of the expected hosts */
623 { "host = 'a'", NULL, 1 },
624 { "host = 'a'", "host = 'x'", 0 }, /* filter never matches */
625 { "host = 'a'",
626 "NOT attribute['x'] = ''", 1 }, /* filter always matches */
627 { "host =~ 'a|b'", NULL, 2 },
628 { "host =~ 'host'", NULL, 0 },
629 { "host =~ '.'", NULL, 3 },
630 { "metric = 'm1'", NULL, 2 },
631 { "metric= 'm1'", "host = 'x'", 0 }, /* filter never matches */
632 { "metric = 'm1'",
633 "NOT attribute['x'] = ''", 2 }, /* filter always matches */
634 { "metric =~ 'm'", NULL, 2 },
635 { "metric !~ 'm'", NULL, 1 },
636 { "metric =~ 'x'", NULL, 0 },
637 { "service = 's1'", NULL, 2 },
638 { "service = 's1'", "host = 'x'", 0 }, /* filter never matches */
639 { "service = 's1'",
640 "NOT attribute['x'] = ''", 2 }, /* filter always matches */
641 { "service =~ 's'", NULL, 2 },
642 { "service !~ 's'", NULL, 1 },
643 { "attribute = 'k1'", NULL, 2 },
644 { "attribute = 'k1'", "host = 'x'", 0 }, /* filter never matches */
645 { "attribute = 'k1'",
646 "NOT attribute['x'] = ''", 2 }, /* filter always matches */
647 { "attribute =~ 'k'", NULL, 2 },
648 { "attribute =~ '1'", NULL, 2 },
649 { "attribute =~ '2'", NULL, 1 },
650 { "attribute = 'x'", NULL, 0 },
651 { "attribute =~ 'x'", NULL, 0 },
652 { "attribute['k1'] = 'v1'", NULL, 1 },
653 { "attribute['k1'] =~ 'v1'", NULL, 1 },
654 { "attribute['k1'] =~ '^v1$'", NULL, 1 },
655 { "attribute['k1'] =~ 'v'", NULL, 2 },
656 { "attribute['k1'] =~ '1'", NULL, 1 },
657 { "attribute['k1'] !~ 'v'", NULL, 1 },
658 { "attribute['k1'] = 'v2'", NULL, 1 },
659 { "attribute['k1'] =~ 'v2'", NULL, 1 },
660 { "attribute['x1'] =~ 'v'", NULL, 0 },
661 { "attribute['x1'] =~ 'NULL'", NULL, 0 },
662 { "attribute['x1'] !~ 'v'", NULL, 3 },
663 { "attribute['k1'] IS NULL", NULL, 1 },
664 { "attribute['x1'] IS NULL", NULL, 3 },
665 { "attribute['k1'] IS NOT NULL", NULL, 2 },
666 { "attribute['x1'] IS NOT NULL", NULL, 0 },
667 { "attribute['k2'] < 123", NULL, 0 },
668 { "attribute['k2'] <= 123", NULL, 1 },
669 { "attribute['k2'] >= 123", NULL, 1 },
670 { "attribute['k2'] > 123", NULL, 0 },
671 { "attribute['k2'] = 123", NULL, 1 },
672 { "attribute['k2'] != 123", NULL, 0 },
673 { "attribute['k1'] != 'v1'", NULL, 1 },
674 { "attribute['k1'] != 'v2'", NULL, 1 },
675 { "attribute != 'x' "
676 "AND attribute['y'] !~ 'x'", NULL, 3 },
677 };
679 int check, n;
680 size_t i;
682 n = 0;
683 check = sdb_store_scan(/* matcher */ NULL, /* filter */ NULL,
684 scan_cb, &n);
685 fail_unless(check == 0,
686 "sdb_store_scan() = %d; expected: 0", check);
687 fail_unless(n == 3,
688 "sdb_store_scan called callback %d times; expected: 3", (int)n);
690 for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
691 sdb_store_matcher_t *m, *filter = NULL;
693 m = sdb_fe_parse_matcher(golden_data[i].query, -1);
694 fail_unless(m != NULL,
695 "sdb_fe_parse_matcher(%s, -1) = NULL; expected: <matcher>",
696 golden_data[i].query);
698 if (golden_data[i].filter) {
699 filter = sdb_fe_parse_matcher(golden_data[i].filter, -1);
700 fail_unless(filter != NULL,
701 "sdb_fe_parse_matcher(%s, -1) = NULL; "
702 "expected: <matcher>", golden_data[i].filter);
703 }
705 n = 0;
706 sdb_store_scan(m, filter, scan_cb, &n);
707 fail_unless(n == golden_data[i].expected,
708 "sdb_store_scan(matcher{%s}, filter{%s}) found %d hosts; "
709 "expected: %d", golden_data[i].query, golden_data[i].filter,
710 n, golden_data[i].expected);
711 sdb_object_deref(SDB_OBJ(filter));
712 sdb_object_deref(SDB_OBJ(m));
713 }
714 }
715 END_TEST
717 Suite *
718 core_store_lookup_suite(void)
719 {
720 Suite *s = suite_create("core::store_lookup");
721 TCase *tc;
723 tc = tcase_create("core");
724 tcase_add_checked_fixture(tc, populate, sdb_store_clear);
725 tcase_add_test(tc, test_cmp_name);
726 tcase_add_test(tc, test_cmp_attr);
727 tcase_add_test(tc, test_cmp_obj);
728 tcase_add_test(tc, test_store_match_op);
729 tcase_add_test(tc, test_parse_cmp);
730 tcase_add_test(tc, test_scan);
731 suite_add_tcase(s, tc);
733 return s;
734 } /* core_store_lookup_suite */
736 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */