Code

2573e64765543ee4c6cc2802250083d8ed362eec
[sysdb.git] / t / integration / mock_plugin.c
1 /*
2  * SysDB - t/integration/mock_plugin.c
3  * Copyright (C) 2014 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 #if HAVE_CONFIG_H
29 #       include "config.h"
30 #endif /* HAVE_CONFIG_H */
32 #include "sysdb.h"
33 #include "core/plugin.h"
34 #include "core/store.h"
35 #include "utils/error.h"
37 #include "liboconfig/utils.h"
39 #include <stdlib.h>
40 #include <string.h>
42 #define MAGIC_DATA (void *)0x4711
44 SDB_PLUGIN_MAGIC;
46 static const char *hostnames[] = {
47         "some.host.name",
48         "other.host.name",
49         "host1.example.com",
50         "host2.example.com",
51         "localhost",
52 };
54 static struct {
55         const char *hostname;
56         const char *service;
57 } services[] = {
58         { "some.host.name", "mock service" },
59         { "some.host.name", "other service" },
60         { "some.host.name", "database" },
61         { "host1.example.com", "mock service" },
62         { "host1.example.com", "example service one" },
63         { "host1.example.com", "example service two" },
64         { "host1.example.com", "example service three" },
65         { "host2.example.com", "mock service" },
66         { "host2.example.com", "example service one" },
67         { "host2.example.com", "example service two" },
68         { "host2.example.com", "example service three" },
69         { "localhost", "sysdbd" },
70 };
72 static struct {
73         const char *hostname;
74         const char *name;
75         const char *value;
76 } attributes[] = {
77         { "other.host.name", "attribute", "value" },
78         { "other.host.name", "architecture", "varch" },
79         { "other.host.name", "processor0", "Vendor TYPE4711 CPU MAGIC" },
80         { "other.host.name", "processor1", "Vendor TYPE4711 CPU MAGIC" },
81         { "other.host.name", "processor2", "Vendor TYPE4711 CPU MAGIC" },
82         { "other.host.name", "processor3", "Vendor TYPE4711 CPU MAGIC" },
83         { "host1.example.com", "other attribute", "special value" },
84         { "host1.example.com", "architecture", "x42" },
85         { "host1.example.com", "timezone", "UTC" },
86         { "host2.example.com", "other attribute", "special value" },
87         { "host2.example.com", "architecture", "x42" },
88         { "host2.example.com", "timezone", "UTC" },
89         { "localhost", "attr1", "value1" },
90         { "localhost", "attr2", "value2" },
91         { "localhost", "attr3", "value3" },
92 };
94 /*
95  * plugin API
96  */
98 static int
99 mock_init(sdb_object_t *user_data)
101         if (SDB_OBJ_WRAPPER(user_data)->data != MAGIC_DATA) {
102                 sdb_log(SDB_LOG_ERR, "mock::plugin: Invalid user data %p "
103                                 "passed to init", SDB_OBJ_WRAPPER(user_data)->data);
104                 exit(1);
105         }
106         return 0;
107 } /* mock_init */
109 static int
110 mock_shutdown(sdb_object_t *user_data)
112         if (SDB_OBJ_WRAPPER(user_data)->data != MAGIC_DATA) {
113                 sdb_log(SDB_LOG_ERR, "mock::plugin: Invalid user data %p "
114                                 "passed to shutdown", SDB_OBJ_WRAPPER(user_data)->data);
115                 exit(1);
116         }
117         return 0;
118 } /* mock_shutdown */
120 static int
121 mock_collect(sdb_object_t *user_data)
123         size_t i;
124         int check;
126         if (SDB_OBJ_WRAPPER(user_data)->data != MAGIC_DATA) {
127                 sdb_log(SDB_LOG_ERR, "mock::plugin: Invalid user data %p "
128                                 "passed to collect", SDB_OBJ_WRAPPER(user_data)->data);
129                 exit(1);
130         }
132         for (i = 0; i < SDB_STATIC_ARRAY_LEN(hostnames); ++i) {
133                 if ((check = sdb_store_host(hostnames[i], sdb_gettime()))) {
134                         sdb_log(SDB_LOG_ERR, "mock::plugin: Failed to store host: "
135                                         "status %d", check);
136                         exit(1);
137                 }
138         }
139         for (i = 0; i < SDB_STATIC_ARRAY_LEN(services); ++i) {
140                 if ((check = sdb_store_service(services[i].hostname,
141                                                 services[i].service, sdb_gettime()))) {
142                         sdb_log(SDB_LOG_ERR, "mock::plugin: Failed to store service: "
143                                         "status %d", check);
144                         exit(1);
145                 }
146         }
147         for (i = 0; i < SDB_STATIC_ARRAY_LEN(attributes); ++i) {
148                 sdb_data_t datum = { SDB_TYPE_STRING, { .string = NULL } };
149                 datum.data.string = strdup(attributes[i].value);
151                 if ((check = sdb_store_attribute(attributes[i].hostname,
152                                                 attributes[i].name, &datum, sdb_gettime()))) {
153                         sdb_log(SDB_LOG_ERR, "mock::plugin: Failed to store attribute: "
154                                         "status %d", check);
155                         exit(1);
156                 }
158                 free(datum.data.string);
159         }
160         return 0;
161 } /* mock_collect */
163 static int
164 mock_config(oconfig_item_t *ci)
166         sdb_object_t *user_data;
168         int i;
170         if (! ci) /* deconfiguration */
171                 return 0;
173         for (i = 0; i < ci->children_num; ++i) {
174                 oconfig_item_t *child = ci->children + i;
176                 sdb_log(SDB_LOG_WARNING, "mock::plugin: Ignoring unknown config "
177                                 "option '%s'", child->key);
178         }
180         user_data = sdb_object_create_wrapper("mock_data", MAGIC_DATA, NULL);
181         if (! user_data) {
182                 sdb_log(SDB_LOG_ERR, "mock::plugin: Failed to allocate user data");
183                 exit(1);
184         }
186         sdb_plugin_register_init("main", mock_init, user_data);
187         sdb_plugin_register_shutdown("main", mock_shutdown, user_data);
188         sdb_plugin_register_collector("main", mock_collect,
189                         /* interval = */ NULL, user_data);
191         sdb_object_deref(user_data);
192         return 0;
193 } /* mock_config */
195 int
196 sdb_module_init(sdb_plugin_info_t *info)
198         sdb_plugin_set_info(info, SDB_PLUGIN_INFO_NAME, "test::integration::mock");
199         sdb_plugin_set_info(info, SDB_PLUGIN_INFO_DESC, "a mock plugin");
200         sdb_plugin_set_info(info, SDB_PLUGIN_INFO_COPYRIGHT,
201                         "Copyright (C) 2012 Sebastian 'tokkee' Harl <sh@tokkee.org>");
202         sdb_plugin_set_info(info, SDB_PLUGIN_INFO_LICENSE, "BSD");
203         sdb_plugin_set_info(info, SDB_PLUGIN_INFO_VERSION, SDB_VERSION);
204         sdb_plugin_set_info(info, SDB_PLUGIN_INFO_PLUGIN_VERSION, SDB_VERSION);
206         sdb_plugin_register_config(mock_config);
207         return 0;
208 } /* sdb_module_init */
210 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */