Code

store: All store functions now accept a store object.
[sysdb.git] / t / unit / core / store_lookup_test.c
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 #if HAVE_CONFIG_H
29 #       include "config.h"
30 #endif
32 #include "core/plugin.h"
33 #include "core/store.h"
34 #include "core/store-private.h"
35 #include "parser/parser.h"
36 #include "testutils.h"
38 #include <check.h>
39 #include <string.h>
41 static sdb_store_t *store;
43 static void
44 populate(void)
45 {
46         const char *hosts[] = { "a", "b", "c" };
48         struct {
49                 const char *host;
50                 const char *metric;
51         } metrics[] = {
52                 { "a", "m1" },
53                 { "b", "m1" },
54                 { "b", "m2" },
55         };
57         struct {
58                 const char *host;
59                 const char *service;
60         } services[] = {
61                 { "a", "s1" },
62                 { "a", "s2" },
63                 { "b", "s1" },
64                 { "b", "s3" },
65         };
67         struct {
68                 const char *host;
69                 const char *name;
70                 sdb_data_t  value;
71         } attrs[] = {
72                 { "a", "k1", { SDB_TYPE_STRING, { .string = "v1" } } },
73                 { "a", "k2", { SDB_TYPE_INTEGER, { .integer = 123 } } },
74                 { "b", "k1", { SDB_TYPE_STRING, { .string = "v2" } } },
75         };
77         size_t i;
79         store = sdb_store_create();
80         ck_assert(store != NULL);
82         for (i = 0; i < SDB_STATIC_ARRAY_LEN(hosts); ++i) {
83                 int status = sdb_store_host(store, hosts[i], 1);
84                 fail_unless(status == 0,
85                                 "sdb_store_host(%s, 1) = %d; expected: 0",
86                                 hosts[i], status);
87         }
89         for (i = 0; i < SDB_STATIC_ARRAY_LEN(metrics); ++i) {
90                 int status = sdb_store_metric(store, metrics[i].host,
91                                 metrics[i].metric, /* store */ NULL, 1);
92                 fail_unless(status == 0,
93                                 "sdb_store_metric(%s, %s, NULL, 1) = %d; expected: 0",
94                                 metrics[i].host, metrics[i].metric, status);
95         }
97         for (i = 0; i < SDB_STATIC_ARRAY_LEN(services); ++i) {
98                 int status = sdb_store_service(store, services[i].host,
99                                 services[i].service, 1);
100                 fail_unless(status == 0,
101                                 "sdb_store_service(%s, %s, 1) = %d; expected: 0",
102                                 services[i].host, services[i].service, status);
103         }
105         for (i = 0; i < SDB_STATIC_ARRAY_LEN(attrs); ++i) {
106                 int status = sdb_store_attribute(store, attrs[i].host,
107                                 attrs[i].name, &attrs[i].value, 1);
108                 fail_unless(status == 0,
109                                 "sdb_store_attribute(%s, %s, <val>, 1) = %d; expected: 0",
110                                 attrs[i].host, attrs[i].name, status);
111         }
112 } /* populate */
114 static void
115 turndown(void)
117         sdb_object_deref(SDB_OBJ(store));
118         store = NULL;
119 } /* turndown */
121 struct {
122         int type;
123         char *name;
124         _Bool re;
126         int expected;
127 } cmp_name_data[] = {
128         { SDB_HOST,      "a",    0, 1 },
129         { SDB_HOST,      "a",    1, 1 },
130         { SDB_HOST,      "b",    0, 0 },
131         { SDB_HOST,      "b",    1, 0 },
132         { SDB_HOST,      "^a$",  1, 1 },
133         { SDB_HOST,      "^b$",  1, 0 },
134         { SDB_HOST,      "^a$",  0, 0 },
135         { SDB_HOST,      "^b$",  0, 0 },
136         { SDB_METRIC,    "m1",   0, 1 },
137         { SDB_METRIC,    "m1",   1, 1 },
138         { SDB_METRIC,    "^m1$", 1, 1 },
139         { SDB_METRIC,    "m",    1, 1 },
140         { SDB_METRIC,    "s",    1, 0 },
141         { SDB_METRIC,    "m2",   0, 0 },
142         { SDB_METRIC,    "x1",   0, 0 },
143         { SDB_METRIC,    "x1",   1, 0 },
144         { SDB_SERVICE,   "s1",   0, 1 },
145         { SDB_SERVICE,   "s2",   0, 1 },
146         { SDB_SERVICE,   "s3",   0, 0 },
147         { SDB_SERVICE,   "s4",   0, 0 },
148         { SDB_SERVICE,   "^s1$", 1, 1 },
149         { SDB_SERVICE,   "^s1$", 0, 0 },
150         { SDB_SERVICE,   "x1",   0, 0 },
151         { SDB_SERVICE,   "x1",   1, 0 },
152         { SDB_SERVICE,   "x",    1, 0 },
153         { SDB_ATTRIBUTE, "k1",   0, 1 },
154         { SDB_ATTRIBUTE, "k2",   0, 1 },
155         { SDB_ATTRIBUTE, "k3",   0, 0 },
156         { SDB_ATTRIBUTE, "k4",   0, 0 },
157         { SDB_ATTRIBUTE, "k",    1, 1 },
158         { SDB_ATTRIBUTE, "1",    1, 1 },
159         { SDB_ATTRIBUTE, "3",    1, 0 },
160 };
162 START_TEST(test_cmp_name)
164         sdb_store_obj_t *host;
165         sdb_data_t datum;
166         sdb_store_expr_t *obj = NULL, *value;
167         sdb_store_matcher_t *m, *n;
168         int status;
170         host = sdb_store_get_host(store, "a");
171         fail_unless(host != NULL,
172                         "sdb_store_get_host(a) = NULL; expected: <host>");
174         datum.type = SDB_TYPE_STRING;
175         datum.data.string = cmp_name_data[_i].name;
177         if (cmp_name_data[_i].type == SDB_HOST) {
178                 obj = sdb_store_expr_fieldvalue(SDB_FIELD_NAME);
179                 fail_unless(obj != NULL,
180                                 "sdb_store_expr_fieldvalue(SDB_STORE_NAME) = NULL; "
181                                 "expected: <expr>");
182         }
183         value = sdb_store_expr_constvalue(&datum);
184         fail_unless(value != NULL,
185                         "sdb_store_expr_constvalue(%s) = NULL; "
186                         "expected: <expr>", cmp_name_data[_i].name);
188         if (cmp_name_data[_i].re)
189                 m = sdb_store_regex_matcher(obj, value);
190         else
191                 m = sdb_store_eq_matcher(obj, value);
193         if (cmp_name_data[_i].type != SDB_HOST) {
194                 sdb_store_expr_t *iter;
195                 sdb_store_matcher_t *tmp;
196                 obj = sdb_store_expr_fieldvalue(SDB_FIELD_NAME);
197                 iter = sdb_store_expr_typed(cmp_name_data[_i].type, obj);
198                 tmp = sdb_store_any_matcher(iter, m);
199                 ck_assert(iter && m);
200                 sdb_object_deref(SDB_OBJ(iter));
201                 sdb_object_deref(SDB_OBJ(m));
202                 m = tmp;
203         }
204         sdb_object_deref(SDB_OBJ(obj));
205         sdb_object_deref(SDB_OBJ(value));
206         fail_unless(m != NULL,
207                         "sdb_store_%s_matcher(%s, %s) = NULL; "
208                         "expected: <matcher>",
209                         cmp_name_data[_i].re ? "regex" : "eq",
210                         SDB_STORE_TYPE_TO_NAME(cmp_name_data[_i].type),
211                         cmp_name_data[_i].name);
213         status = sdb_store_matcher_matches(m, host, /* filter */ NULL);
214         fail_unless(status == cmp_name_data[_i].expected,
215                         "sdb_store_matcher_matches(%s->%s, <host a>, NULL) = %d; "
216                         "expected: %d", SDB_STORE_TYPE_TO_NAME(cmp_name_data[_i].type),
217                         cmp_name_data[_i].name, status, cmp_name_data[_i].expected);
219         n = sdb_store_inv_matcher(m);
220         fail_unless(n != NULL,
221                         "sdb_store_inv_matcher() = NULL; expected: <matcher>");
222         sdb_object_deref(SDB_OBJ(m));
224         /* now match the inverted set of objects */
225         status = sdb_store_matcher_matches(n, host, /* filter */ NULL);
226         fail_unless(status == !cmp_name_data[_i].expected,
227                         "sdb_store_matcher_matches(%s->%s, <host a>, NULL) = %d; "
228                         "expected: %d", SDB_STORE_TYPE_TO_NAME(cmp_name_data[_i].type),
229                         cmp_name_data[_i].name, status, !cmp_name_data[_i].expected);
231         sdb_object_deref(SDB_OBJ(n));
232         sdb_object_deref(SDB_OBJ(host));
234 END_TEST
236 struct {
237         const char *attr;
238         const sdb_data_t value;
239         int expected_lt, expected_le, expected_eq, expected_ge, expected_gt;
240 } cmp_attr_data[] = {
241         { "k1", { SDB_TYPE_STRING,  { .string  = "v1" } },  0, 1, 1, 1, 0 },
242         { "k1", { SDB_TYPE_STRING,  { .string  = "v2" } },  1, 1, 0, 0, 0 },
243         { "k1", { SDB_TYPE_STRING,  { .string  = "v0" } },  0, 0, 0, 1, 1 },
244         { "k1", { SDB_TYPE_STRING,  { .string  = "0" } },   0, 0, 0, 1, 1 },
245         { "k2", { SDB_TYPE_INTEGER, { .integer = 123 } },   0, 1, 1, 1, 0 },
246         { "k2", { SDB_TYPE_INTEGER, { .integer = 124 } },   1, 1, 0, 0, 0 },
247         { "k2", { SDB_TYPE_INTEGER, { .integer = 122 } },   0, 0, 0, 1, 1 },
248         /* key does not exist */
249         { "k3", { SDB_TYPE_STRING,  { .string  = "v1" } },  0, 0, 0, 0, 0 },
250         { "k3", { SDB_TYPE_STRING,  { .string  = "123" } }, 0, 0, 0, 0, 0 },
251         { "k3", { SDB_TYPE_INTEGER, { .integer = 123 } },   0, 0, 0, 0, 0 },
252         /* type mismatch */
253         { "k1", { SDB_TYPE_INTEGER, { .integer = 0 } },     0, 0, 0, 1, 1 },
254         { "k2", { SDB_TYPE_STRING,  { .string  = "123" } }, 0, 1, 1, 1, 0 },
255 };
257 START_TEST(test_cmp_attr)
259         sdb_store_obj_t *host;
260         sdb_store_expr_t *attr;
261         sdb_store_expr_t *value;
262         char value_str[1024];
263         int status;
264         size_t j;
266         struct {
267                 sdb_store_matcher_t *(*matcher)(sdb_store_expr_t *,
268                                 sdb_store_expr_t *);
269                 int expected;
270         } tests[] = {
271                 { sdb_store_lt_matcher, cmp_attr_data[_i].expected_lt },
272                 { sdb_store_le_matcher, cmp_attr_data[_i].expected_le },
273                 { sdb_store_eq_matcher, cmp_attr_data[_i].expected_eq },
274                 { sdb_store_ge_matcher, cmp_attr_data[_i].expected_ge },
275                 { sdb_store_gt_matcher, cmp_attr_data[_i].expected_gt },
276         };
278         const char *op_str[] = { "<", "<=", "=", ">=", ">" };
279         ck_assert(SDB_STATIC_ARRAY_LEN(tests) == SDB_STATIC_ARRAY_LEN(op_str));
281         host = sdb_store_get_host(store, "a");
282         fail_unless(host != NULL,
283                         "sdb_store_get_host(a) = NULL; expected: <host>");
285         sdb_data_format(&cmp_attr_data[_i].value,
286                         value_str, sizeof(value_str), SDB_UNQUOTED);
288         attr = sdb_store_expr_attrvalue(cmp_attr_data[_i].attr);
289         fail_unless(attr != NULL,
290                         "sdb_store_expr_attrvalue(%s) = NULL; expected: <expr>",
291                         cmp_attr_data[_i].attr);
293         value = sdb_store_expr_constvalue(&cmp_attr_data[_i].value);
294         fail_unless(value != NULL,
295                         "sdb_store_expr_constvalue(%s) = NULL; expected: <expr>",
296                         value_str);
298         for (j = 0; j < SDB_STATIC_ARRAY_LEN(tests); ++j) {
299                 sdb_store_matcher_t *m;
301                 m = tests[j].matcher(attr, value);
302                 fail_unless(m != NULL,
303                                 "sdb_store_<cond>_matcher() = NULL; expected: <matcher>");
305                 status = sdb_store_matcher_matches(m, host, /* filter */ NULL);
306                 fail_unless(status == tests[j].expected,
307                                 "sdb_store_matcher_matches(<attr[%s] %s %s>, "
308                                 "<host>, NULL) = %d; expected: %d",
309                                 cmp_attr_data[_i].attr, op_str[j], value_str,
310                                 status, tests[j].expected);
312                 sdb_object_deref(SDB_OBJ(m));
313         }
315         sdb_object_deref(SDB_OBJ(attr));
316         sdb_object_deref(SDB_OBJ(value));
317         sdb_object_deref(SDB_OBJ(host));
319 END_TEST
321 struct {
322         const char *host;
323         int field;
324         const sdb_data_t value;
325         int expected_lt, expected_le, expected_eq, expected_ge, expected_gt;
326 } cmp_obj_data[] = {
327         { "b", SDB_FIELD_NAME,
328                 { SDB_TYPE_STRING, { .string = "a" } },   0, 0, 0, 1, 1 },
329         { "b", SDB_FIELD_NAME,
330                 { SDB_TYPE_STRING, { .string = "b" } },   0, 1, 1, 1, 0 },
331         { "b", SDB_FIELD_NAME,
332                 { SDB_TYPE_STRING, { .string = "c" } },   1, 1, 0, 0, 0 },
333         /* last-update = 1 for all objects */
334         { "a", SDB_FIELD_LAST_UPDATE,
335                 { SDB_TYPE_DATETIME, { .datetime = 1 } }, 0, 1, 1, 1, 0 },
336         { "a", SDB_FIELD_LAST_UPDATE,
337                 { SDB_TYPE_DATETIME, { .datetime = 2 } }, 1, 1, 0, 0, 0 },
338         { "a", SDB_FIELD_LAST_UPDATE,
339                 { SDB_TYPE_DATETIME, { .datetime = 0 } }, 0, 0, 0, 1, 1 },
340         /* age > 0 for all objects */
341         { "a", SDB_FIELD_AGE,
342                 { SDB_TYPE_DATETIME, { .datetime = 0 } }, 0, 0, 0, 1, 1 },
343         /* interval = 0 for all objects */
344         { "a", SDB_FIELD_INTERVAL,
345                 { SDB_TYPE_DATETIME, { .datetime = 0 } }, 0, 1, 1, 1, 0 },
346         { "a", SDB_FIELD_INTERVAL,
347                 { SDB_TYPE_DATETIME, { .datetime = 1 } }, 1, 1, 0, 0, 0 },
348         /* type mismatch */
349         { "a", SDB_FIELD_LAST_UPDATE,
350                 { SDB_TYPE_INTEGER, { .integer = 0 } }, 0, 0, 0, 0, 0 },
351         { "a", SDB_FIELD_AGE,
352                 { SDB_TYPE_INTEGER, { .integer = 0 } }, 0, 0, 0, 0, 0 },
353         { "a", SDB_FIELD_INTERVAL,
354                 { SDB_TYPE_INTEGER, { .integer = 0 } }, 0, 0, 0, 0, 0 },
355         { "a", SDB_FIELD_BACKEND,
356                 { SDB_TYPE_INTEGER, { .integer = 0 } }, 0, 0, 0, 0, 0 },
357         { "a", SDB_FIELD_BACKEND,
358                 { SDB_TYPE_INTEGER, { .integer = 0 } }, 0, 0, 0, 0, 0 },
359         /* (64bit) integer value without zero-bytes */
360         { "a", SDB_FIELD_BACKEND,
361                 { SDB_TYPE_INTEGER, { .integer = 0xffffffffffffffffL } },
362                 0, 0, 0, 0, 0 },
363 };
365 START_TEST(test_cmp_obj)
367         sdb_store_obj_t *host;
368         sdb_store_expr_t *field;
369         sdb_store_expr_t *value;
370         char value_str[1024];
371         int status;
372         size_t j;
374         struct {
375                 sdb_store_matcher_t *(*matcher)(sdb_store_expr_t *,
376                                 sdb_store_expr_t *);
377                 int expected;
378         } tests[] = {
379                 { sdb_store_lt_matcher, cmp_obj_data[_i].expected_lt },
380                 { sdb_store_le_matcher, cmp_obj_data[_i].expected_le },
381                 { sdb_store_eq_matcher, cmp_obj_data[_i].expected_eq },
382                 { sdb_store_ge_matcher, cmp_obj_data[_i].expected_ge },
383                 { sdb_store_gt_matcher, cmp_obj_data[_i].expected_gt },
384         };
385         char *op_str[] = { "<", "<=", "=", ">=", ">" };
387         ck_assert(SDB_STATIC_ARRAY_LEN(tests) == SDB_STATIC_ARRAY_LEN(op_str));
389         host = sdb_store_get_host(store, cmp_obj_data[_i].host);
390         fail_unless(host != NULL,
391                         "sdb_store_get_host(%s) = NULL; expected: <host>",
392                         cmp_obj_data[_i].host);
394         sdb_data_format(&cmp_obj_data[_i].value,
395                         value_str, sizeof(value_str), SDB_UNQUOTED);
397         field = sdb_store_expr_fieldvalue(cmp_obj_data[_i].field);
398         fail_unless(field != NULL,
399                         "sdb_store_expr_fieldvalue(%d) = NULL; "
400                         "expected: <expr>", cmp_obj_data[_i].field);
402         value = sdb_store_expr_constvalue(&cmp_obj_data[_i].value);
403         fail_unless(value != NULL,
404                         "sdb_store_expr_constvalue(%s) = NULL; "
405                         "expected: <expr>", value_str);
407         for (j = 0; j < SDB_STATIC_ARRAY_LEN(tests); ++j) {
408                 char m_str[1024];
409                 sdb_store_matcher_t *m;
411                 snprintf(m_str, sizeof(m_str), "%s %s %s",
412                                 SDB_FIELD_TO_NAME(cmp_obj_data[_i].field),
413                                 op_str[j], value_str);
415                 m = tests[j].matcher(field, value);
416                 fail_unless(m != NULL,
417                                 "sdb_store_<cond>_matcher() = NULL; expected: <matcher>");
419                 status = sdb_store_matcher_matches(m, host, /* filter */ NULL);
420                 fail_unless(status == tests[j].expected,
421                                 "sdb_store_matcher_matches(<%s>, <host '%s'>, NULL) = %d; "
422                                 "expected: %d", m_str, cmp_obj_data[_i].host, status,
423                                 tests[j].expected);
425                 sdb_object_deref(SDB_OBJ(m));
426         }
428         sdb_object_deref(SDB_OBJ(field));
429         sdb_object_deref(SDB_OBJ(value));
430         sdb_object_deref(SDB_OBJ(host));
432 END_TEST
434 START_TEST(test_store_match_op)
436         sdb_store_obj_t *obj;
438         sdb_data_t d = { SDB_TYPE_STRING, { .string = "a" } };
439         sdb_store_expr_t *e = sdb_store_expr_constvalue(&d);
441         sdb_store_matcher_t *never = sdb_store_isnull_matcher(e);
442         sdb_store_matcher_t *always = sdb_store_inv_matcher(never);
444         struct {
445                 const char *op;
446                 sdb_store_matcher_t *left;
447                 sdb_store_matcher_t *right;
448                 int expected;
449         } golden_data[] = {
450                 { "OR",  always, always, 1 },
451                 { "OR",  always, never,  1 },
452                 { "OR",  never,  always, 1 },
453                 { "OR",  never,  never,  0 },
454                 { "AND", always, always, 1 },
455                 { "AND", always, never,  0 },
456                 { "AND", never,  always, 0 },
457                 { "AND", never,  never,  0 },
458         };
460         int status;
461         size_t i;
463         obj = sdb_store_get_host(store, "a");
465         status = sdb_store_matcher_matches(always, obj, /* filter */ NULL);
466         fail_unless(status == 1,
467                         "INTERNAL ERROR: 'always' did not match host");
468         status = sdb_store_matcher_matches(never, obj, /* filter */ NULL);
469         fail_unless(status == 0,
470                         "INTERNAL ERROR: 'never' matches host");
472         for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
473                 sdb_store_matcher_t *m;
475                 if (! strcmp(golden_data[i].op, "OR"))
476                         m = sdb_store_dis_matcher(golden_data[i].left,
477                                         golden_data[i].right);
478                 else if (! strcmp(golden_data[i].op, "AND"))
479                         m = sdb_store_con_matcher(golden_data[i].left,
480                                         golden_data[i].right);
481                 else {
482                         fail("INTERNAL ERROR: unexpected operator %s", golden_data[i].op);
483                         continue;
484                 }
486 #define TO_NAME(v) (((v) == always) ? "always" \
487                 : ((v) == never) ? "never" : "<unknown>")
489                 status = sdb_store_matcher_matches(m, obj, /* filter */ NULL);
490                 fail_unless(status == golden_data[i].expected,
491                                 "%s(%s, %s, NULL) = %d; expected: %d", golden_data[i].op,
492                                 TO_NAME(golden_data[i].left), TO_NAME(golden_data[i].right),
493                                 status, golden_data[i].expected);
495 #undef TO_NAME
497                 sdb_object_deref(SDB_OBJ(m));
498         }
500         sdb_object_deref(SDB_OBJ(always));
501         sdb_object_deref(SDB_OBJ(never));
502         sdb_object_deref(SDB_OBJ(e));
504         sdb_object_deref(SDB_OBJ(obj));
506 END_TEST
508 static int
509 scan_cb(sdb_store_obj_t *obj, sdb_store_matcher_t *filter, void *user_data)
511         int *i = user_data;
513         if (! sdb_store_matcher_matches(filter, obj, NULL))
514                 return 0;
516         fail_unless(obj != NULL,
517                         "sdb_store_scan callback received NULL obj; expected: "
518                         "<store base obj>");
519         fail_unless(i != NULL,
520                         "sdb_store_scan callback received NULL user_data; "
521                         "expected: <pointer to data>");
523         ++(*i);
524         return 0;
525 } /* scan_cb */
527 struct {
528         const char *query;
529         const char *filter;
530         int expected;
531 } scan_data[] = {
532         /* TODO: check the name of the expected hosts */
533         { "name = 'a'", NULL,                        1 },
534         { "name = 'a'", "name = 'x'",                0 }, /* filter never matches */
535         { "name = 'a'",
536                 "NOT attribute['x'] = ''",               1 }, /* filter always matches */
537         { "name =~ 'a|b'", NULL,                     2 },
538         { "name =~ 'host'", NULL,                    0 },
539         { "name =~ '.'", NULL,                       3 },
540         { "ANY backend = 'backend'", NULL,           0 },
541         { "ALL backend = ''", NULL,                  3 }, /* backend is empty */
542         { "backend = ['backend']", NULL,             0 },
543         { "backend != ['backend']", NULL,            3 },
544         { "backend < ['backend']", NULL,             3 },
545         { "backend <= ['backend']", NULL,            3 },
546         { "backend >= ['backend']", NULL,            0 },
547         { "backend > ['backend']", NULL,             0 },
548         { "ANY metric.name = 'm1'", NULL,            2 },
549         { "ANY metric.name = 'm1'", "name = 'x'",    0 }, /* filter never matches */
550         { "ANY metric.name = 'm1'",
551                 "NOT attribute['x'] = ''",               2 }, /* filter always matches */
552         { "ANY metric.name =~ 'm'", NULL,            2 },
553         { "ALL metric.name =~ 'm'", NULL,            3 },
554         { "ANY metric.name =~ 'm'", "name !~ '1'",   1 },
555         { "ANY metric.name =~ 'm'", "name !~ 'm'",   0 },
556         { "ALL metric.name =~ '1'", NULL,            2 },
557         { "ALL metric.name =~ '2'", NULL,            1 },
558         { "ANY metric.name !~ 'm'", NULL,            0 },
559         { "ALL metric.name !~ 'm'", NULL,            1 },
560         { "ANY metric.name =~ 'x'", NULL,            0 },
561         { "ANY service.name = 's1'", NULL,           2 },
562         { "ANY service.name = 's1'", "name = 'x'",   0 }, /* filter never matches */
563         { "ANY service.name = 's1'",
564                 "NOT attribute['x'] = ''",               2 }, /* filter always matches */
565         { "ANY service.name =~ 's'", NULL,           2 },
566         { "ANY service.name =~ 's'", "name !~ 's'",  0 },
567         { "ANY service.name =~ 's'", "name !~ '1'",  2 },
568         { "ANY service.name !~ 's'", NULL,           0 },
569         { "ANY attribute.name = 'k1'", NULL,         2 },
570         { "ANY attribute.name = 'k1'", "name = 'x'", 0 }, /* filter never matches */
571         { "ANY attribute.name = 'k1'",
572                 "NOT attribute['x'] = ''",         2 }, /* filter always matches */
573         { "ANY attribute.name =~ 'k'", NULL,   2 },
574         { "ANY attribute.name =~ 'k'",
575                 "name !~ '1'",                     1 },
576         { "ANY attribute.name =~ 'k'",
577                 "name !~ 'k'",                     0 },
578         { "ANY attribute.name =~ '1'", NULL,   2 },
579         { "ANY attribute.name =~ '2'", NULL,   1 },
580         { "ANY attribute.name = 'x'", NULL,    0 },
581         { "ANY attribute.name =~ 'x'", NULL,   0 },
582         { "ALL attribute.name = 'k1'", NULL,   2 },
583         { "ANY attribute.value = 'v1'", NULL,  1 },
584         { "ANY attribute.value =~ 'v'", NULL,  2 },
585         { "ANY attribute.value = 123", NULL,   1 },
586         { "host.name = 'a'", NULL,             1 },
587         { "host.attribute['k1'] =~ 'v1'",
588                 NULL,                              1 },
589         { "host.attribute['x1'] IS NULL",
590                 NULL,                              3 },
591         /* not a boolean so neither TRUE nor FALSE: */
592         { "attribute['k1'] IS TRUE", NULL,     0 },
593         { "attribute['k1'] IS FALSE", NULL,    0 },
594         { "attribute['k1'] = 'v1'", NULL,      1 },
595         { "attribute['k1'] = 'v1'",
596                 "name != 'k1'",                    0 },
597         { "attribute['k1'] =~ 'v1'", NULL,     1 },
598         { "attribute['k1'] =~ '^v1$'", NULL,   1 },
599         { "attribute['k1'] =~ 'v'", NULL,      2 },
600         { "attribute['k1'] =~ '1'", NULL,      1 },
601         { "attribute['k1'] !~ 'v'", NULL,      0 },
602         { "attribute['k1'] = 'v2'", NULL,      1 },
603         { "attribute['k1'] =~ 'v2'", NULL,     1 },
604         { "attribute['x1'] =~ 'v'", NULL,      0 },
605         { "attribute['x1'] =~ 'NULL'", NULL,   0 },
606         { "attribute['x1'] !~ 'v'", NULL,      0 },
607         { "attribute['k1'] IS NULL", NULL,     1 },
608         { "attribute['x1'] IS NULL", NULL,     3 },
609         { "attribute['k1'] IS TRUE", NULL,     0 },
610         { "attribute['x1'] IS TRUE", NULL,     0 },
611         { "attribute['k1'] IS FALSE", NULL,    0 },
612         { "attribute['x1'] IS FALSE", NULL,    0 },
613         { "attribute['k1'] IS NOT NULL", NULL, 2 },
614         { "attribute['x1'] IS NOT NULL", NULL, 0 },
615         { "attribute['x1'] IS NOT TRUE", NULL, 3 },
616         { "attribute['k2'] < 123", NULL,       0 },
617         { "attribute['k2'] <= 123", NULL,      1 },
618         { "attribute['k2'] >= 123", NULL,      1 },
619         { "attribute['k2'] > 123", NULL,       0 },
620         { "attribute['k2'] = 123", NULL,       1 },
621         { "attribute['k2'] != 123", NULL,      0 },
622         { "attribute['k1'] != 'v1'", NULL,     1 },
623         { "attribute['k1'] != 'v2'", NULL,     1 },
624         { "ANY attribute.name != 'x' "
625           "AND attribute['k1'] !~ 'x'", NULL,  2 },
626 };
628 START_TEST(test_scan)
630         sdb_strbuf_t *errbuf = sdb_strbuf_create(64);
631         sdb_store_matcher_t *m, *filter = NULL;
632         sdb_ast_node_t *ast;
633         int check, n;
635         n = 0;
636         check = sdb_store_scan(store, SDB_HOST,
637                         /* matcher */ NULL, /* filter */ NULL,
638                         scan_cb, &n);
639         fail_unless(check == 0,
640                         "sdb_store_scan() = %d; expected: 0", check);
641         fail_unless(n == 3,
642                         "sdb_store_scan called callback %d times; expected: 3", (int)n);
644         ast = sdb_parser_parse_conditional(scan_data[_i].query, -1, errbuf);
645         m = sdb_store_query_prepare_matcher(ast);
646         sdb_object_deref(SDB_OBJ(ast));
647         fail_unless(m != NULL,
648                         "sdb_parser_parse_conditional(%s, -1) = NULL; expected: <ast> "
649                         "(parser error: %s)", scan_data[_i].query,
650                         sdb_strbuf_string(errbuf));
652         if (scan_data[_i].filter) {
653                 ast = sdb_parser_parse_conditional(scan_data[_i].filter, -1, errbuf);
654                 filter = sdb_store_query_prepare_matcher(ast);
655                 sdb_object_deref(SDB_OBJ(ast));
656                 fail_unless(filter != NULL,
657                                 "sdb_parser_parse_conditional(%s, -1) = NULL; "
658                                 "expected: <ast> (parser error: %s)",
659                                 scan_data[_i].filter, sdb_strbuf_string(errbuf));
660         }
662         n = 0;
663         sdb_store_scan(store, SDB_HOST, m, filter, scan_cb, &n);
664         fail_unless(n == scan_data[_i].expected,
665                         "sdb_store_scan(HOST, matcher{%s}, filter{%s}) "
666                         "found %d hosts; expected: %d", scan_data[_i].query,
667                         scan_data[_i].filter, n, scan_data[_i].expected);
669         sdb_object_deref(SDB_OBJ(filter));
670         sdb_object_deref(SDB_OBJ(m));
671         sdb_strbuf_destroy(errbuf);
673 END_TEST
675 TEST_MAIN("core::store_lookup")
677         TCase *tc = tcase_create("core");
678         tcase_add_checked_fixture(tc, populate, turndown);
679         TC_ADD_LOOP_TEST(tc, cmp_name);
680         TC_ADD_LOOP_TEST(tc, cmp_attr);
681         TC_ADD_LOOP_TEST(tc, cmp_obj);
682         TC_ADD_LOOP_TEST(tc, scan);
683         tcase_add_test(tc, test_store_match_op);
684         ADD_TCASE(tc);
686 TEST_MAIN_END
688 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */