From: Sebastian Harl Date: Sat, 7 Dec 2013 14:46:21 +0000 (+0100) Subject: t/core/store_test: Added initial tests for the object store. X-Git-Tag: sysdb-0.1.0~314^2~5 X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=d8ccc0a6f81d7f658a4667cfe4105fab34f1b4a4;p=sysdb.git t/core/store_test: Added initial tests for the object store. For now, testing basic insertion operations. --- diff --git a/t/Makefile.am b/t/Makefile.am index c31ac50..87140cf 100644 --- a/t/Makefile.am +++ b/t/Makefile.am @@ -7,6 +7,7 @@ check_PROGRAMS = libsysdb_test libsysdb_net_test libsysdb_test_SOURCES = \ libsysdb_test.c libsysdb_test.h \ core/object_test.c \ + core/store_test.c \ utils/channel_test.c \ utils/dbi_test.c \ utils/llist_test.c \ diff --git a/t/core/store_test.c b/t/core/store_test.c new file mode 100644 index 0000000..0c06436 --- /dev/null +++ b/t/core/store_test.c @@ -0,0 +1,171 @@ +/* + * SysDB - t/core/store_test.c + * Copyright (C) 2013 Sebastian 'tokkee' Harl + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "core/store.h" +#include "libsysdb_test.h" + +#include + +START_TEST(test_store_host) +{ + struct { + const char *name; + sdb_time_t last_update; + int expected; + } golden_data[] = { + { "a", 2, 0 }, + { "a", 3, 0 }, + { "a", 1, 1 }, + { "b", 2, 0 }, + { "b", 1, 1 }, + { "A", 1, 1 }, /* case-insensitive */ + { "A", 4, 0 }, + }; + + struct { + const char *name; + _Bool has; + } golden_hosts[] = { + { "a", 1 == 1 }, + { "b", 1 == 1 }, + { "c", 0 == 1 }, + { "A", 1 == 1 }, + }; + + size_t i; + + for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) { + int status; + + status = sdb_store_host(golden_data[i].name, + golden_data[i].last_update); + fail_unless(status == golden_data[i].expected, + "sdb_store_host(%s, %d) = %d; expected: %d", + golden_data[i].name, (int)golden_data[i].last_update, + status, golden_data[i].expected); + } + + for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_hosts); ++i) { + _Bool has; + + has = sdb_store_has_host(golden_hosts[i].name); + fail_unless(has == golden_hosts[i].has, + "sdb_store_has_host(%s) = %d; expected: %d", + golden_hosts[i].name, has, golden_hosts[i].has); + } +} +END_TEST + +START_TEST(test_store_attr) +{ + struct { + const char *host; + const char *key; + const char *value; + sdb_time_t last_update; + int expected; + } golden_data[] = { + { "k", "k", "v", 1, -1 }, + { "k", "k", "v", 1, -1 }, /* retry to ensure the host is not created */ + { "l", "k1", "v1", 1, 0 }, + { "l", "k1", "v2", 2, 0 }, + { "l", "k1", "v3", 1, 1 }, + { "l", "k2", "v1", 1, 0 }, + { "m", "k", "v1", 2, 0 }, + { "m", "k", "v2", 1, 1 }, + }; + + size_t i; + + sdb_store_host("l", 1); + sdb_store_host("m", 1); + for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) { + int status; + + status = sdb_store_attribute(golden_data[i].host, + golden_data[i].key, golden_data[i].value, + golden_data[i].last_update); + fail_unless(status == golden_data[i].expected, + "sdb_store_attribute(%s, %s, %s, %d) = %d; expected: %d", + golden_data[i].host, golden_data[i].key, golden_data[i].value, + golden_data[i].last_update, status, golden_data[i].expected); + } +} +END_TEST + +START_TEST(test_store_service) +{ + struct { + const char *host; + const char *svc; + sdb_time_t last_update; + int expected; + } golden_data[] = { + { "k", "s", 1, -1 }, + { "k", "s", 1, -1 }, /* retry to ensure the host is not created */ + { "l", "s1", 1, 0 }, + { "l", "s1", 2, 0 }, + { "l", "s1", 1, 1 }, + { "l", "s2", 1, 0 }, + { "m", "s", 2, 0 }, + { "m", "s", 1, 1 }, + }; + + size_t i; + + sdb_store_host("m", 1); + sdb_store_host("l", 1); + for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) { + int status; + + status = sdb_store_service(golden_data[i].host, + golden_data[i].svc, golden_data[i].last_update); + fail_unless(status == golden_data[i].expected, + "sdb_store_attribute(%s, %s, %d) = %d; expected: %d", + golden_data[i].host, golden_data[i].svc, + golden_data[i].last_update, status, golden_data[i].expected); + } +} +END_TEST + +Suite * +core_store_suite(void) +{ + Suite *s = suite_create("core::store"); + TCase *tc; + + tc = tcase_create("core"); + tcase_add_test(tc, test_store_host); + tcase_add_test(tc, test_store_attr); + tcase_add_test(tc, test_store_service); + suite_add_tcase(s, tc); + + return s; +} /* core_store_suite */ + +/* vim: set tw=78 sw=4 ts=4 noexpandtab : */ + diff --git a/t/libsysdb_test.c b/t/libsysdb_test.c index 91909b8..c21f0be 100644 --- a/t/libsysdb_test.c +++ b/t/libsysdb_test.c @@ -38,6 +38,7 @@ main(void) suite_creator_t creators[] = { { core_object_suite, NULL }, + { core_store_suite, NULL }, { util_channel_suite, NULL }, { util_dbi_suite, NULL }, { util_llist_suite, NULL }, diff --git a/t/libsysdb_test.h b/t/libsysdb_test.h index eadeca5..f9fddeb 100644 --- a/t/libsysdb_test.h +++ b/t/libsysdb_test.h @@ -63,6 +63,10 @@ typedef struct { Suite * core_object_suite(void); +/* t/core/store_test */ +Suite * +core_store_suite(void); + /* t/utils/channel_test */ Suite * util_channel_suite(void);