6265b321f0b5481edf6bda51cb572793effdb254
[collectd.git] / src / utils_vl_lookup_test.c
1 /**
2  * collectd - src/utils_vl_lookup_test.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 "utils_vl_lookup.h"
29
30 static _Bool expect_new_obj = 0;
31 static _Bool have_new_obj = 0;
32
33 static identifier_t last_class_ident;
34 static identifier_t last_obj_ident;
35
36 static data_source_t dsrc_test = { "value", DS_TYPE_DERIVE, 0.0, NAN };
37 static data_set_t const ds_test = { "test", 1, &dsrc_test };
38
39 static data_source_t dsrc_unknown = { "value", DS_TYPE_DERIVE, 0.0, NAN };
40 static data_set_t const ds_unknown = { "unknown", 1, &dsrc_unknown };
41
42 static int lookup_obj_callback (data_set_t const *ds,
43     value_list_t const *vl,
44     void *user_class, void *user_obj)
45 {
46   identifier_t *class = user_class;
47   identifier_t *obj = user_obj;
48
49   assert (expect_new_obj == have_new_obj);
50
51   memcpy (&last_class_ident, class, sizeof (last_class_ident));
52   memcpy (&last_obj_ident, obj, sizeof (last_obj_ident));
53
54   if (strcmp (obj->plugin_instance, "failure") == 0)
55     return (-1);
56
57   return (0);
58 }
59
60 static void *lookup_class_callback (data_set_t const *ds,
61     value_list_t const *vl, void *user_class)
62 {
63   identifier_t *class = user_class;
64   identifier_t *obj;
65
66   assert (expect_new_obj);
67
68   memcpy (&last_class_ident, class, sizeof (last_class_ident));
69   
70   obj = malloc (sizeof (*obj));
71   strncpy (obj->host, vl->host, sizeof (obj->host));
72   strncpy (obj->plugin, vl->plugin, sizeof (obj->plugin));
73   strncpy (obj->plugin_instance, vl->plugin_instance, sizeof (obj->plugin_instance));
74   strncpy (obj->type, vl->type, sizeof (obj->type));
75   strncpy (obj->type_instance, vl->type_instance, sizeof (obj->type_instance));
76
77   have_new_obj = 1;
78
79   return ((void *) obj);
80 }
81
82 static void checked_lookup_add (lookup_t *obj, /* {{{ */
83     char const *host,
84     char const *plugin, char const *plugin_instance,
85     char const *type, char const *type_instance)
86 {
87   identifier_t ident;
88   void *user_class;
89   int status;
90
91   memset (&ident, 0, sizeof (ident));
92   strncpy (ident.host, host, sizeof (ident.host));
93   strncpy (ident.plugin, plugin, sizeof (ident.plugin));
94   strncpy (ident.plugin_instance, plugin_instance, sizeof (ident.plugin_instance));
95   strncpy (ident.type, type, sizeof (ident.type));
96   strncpy (ident.type_instance, type_instance, sizeof (ident.type_instance));
97
98   user_class = malloc (sizeof (ident));
99   memmove (user_class, &ident, sizeof (ident));
100
101   status = lookup_add (obj, &ident, user_class);
102   assert (status == 0);
103 } /* }}} void test_add */
104
105 static int checked_lookup_search (lookup_t *obj,
106     char const *host,
107     char const *plugin, char const *plugin_instance,
108     char const *type, char const *type_instance,
109     _Bool expect_new)
110 {
111   int status;
112   value_list_t vl = VALUE_LIST_STATIC;
113   data_set_t const *ds = &ds_unknown;
114
115   strncpy (vl.host, host, sizeof (vl.host));
116   strncpy (vl.plugin, plugin, sizeof (vl.plugin));
117   strncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
118   strncpy (vl.type, type, sizeof (vl.type));
119   strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
120
121   if (strcmp (vl.type, "test") == 0)
122     ds = &ds_test;
123
124   expect_new_obj = expect_new;
125   have_new_obj = 0;
126
127   status = lookup_search (obj, ds, &vl);
128   return (status);
129 }
130
131 static lookup_t *checked_lookup_create (void)
132 {
133   lookup_t *obj = lookup_create (
134       lookup_class_callback,
135       lookup_obj_callback,
136       (void *) free,
137       (void *) free);
138   assert (obj != NULL);
139   return (obj);
140 }
141
142 static void testcase0 (void)
143 {
144   lookup_t *obj = checked_lookup_create ();
145
146   checked_lookup_add (obj, "/any/", "test", "", "test", "/all/");
147   checked_lookup_search (obj, "host0", "test", "", "test", "0",
148       /* expect new = */ 1);
149   checked_lookup_search (obj, "host0", "test", "", "test", "1",
150       /* expect new = */ 0);
151   checked_lookup_search (obj, "host1", "test", "", "test", "0",
152       /* expect new = */ 1);
153   checked_lookup_search (obj, "host1", "test", "", "test", "1",
154       /* expect new = */ 0);
155
156   lookup_destroy (obj);
157 }
158
159 static void testcase1 (void)
160 {
161   lookup_t *obj = checked_lookup_create ();
162
163   checked_lookup_add (obj, "/any/", "/all/", "/all/", "test", "/all/");
164   checked_lookup_search (obj, "host0", "plugin0", "", "test", "0",
165       /* expect new = */ 1);
166   checked_lookup_search (obj, "host0", "plugin0", "", "test", "1",
167       /* expect new = */ 0);
168   checked_lookup_search (obj, "host0", "plugin1", "", "test", "0",
169       /* expect new = */ 0);
170   checked_lookup_search (obj, "host0", "plugin1", "", "test", "1",
171       /* expect new = */ 0);
172   checked_lookup_search (obj, "host1", "plugin0", "", "test", "0",
173       /* expect new = */ 1);
174   checked_lookup_search (obj, "host1", "plugin0", "", "test", "1",
175       /* expect new = */ 0);
176   checked_lookup_search (obj, "host1", "plugin1", "", "test", "0",
177       /* expect new = */ 0);
178   checked_lookup_search (obj, "host1", "plugin1", "", "test", "1",
179       /* expect new = */ 0);
180
181   lookup_destroy (obj);
182 }
183
184 static void testcase2 (void)
185 {
186   lookup_t *obj = checked_lookup_create ();
187   int status;
188
189   checked_lookup_add (obj, "/any/", "plugin0", "", "test", "/all/");
190   checked_lookup_add (obj, "/any/", "/all/", "", "test", "ti0");
191
192   status = checked_lookup_search (obj, "host0", "plugin1", "", "test", "",
193       /* expect new = */ 0);
194   assert (status == 0);
195   status = checked_lookup_search (obj, "host0", "plugin0", "", "test", "",
196       /* expect new = */ 1);
197   assert (status == 1);
198   status = checked_lookup_search (obj, "host0", "plugin1", "", "test", "ti0",
199       /* expect new = */ 1);
200   assert (status == 1);
201   status = checked_lookup_search (obj, "host0", "plugin0", "", "test", "ti0",
202       /* expect new = */ 0);
203   assert (status == 2);
204
205   lookup_destroy (obj);
206 }
207
208 int main (int argc, char **argv) /* {{{ */
209 {
210   testcase0 ();
211   testcase1 ();
212   testcase2 ();
213   return (EXIT_SUCCESS);
214 } /* }}} int main */