Code

parser: Make sure that each comparator matcher is a host matcher.
[sysdb.git] / t / core / store_lookup_test.c
index a16d343d37d0a9991e244d419c6226ecc64c23d5..3b9a6d0cc9c65dec37cdd9fe16a109a7d2e370f8 100644 (file)
@@ -27,6 +27,7 @@
 
 #include "core/store.h"
 #include "core/store-private.h"
+#include "frontend/parser.h"
 #include "libsysdb_test.h"
 
 #include <check.h>
@@ -354,20 +355,20 @@ START_TEST(test_parse_cmp)
                { "host",      "attr", "=",  "hostname", -1 },
                { "host",      "attr", "!=", "hostname", -1 },
                { "host",      "name", "&^", "hostname", -1 },
-               { "service",   "name", "=",  "srvname",  MATCHER_SERVICE },
+               { "service",   "name", "=",  "srvname",  MATCHER_HOST },
                { "service",   "name", "!=", "srvname",  MATCHER_NOT },
-               { "service",   "name", "=~", "srvname",  MATCHER_SERVICE },
+               { "service",   "name", "=~", "srvname",  MATCHER_HOST },
                { "service",   "name", "!~", "srvname",  MATCHER_NOT },
                { "service",   "attr", "=",  "srvname",  -1 },
                { "service",   "attr", "!=", "srvname",  -1 },
                { "service",   "name", "&^", "srvname",  -1 },
-               { "attribute", "name", "=",  "attrname", MATCHER_ATTR },
+               { "attribute", "name", "=",  "attrname", MATCHER_HOST },
                { "attribute", "name", "!=", "attrname", MATCHER_NOT },
-               { "attribute", "name", "=~", "attrname", MATCHER_ATTR },
+               { "attribute", "name", "=~", "attrname", MATCHER_HOST },
                { "attribute", "name", "!~", "attrname", MATCHER_NOT },
-               { "attribute", "attr", "=",  "attrname", MATCHER_ATTR },
+               { "attribute", "attr", "=",  "attrname", MATCHER_HOST },
                { "attribute", "attr", "!=", "attrname", MATCHER_NOT },
-               { "attribute", "attr", "=~", "attrname", MATCHER_ATTR },
+               { "attribute", "attr", "=~", "attrname", MATCHER_HOST },
                { "attribute", "attr", "!~", "attrname", MATCHER_NOT },
                { "attribute", "attr", "&^", "attrname", -1 },
        };
@@ -404,7 +405,7 @@ END_TEST
 static int
 lookup_cb(sdb_store_base_t *obj, void *user_data)
 {
-       intptr_t *i = user_data;
+       int *i = user_data;
 
        fail_unless(obj != NULL,
                        "sdb_store_lookup callback received NULL obj; expected: "
@@ -419,14 +420,47 @@ lookup_cb(sdb_store_base_t *obj, void *user_data)
 
 START_TEST(test_lookup)
 {
-       intptr_t i = 0;
-       int check;
+       struct {
+               const char *query;
+               int expected;
+       } golden_data[] = {
+               { "host.name = 'a'",       1 },
+               { "host.name =~ 'a|b'",    2 },
+               { "host.name =~ 'host'",   0 },
+               { "host.name =~ '.'",      3 },
+               { "service.name = 's1'",   2 },
+               { "service.name =~ 's'",   2 },
+               { "service.name !~ 's'",   1 },
+               { "attribute.name = 'k1'", 1 },
+               { "attribute.name = 'x'",  0 },
+               { "attribute.k1 = 'v1'",   1 },
+               { "attribute.k1 != 'v1'",  2 },
+               { "attribute.k1 != 'v2'",  3 },
+       };
+
+       size_t i;
+       int check, n;
 
+       i = 0;
        check = sdb_store_lookup(NULL, lookup_cb, &i);
        fail_unless(check == 0,
                        "sdb_store_lookup() = %d; expected: 0", check);
        fail_unless(i == 3,
                        "sdb_store_lookup called callback %d times; expected: 3", (int)i);
+
+       for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
+               sdb_store_matcher_t *m = sdb_fe_parse_matcher(golden_data[i].query, -1);
+               fail_unless(m != NULL,
+                               "sdb_fe_parse_matcher(%s, -1) = NULL; expected: <matcher>",
+                               golden_data[i].query);
+
+               n = 0;
+               sdb_store_lookup(m, lookup_cb, &n);
+               fail_unless(n == golden_data[i].expected,
+                               "sdb_store_lookup(matcher{%s}) found %d hosts; expected: %d",
+                               golden_data[i].query, n, golden_data[i].expected);
+               sdb_object_deref(SDB_OBJ(m));
+       }
 }
 END_TEST