index 15a67b10a7809ac5f92d4a534a99f3600fd1cf65..8ead947bfa5b600bd51409e24735d2e8d4c80030 100644 (file)
#include "core/store.h"
#include "core/store-private.h"
+#include "frontend/parser.h"
#include "libsysdb_test.h"
#include <check.h>
"sdb_store_get_host(a) = NULL; expected: <host>");
for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
- sdb_store_matcher_t *h, *s, *a;
+ sdb_store_matcher_t *h, *s, *a, *n;
int status;
s = sdb_store_service_matcher(golden_data[i].service_name,
golden_data[i].attr_value, golden_data[i].attr_value_re,
status, golden_data[i].expected);
+ n = sdb_store_inv_matcher(h);
+ fail_unless(n != NULL,
+ "sdb_store_inv_matcher() = NULL; expected: <matcher>");
sdb_object_deref(SDB_OBJ(h));
+
+ /* now match the inverted set of objects */
+ status = sdb_store_matcher_matches(n, obj);
+ fail_unless(status == !golden_data[i].expected,
+ "sdb_store_matcher_matches(NOT{{%s, %s},{%s, %s},"
+ "{%s, %s, %s, %s}}, <host a>) = %d; expected: %d",
+ golden_data[i].hostname, golden_data[i].hostname_re,
+ golden_data[i].service_name, golden_data[i].service_name_re,
+ golden_data[i].attr_name, golden_data[i].attr_name_re,
+ golden_data[i].attr_value, golden_data[i].attr_value_re,
+ status, !golden_data[i].expected);
+
+ sdb_object_deref(SDB_OBJ(n));
}
+
+ sdb_object_deref(SDB_OBJ(obj));
}
END_TEST
sdb_object_deref(SDB_OBJ(always));
sdb_object_deref(SDB_OBJ(never));
+
+ sdb_object_deref(SDB_OBJ(obj));
}
END_TEST
int expected;
} golden_data[] = {
{ "host", "name", "=", "hostname", MATCHER_HOST },
+ { "host", "name", "!=", "hostname", MATCHER_NOT },
{ "host", "name", "=~", "hostname", MATCHER_HOST },
+ { "host", "name", "!~", "hostname", MATCHER_NOT },
{ "host", "attr", "=", "hostname", -1 },
+ { "host", "attr", "!=", "hostname", -1 },
{ "host", "name", "&^", "hostname", -1 },
- { "service", "name", "=", "srvname", MATCHER_SERVICE },
- { "service", "name", "=", "srvname", MATCHER_SERVICE },
+ { "service", "name", "=", "srvname", MATCHER_HOST },
+ { "service", "name", "!=", "srvname", MATCHER_NOT },
+ { "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_ATTR },
- { "attribute", "attr", "=", "attrname", MATCHER_ATTR },
- { "attribute", "attr", "=~", "attrname", MATCHER_ATTR },
+ { "attribute", "name", "=", "attrname", MATCHER_HOST },
+ { "attribute", "name", "!=", "attrname", MATCHER_NOT },
+ { "attribute", "name", "=~", "attrname", MATCHER_HOST },
+ { "attribute", "name", "!~", "attrname", MATCHER_NOT },
+ { "attribute", "attr", "=", "attrname", MATCHER_HOST },
+ { "attribute", "attr", "!=", "attrname", MATCHER_NOT },
+ { "attribute", "attr", "=~", "attrname", MATCHER_HOST },
+ { "attribute", "attr", "!~", "attrname", MATCHER_NOT },
{ "attribute", "attr", "&^", "attrname", -1 },
};
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: "
START_TEST(test_lookup)
{
- intptr_t i = 0;
- int check;
+#define PTR_RE "0x[0-9a-f]+"
+ struct {
+ const char *query;
+ int expected;
+ const char *tostring_re;
+ } golden_data[] = {
+ { "host.name = 'a'", 1,
+ "HOST\\{ NAME\\{ 'a', \\(nil\\) \\}, SERVICE\\{\\}, ATTR\\{\\} \\}" },
+ { "host.name =~ 'a|b'", 2,
+ "HOST\\{ NAME\\{ NULL, "PTR_RE" \\}, SERVICE\\{\\}, ATTR\\{\\} \\}" },
+ { "host.name =~ 'host'", 0,
+ "HOST\\{ NAME\\{ NULL, "PTR_RE" \\}, SERVICE\\{\\}, ATTR\\{\\} \\}" },
+ { "host.name =~ '.'", 3,
+ "HOST\\{ NAME\\{ NULL, "PTR_RE" \\}, SERVICE\\{\\}, ATTR\\{\\} \\}" },
+ { "service.name = 's1'", 2,
+ "HOST\\{ NAME\\{ NULL, \\(nil\\) \\}, SERVICE\\{ "
+ "NAME\\{ 's1', \\(nil\\) }, ATTR\\{\\} "
+ "\\}, ATTR\\{\\} \\}" },
+ { "service.name =~ 's'", 2,
+ "HOST\\{ NAME\\{ NULL, \\(nil\\) \\}, SERVICE\\{ "
+ "NAME\\{ NULL, "PTR_RE" }, ATTR\\{\\} "
+ "\\}, ATTR\\{\\} \\}" },
+ { "service.name !~ 's'", 1,
+ "(NOT, HOST\\{ NAME\\{ NULL, \\(nil\\) \\}, SERVICE\\{ "
+ "NAME\\{ NULL, "PTR_RE" }, ATTR\\{\\} "
+ "\\}, ATTR\\{\\} \\})" },
+ { "attribute.name = 'k1'", 1,
+ "HOST\\{ NAME\\{ NULL, \\(nil\\) \\}, SERVICE\\{\\}, ATTR\\{ "
+ "NAME\\{ 'k1', \\(nil\\) }, VALUE\\{ NULL, \\(nil\\) \\} "
+ "\\} \\}" },
+ { "attribute.name = 'x'", 0,
+ "HOST\\{ NAME\\{ NULL, \\(nil\\) \\}, SERVICE\\{\\}, ATTR\\{ "
+ "NAME\\{ 'x', \\(nil\\) }, VALUE\\{ NULL, \\(nil\\) \\} "
+ "\\} \\}" },
+ { "attribute.k1 = 'v1'", 1,
+ "HOST\\{ NAME\\{ NULL, \\(nil\\) \\}, SERVICE\\{\\}, ATTR\\{ "
+ "NAME\\{ 'k1', \\(nil\\) }, VALUE\\{ 'v1', \\(nil\\) \\} "
+ "\\} \\}" },
+ { "attribute.k1 != 'v1'", 2,
+ "(NOT, HOST\\{ NAME\\{ NULL, \\(nil\\) \\}, SERVICE\\{\\}, ATTR\\{ "
+ "NAME\\{ 'k1', \\(nil\\) }, VALUE\\{ 'v1', \\(nil\\) \\} "
+ "\\} \\})" },
+ { "attribute.k1 != 'v2'", 3,
+ "(NOT, HOST\\{ NAME\\{ NULL, \\(nil\\) \\}, SERVICE\\{\\}, ATTR\\{ "
+ "NAME\\{ 'k1', \\(nil\\) }, VALUE\\{ 'v2', \\(nil\\) \\} "
+ "\\} \\})" },
+ { "attribute.name != 'x' "
+ "AND attribute.y !~ 'x'", 3,
+ "\\(AND, \\(NOT, HOST\\{ NAME\\{ NULL, \\(nil\\) \\}, SERVICE\\{\\}, ATTR\\{ "
+ "NAME\\{ 'x', \\(nil\\) }, VALUE\\{ NULL, \\(nil\\) \\} "
+ "\\} \\}\\), \\(NOT, HOST\\{ NAME\\{ NULL, \\(nil\\) \\}, SERVICE\\{\\}, ATTR\\{ "
+ "NAME\\{ 'y', \\(nil\\) }, VALUE\\{ NULL, "PTR_RE" \\} "
+ "\\} \\}\\)\\)" },
+ };
+
+ int check, n;
+ size_t i;
- check = sdb_store_lookup(NULL, lookup_cb, &i);
+ n = 0;
+ check = sdb_store_lookup(NULL, lookup_cb, &n);
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);
+ fail_unless(n == 3,
+ "sdb_store_lookup called callback %d times; expected: 3", (int)n);
+
+ for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
+ sdb_store_matcher_t *m;
+ char buf[4096];
+
+ 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);
+ fail_unless(sdb_regmatches(golden_data[i].tostring_re,
+ sdb_store_matcher_tostring(m, buf, sizeof(buf))) == 0,
+ "sdb_fe_parse_matcher(%s, -1) = %s; expected: %s",
+ golden_data[i].query,
+ sdb_store_matcher_tostring(m, buf, sizeof(buf)),
+ golden_data[i].tostring_re);
+
+ 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