Code

Replaced sdb_store_<type> with sdb_plugin_store_<type>.
[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 void
42 populate(void)
43 {
44         const char *hosts[] = { "a", "b", "c" };
46         struct {
47                 const char *host;
48                 const char *metric;
49         } metrics[] = {
50                 { "a", "m1" },
51                 { "b", "m1" },
52                 { "b", "m2" },
53         };
55         struct {
56                 const char *host;
57                 const char *service;
58         } services[] = {
59                 { "a", "s1" },
60                 { "a", "s2" },
61                 { "b", "s1" },
62                 { "b", "s3" },
63         };
65         struct {
66                 const char *host;
67                 const char *name;
68                 sdb_data_t  value;
69         } attrs[] = {
70                 { "a", "k1", { SDB_TYPE_STRING, { .string = "v1" } } },
71                 { "a", "k2", { SDB_TYPE_INTEGER, { .integer = 123 } } },
72                 { "b", "k1", { SDB_TYPE_STRING, { .string = "v2" } } },
73         };
75         size_t i;
77         sdb_store_init();
79         for (i = 0; i < SDB_STATIC_ARRAY_LEN(hosts); ++i) {
80                 int status = sdb_plugin_store_host(hosts[i], 1);
81                 fail_unless(status == 0,
82                                 "sdb_plugin_store_host(%s, 1) = %d; expected: 0",
83                                 hosts[i], status);
84         }
86         for (i = 0; i < SDB_STATIC_ARRAY_LEN(metrics); ++i) {
87                 int status = sdb_plugin_store_metric(metrics[i].host,
88                                 metrics[i].metric, /* store */ NULL, 1);
89                 fail_unless(status == 0,
90                                 "sdb_plugin_store_metric(%s, %s, NULL, 1) = %d; expected: 0",
91                                 metrics[i].host, metrics[i].metric, status);
92         }
94         for (i = 0; i < SDB_STATIC_ARRAY_LEN(services); ++i) {
95                 int status = sdb_plugin_store_service(services[i].host,
96                                 services[i].service, 1);
97                 fail_unless(status == 0,
98                                 "sdb_plugin_store_service(%s, %s, 1) = %d; expected: 0",
99                                 services[i].host, services[i].service, status);
100         }
102         for (i = 0; i < SDB_STATIC_ARRAY_LEN(attrs); ++i) {
103                 int status = sdb_plugin_store_attribute(attrs[i].host,
104                                 attrs[i].name, &attrs[i].value, 1);
105                 fail_unless(status == 0,
106                                 "sdb_plugin_store_attribute(%s, %s, <val>, 1) = %d; expected: 0",
107                                 attrs[i].host, attrs[i].name, status);
108         }
109 } /* populate */
111 struct {
112         int type;
113         char *name;
114         _Bool re;
116         int expected;
117 } cmp_name_data[] = {
118         { SDB_HOST,      "a",    0, 1 },
119         { SDB_HOST,      "a",    1, 1 },
120         { SDB_HOST,      "b",    0, 0 },
121         { SDB_HOST,      "b",    1, 0 },
122         { SDB_HOST,      "^a$",  1, 1 },
123         { SDB_HOST,      "^b$",  1, 0 },
124         { SDB_HOST,      "^a$",  0, 0 },
125         { SDB_HOST,      "^b$",  0, 0 },
126         { SDB_METRIC,    "m1",   0, 1 },
127         { SDB_METRIC,    "m1",   1, 1 },
128         { SDB_METRIC,    "^m1$", 1, 1 },
129         { SDB_METRIC,    "m",    1, 1 },
130         { SDB_METRIC,    "s",    1, 0 },
131         { SDB_METRIC,    "m2",   0, 0 },
132         { SDB_METRIC,    "x1",   0, 0 },
133         { SDB_METRIC,    "x1",   1, 0 },
134         { SDB_SERVICE,   "s1",   0, 1 },
135         { SDB_SERVICE,   "s2",   0, 1 },
136         { SDB_SERVICE,   "s3",   0, 0 },
137         { SDB_SERVICE,   "s4",   0, 0 },
138         { SDB_SERVICE,   "^s1$", 1, 1 },
139         { SDB_SERVICE,   "^s1$", 0, 0 },
140         { SDB_SERVICE,   "x1",   0, 0 },
141         { SDB_SERVICE,   "x1",   1, 0 },
142         { SDB_SERVICE,   "x",    1, 0 },
143         { SDB_ATTRIBUTE, "k1",   0, 1 },
144         { SDB_ATTRIBUTE, "k2",   0, 1 },
145         { SDB_ATTRIBUTE, "k3",   0, 0 },
146         { SDB_ATTRIBUTE, "k4",   0, 0 },
147         { SDB_ATTRIBUTE, "k",    1, 1 },
148         { SDB_ATTRIBUTE, "1",    1, 1 },
149         { SDB_ATTRIBUTE, "3",    1, 0 },
150 };
152 START_TEST(test_cmp_name)
154         sdb_store_obj_t *host;
155         sdb_data_t datum;
156         sdb_store_expr_t *obj = NULL, *value;
157         sdb_store_matcher_t *m, *n;
158         int status;
160         host = sdb_store_get_host("a");
161         fail_unless(host != NULL,
162                         "sdb_store_get_host(a) = NULL; expected: <host>");
164         datum.type = SDB_TYPE_STRING;
165         datum.data.string = cmp_name_data[_i].name;
167         if (cmp_name_data[_i].type == SDB_HOST) {
168                 obj = sdb_store_expr_fieldvalue(SDB_FIELD_NAME);
169                 fail_unless(obj != NULL,
170                                 "sdb_store_expr_fieldvalue(SDB_STORE_NAME) = NULL; "
171                                 "expected: <expr>");
172         }
173         value = sdb_store_expr_constvalue(&datum);
174         fail_unless(value != NULL,
175                         "sdb_store_expr_constvalue(%s) = NULL; "
176                         "expected: <expr>", cmp_name_data[_i].name);
178         if (cmp_name_data[_i].re)
179                 m = sdb_store_regex_matcher(obj, value);
180         else
181                 m = sdb_store_eq_matcher(obj, value);
183         if (cmp_name_data[_i].type != SDB_HOST) {
184                 sdb_store_expr_t *iter;
185                 sdb_store_matcher_t *tmp;
186                 obj = sdb_store_expr_fieldvalue(SDB_FIELD_NAME);
187                 iter = sdb_store_expr_typed(cmp_name_data[_i].type, obj);
188                 tmp = sdb_store_any_matcher(iter, m);
189                 ck_assert(iter && m);
190                 sdb_object_deref(SDB_OBJ(iter));
191                 sdb_object_deref(SDB_OBJ(m));
192                 m = tmp;
193         }
194         sdb_object_deref(SDB_OBJ(obj));
195         sdb_object_deref(SDB_OBJ(value));
196         fail_unless(m != NULL,
197                         "sdb_store_%s_matcher(%s, %s) = NULL; "
198                         "expected: <matcher>",
199                         cmp_name_data[_i].re ? "regex" : "eq",
200                         SDB_STORE_TYPE_TO_NAME(cmp_name_data[_i].type),
201                         cmp_name_data[_i].name);
203         status = sdb_store_matcher_matches(m, host, /* filter */ NULL);
204         fail_unless(status == cmp_name_data[_i].expected,
205                         "sdb_store_matcher_matches(%s->%s, <host a>, NULL) = %d; "
206                         "expected: %d", SDB_STORE_TYPE_TO_NAME(cmp_name_data[_i].type),
207                         cmp_name_data[_i].name, status, cmp_name_data[_i].expected);
209         n = sdb_store_inv_matcher(m);
210         fail_unless(n != NULL,
211                         "sdb_store_inv_matcher() = NULL; expected: <matcher>");
212         sdb_object_deref(SDB_OBJ(m));
214         /* now match the inverted set of objects */
215         status = sdb_store_matcher_matches(n, host, /* filter */ NULL);
216         fail_unless(status == !cmp_name_data[_i].expected,
217                         "sdb_store_matcher_matches(%s->%s, <host a>, NULL) = %d; "
218                         "expected: %d", SDB_STORE_TYPE_TO_NAME(cmp_name_data[_i].type),
219                         cmp_name_data[_i].name, status, !cmp_name_data[_i].expected);
221         sdb_object_deref(SDB_OBJ(n));
222         sdb_object_deref(SDB_OBJ(host));
224 END_TEST
226 struct {
227         const char *attr;
228         const sdb_data_t value;
229         int expected_lt, expected_le, expected_eq, expected_ge, expected_gt;
230 } cmp_attr_data[] = {
231         { "k1", { SDB_TYPE_STRING,  { .string  = "v1" } },  0, 1, 1, 1, 0 },
232         { "k1", { SDB_TYPE_STRING,  { .string  = "v2" } },  1, 1, 0, 0, 0 },
233         { "k1", { SDB_TYPE_STRING,  { .string  = "v0" } },  0, 0, 0, 1, 1 },
234         { "k1", { SDB_TYPE_STRING,  { .string  = "0" } },   0, 0, 0, 1, 1 },
235         { "k2", { SDB_TYPE_INTEGER, { .integer = 123 } },   0, 1, 1, 1, 0 },
236         { "k2", { SDB_TYPE_INTEGER, { .integer = 124 } },   1, 1, 0, 0, 0 },
237         { "k2", { SDB_TYPE_INTEGER, { .integer = 122 } },   0, 0, 0, 1, 1 },
238         /* key does not exist */
239         { "k3", { SDB_TYPE_STRING,  { .string  = "v1" } },  0, 0, 0, 0, 0 },
240         { "k3", { SDB_TYPE_STRING,  { .string  = "123" } }, 0, 0, 0, 0, 0 },
241         { "k3", { SDB_TYPE_INTEGER, { .integer = 123 } },   0, 0, 0, 0, 0 },
242         /* type mismatch */
243         { "k1", { SDB_TYPE_INTEGER, { .integer = 0 } },     0, 0, 0, 1, 1 },
244         { "k2", { SDB_TYPE_STRING,  { .string  = "123" } }, 0, 1, 1, 1, 0 },
245 };
247 START_TEST(test_cmp_attr)
249         sdb_store_obj_t *host;
250         sdb_store_expr_t *attr;
251         sdb_store_expr_t *value;
252         char value_str[1024];
253         int status;
254         size_t j;
256         struct {
257                 sdb_store_matcher_t *(*matcher)(sdb_store_expr_t *,
258                                 sdb_store_expr_t *);
259                 int expected;
260         } tests[] = {
261                 { sdb_store_lt_matcher, cmp_attr_data[_i].expected_lt },
262                 { sdb_store_le_matcher, cmp_attr_data[_i].expected_le },
263                 { sdb_store_eq_matcher, cmp_attr_data[_i].expected_eq },
264                 { sdb_store_ge_matcher, cmp_attr_data[_i].expected_ge },
265                 { sdb_store_gt_matcher, cmp_attr_data[_i].expected_gt },
266         };
268         const char *op_str[] = { "<", "<=", "=", ">=", ">" };
269         ck_assert(SDB_STATIC_ARRAY_LEN(tests) == SDB_STATIC_ARRAY_LEN(op_str));
271         host = sdb_store_get_host("a");
272         fail_unless(host != NULL,
273                         "sdb_store_get_host(a) = NULL; expected: <host>");
275         sdb_data_format(&cmp_attr_data[_i].value,
276                         value_str, sizeof(value_str), SDB_UNQUOTED);
278         attr = sdb_store_expr_attrvalue(cmp_attr_data[_i].attr);
279         fail_unless(attr != NULL,
280                         "sdb_store_expr_attrvalue(%s) = NULL; expected: <expr>",
281                         cmp_attr_data[_i].attr);
283         value = sdb_store_expr_constvalue(&cmp_attr_data[_i].value);
284         fail_unless(value != NULL,
285                         "sdb_store_expr_constvalue(%s) = NULL; expected: <expr>",
286                         value_str);
288         for (j = 0; j < SDB_STATIC_ARRAY_LEN(tests); ++j) {
289                 sdb_store_matcher_t *m;
291                 m = tests[j].matcher(attr, value);
292                 fail_unless(m != NULL,
293                                 "sdb_store_<cond>_matcher() = NULL; expected: <matcher>");
295                 status = sdb_store_matcher_matches(m, host, /* filter */ NULL);
296                 fail_unless(status == tests[j].expected,
297                                 "sdb_store_matcher_matches(<attr[%s] %s %s>, "
298                                 "<host>, NULL) = %d; expected: %d",
299                                 cmp_attr_data[_i].attr, op_str[j], value_str,
300                                 status, tests[j].expected);
302                 sdb_object_deref(SDB_OBJ(m));
303         }
305         sdb_object_deref(SDB_OBJ(attr));
306         sdb_object_deref(SDB_OBJ(value));
307         sdb_object_deref(SDB_OBJ(host));
309 END_TEST
311 struct {
312         const char *host;
313         int field;
314         const sdb_data_t value;
315         int expected_lt, expected_le, expected_eq, expected_ge, expected_gt;
316 } cmp_obj_data[] = {
317         { "b", SDB_FIELD_NAME,
318                 { SDB_TYPE_STRING, { .string = "a" } },   0, 0, 0, 1, 1 },
319         { "b", SDB_FIELD_NAME,
320                 { SDB_TYPE_STRING, { .string = "b" } },   0, 1, 1, 1, 0 },
321         { "b", SDB_FIELD_NAME,
322                 { SDB_TYPE_STRING, { .string = "c" } },   1, 1, 0, 0, 0 },
323         /* last-update = 1 for all objects */
324         { "a", SDB_FIELD_LAST_UPDATE,
325                 { SDB_TYPE_DATETIME, { .datetime = 1 } }, 0, 1, 1, 1, 0 },
326         { "a", SDB_FIELD_LAST_UPDATE,
327                 { SDB_TYPE_DATETIME, { .datetime = 2 } }, 1, 1, 0, 0, 0 },
328         { "a", SDB_FIELD_LAST_UPDATE,
329                 { SDB_TYPE_DATETIME, { .datetime = 0 } }, 0, 0, 0, 1, 1 },
330         /* age > 0 for all objects */
331         { "a", SDB_FIELD_AGE,
332                 { SDB_TYPE_DATETIME, { .datetime = 0 } }, 0, 0, 0, 1, 1 },
333         /* interval = 0 for all objects */
334         { "a", SDB_FIELD_INTERVAL,
335                 { SDB_TYPE_DATETIME, { .datetime = 0 } }, 0, 1, 1, 1, 0 },
336         { "a", SDB_FIELD_INTERVAL,
337                 { SDB_TYPE_DATETIME, { .datetime = 1 } }, 1, 1, 0, 0, 0 },
338         /* type mismatch */
339         { "a", SDB_FIELD_LAST_UPDATE,
340                 { SDB_TYPE_INTEGER, { .integer = 0 } }, 0, 0, 0, 0, 0 },
341         { "a", SDB_FIELD_AGE,
342                 { SDB_TYPE_INTEGER, { .integer = 0 } }, 0, 0, 0, 0, 0 },
343         { "a", SDB_FIELD_INTERVAL,
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         { "a", SDB_FIELD_BACKEND,
348                 { SDB_TYPE_INTEGER, { .integer = 0 } }, 0, 0, 0, 0, 0 },
349         /* (64bit) integer value without zero-bytes */
350         { "a", SDB_FIELD_BACKEND,
351                 { SDB_TYPE_INTEGER, { .integer = 0xffffffffffffffffL } },
352                 0, 0, 0, 0, 0 },
353 };
355 START_TEST(test_cmp_obj)
357         sdb_store_obj_t *host;
358         sdb_store_expr_t *field;
359         sdb_store_expr_t *value;
360         char value_str[1024];
361         int status;
362         size_t j;
364         struct {
365                 sdb_store_matcher_t *(*matcher)(sdb_store_expr_t *,
366                                 sdb_store_expr_t *);
367                 int expected;
368         } tests[] = {
369                 { sdb_store_lt_matcher, cmp_obj_data[_i].expected_lt },
370                 { sdb_store_le_matcher, cmp_obj_data[_i].expected_le },
371                 { sdb_store_eq_matcher, cmp_obj_data[_i].expected_eq },
372                 { sdb_store_ge_matcher, cmp_obj_data[_i].expected_ge },
373                 { sdb_store_gt_matcher, cmp_obj_data[_i].expected_gt },
374         };
375         char *op_str[] = { "<", "<=", "=", ">=", ">" };
377         ck_assert(SDB_STATIC_ARRAY_LEN(tests) == SDB_STATIC_ARRAY_LEN(op_str));
379         host = sdb_store_get_host(cmp_obj_data[_i].host);
380         fail_unless(host != NULL,
381                         "sdb_store_get_host(%s) = NULL; expected: <host>",
382                         cmp_obj_data[_i].host);
384         sdb_data_format(&cmp_obj_data[_i].value,
385                         value_str, sizeof(value_str), SDB_UNQUOTED);
387         field = sdb_store_expr_fieldvalue(cmp_obj_data[_i].field);
388         fail_unless(field != NULL,
389                         "sdb_store_expr_fieldvalue(%d) = NULL; "
390                         "expected: <expr>", cmp_obj_data[_i].field);
392         value = sdb_store_expr_constvalue(&cmp_obj_data[_i].value);
393         fail_unless(value != NULL,
394                         "sdb_store_expr_constvalue(%s) = NULL; "
395                         "expected: <expr>", value_str);
397         for (j = 0; j < SDB_STATIC_ARRAY_LEN(tests); ++j) {
398                 char m_str[1024];
399                 sdb_store_matcher_t *m;
401                 snprintf(m_str, sizeof(m_str), "%s %s %s",
402                                 SDB_FIELD_TO_NAME(cmp_obj_data[_i].field),
403                                 op_str[j], value_str);
405                 m = tests[j].matcher(field, value);
406                 fail_unless(m != NULL,
407                                 "sdb_store_<cond>_matcher() = NULL; expected: <matcher>");
409                 status = sdb_store_matcher_matches(m, host, /* filter */ NULL);
410                 fail_unless(status == tests[j].expected,
411                                 "sdb_store_matcher_matches(<%s>, <host '%s'>, NULL) = %d; "
412                                 "expected: %d", m_str, cmp_obj_data[_i].host, status,
413                                 tests[j].expected);
415                 sdb_object_deref(SDB_OBJ(m));
416         }
418         sdb_object_deref(SDB_OBJ(field));
419         sdb_object_deref(SDB_OBJ(value));
420         sdb_object_deref(SDB_OBJ(host));
422 END_TEST
424 START_TEST(test_store_match_op)
426         sdb_store_obj_t *obj;
428         sdb_data_t d = { SDB_TYPE_STRING, { .string = "a" } };
429         sdb_store_expr_t *e = sdb_store_expr_constvalue(&d);
431         sdb_store_matcher_t *never = sdb_store_isnull_matcher(e);
432         sdb_store_matcher_t *always = sdb_store_inv_matcher(never);
434         struct {
435                 const char *op;
436                 sdb_store_matcher_t *left;
437                 sdb_store_matcher_t *right;
438                 int expected;
439         } golden_data[] = {
440                 { "OR",  always, always, 1 },
441                 { "OR",  always, never,  1 },
442                 { "OR",  never,  always, 1 },
443                 { "OR",  never,  never,  0 },
444                 { "AND", always, always, 1 },
445                 { "AND", always, never,  0 },
446                 { "AND", never,  always, 0 },
447                 { "AND", never,  never,  0 },
448         };
450         int status;
451         size_t i;
453         obj = sdb_store_get_host("a");
455         status = sdb_store_matcher_matches(always, obj, /* filter */ NULL);
456         fail_unless(status == 1,
457                         "INTERNAL ERROR: 'always' did not match host");
458         status = sdb_store_matcher_matches(never, obj, /* filter */ NULL);
459         fail_unless(status == 0,
460                         "INTERNAL ERROR: 'never' matches host");
462         for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
463                 sdb_store_matcher_t *m;
465                 if (! strcmp(golden_data[i].op, "OR"))
466                         m = sdb_store_dis_matcher(golden_data[i].left,
467                                         golden_data[i].right);
468                 else if (! strcmp(golden_data[i].op, "AND"))
469                         m = sdb_store_con_matcher(golden_data[i].left,
470                                         golden_data[i].right);
471                 else {
472                         fail("INTERNAL ERROR: unexpected operator %s", golden_data[i].op);
473                         continue;
474                 }
476 #define TO_NAME(v) (((v) == always) ? "always" \
477                 : ((v) == never) ? "never" : "<unknown>")
479                 status = sdb_store_matcher_matches(m, obj, /* filter */ NULL);
480                 fail_unless(status == golden_data[i].expected,
481                                 "%s(%s, %s, NULL) = %d; expected: %d", golden_data[i].op,
482                                 TO_NAME(golden_data[i].left), TO_NAME(golden_data[i].right),
483                                 status, golden_data[i].expected);
485 #undef TO_NAME
487                 sdb_object_deref(SDB_OBJ(m));
488         }
490         sdb_object_deref(SDB_OBJ(always));
491         sdb_object_deref(SDB_OBJ(never));
492         sdb_object_deref(SDB_OBJ(e));
494         sdb_object_deref(SDB_OBJ(obj));
496 END_TEST
498 static int
499 scan_cb(sdb_store_obj_t *obj, sdb_store_matcher_t *filter, void *user_data)
501         int *i = user_data;
503         if (! sdb_store_matcher_matches(filter, obj, NULL))
504                 return 0;
506         fail_unless(obj != NULL,
507                         "sdb_store_scan callback received NULL obj; expected: "
508                         "<store base obj>");
509         fail_unless(i != NULL,
510                         "sdb_store_scan callback received NULL user_data; "
511                         "expected: <pointer to data>");
513         ++(*i);
514         return 0;
515 } /* scan_cb */
517 struct {
518         const char *query;
519         const char *filter;
520         int expected;
521 } scan_data[] = {
522         /* TODO: check the name of the expected hosts */
523         { "name = 'a'", NULL,                        1 },
524         { "name = 'a'", "name = 'x'",                0 }, /* filter never matches */
525         { "name = 'a'",
526                 "NOT attribute['x'] = ''",               1 }, /* filter always matches */
527         { "name =~ 'a|b'", NULL,                     2 },
528         { "name =~ 'host'", NULL,                    0 },
529         { "name =~ '.'", NULL,                       3 },
530         { "ANY backend = 'backend'", NULL,           0 },
531         { "ALL backend = ''", NULL,                  3 }, /* backend is empty */
532         { "backend = ['backend']", NULL,             0 },
533         { "backend != ['backend']", NULL,            3 },
534         { "backend < ['backend']", NULL,             3 },
535         { "backend <= ['backend']", NULL,            3 },
536         { "backend >= ['backend']", NULL,            0 },
537         { "backend > ['backend']", NULL,             0 },
538         { "ANY metric.name = 'm1'", NULL,            2 },
539         { "ANY metric.name = 'm1'", "name = 'x'",    0 }, /* filter never matches */
540         { "ANY metric.name = 'm1'",
541                 "NOT attribute['x'] = ''",               2 }, /* filter always matches */
542         { "ANY metric.name =~ 'm'", NULL,            2 },
543         { "ALL metric.name =~ 'm'", NULL,            3 },
544         { "ANY metric.name =~ 'm'", "name !~ '1'",   1 },
545         { "ANY metric.name =~ 'm'", "name !~ 'm'",   0 },
546         { "ALL metric.name =~ '1'", NULL,            2 },
547         { "ALL metric.name =~ '2'", NULL,            1 },
548         { "ANY metric.name !~ 'm'", NULL,            0 },
549         { "ALL metric.name !~ 'm'", NULL,            1 },
550         { "ANY metric.name =~ 'x'", NULL,            0 },
551         { "ANY service.name = 's1'", NULL,           2 },
552         { "ANY service.name = 's1'", "name = 'x'",   0 }, /* filter never matches */
553         { "ANY service.name = 's1'",
554                 "NOT attribute['x'] = ''",               2 }, /* filter always matches */
555         { "ANY service.name =~ 's'", NULL,           2 },
556         { "ANY service.name =~ 's'", "name !~ 's'",  0 },
557         { "ANY service.name =~ 's'", "name !~ '1'",  2 },
558         { "ANY service.name !~ 's'", NULL,           0 },
559         { "ANY attribute.name = 'k1'", NULL,         2 },
560         { "ANY attribute.name = 'k1'", "name = 'x'", 0 }, /* filter never matches */
561         { "ANY attribute.name = 'k1'",
562                 "NOT attribute['x'] = ''",         2 }, /* filter always matches */
563         { "ANY attribute.name =~ 'k'", NULL,   2 },
564         { "ANY attribute.name =~ 'k'",
565                 "name !~ '1'",                     1 },
566         { "ANY attribute.name =~ 'k'",
567                 "name !~ 'k'",                     0 },
568         { "ANY attribute.name =~ '1'", NULL,   2 },
569         { "ANY attribute.name =~ '2'", NULL,   1 },
570         { "ANY attribute.name = 'x'", NULL,    0 },
571         { "ANY attribute.name =~ 'x'", NULL,   0 },
572         { "ALL attribute.name = 'k1'", NULL,   2 },
573         { "ANY attribute.value = 'v1'", NULL,  1 },
574         { "ANY attribute.value =~ 'v'", NULL,  2 },
575         { "ANY attribute.value = 123", NULL,   1 },
576         { "host.name = 'a'", NULL,             1 },
577         { "host.attribute['k1'] =~ 'v1'",
578                 NULL,                              1 },
579         { "host.attribute['x1'] IS NULL",
580                 NULL,                              3 },
581         /* not a boolean so neither TRUE nor FALSE: */
582         { "attribute['k1'] IS TRUE", NULL,     0 },
583         { "attribute['k1'] IS FALSE", NULL,    0 },
584         { "attribute['k1'] = 'v1'", NULL,      1 },
585         { "attribute['k1'] = 'v1'",
586                 "name != 'k1'",                    0 },
587         { "attribute['k1'] =~ 'v1'", NULL,     1 },
588         { "attribute['k1'] =~ '^v1$'", NULL,   1 },
589         { "attribute['k1'] =~ 'v'", NULL,      2 },
590         { "attribute['k1'] =~ '1'", NULL,      1 },
591         { "attribute['k1'] !~ 'v'", NULL,      0 },
592         { "attribute['k1'] = 'v2'", NULL,      1 },
593         { "attribute['k1'] =~ 'v2'", NULL,     1 },
594         { "attribute['x1'] =~ 'v'", NULL,      0 },
595         { "attribute['x1'] =~ 'NULL'", NULL,   0 },
596         { "attribute['x1'] !~ 'v'", NULL,      0 },
597         { "attribute['k1'] IS NULL", NULL,     1 },
598         { "attribute['x1'] IS NULL", NULL,     3 },
599         { "attribute['k1'] IS TRUE", NULL,     0 },
600         { "attribute['x1'] IS TRUE", NULL,     0 },
601         { "attribute['k1'] IS FALSE", NULL,    0 },
602         { "attribute['x1'] IS FALSE", NULL,    0 },
603         { "attribute['k1'] IS NOT NULL", NULL, 2 },
604         { "attribute['x1'] IS NOT NULL", NULL, 0 },
605         { "attribute['x1'] IS NOT TRUE", NULL, 3 },
606         { "attribute['k2'] < 123", NULL,       0 },
607         { "attribute['k2'] <= 123", NULL,      1 },
608         { "attribute['k2'] >= 123", NULL,      1 },
609         { "attribute['k2'] > 123", NULL,       0 },
610         { "attribute['k2'] = 123", NULL,       1 },
611         { "attribute['k2'] != 123", NULL,      0 },
612         { "attribute['k1'] != 'v1'", NULL,     1 },
613         { "attribute['k1'] != 'v2'", NULL,     1 },
614         { "ANY attribute.name != 'x' "
615           "AND attribute['k1'] !~ 'x'", NULL,  2 },
616 };
618 START_TEST(test_scan)
620         sdb_strbuf_t *errbuf = sdb_strbuf_create(64);
621         sdb_store_matcher_t *m, *filter = NULL;
622         sdb_ast_node_t *ast;
623         int check, n;
625         n = 0;
626         check = sdb_store_scan(SDB_HOST, /* matcher */ NULL, /* filter */ NULL,
627                         scan_cb, &n);
628         fail_unless(check == 0,
629                         "sdb_store_scan() = %d; expected: 0", check);
630         fail_unless(n == 3,
631                         "sdb_store_scan called callback %d times; expected: 3", (int)n);
633         ast = sdb_parser_parse_conditional(scan_data[_i].query, -1, errbuf);
634         m = sdb_store_query_prepare_matcher(ast);
635         sdb_object_deref(SDB_OBJ(ast));
636         fail_unless(m != NULL,
637                         "sdb_parser_parse_conditional(%s, -1) = NULL; expected: <ast> "
638                         "(parser error: %s)", scan_data[_i].query,
639                         sdb_strbuf_string(errbuf));
641         if (scan_data[_i].filter) {
642                 ast = sdb_parser_parse_conditional(scan_data[_i].filter, -1, errbuf);
643                 filter = sdb_store_query_prepare_matcher(ast);
644                 sdb_object_deref(SDB_OBJ(ast));
645                 fail_unless(filter != NULL,
646                                 "sdb_parser_parse_conditional(%s, -1) = NULL; "
647                                 "expected: <ast> (parser error: %s)",
648                                 scan_data[_i].filter, sdb_strbuf_string(errbuf));
649         }
651         n = 0;
652         sdb_store_scan(SDB_HOST, m, filter, scan_cb, &n);
653         fail_unless(n == scan_data[_i].expected,
654                         "sdb_store_scan(HOST, matcher{%s}, filter{%s}) "
655                         "found %d hosts; expected: %d", scan_data[_i].query,
656                         scan_data[_i].filter, n, scan_data[_i].expected);
658         sdb_object_deref(SDB_OBJ(filter));
659         sdb_object_deref(SDB_OBJ(m));
660         sdb_strbuf_destroy(errbuf);
662 END_TEST
664 TEST_MAIN("core::store_lookup")
666         TCase *tc = tcase_create("core");
667         tcase_add_checked_fixture(tc, populate, sdb_store_clear);
668         TC_ADD_LOOP_TEST(tc, cmp_name);
669         TC_ADD_LOOP_TEST(tc, cmp_attr);
670         TC_ADD_LOOP_TEST(tc, cmp_obj);
671         TC_ADD_LOOP_TEST(tc, scan);
672         tcase_add_test(tc, test_store_match_op);
673         ADD_TCASE(tc);
675 TEST_MAIN_END
677 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */