Code

t/core/store_test: Added initial tests for the object store.
[sysdb.git] / t / core / store_test.c
1 /*
2  * SysDB - t/core/store_test.c
3  * Copyright (C) 2013 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 #include "core/store.h"
29 #include "libsysdb_test.h"
31 #include <check.h>
33 START_TEST(test_store_host)
34 {
35         struct {
36                 const char *name;
37                 sdb_time_t  last_update;
38                 int         expected;
39         } golden_data[] = {
40                 { "a", 2, 0 },
41                 { "a", 3, 0 },
42                 { "a", 1, 1 },
43                 { "b", 2, 0 },
44                 { "b", 1, 1 },
45                 { "A", 1, 1 }, /* case-insensitive */
46                 { "A", 4, 0 },
47         };
49         struct {
50                 const char *name;
51                 _Bool       has;
52         } golden_hosts[] = {
53                 { "a", 1 == 1 },
54                 { "b", 1 == 1 },
55                 { "c", 0 == 1 },
56                 { "A", 1 == 1 },
57         };
59         size_t i;
61         for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
62                 int status;
64                 status = sdb_store_host(golden_data[i].name,
65                                 golden_data[i].last_update);
66                 fail_unless(status == golden_data[i].expected,
67                                 "sdb_store_host(%s, %d) = %d; expected: %d",
68                                 golden_data[i].name, (int)golden_data[i].last_update,
69                                 status, golden_data[i].expected);
70         }
72         for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_hosts); ++i) {
73                 _Bool has;
75                 has = sdb_store_has_host(golden_hosts[i].name);
76                 fail_unless(has == golden_hosts[i].has,
77                                 "sdb_store_has_host(%s) = %d; expected: %d",
78                                 golden_hosts[i].name, has, golden_hosts[i].has);
79         }
80 }
81 END_TEST
83 START_TEST(test_store_attr)
84 {
85         struct {
86                 const char *host;
87                 const char *key;
88                 const char *value;
89                 sdb_time_t  last_update;
90                 int         expected;
91         } golden_data[] = {
92                 { "k", "k", "v", 1, -1 },
93                 { "k", "k", "v", 1, -1 }, /* retry to ensure the host is not created */
94                 { "l", "k1", "v1", 1, 0 },
95                 { "l", "k1", "v2", 2, 0 },
96                 { "l", "k1", "v3", 1, 1 },
97                 { "l", "k2", "v1", 1, 0 },
98                 { "m", "k", "v1", 2, 0 },
99                 { "m", "k", "v2", 1, 1 },
100         };
102         size_t i;
104         sdb_store_host("l", 1);
105         sdb_store_host("m", 1);
106         for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
107                 int status;
109                 status = sdb_store_attribute(golden_data[i].host,
110                                 golden_data[i].key, golden_data[i].value,
111                                 golden_data[i].last_update);
112                 fail_unless(status == golden_data[i].expected,
113                                 "sdb_store_attribute(%s, %s, %s, %d) = %d; expected: %d",
114                                 golden_data[i].host, golden_data[i].key, golden_data[i].value,
115                                 golden_data[i].last_update, status, golden_data[i].expected);
116         }
118 END_TEST
120 START_TEST(test_store_service)
122         struct {
123                 const char *host;
124                 const char *svc;
125                 sdb_time_t  last_update;
126                 int         expected;
127         } golden_data[] = {
128                 { "k", "s", 1, -1 },
129                 { "k", "s", 1, -1 }, /* retry to ensure the host is not created */
130                 { "l", "s1", 1, 0 },
131                 { "l", "s1", 2, 0 },
132                 { "l", "s1", 1, 1 },
133                 { "l", "s2", 1, 0 },
134                 { "m", "s", 2, 0 },
135                 { "m", "s", 1, 1 },
136         };
138         size_t i;
140         sdb_store_host("m", 1);
141         sdb_store_host("l", 1);
142         for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
143                 int status;
145                 status = sdb_store_service(golden_data[i].host,
146                                 golden_data[i].svc, golden_data[i].last_update);
147                 fail_unless(status == golden_data[i].expected,
148                                 "sdb_store_attribute(%s, %s, %d) = %d; expected: %d",
149                                 golden_data[i].host, golden_data[i].svc,
150                                 golden_data[i].last_update, status, golden_data[i].expected);
151         }
153 END_TEST
155 Suite *
156 core_store_suite(void)
158         Suite *s = suite_create("core::store");
159         TCase *tc;
161         tc = tcase_create("core");
162         tcase_add_test(tc, test_store_host);
163         tcase_add_test(tc, test_store_attr);
164         tcase_add_test(tc, test_store_service);
165         suite_add_tcase(s, tc);
167         return s;
168 } /* core_store_suite */
170 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */