X-Git-Url: https://git.tokkee.org/?p=sysdb.git;a=blobdiff_plain;f=t%2Funit%2Fcore%2Fstore_test.c;h=dbf220cef4f26d718a505b8a9678812bbd0a1360;hp=ab3087acbc2cbe2101f1c877737048273e1c19b2;hb=da52b4da948c741d20c7a3995ce5d133af377def;hpb=f20bc8d5ec8abe86914eeedc741ca00109fb6ece diff --git a/t/unit/core/store_test.c b/t/unit/core/store_test.c index ab3087a..dbf220c 100644 --- a/t/unit/core/store_test.c +++ b/t/unit/core/store_test.c @@ -25,12 +25,17 @@ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +#if HAVE_CONFIG_H +# include "config.h" +#endif + #include "core/store.h" #include "core/store-private.h" -#include "libsysdb_test.h" +#include "testutils.h" #include #include +#include static void populate(void) @@ -407,97 +412,209 @@ START_TEST(test_store_service_attr) } END_TEST +static struct { + const char *hostname; + const char *attr; /* optional */ + int field; + int expected; + sdb_data_t value; +} get_field_data[] = { + { NULL, NULL, 0, -1, { SDB_TYPE_NULL, { 0 } } }, + { NULL, NULL, SDB_FIELD_LAST_UPDATE, -1, { SDB_TYPE_NULL, { 0 } } }, + { NULL, NULL, SDB_FIELD_INTERVAL, -1, { SDB_TYPE_NULL, { 0 } } }, + { NULL, NULL, SDB_FIELD_AGE, -1, { SDB_TYPE_NULL, { 0 } } }, + { NULL, NULL, SDB_FIELD_NAME, -1, { SDB_TYPE_NULL, { 0 } } }, + { NULL, NULL, SDB_FIELD_BACKEND, -1, { SDB_TYPE_NULL, { 0 } } }, + { NULL, NULL, SDB_FIELD_VALUE, -1, { SDB_TYPE_NULL, { 0 } } }, + { "host", NULL, SDB_FIELD_LAST_UPDATE, 0, { SDB_TYPE_DATETIME, { .datetime = 20 } } }, + { "host", NULL, SDB_FIELD_INTERVAL, 0, { SDB_TYPE_DATETIME, { .datetime = 10 } } }, + /* the test will handle AGE specially */ + { "host", NULL, SDB_FIELD_AGE, 0, { SDB_TYPE_NULL, { 0 } } }, + { "host", NULL, SDB_FIELD_NAME, 0, { SDB_TYPE_STRING, { .string = "host" } } }, + { "host", NULL, SDB_FIELD_BACKEND, 0, { SDB_TYPE_ARRAY | SDB_TYPE_STRING, { .array = { 0, NULL } } } }, + { "host", NULL, SDB_FIELD_VALUE, -1, { SDB_TYPE_NULL, { 0 } } }, + { "host", "attr", SDB_FIELD_LAST_UPDATE, 0, { SDB_TYPE_DATETIME, { .datetime = 20 } } }, + { "host", "attr", SDB_FIELD_INTERVAL, 0, { SDB_TYPE_DATETIME, { .datetime = 10 } } }, + /* the test will handle AGE specially */ + { "host", "attr", SDB_FIELD_AGE, 0, { SDB_TYPE_NULL, { 0 } } }, + { "host", "attr", SDB_FIELD_NAME, 0, { SDB_TYPE_STRING, { .string = "attr" } } }, + { "host", "attr", SDB_FIELD_BACKEND, 0, { SDB_TYPE_ARRAY | SDB_TYPE_STRING, { .array = { 0, NULL } } } }, + { "host", "attr", SDB_FIELD_VALUE, 0, { SDB_TYPE_INTEGER, { .integer = 1 } } }, + { "host", "attr", SDB_FIELD_VALUE, 0, { SDB_TYPE_DECIMAL, { .decimal = 2.0 } } }, + { "host", "attr", SDB_FIELD_VALUE, 0, { SDB_TYPE_STRING, { .string = "foo" } } }, + { "host", "attr", SDB_FIELD_VALUE, 0, { SDB_TYPE_DATETIME, { .datetime = 1234567890L } } }, + { "host", "a", SDB_FIELD_LAST_UPDATE, -1, { SDB_TYPE_NULL, { 0 } } }, + { "host", "a", SDB_FIELD_INTERVAL, -1, { SDB_TYPE_NULL, { 0 } } }, + { "host", "a", SDB_FIELD_AGE, -1, { SDB_TYPE_NULL, { 0 } } }, + { "host", "a", SDB_FIELD_NAME, -1, { SDB_TYPE_NULL, { 0 } } }, + { "host", "a", SDB_FIELD_BACKEND, -1, { SDB_TYPE_NULL, { 0 } } }, + { "host", "a", SDB_FIELD_VALUE, -1, { SDB_TYPE_NULL, { 0 } } }, + { "host", "a", SDB_FIELD_VALUE, -1, { SDB_TYPE_NULL, { 0 } } }, + { "host", "a", SDB_FIELD_VALUE, -1, { SDB_TYPE_NULL, { 0 } } }, + { "host", "a", SDB_FIELD_VALUE, -1, { SDB_TYPE_NULL, { 0 } } }, +}; + +/* returns a tuple */ +#define OBJ_NAME(obj) \ + (obj) ? SDB_STORE_TYPE_TO_NAME(obj->type) : "NULL", \ + (obj) ? SDB_OBJ(obj)->name : "" START_TEST(test_get_field) { - sdb_store_obj_t *host; + sdb_store_obj_t *obj = NULL; sdb_data_t value = SDB_DATA_INIT; + char value_str[128], expected_value_str[128]; + sdb_time_t now = sdb_gettime(); int check; sdb_store_host("host", 10); sdb_store_host("host", 20); + sdb_store_attribute("host", "attr", &get_field_data[_i].value, 10); + sdb_store_attribute("host", "attr", &get_field_data[_i].value, 20); + + if (get_field_data[_i].hostname) { + obj = sdb_store_get_host(get_field_data[_i].hostname); + ck_assert(obj != NULL); + + if (get_field_data[_i].attr) { + sdb_store_obj_t *tmp = sdb_store_get_child(obj, + SDB_ATTRIBUTE, get_field_data[_i].attr); + sdb_object_deref(SDB_OBJ(obj)); + obj = tmp; + } + } - host = sdb_store_get_host("host"); - fail_unless(host != NULL, - "INTERNAL ERROR: store doesn't have host after adding it"); + check = sdb_store_get_field(obj, get_field_data[_i].field, NULL); + fail_unless(check == get_field_data[_i].expected, + "sdb_store_get_field(%s %s, %s, NULL) = %d; expected: %d", + OBJ_NAME(obj), SDB_FIELD_TO_NAME(get_field_data[_i].field), + check, get_field_data[_i].expected); + check = sdb_store_get_field(obj, get_field_data[_i].field, &value); + fail_unless(check == get_field_data[_i].expected, + "sdb_store_get_field(%s %s, %s, ) = %d; expected: %d", + OBJ_NAME(obj), SDB_FIELD_TO_NAME(get_field_data[_i].field), + check, get_field_data[_i].expected); + + if (get_field_data[_i].expected) { + sdb_object_deref(SDB_OBJ(obj)); + return; + } - check = sdb_store_get_field(NULL, 0, NULL); - fail_unless(check < 0, - "sdb_store_get_field(NULL, 0, NULL) = %d; expected: <0"); - check = sdb_store_get_field(NULL, SDB_FIELD_LAST_UPDATE, NULL); - fail_unless(check < 0, - "sdb_store_get_field(NULL, SDB_FIELD_LAST_UPDATE, NULL) = %d; " - "expected: <0"); - check = sdb_store_get_field(NULL, SDB_FIELD_LAST_UPDATE, &value); - fail_unless(check < 0, - "sdb_store_get_field(NULL, SDB_FIELD_LAST_UPDATE, ) = %d; " - "expected: <0"); - - check = sdb_store_get_field(host, SDB_FIELD_LAST_UPDATE, NULL); - fail_unless(check == 0, - "sdb_store_get_field(, SDB_FIELD_LAST_UPDATE, NULL) = %d; " - "expected: 0"); - /* 'name' is dynamically allocated; make sure it's not leaked even - * if there is no result parameter */ - check = sdb_store_get_field(host, SDB_FIELD_NAME, NULL); - fail_unless(check == 0, - "sdb_store_get_field(, SDB_FIELD_LAST_UPDATE, NULL) = %d; " - "expected: 0"); + if (get_field_data[_i].field == SDB_FIELD_AGE) { + get_field_data[_i].value.type = SDB_TYPE_DATETIME; + get_field_data[_i].value.data.datetime = now; + } - check = sdb_store_get_field(host, SDB_FIELD_NAME, &value); - fail_unless(check == 0, - "sdb_store_get_field(, SDB_FIELD_NAME, ) = " - "%d; expected: 0"); - fail_unless((value.type == SDB_TYPE_STRING) - && (! strcmp(value.data.string, "host")), - "sdb_store_get_field(, SDB_FIELD_NAME, ) " - "returned value {%d, %s}; expected {%d, host}", - value.type, value.data.string, SDB_TYPE_STRING); + sdb_data_format(&value, value_str, sizeof(value_str), 0); + sdb_data_format(&get_field_data[_i].value, expected_value_str, + sizeof(expected_value_str), 0); + + if (get_field_data[_i].field == SDB_FIELD_AGE) { + fail_unless((value.type == SDB_TYPE_DATETIME) + && (value.data.datetime >= now), + "sdb_store_get_field(%s %s, %s, ) " + "returned value %s; expected >=%s", OBJ_NAME(obj), + SDB_FIELD_TO_NAME(get_field_data[_i].field), + value_str, expected_value_str); + } + else { + fail_unless(! sdb_data_cmp(&value, &get_field_data[_i].value), + "sdb_store_get_field(%s %s, %s, ) " + "returned value %s; expected %s", OBJ_NAME(obj), + SDB_FIELD_TO_NAME(get_field_data[_i].field), + value_str, expected_value_str); + } sdb_data_free_datum(&value); + sdb_object_deref(SDB_OBJ(obj)); +} +END_TEST +#undef OBJ_NAME - check = sdb_store_get_field(host, SDB_FIELD_LAST_UPDATE, &value); - fail_unless(check == 0, - "sdb_store_get_field(, SDB_FIELD_LAST_UPDATE, ) = " - "%d; expected: 0"); - fail_unless((value.type == SDB_TYPE_DATETIME) - && (value.data.datetime == 20), - "sdb_store_get_field(, SDB_FIELD_LAST_UPDATE, ) " - "returned value {%d, %lu}; expected {%d, 20}", - value.type, value.data.datetime, SDB_TYPE_DATETIME); - - check = sdb_store_get_field(host, SDB_FIELD_AGE, &value); - fail_unless(check == 0, - "sdb_store_get_field(, SDB_FIELD_AGE, ) = " - "%d; expected: 0"); - /* let's assume we're at least in year 1980 ;-) */ - fail_unless((value.type == SDB_TYPE_DATETIME) - && (value.data.datetime > 10L * SDB_INTERVAL_YEAR), - "sdb_store_get_field(, SDB_FIELD_AGE, ) " - "returned value {%d, %lu}; expected {%d, >%lu}", - value.type, value.data.datetime, - SDB_TYPE_DATETIME, 10L * SDB_INTERVAL_YEAR); - - check = sdb_store_get_field(host, SDB_FIELD_INTERVAL, &value); - fail_unless(check == 0, - "sdb_store_get_field(, SDB_FIELD_INTERVAL, ) = " - "%d; expected: 0"); - fail_unless((value.type == SDB_TYPE_DATETIME) - && (value.data.datetime == 10), - "sdb_store_get_field(, SDB_FIELD_INTERVAL, ) " - "returned value {%d, %lu}; expected {%d, 10}", - value.type, value.data.datetime, SDB_TYPE_DATETIME); - - check = sdb_store_get_field(host, SDB_FIELD_BACKEND, &value); - fail_unless(check == 0, - "sdb_store_get_field(, SDB_FIELD_BACKEND, ) = " - "%d; expected: 0"); - /* there are no backends in this test */ - fail_unless((value.type == (SDB_TYPE_ARRAY | SDB_TYPE_STRING)) - && (value.data.array.length == 0) - && (value.data.array.values == NULL), - "sdb_store_get_field(, SDB_FIELD_BACKEND, ) " - "returned value {%d, %lu, %p}; expected {%d, 0, NULL}", - value.type, value.data.array.length, value.data.array.values, - SDB_TYPE_ARRAY | SDB_TYPE_STRING); +START_TEST(test_get_child) +{ + struct { + const char *host; + const char *name; + int type; + int expected; + } golden_data[] = { + { "h1", NULL, SDB_HOST, 0 }, + { "h1", NULL, SDB_SERVICE, -1 }, + { "h1", NULL, SDB_METRIC, -1 }, + { "h1", NULL, SDB_ATTRIBUTE, -1 }, + { "h2", NULL, SDB_HOST, 0 }, + { "h2", NULL, SDB_SERVICE, -1 }, + { "h2", NULL, SDB_METRIC, -1 }, + { "h2", NULL, SDB_ATTRIBUTE, -1 }, + { "h3", NULL, SDB_HOST, -1 }, + { "h1", "k1", SDB_ATTRIBUTE, 0 }, + { "h1", "x1", SDB_ATTRIBUTE, -1 }, + { "h2", "k1", SDB_ATTRIBUTE, -1 }, + { "h1", "k1", SDB_SERVICE, -1 }, + { "h1", "k1", SDB_METRIC, -1 }, + { "h1", "s1", SDB_SERVICE, -1 }, + { "h2", "s1", SDB_SERVICE, 0 }, + { "h1", "m2", SDB_METRIC, 0 }, + { "h2", "m2", SDB_METRIC, -1 }, + }; + + size_t i; + + populate(); + + for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) { + sdb_store_obj_t *obj; + const char *expected_name = golden_data[i].host; + + obj = sdb_store_get_host(golden_data[i].host); + if (golden_data[i].expected && (golden_data[i].type == SDB_HOST)) + fail_unless(obj == NULL, + "sdb_store_get_host(%s) = %p; expected: NULL", + golden_data[i].host, obj); + else + fail_unless(obj != NULL, + "sdb_store_get_host(%s) = NULL; expected: ", + golden_data[i].host); + + if (golden_data[i].type != SDB_HOST) { + sdb_store_obj_t *tmp; + + expected_name = golden_data[i].name; + + tmp = sdb_store_get_child(obj, + golden_data[i].type, golden_data[i].name); + if (golden_data[i].expected) + fail_unless(tmp == NULL, + "sdb_store_get_child(<%s>, %s, %s) = %p; " + "expected: NULL", golden_data[i].host, + SDB_STORE_TYPE_TO_NAME(golden_data[i].type), + golden_data[i].name, tmp); + else + fail_unless(tmp != NULL, + "sdb_store_get_child(<%s>, %s, %s) = NULL; " + "expected: ", golden_data[i].host, + SDB_STORE_TYPE_TO_NAME(golden_data[i].type), + golden_data[i].name); + + sdb_object_deref(SDB_OBJ(obj)); + obj = tmp; + } + + if (golden_data[i].expected) + continue; + + fail_unless(obj->type == golden_data[i].type, + "sdb_store_get_<%s>(%s, %s) returned object of type %d; " + "expected: %d", SDB_STORE_TYPE_TO_NAME(golden_data[i].type), + golden_data[i].host, golden_data[i].name, obj->type, + golden_data[i].type); + fail_unless(! strcasecmp(SDB_OBJ(obj)->name, expected_name), + "sdb_store_get_<%s>(%s, %s) returned object named '%s'; " + "expected: '%s'", SDB_STORE_TYPE_TO_NAME(golden_data[i].type), + golden_data[i].host, golden_data[i].name, SDB_OBJ(obj)->name, + expected_name); + + sdb_object_deref(SDB_OBJ(obj)); + } } END_TEST @@ -603,8 +720,8 @@ START_TEST(test_scan) /* empty store */ check = sdb_store_scan(SDB_HOST, /* m, filter = */ NULL, NULL, scan_count, &i); - fail_unless(check == -1, - "sdb_store_scan(HOST), empty store = %d; expected: -1", check); + fail_unless(check == 0, + "sdb_store_scan(HOST), empty store = %d; expected: 0", check); fail_unless(i == 0, "sdb_store_scan(HOST) called callback %d times; " "expected: 0", (int)i); @@ -648,13 +765,9 @@ START_TEST(test_scan) } END_TEST -Suite * -core_store_suite(void) +TEST_MAIN("core::store") { - Suite *s = suite_create("core::store"); - TCase *tc; - - tc = tcase_create("core"); + TCase *tc = tcase_create("core"); tcase_add_test(tc, test_store_host); tcase_add_test(tc, test_store_get_host); tcase_add_test(tc, test_store_attr); @@ -662,14 +775,14 @@ core_store_suite(void) tcase_add_test(tc, test_store_metric_attr); tcase_add_test(tc, test_store_service); tcase_add_test(tc, test_store_service_attr); - tcase_add_test(tc, test_get_field); + TC_ADD_LOOP_TEST(tc, get_field); + tcase_add_test(tc, test_get_child); tcase_add_test(tc, test_interval); tcase_add_test(tc, test_scan); tcase_add_unchecked_fixture(tc, NULL, sdb_store_clear); - suite_add_tcase(s, tc); - - return s; -} /* core_store_suite */ + ADD_TCASE(tc); +} +TEST_MAIN_END /* vim: set tw=78 sw=4 ts=4 noexpandtab : */