Code

t/: Use ck_assert() instead of assert() in all tests.
[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/store.h"
33 #include "core/store-private.h"
34 #include "parser/parser.h"
35 #include "testutils.h"
37 #include <check.h>
38 #include <string.h>
40 static void
41 populate(void)
42 {
43         const char *hosts[] = { "a", "b", "c" };
45         struct {
46                 const char *host;
47                 const char *metric;
48         } metrics[] = {
49                 { "a", "m1" },
50                 { "b", "m1" },
51                 { "b", "m2" },
52         };
54         struct {
55                 const char *host;
56                 const char *service;
57         } services[] = {
58                 { "a", "s1" },
59                 { "a", "s2" },
60                 { "b", "s1" },
61                 { "b", "s3" },
62         };
64         struct {
65                 const char *host;
66                 const char *name;
67                 sdb_data_t  value;
68         } attrs[] = {
69                 { "a", "k1", { SDB_TYPE_STRING, { .string = "v1" } } },
70                 { "a", "k2", { SDB_TYPE_INTEGER, { .integer = 123 } } },
71                 { "b", "k1", { SDB_TYPE_STRING, { .string = "v2" } } },
72         };
74         size_t i;
76         for (i = 0; i < SDB_STATIC_ARRAY_LEN(hosts); ++i) {
77                 int status = sdb_store_host(hosts[i], 1);
78                 fail_unless(status == 0,
79                                 "sdb_store_host(%s, 1) = %d; expected: 0",
80                                 hosts[i], status);
81         }
83         for (i = 0; i < SDB_STATIC_ARRAY_LEN(metrics); ++i) {
84                 int status = sdb_store_metric(metrics[i].host,
85                                 metrics[i].metric, /* store */ NULL, 1);
86                 fail_unless(status == 0,
87                                 "sdb_store_metric(%s, %s, NULL, 1) = %d; expected: 0",
88                                 metrics[i].host, metrics[i].metric, status);
89         }
91         for (i = 0; i < SDB_STATIC_ARRAY_LEN(services); ++i) {
92                 int status = sdb_store_service(services[i].host,
93                                 services[i].service, 1);
94                 fail_unless(status == 0,
95                                 "sdb_store_service(%s, %s, 1) = %d; expected: 0",
96                                 services[i].host, services[i].service, status);
97         }
99         for (i = 0; i < SDB_STATIC_ARRAY_LEN(attrs); ++i) {
100                 int status = sdb_store_attribute(attrs[i].host,
101                                 attrs[i].name, &attrs[i].value, 1);
102                 fail_unless(status == 0,
103                                 "sdb_store_attribute(%s, %s, <val>, 1) = %d; expected: 0",
104                                 attrs[i].host, attrs[i].name, status);
105         }
106 } /* populate */
108 struct {
109         int type;
110         char *name;
111         _Bool re;
113         int expected;
114 } cmp_name_data[] = {
115         { SDB_HOST,      "a",    0, 1 },
116         { SDB_HOST,      "a",    1, 1 },
117         { SDB_HOST,      "b",    0, 0 },
118         { SDB_HOST,      "b",    1, 0 },
119         { SDB_HOST,      "^a$",  1, 1 },
120         { SDB_HOST,      "^b$",  1, 0 },
121         { SDB_HOST,      "^a$",  0, 0 },
122         { SDB_HOST,      "^b$",  0, 0 },
123         { SDB_METRIC,    "m1",   0, 1 },
124         { SDB_METRIC,    "m1",   1, 1 },
125         { SDB_METRIC,    "^m1$", 1, 1 },
126         { SDB_METRIC,    "m",    1, 1 },
127         { SDB_METRIC,    "s",    1, 0 },
128         { SDB_METRIC,    "m2",   0, 0 },
129         { SDB_METRIC,    "x1",   0, 0 },
130         { SDB_METRIC,    "x1",   1, 0 },
131         { SDB_SERVICE,   "s1",   0, 1 },
132         { SDB_SERVICE,   "s2",   0, 1 },
133         { SDB_SERVICE,   "s3",   0, 0 },
134         { SDB_SERVICE,   "s4",   0, 0 },
135         { SDB_SERVICE,   "^s1$", 1, 1 },
136         { SDB_SERVICE,   "^s1$", 0, 0 },
137         { SDB_SERVICE,   "x1",   0, 0 },
138         { SDB_SERVICE,   "x1",   1, 0 },
139         { SDB_SERVICE,   "x",    1, 0 },
140         { SDB_ATTRIBUTE, "k1",   0, 1 },
141         { SDB_ATTRIBUTE, "k2",   0, 1 },
142         { SDB_ATTRIBUTE, "k3",   0, 0 },
143         { SDB_ATTRIBUTE, "k4",   0, 0 },
144         { SDB_ATTRIBUTE, "k",    1, 1 },
145         { SDB_ATTRIBUTE, "1",    1, 1 },
146         { SDB_ATTRIBUTE, "3",    1, 0 },
147 };
149 START_TEST(test_cmp_name)
151         sdb_store_obj_t *host;
152         sdb_data_t datum;
153         sdb_store_expr_t *obj = NULL, *value;
154         sdb_store_matcher_t *m, *n;
155         int status;
157         host = sdb_store_get_host("a");
158         fail_unless(host != NULL,
159                         "sdb_store_get_host(a) = NULL; expected: <host>");
161         datum.type = SDB_TYPE_STRING;
162         datum.data.string = cmp_name_data[_i].name;
164         if (cmp_name_data[_i].type == SDB_HOST) {
165                 obj = sdb_store_expr_fieldvalue(SDB_FIELD_NAME);
166                 fail_unless(obj != NULL,
167                                 "sdb_store_expr_fieldvalue(SDB_STORE_NAME) = NULL; "
168                                 "expected: <expr>");
169         }
170         value = sdb_store_expr_constvalue(&datum);
171         fail_unless(value != NULL,
172                         "sdb_store_expr_constvalue(%s) = NULL; "
173                         "expected: <expr>", cmp_name_data[_i].name);
175         if (cmp_name_data[_i].re)
176                 m = sdb_store_regex_matcher(obj, value);
177         else
178                 m = sdb_store_eq_matcher(obj, value);
180         if (cmp_name_data[_i].type != SDB_HOST) {
181                 sdb_store_expr_t *iter;
182                 sdb_store_matcher_t *tmp;
183                 obj = sdb_store_expr_fieldvalue(SDB_FIELD_NAME);
184                 iter = sdb_store_expr_typed(cmp_name_data[_i].type, obj);
185                 tmp = sdb_store_any_matcher(iter, m);
186                 ck_assert(iter && m);
187                 sdb_object_deref(SDB_OBJ(iter));
188                 sdb_object_deref(SDB_OBJ(m));
189                 m = tmp;
190         }
191         sdb_object_deref(SDB_OBJ(obj));
192         sdb_object_deref(SDB_OBJ(value));
193         fail_unless(m != NULL,
194                         "sdb_store_%s_matcher(%s, %s) = NULL; "
195                         "expected: <matcher>",
196                         cmp_name_data[_i].re ? "regex" : "eq",
197                         SDB_STORE_TYPE_TO_NAME(cmp_name_data[_i].type),
198                         cmp_name_data[_i].name);
200         status = sdb_store_matcher_matches(m, host, /* filter */ NULL);
201         fail_unless(status == cmp_name_data[_i].expected,
202                         "sdb_store_matcher_matches(%s->%s, <host a>, NULL) = %d; "
203                         "expected: %d", SDB_STORE_TYPE_TO_NAME(cmp_name_data[_i].type),
204                         cmp_name_data[_i].name, status, cmp_name_data[_i].expected);
206         n = sdb_store_inv_matcher(m);
207         fail_unless(n != NULL,
208                         "sdb_store_inv_matcher() = NULL; expected: <matcher>");
209         sdb_object_deref(SDB_OBJ(m));
211         /* now match the inverted set of objects */
212         status = sdb_store_matcher_matches(n, host, /* filter */ NULL);
213         fail_unless(status == !cmp_name_data[_i].expected,
214                         "sdb_store_matcher_matches(%s->%s, <host a>, NULL) = %d; "
215                         "expected: %d", SDB_STORE_TYPE_TO_NAME(cmp_name_data[_i].type),
216                         cmp_name_data[_i].name, status, !cmp_name_data[_i].expected);
218         sdb_object_deref(SDB_OBJ(n));
219         sdb_object_deref(SDB_OBJ(host));
221 END_TEST
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 } cmp_attr_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 START_TEST(test_cmp_attr)
246         sdb_store_obj_t *host;
247         sdb_store_expr_t *attr;
248         sdb_store_expr_t *value;
249         char value_str[1024];
250         int status;
251         size_t j;
253         struct {
254                 sdb_store_matcher_t *(*matcher)(sdb_store_expr_t *,
255                                 sdb_store_expr_t *);
256                 int expected;
257         } tests[] = {
258                 { sdb_store_lt_matcher, cmp_attr_data[_i].expected_lt },
259                 { sdb_store_le_matcher, cmp_attr_data[_i].expected_le },
260                 { sdb_store_eq_matcher, cmp_attr_data[_i].expected_eq },
261                 { sdb_store_ge_matcher, cmp_attr_data[_i].expected_ge },
262                 { sdb_store_gt_matcher, cmp_attr_data[_i].expected_gt },
263         };
265         const char *op_str[] = { "<", "<=", "=", ">=", ">" };
266         ck_assert(SDB_STATIC_ARRAY_LEN(tests) == SDB_STATIC_ARRAY_LEN(op_str));
268         host = sdb_store_get_host("a");
269         fail_unless(host != NULL,
270                         "sdb_store_get_host(a) = NULL; expected: <host>");
272         sdb_data_format(&cmp_attr_data[_i].value,
273                         value_str, sizeof(value_str), SDB_UNQUOTED);
275         attr = sdb_store_expr_attrvalue(cmp_attr_data[_i].attr);
276         fail_unless(attr != NULL,
277                         "sdb_store_expr_attrvalue(%s) = NULL; expected: <expr>",
278                         cmp_attr_data[_i].attr);
280         value = sdb_store_expr_constvalue(&cmp_attr_data[_i].value);
281         fail_unless(value != NULL,
282                         "sdb_store_expr_constvalue(%s) = NULL; expected: <expr>",
283                         value_str);
285         for (j = 0; j < SDB_STATIC_ARRAY_LEN(tests); ++j) {
286                 sdb_store_matcher_t *m;
288                 m = tests[j].matcher(attr, value);
289                 fail_unless(m != NULL,
290                                 "sdb_store_<cond>_matcher() = NULL; expected: <matcher>");
292                 status = sdb_store_matcher_matches(m, host, /* filter */ NULL);
293                 fail_unless(status == tests[j].expected,
294                                 "sdb_store_matcher_matches(<attr[%s] %s %s>, "
295                                 "<host>, NULL) = %d; expected: %d",
296                                 cmp_attr_data[_i].attr, op_str[j], value_str,
297                                 status, tests[j].expected);
299                 sdb_object_deref(SDB_OBJ(m));
300         }
302         sdb_object_deref(SDB_OBJ(attr));
303         sdb_object_deref(SDB_OBJ(value));
304         sdb_object_deref(SDB_OBJ(host));
306 END_TEST
308 struct {
309         const char *host;
310         int field;
311         const sdb_data_t value;
312         int expected_lt, expected_le, expected_eq, expected_ge, expected_gt;
313 } cmp_obj_data[] = {
314         { "b", SDB_FIELD_NAME,
315                 { SDB_TYPE_STRING, { .string = "a" } },   0, 0, 0, 1, 1 },
316         { "b", SDB_FIELD_NAME,
317                 { SDB_TYPE_STRING, { .string = "b" } },   0, 1, 1, 1, 0 },
318         { "b", SDB_FIELD_NAME,
319                 { SDB_TYPE_STRING, { .string = "c" } },   1, 1, 0, 0, 0 },
320         /* last-update = 1 for all objects */
321         { "a", SDB_FIELD_LAST_UPDATE,
322                 { SDB_TYPE_DATETIME, { .datetime = 1 } }, 0, 1, 1, 1, 0 },
323         { "a", SDB_FIELD_LAST_UPDATE,
324                 { SDB_TYPE_DATETIME, { .datetime = 2 } }, 1, 1, 0, 0, 0 },
325         { "a", SDB_FIELD_LAST_UPDATE,
326                 { SDB_TYPE_DATETIME, { .datetime = 0 } }, 0, 0, 0, 1, 1 },
327         /* age > 0 for all objects */
328         { "a", SDB_FIELD_AGE,
329                 { SDB_TYPE_DATETIME, { .datetime = 0 } }, 0, 0, 0, 1, 1 },
330         /* interval = 0 for all objects */
331         { "a", SDB_FIELD_INTERVAL,
332                 { SDB_TYPE_DATETIME, { .datetime = 0 } }, 0, 1, 1, 1, 0 },
333         { "a", SDB_FIELD_INTERVAL,
334                 { SDB_TYPE_DATETIME, { .datetime = 1 } }, 1, 1, 0, 0, 0 },
335         /* type mismatch */
336         { "a", SDB_FIELD_LAST_UPDATE,
337                 { SDB_TYPE_INTEGER, { .integer = 0 } }, 0, 0, 0, 0, 0 },
338         { "a", SDB_FIELD_AGE,
339                 { SDB_TYPE_INTEGER, { .integer = 0 } }, 0, 0, 0, 0, 0 },
340         { "a", SDB_FIELD_INTERVAL,
341                 { SDB_TYPE_INTEGER, { .integer = 0 } }, 0, 0, 0, 0, 0 },
342         { "a", SDB_FIELD_BACKEND,
343                 { SDB_TYPE_INTEGER, { .integer = 0 } }, 0, 0, 0, 0, 0 },
344         { "a", SDB_FIELD_BACKEND,
345                 { SDB_TYPE_INTEGER, { .integer = 0 } }, 0, 0, 0, 0, 0 },
346         /* (64bit) integer value without zero-bytes */
347         { "a", SDB_FIELD_BACKEND,
348                 { SDB_TYPE_INTEGER, { .integer = 0xffffffffffffffffL } },
349                 0, 0, 0, 0, 0 },
350 };
352 START_TEST(test_cmp_obj)
354         sdb_store_obj_t *host;
355         sdb_store_expr_t *field;
356         sdb_store_expr_t *value;
357         char value_str[1024];
358         int status;
359         size_t j;
361         struct {
362                 sdb_store_matcher_t *(*matcher)(sdb_store_expr_t *,
363                                 sdb_store_expr_t *);
364                 int expected;
365         } tests[] = {
366                 { sdb_store_lt_matcher, cmp_obj_data[_i].expected_lt },
367                 { sdb_store_le_matcher, cmp_obj_data[_i].expected_le },
368                 { sdb_store_eq_matcher, cmp_obj_data[_i].expected_eq },
369                 { sdb_store_ge_matcher, cmp_obj_data[_i].expected_ge },
370                 { sdb_store_gt_matcher, cmp_obj_data[_i].expected_gt },
371         };
372         char *op_str[] = { "<", "<=", "=", ">=", ">" };
374         ck_assert(SDB_STATIC_ARRAY_LEN(tests) == SDB_STATIC_ARRAY_LEN(op_str));
376         host = sdb_store_get_host(cmp_obj_data[_i].host);
377         fail_unless(host != NULL,
378                         "sdb_store_get_host(%s) = NULL; expected: <host>",
379                         cmp_obj_data[_i].host);
381         sdb_data_format(&cmp_obj_data[_i].value,
382                         value_str, sizeof(value_str), SDB_UNQUOTED);
384         field = sdb_store_expr_fieldvalue(cmp_obj_data[_i].field);
385         fail_unless(field != NULL,
386                         "sdb_store_expr_fieldvalue(%d) = NULL; "
387                         "expected: <expr>", cmp_obj_data[_i].field);
389         value = sdb_store_expr_constvalue(&cmp_obj_data[_i].value);
390         fail_unless(value != NULL,
391                         "sdb_store_expr_constvalue(%s) = NULL; "
392                         "expected: <expr>", value_str);
394         for (j = 0; j < SDB_STATIC_ARRAY_LEN(tests); ++j) {
395                 char m_str[1024];
396                 sdb_store_matcher_t *m;
398                 snprintf(m_str, sizeof(m_str), "%s %s %s",
399                                 SDB_FIELD_TO_NAME(cmp_obj_data[_i].field),
400                                 op_str[j], value_str);
402                 m = tests[j].matcher(field, value);
403                 fail_unless(m != NULL,
404                                 "sdb_store_<cond>_matcher() = NULL; expected: <matcher>");
406                 status = sdb_store_matcher_matches(m, host, /* filter */ NULL);
407                 fail_unless(status == tests[j].expected,
408                                 "sdb_store_matcher_matches(<%s>, <host '%s'>, NULL) = %d; "
409                                 "expected: %d", m_str, cmp_obj_data[_i].host, status,
410                                 tests[j].expected);
412                 sdb_object_deref(SDB_OBJ(m));
413         }
415         sdb_object_deref(SDB_OBJ(field));
416         sdb_object_deref(SDB_OBJ(value));
417         sdb_object_deref(SDB_OBJ(host));
419 END_TEST
421 START_TEST(test_store_match_op)
423         sdb_store_obj_t *obj;
425         sdb_data_t d = { SDB_TYPE_STRING, { .string = "a" } };
426         sdb_store_expr_t *e = sdb_store_expr_constvalue(&d);
428         sdb_store_matcher_t *never = sdb_store_isnull_matcher(e);
429         sdb_store_matcher_t *always = sdb_store_inv_matcher(never);
431         struct {
432                 const char *op;
433                 sdb_store_matcher_t *left;
434                 sdb_store_matcher_t *right;
435                 int expected;
436         } golden_data[] = {
437                 { "OR",  always, always, 1 },
438                 { "OR",  always, never,  1 },
439                 { "OR",  never,  always, 1 },
440                 { "OR",  never,  never,  0 },
441                 { "AND", always, always, 1 },
442                 { "AND", always, never,  0 },
443                 { "AND", never,  always, 0 },
444                 { "AND", never,  never,  0 },
445         };
447         int status;
448         size_t i;
450         obj = sdb_store_get_host("a");
452         status = sdb_store_matcher_matches(always, obj, /* filter */ NULL);
453         fail_unless(status == 1,
454                         "INTERNAL ERROR: 'always' did not match host");
455         status = sdb_store_matcher_matches(never, obj, /* filter */ NULL);
456         fail_unless(status == 0,
457                         "INTERNAL ERROR: 'never' matches host");
459         for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
460                 sdb_store_matcher_t *m;
462                 if (! strcmp(golden_data[i].op, "OR"))
463                         m = sdb_store_dis_matcher(golden_data[i].left,
464                                         golden_data[i].right);
465                 else if (! strcmp(golden_data[i].op, "AND"))
466                         m = sdb_store_con_matcher(golden_data[i].left,
467                                         golden_data[i].right);
468                 else {
469                         fail("INTERNAL ERROR: unexpected operator %s", golden_data[i].op);
470                         continue;
471                 }
473 #define TO_NAME(v) (((v) == always) ? "always" \
474                 : ((v) == never) ? "never" : "<unknown>")
476                 status = sdb_store_matcher_matches(m, obj, /* filter */ NULL);
477                 fail_unless(status == golden_data[i].expected,
478                                 "%s(%s, %s, NULL) = %d; expected: %d", golden_data[i].op,
479                                 TO_NAME(golden_data[i].left), TO_NAME(golden_data[i].right),
480                                 status, golden_data[i].expected);
482 #undef TO_NAME
484                 sdb_object_deref(SDB_OBJ(m));
485         }
487         sdb_object_deref(SDB_OBJ(always));
488         sdb_object_deref(SDB_OBJ(never));
489         sdb_object_deref(SDB_OBJ(e));
491         sdb_object_deref(SDB_OBJ(obj));
493 END_TEST
495 static int
496 scan_cb(sdb_store_obj_t *obj, sdb_store_matcher_t *filter, void *user_data)
498         int *i = user_data;
500         if (! sdb_store_matcher_matches(filter, obj, NULL))
501                 return 0;
503         fail_unless(obj != NULL,
504                         "sdb_store_scan callback received NULL obj; expected: "
505                         "<store base obj>");
506         fail_unless(i != NULL,
507                         "sdb_store_scan callback received NULL user_data; "
508                         "expected: <pointer to data>");
510         ++(*i);
511         return 0;
512 } /* scan_cb */
514 struct {
515         const char *query;
516         const char *filter;
517         int expected;
518 } scan_data[] = {
519         /* TODO: check the name of the expected hosts */
520         { "name = 'a'", NULL,                        1 },
521         { "name = 'a'", "name = 'x'",                0 }, /* filter never matches */
522         { "name = 'a'",
523                 "NOT attribute['x'] = ''",               1 }, /* filter always matches */
524         { "name =~ 'a|b'", NULL,                     2 },
525         { "name =~ 'host'", NULL,                    0 },
526         { "name =~ '.'", NULL,                       3 },
527         { "ANY backend = 'backend'", NULL,           0 },
528         { "ALL backend = ''", NULL,                  3 }, /* backend is empty */
529         { "backend = ['backend']", NULL,             0 },
530         { "backend != ['backend']", NULL,            3 },
531         { "backend < ['backend']", NULL,             3 },
532         { "backend <= ['backend']", NULL,            3 },
533         { "backend >= ['backend']", NULL,            0 },
534         { "backend > ['backend']", NULL,             0 },
535         { "ANY metric.name = 'm1'", NULL,            2 },
536         { "ANY metric.name = 'm1'", "name = 'x'",    0 }, /* filter never matches */
537         { "ANY metric.name = 'm1'",
538                 "NOT attribute['x'] = ''",               2 }, /* filter always matches */
539         { "ANY metric.name =~ 'm'", NULL,            2 },
540         { "ALL metric.name =~ 'm'", NULL,            3 },
541         { "ANY metric.name =~ 'm'", "name !~ '1'",   1 },
542         { "ANY metric.name =~ 'm'", "name !~ 'm'",   0 },
543         { "ALL metric.name =~ '1'", NULL,            2 },
544         { "ALL metric.name =~ '2'", NULL,            1 },
545         { "ANY metric.name !~ 'm'", NULL,            0 },
546         { "ALL metric.name !~ 'm'", NULL,            1 },
547         { "ANY metric.name =~ 'x'", NULL,            0 },
548         { "ANY service.name = 's1'", NULL,           2 },
549         { "ANY service.name = 's1'", "name = 'x'",   0 }, /* filter never matches */
550         { "ANY service.name = 's1'",
551                 "NOT attribute['x'] = ''",               2 }, /* filter always matches */
552         { "ANY service.name =~ 's'", NULL,           2 },
553         { "ANY service.name =~ 's'", "name !~ 's'",  0 },
554         { "ANY service.name =~ 's'", "name !~ '1'",  2 },
555         { "ANY service.name !~ 's'", NULL,           0 },
556         { "ANY attribute.name = 'k1'", NULL,         2 },
557         { "ANY attribute.name = 'k1'", "name = 'x'", 0 }, /* filter never matches */
558         { "ANY attribute.name = 'k1'",
559                 "NOT attribute['x'] = ''",         2 }, /* filter always matches */
560         { "ANY attribute.name =~ 'k'", NULL,   2 },
561         { "ANY attribute.name =~ 'k'",
562                 "name !~ '1'",                     1 },
563         { "ANY attribute.name =~ 'k'",
564                 "name !~ 'k'",                     0 },
565         { "ANY attribute.name =~ '1'", NULL,   2 },
566         { "ANY attribute.name =~ '2'", NULL,   1 },
567         { "ANY attribute.name = 'x'", NULL,    0 },
568         { "ANY attribute.name =~ 'x'", NULL,   0 },
569         { "ALL attribute.name = 'k1'", NULL,   2 },
570         { "ANY attribute.value = 'v1'", NULL,  1 },
571         { "ANY attribute.value =~ 'v'", NULL,  2 },
572         { "ANY attribute.value = 123", NULL,   1 },
573         { "host.name = 'a'", NULL,             1 },
574         { "host.attribute['k1'] =~ 'v1'",
575                 NULL,                              1 },
576         { "host.attribute['x1'] IS NULL",
577                 NULL,                              3 },
578         /* not a boolean so neither TRUE nor FALSE: */
579         { "attribute['k1'] IS TRUE", NULL,     0 },
580         { "attribute['k1'] IS FALSE", NULL,    0 },
581         { "attribute['k1'] = 'v1'", NULL,      1 },
582         { "attribute['k1'] = 'v1'",
583                 "name != 'k1'",                    0 },
584         { "attribute['k1'] =~ 'v1'", NULL,     1 },
585         { "attribute['k1'] =~ '^v1$'", NULL,   1 },
586         { "attribute['k1'] =~ 'v'", NULL,      2 },
587         { "attribute['k1'] =~ '1'", NULL,      1 },
588         { "attribute['k1'] !~ 'v'", NULL,      0 },
589         { "attribute['k1'] = 'v2'", NULL,      1 },
590         { "attribute['k1'] =~ 'v2'", NULL,     1 },
591         { "attribute['x1'] =~ 'v'", NULL,      0 },
592         { "attribute['x1'] =~ 'NULL'", NULL,   0 },
593         { "attribute['x1'] !~ 'v'", NULL,      0 },
594         { "attribute['k1'] IS NULL", NULL,     1 },
595         { "attribute['x1'] IS NULL", NULL,     3 },
596         { "attribute['k1'] IS TRUE", NULL,     0 },
597         { "attribute['x1'] IS TRUE", NULL,     0 },
598         { "attribute['k1'] IS FALSE", NULL,    0 },
599         { "attribute['x1'] IS FALSE", NULL,    0 },
600         { "attribute['k1'] IS NOT NULL", NULL, 2 },
601         { "attribute['x1'] IS NOT NULL", NULL, 0 },
602         { "attribute['x1'] IS NOT TRUE", NULL, 3 },
603         { "attribute['k2'] < 123", NULL,       0 },
604         { "attribute['k2'] <= 123", NULL,      1 },
605         { "attribute['k2'] >= 123", NULL,      1 },
606         { "attribute['k2'] > 123", NULL,       0 },
607         { "attribute['k2'] = 123", NULL,       1 },
608         { "attribute['k2'] != 123", NULL,      0 },
609         { "attribute['k1'] != 'v1'", NULL,     1 },
610         { "attribute['k1'] != 'v2'", NULL,     1 },
611         { "ANY attribute.name != 'x' "
612           "AND attribute['k1'] !~ 'x'", NULL,  2 },
613 };
615 START_TEST(test_scan)
617         sdb_strbuf_t *errbuf = sdb_strbuf_create(64);
618         sdb_store_matcher_t *m, *filter = NULL;
619         sdb_ast_node_t *ast;
620         int check, n;
622         n = 0;
623         check = sdb_store_scan(SDB_HOST, /* matcher */ NULL, /* filter */ NULL,
624                         scan_cb, &n);
625         fail_unless(check == 0,
626                         "sdb_store_scan() = %d; expected: 0", check);
627         fail_unless(n == 3,
628                         "sdb_store_scan called callback %d times; expected: 3", (int)n);
630         ast = sdb_parser_parse_conditional(scan_data[_i].query, -1, errbuf);
631         m = sdb_store_query_prepare_matcher(ast);
632         sdb_object_deref(SDB_OBJ(ast));
633         fail_unless(m != NULL,
634                         "sdb_parser_parse_conditional(%s, -1) = NULL; expected: <ast> "
635                         "(parser error: %s)", scan_data[_i].query,
636                         sdb_strbuf_string(errbuf));
638         if (scan_data[_i].filter) {
639                 ast = sdb_parser_parse_conditional(scan_data[_i].filter, -1, errbuf);
640                 filter = sdb_store_query_prepare_matcher(ast);
641                 sdb_object_deref(SDB_OBJ(ast));
642                 fail_unless(filter != NULL,
643                                 "sdb_parser_parse_conditional(%s, -1) = NULL; "
644                                 "expected: <ast> (parser error: %s)",
645                                 scan_data[_i].filter, sdb_strbuf_string(errbuf));
646         }
648         n = 0;
649         sdb_store_scan(SDB_HOST, m, filter, scan_cb, &n);
650         fail_unless(n == scan_data[_i].expected,
651                         "sdb_store_scan(HOST, matcher{%s}, filter{%s}) "
652                         "found %d hosts; expected: %d", scan_data[_i].query,
653                         scan_data[_i].filter, n, scan_data[_i].expected);
655         sdb_object_deref(SDB_OBJ(filter));
656         sdb_object_deref(SDB_OBJ(m));
657         sdb_strbuf_destroy(errbuf);
659 END_TEST
661 TEST_MAIN("core::store_lookup")
663         TCase *tc = tcase_create("core");
664         tcase_add_checked_fixture(tc, populate, sdb_store_clear);
665         TC_ADD_LOOP_TEST(tc, cmp_name);
666         TC_ADD_LOOP_TEST(tc, cmp_attr);
667         TC_ADD_LOOP_TEST(tc, cmp_obj);
668         TC_ADD_LOOP_TEST(tc, scan);
669         tcase_add_test(tc, test_store_match_op);
670         ADD_TCASE(tc);
672 TEST_MAIN_END
674 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */