treewide: replace memset to 0 with initializers
[collectd.git] / src / utils_vl_lookup_test.c
1 /**
2  * collectd - src/tests/test_utils_vl_lookup.c
3  * Copyright (C) 2012       Florian Forster
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  *
23  * Authors:
24  *   Florian Forster <octo at collectd.org>
25  **/
26
27 #include "collectd.h"
28 #include "testing.h"
29 #include "utils_vl_lookup.h"
30
31 static _Bool expect_new_obj = 0;
32 static _Bool have_new_obj = 0;
33
34 static identifier_t last_class_ident;
35 static identifier_t last_obj_ident;
36
37 static data_source_t dsrc_test = { "value", DS_TYPE_DERIVE, 0.0, NAN };
38 static data_set_t const ds_test = { "test", 1, &dsrc_test };
39
40 static data_source_t dsrc_unknown = { "value", DS_TYPE_DERIVE, 0.0, NAN };
41 static data_set_t const ds_unknown = { "unknown", 1, &dsrc_unknown };
42
43 static int lookup_obj_callback (data_set_t const *ds,
44     value_list_t const *vl,
45     void *user_class, void *user_obj)
46 {
47   identifier_t *class = user_class;
48   identifier_t *obj = user_obj;
49
50   OK1(expect_new_obj == have_new_obj,
51       (expect_new_obj ? "New obj is created." : "Updating existing obj."));
52
53   memcpy (&last_class_ident, class, sizeof (last_class_ident));
54   memcpy (&last_obj_ident, obj, sizeof (last_obj_ident));
55
56   if (strcmp (obj->plugin_instance, "failure") == 0)
57     return (-1);
58
59   return (0);
60 }
61
62 static void *lookup_class_callback (data_set_t const *ds,
63     value_list_t const *vl, void *user_class)
64 {
65   identifier_t *class = user_class;
66   identifier_t *obj;
67
68   assert (expect_new_obj);
69
70   memcpy (&last_class_ident, class, sizeof (last_class_ident));
71
72   obj = malloc (sizeof (*obj));
73   strncpy (obj->host, vl->host, sizeof (obj->host));
74   strncpy (obj->plugin, vl->plugin, sizeof (obj->plugin));
75   strncpy (obj->plugin_instance, vl->plugin_instance, sizeof (obj->plugin_instance));
76   strncpy (obj->type, vl->type, sizeof (obj->type));
77   strncpy (obj->type_instance, vl->type_instance, sizeof (obj->type_instance));
78
79   have_new_obj = 1;
80
81   return ((void *) obj);
82 }
83
84 static int checked_lookup_add (lookup_t *obj, /* {{{ */
85     char const *host,
86     char const *plugin, char const *plugin_instance,
87     char const *type, char const *type_instance,
88     unsigned int group_by)
89 {
90   identifier_t ident;
91   void *user_class;
92
93   strncpy (ident.host, host, sizeof (ident.host));
94   strncpy (ident.plugin, plugin, sizeof (ident.plugin));
95   strncpy (ident.plugin_instance, plugin_instance, sizeof (ident.plugin_instance));
96   strncpy (ident.type, type, sizeof (ident.type));
97   strncpy (ident.type_instance, type_instance, sizeof (ident.type_instance));
98
99   user_class = malloc (sizeof (ident));
100   memmove (user_class, &ident, sizeof (ident));
101
102   OK(lookup_add (obj, &ident, group_by, user_class) == 0);
103   return 0;
104 } /* }}} int checked_lookup_add */
105
106 static int checked_lookup_search (lookup_t *obj,
107     char const *host,
108     char const *plugin, char const *plugin_instance,
109     char const *type, char const *type_instance,
110     _Bool expect_new)
111 {
112   int status;
113   value_list_t vl = VALUE_LIST_STATIC;
114   data_set_t const *ds = &ds_unknown;
115
116   strncpy (vl.host, host, sizeof (vl.host));
117   strncpy (vl.plugin, plugin, sizeof (vl.plugin));
118   strncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
119   strncpy (vl.type, type, sizeof (vl.type));
120   strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
121
122   if (strcmp (vl.type, "test") == 0)
123     ds = &ds_test;
124
125   expect_new_obj = expect_new;
126   have_new_obj = 0;
127
128   status = lookup_search (obj, ds, &vl);
129   return (status);
130 }
131
132 DEF_TEST(group_by_specific_host)
133 {
134   lookup_t *obj;
135   CHECK_NOT_NULL (obj = lookup_create (
136         lookup_class_callback, lookup_obj_callback, (void *) free, (void *) free));
137
138   checked_lookup_add (obj, "/.*/", "test", "", "test", "/.*/", LU_GROUP_BY_HOST);
139   checked_lookup_search (obj, "host0", "test", "", "test", "0",
140       /* expect new = */ 1);
141   checked_lookup_search (obj, "host0", "test", "", "test", "1",
142       /* expect new = */ 0);
143   checked_lookup_search (obj, "host1", "test", "", "test", "0",
144       /* expect new = */ 1);
145   checked_lookup_search (obj, "host1", "test", "", "test", "1",
146       /* expect new = */ 0);
147
148   lookup_destroy (obj);
149   return (0);
150 }
151
152 DEF_TEST(group_by_any_host)
153 {
154   lookup_t *obj;
155   CHECK_NOT_NULL (obj = lookup_create (
156         lookup_class_callback, lookup_obj_callback, (void *) free, (void *) free));
157
158   checked_lookup_add (obj, "/.*/", "/.*/", "/.*/", "test", "/.*/", LU_GROUP_BY_HOST);
159   checked_lookup_search (obj, "host0", "plugin0", "", "test", "0",
160       /* expect new = */ 1);
161   checked_lookup_search (obj, "host0", "plugin0", "", "test", "1",
162       /* expect new = */ 0);
163   checked_lookup_search (obj, "host0", "plugin1", "", "test", "0",
164       /* expect new = */ 0);
165   checked_lookup_search (obj, "host0", "plugin1", "", "test", "1",
166       /* expect new = */ 0);
167   checked_lookup_search (obj, "host1", "plugin0", "", "test", "0",
168       /* expect new = */ 1);
169   checked_lookup_search (obj, "host1", "plugin0", "", "test", "1",
170       /* expect new = */ 0);
171   checked_lookup_search (obj, "host1", "plugin1", "", "test", "0",
172       /* expect new = */ 0);
173   checked_lookup_search (obj, "host1", "plugin1", "", "test", "1",
174       /* expect new = */ 0);
175
176   lookup_destroy (obj);
177   return (0);
178 }
179
180 DEF_TEST(multiple_lookups)
181 {
182   lookup_t *obj;
183   int status;
184
185   CHECK_NOT_NULL (obj = lookup_create (
186         lookup_class_callback, lookup_obj_callback, (void *) free, (void *) free));
187
188   checked_lookup_add (obj, "/.*/", "plugin0", "", "test", "/.*/", LU_GROUP_BY_HOST);
189   checked_lookup_add (obj, "/.*/", "/.*/", "", "test", "ti0", LU_GROUP_BY_HOST);
190
191   status = checked_lookup_search (obj, "host0", "plugin1", "", "test", "",
192       /* expect new = */ 0);
193   assert (status == 0);
194   status = checked_lookup_search (obj, "host0", "plugin0", "", "test", "",
195       /* expect new = */ 1);
196   assert (status == 1);
197   status = checked_lookup_search (obj, "host0", "plugin1", "", "test", "ti0",
198       /* expect new = */ 1);
199   assert (status == 1);
200   status = checked_lookup_search (obj, "host0", "plugin0", "", "test", "ti0",
201       /* expect new = */ 0);
202   assert (status == 2);
203
204   lookup_destroy (obj);
205   return (0);
206 }
207
208 DEF_TEST(regex)
209 {
210   lookup_t *obj;
211   CHECK_NOT_NULL (obj = lookup_create (
212         lookup_class_callback, lookup_obj_callback, (void *) free, (void *) free));
213
214   checked_lookup_add (obj, "/^db[0-9]\\./", "cpu", "/.*/", "cpu", "/.*/",
215       LU_GROUP_BY_TYPE_INSTANCE);
216   checked_lookup_search (obj, "db0.example.com", "cpu", "0", "cpu", "user",
217       /* expect new = */ 1);
218   checked_lookup_search (obj, "db0.example.com", "cpu", "0", "cpu", "idle",
219       /* expect new = */ 1);
220   checked_lookup_search (obj, "db0.example.com", "cpu", "1", "cpu", "user",
221       /* expect new = */ 0);
222   checked_lookup_search (obj, "db0.example.com", "cpu", "1", "cpu", "idle",
223       /* expect new = */ 0);
224   checked_lookup_search (obj, "app0.example.com", "cpu", "0", "cpu", "user",
225       /* expect new = */ 0);
226   checked_lookup_search (obj, "app0.example.com", "cpu", "0", "cpu", "idle",
227       /* expect new = */ 0);
228   checked_lookup_search (obj, "db1.example.com", "cpu", "0", "cpu", "user",
229       /* expect new = */ 0);
230   checked_lookup_search (obj, "db1.example.com", "cpu", "0", "cpu", "idle",
231       /* expect new = */ 0);
232   checked_lookup_search (obj, "db1.example.com", "cpu", "0", "cpu", "system",
233       /* expect new = */ 1);
234
235   lookup_destroy (obj);
236   return (0);
237 }
238
239 int main (int argc, char **argv) /* {{{ */
240 {
241   RUN_TEST(group_by_specific_host);
242   RUN_TEST(group_by_any_host);
243   RUN_TEST(multiple_lookups);
244   RUN_TEST(regex);
245
246   END_TEST;
247 } /* }}} int main */