Debian Codegraph @

src/redfish_test.c

raw
1 /**
2 * collectd - src/redfish_test.c
3 *
4 * Copyright(c) 2018 Intel Corporation. All rights reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 *
24 * Authors:
25 * Martin Kennelly <[email protected]>
26 * Marcin Mozejko <[email protected]>
27 * Adrian Boczkowski <[email protected]>
28 **/
29
30 #define plugin_dispatch_values redfish_test_plugin_dispatch_values_mock
31 #include "redfish.c"
32 #include "testing.h"
33
34 #define VALUE_CACHE_SIZE (1)
35
36 static value_list_t last_dispatched_value_list;
37 static value_t last_dispatched_values[VALUE_CACHE_SIZE];
38 int redfish_test_plugin_dispatch_values_mock(value_list_t const *vl) {
39 last_dispatched_value_list = *vl;
40 size_t len = MIN(vl->values_len, VALUE_CACHE_SIZE);
41 for (size_t i = 0; i < len; ++i) {
42 last_dispatched_values[i] = vl->values[i];
43 }
44 last_dispatched_value_list.values = last_dispatched_values;
45 return 0;
46 }
47
48 static value_list_t *redfish_test_get_last_dispatched_value_list() {
49 return &last_dispatched_value_list;
50 }
51
52 DEF_TEST(read_queries) {
53 oconfig_item_t *ci = calloc(1, sizeof(*ci));
54
55 assert(ci != NULL);
56 ci->values = calloc(3, sizeof(*ci->values));
57 assert(ci->values != NULL);
58 ci->values_num = 3;
59 ci->values[0].value.string = "temperatures";
60 ci->values[0].type = OCONFIG_TYPE_STRING;
61 ci->values[1].value.string = "fans";
62 ci->values[1].type = OCONFIG_TYPE_STRING;
63 ci->values[2].value.string = "power";
64 ci->values[2].type = OCONFIG_TYPE_STRING;
65
66 char **queries;
67 int ret = redfish_read_queries(ci, &queries);
68
69 EXPECT_EQ_INT(0, ret);
70 EXPECT_EQ_STR("temperatures", queries[0]);
71 EXPECT_EQ_STR("fans", queries[1]);
72 EXPECT_EQ_STR("power", queries[2]);
73
74 sfree(ci->values);
75 sfree(ci);
76
77 for (int j = 0; j < 3; j++)
78 sfree(queries[j]);
79 sfree(queries);
80 return 0;
81 }
82
83 DEF_TEST(convert_val) {
84 redfish_value_t val = {.string = "1"};
85 redfish_value_type_t src_type = VAL_TYPE_STR;
86 int dst_type = DS_TYPE_GAUGE;
87 value_t vl = {0};
88 int ret = redfish_convert_val(&val, src_type, &vl, dst_type);
89 EXPECT_EQ_INT(0, ret);
90 OK(vl.gauge == 1.0);
91
92 val.integer = 1;
93 src_type = VAL_TYPE_INT;
94 dst_type = DS_TYPE_GAUGE;
95 ret = redfish_convert_val(&val, src_type, &vl, dst_type);
96 EXPECT_EQ_INT(0, ret);
97 OK(vl.gauge == 1.0);
98
99 val.real = 1.0;
100 src_type = VAL_TYPE_REAL;
101 dst_type = DS_TYPE_GAUGE;
102 ret = redfish_convert_val(&val, src_type, &vl, dst_type);
103 EXPECT_EQ_INT(0, ret);
104 OK(vl.gauge == 1.0);
105
106 val.string = "-1";
107 src_type = VAL_TYPE_STR;
108 dst_type = DS_TYPE_DERIVE;
109 ret = redfish_convert_val(&val, src_type, &vl, dst_type);
110 EXPECT_EQ_INT(0, ret);
111 OK(vl.derive == -1);
112
113 val.integer = -1;
114 src_type = VAL_TYPE_INT;
115 dst_type = DS_TYPE_DERIVE;
116 ret = redfish_convert_val(&val, src_type, &vl, dst_type);
117 EXPECT_EQ_INT(0, ret);
118 OK(vl.derive == -1);
119
120 val.real = -1.0;
121 src_type = VAL_TYPE_REAL;
122 dst_type = DS_TYPE_DERIVE;
123 ret = redfish_convert_val(&val, src_type, &vl, dst_type);
124 EXPECT_EQ_INT(0, ret);
125 OK(vl.derive == -1);
126
127 val.string = "1";
128 src_type = VAL_TYPE_STR;
129 dst_type = DS_TYPE_COUNTER;
130 ret = redfish_convert_val(&val, src_type, &vl, dst_type);
131 EXPECT_EQ_INT(0, ret);
132 OK(vl.counter == 1);
133
134 val.integer = 1;
135 src_type = VAL_TYPE_INT;
136 dst_type = DS_TYPE_COUNTER;
137 ret = redfish_convert_val(&val, src_type, &vl, dst_type);
138 EXPECT_EQ_INT(0, ret);
139 OK(vl.counter == 1);
140
141 val.real = 1.0;
142 src_type = VAL_TYPE_REAL;
143 dst_type = DS_TYPE_COUNTER;
144 ret = redfish_convert_val(&val, src_type, &vl, dst_type);
145 EXPECT_EQ_INT(0, ret);
146 OK(vl.counter == 1);
147
148 val.string = "1";
149 src_type = VAL_TYPE_STR;
150 dst_type = DS_TYPE_ABSOLUTE;
151 ret = redfish_convert_val(&val, src_type, &vl, dst_type);
152 EXPECT_EQ_INT(0, ret);
153 OK(vl.absolute == 1);
154
155 val.integer = 1;
156 src_type = VAL_TYPE_INT;
157 dst_type = DS_TYPE_ABSOLUTE;
158 ret = redfish_convert_val(&val, src_type, &vl, dst_type);
159 EXPECT_EQ_INT(0, ret);
160 OK(vl.absolute == 1);
161
162 val.real = 1.0;
163 src_type = VAL_TYPE_REAL;
164 dst_type = DS_TYPE_ABSOLUTE;
165 ret = redfish_convert_val(&val, src_type, &vl, dst_type);
166 EXPECT_EQ_INT(0, ret);
167 OK(vl.absolute == 1);
168
169 return 0;
170 }
171
172 /* Testing allocation of memory for ctx struct. Creation of services list
173 * & queries avl tree */
174 DEF_TEST(redfish_preconfig) {
175 int ret = redfish_preconfig();
176
177 EXPECT_EQ_INT(0, ret);
178 CHECK_NOT_NULL(ctx.queries);
179 CHECK_NOT_NULL(ctx.services);
180
181 llist_destroy(ctx.services);
182 c_avl_destroy(ctx.queries);
183
184 return 0;
185 }
186
187 /* Testing correct input of properties from conf file */
188 DEF_TEST(config_property) {
189 redfish_resource_t *resource = calloc(1, sizeof(*resource));
190 assert(resource != NULL);
191 resource->name = "test property";
192 resource->properties = llist_create();
193 oconfig_item_t *ci = calloc(1, sizeof(*ci));
194
195 assert(ci != NULL);
196 ci->values_num = 1;
197 ci->values = calloc(1, sizeof(*ci->values));
198 ci->values[0].type = OCONFIG_TYPE_STRING;
199 ci->values[0].value.string = "ReadingRPM";
200
201 ci->children_num = 3;
202 ci->children = calloc(1, sizeof(*ci->children) * ci->children_num);
203
204 ci->children[0].key = "PluginInstance";
205 ci->children[0].parent = ci;
206 ci->children[0].values = calloc(1, sizeof(*ci->children[0].values));
207 assert(ci->children[0].values != NULL);
208 ci->children[0].values_num = 1;
209 ci->children[0].values->value.string = "chassis1";
210 ci->children[0].values->type = OCONFIG_TYPE_STRING;
211
212 ci->children[1].key = "Type";
213 ci->children[1].parent = ci;
214 ci->children[1].values = calloc(1, sizeof(*ci->children[1].values));
215 assert(ci->children[1].values != NULL);
216 ci->children[1].values_num = 1;
217 ci->children[1].values->value.string = "degrees";
218 ci->children[1].values->type = OCONFIG_TYPE_STRING;
219
220 ci->children[2].key = "TypeInstance";
221 ci->children[2].parent = ci;
222 ci->children[2].values = calloc(1, sizeof(*ci->children[2].values));
223 assert(ci->children[2].values != NULL);
224 ci->children[2].values_num = 1;
225 ci->children[2].values->value.string = "0";
226 ci->children[2].values->type = OCONFIG_TYPE_STRING;
227
228 int ret = redfish_config_property(resource, ci);
229
230 EXPECT_EQ_INT(0, ret);
231 EXPECT_EQ_INT(1, llist_size(resource->properties));
232
233 sfree(ci->children[0].values);
234 sfree(ci->children[1].values);
235 sfree(ci->children[2].values);
236
237 sfree(ci->children);
238 sfree(ci->values);
239 sfree(ci);
240
241 for (llentry_t *llprop = llist_head(resource->properties); llprop != NULL;
242 llprop = llprop->next) {
243 redfish_property_t *property = (redfish_property_t *)llprop->value;
244 sfree(property->name);
245 sfree(property->plugin_inst);
246 sfree(property->type);
247 sfree(property->type_inst);
248 sfree(property);
249 }
250 llist_destroy(resource->properties);
251 free(resource);
252 return 0;
253 }
254
255 DEF_TEST(config_resource) {
256 oconfig_item_t *ci = calloc(1, sizeof(*ci));
257 assert(ci != NULL);
258
259 ci->values_num = 1;
260 ci->values = calloc(1, sizeof(*ci->values));
261 assert(ci->values != NULL);
262 ci->values[0].value.string = "Temperatures";
263
264 ci->children_num = 1;
265 ci->children = calloc(1, sizeof(*ci->children));
266 assert(ci->children != NULL);
267 ci->children[0].children_num = 1;
268 ci->children[0].parent = ci;
269 ci->children[0].key = "Property";
270 ci->children[0].values_num = 1;
271 ci->children[0].values = calloc(1, sizeof(*ci->children[0].values));
272 ci->children[0].values->value.string = "ReadingRPM";
273 assert(ci->children[0].values != NULL);
274
275 oconfig_item_t *ci_prop = calloc(1, sizeof(*ci_prop));
276 assert(ci_prop != NULL);
277
278 ci->children[0].children = ci_prop;
279 ci->children_num = 1;
280
281 ci_prop->key = "PluginInstance";
282 ci_prop->parent = ci;
283 ci_prop->values_num = 1;
284 ci_prop->values = calloc(1, sizeof(*ci_prop->values));
285 assert(ci_prop->values != NULL);
286 ci_prop->values[0].type = OCONFIG_TYPE_STRING;
287 ci_prop->values[0].value.string = "chassis-1";
288
289 redfish_query_t *query = calloc(1, sizeof(*query));
290 query->endpoint = "/redfish/v1/Chassis/Chassis-1/Thermal";
291 query->name = "fans";
292 query->resources = llist_create();
293
294 int ret = redfish_config_resource(query, ci);
295 EXPECT_EQ_INT(0, ret);
296 EXPECT_EQ_INT(1, llist_size(query->resources));
297
298 sfree(ci_prop->values);
299 sfree(ci_prop);
300
301 sfree(ci->values);
302 sfree(ci->children[0].values);
303 sfree(ci->children);
304 sfree(ci);
305
306 for (llentry_t *llres = llist_head(query->resources); llres != NULL;
307 llres = llres->next) {
308 redfish_resource_t *resource = (redfish_resource_t *)llres->value;
309 for (llentry_t *llprop = llist_head(resource->properties); llprop != NULL;
310 llprop = llprop->next) {
311 redfish_property_t *property = (redfish_property_t *)llprop->value;
312 sfree(property->name);
313 sfree(property->plugin_inst);
314 sfree(property->type);
315 sfree(property->type_inst);
316 free(property);
317 }
318 llist_destroy(resource->properties);
319 free(resource->name);
320 free(resource);
321 }
322 llist_destroy(query->resources);
323 sfree(query);
324 return 0;
325 }
326
327 DEF_TEST(config_query) {
328 oconfig_item_t *qci = calloc(1, sizeof(*qci));
329 assert(qci != NULL);
330 qci->key = "Query";
331 qci->values_num = 1;
332 qci->values = calloc(1, sizeof(*qci->values));
333 assert(qci->values != NULL);
334 qci->values->type = OCONFIG_TYPE_STRING;
335 qci->values->value.string = "fans";
336
337 qci->children_num = 2;
338 qci->children = calloc(1, sizeof(*qci->children) * qci->children_num);
339 assert(qci->children != NULL);
340
341 qci->children[0].key = "Endpoint";
342 qci->children[0].values = calloc(1, sizeof(*qci->children[0].values));
343 assert(qci->children[0].values != NULL);
344 qci->children[0].values->type = OCONFIG_TYPE_STRING;
345 qci->children[0].values_num = 1;
346 qci->children[0].values->value.string =
347 "/redfish/v1/Chassis/Chassis-1/Thermal";
348
349 qci->children[1].key = "Resource";
350 qci->children[1].values_num = 1;
351 qci->children[1].values = calloc(1, sizeof(*qci->children[1].values));
352 assert(qci->children[1].values != NULL);
353 qci->children[1].values->type = OCONFIG_TYPE_STRING;
354 qci->children[1].values->value.string = "Temperature";
355 qci->children[1].children_num = 1;
356
357 oconfig_item_t *ci = calloc(1, sizeof(*ci));
358 assert(ci != NULL);
359
360 qci->children[1].children = ci;
361
362 ci->key = "Property";
363 ci->values_num = 1;
364 ci->values = calloc(1, sizeof(*ci->values));
365 assert(ci->values != NULL);
366 ci->values->type = OCONFIG_TYPE_STRING;
367 ci->values->value.string = "ReadingRPM";
368
369 oconfig_item_t *ci_prop = calloc(1, sizeof(*ci_prop));
370 assert(ci_prop != NULL);
371
372 ci->children = ci_prop;
373 ci->children_num = 1;
374
375 ci_prop->key = "PluginInstance";
376 ci_prop->parent = ci;
377 ci_prop->values_num = 1;
378 ci_prop->values = calloc(1, sizeof(*ci_prop->values));
379 assert(ci_prop->values != NULL);
380 ci_prop->values[0].type = OCONFIG_TYPE_STRING;
381 ci_prop->values[0].value.string = "chassis-1";
382
383 c_avl_tree_t *queries =
384 c_avl_create((int (*)(const void *, const void *))strcmp);
385
386 int ret = redfish_config_query(qci, queries);
387 EXPECT_EQ_INT(0, ret);
388
389 sfree(ci_prop->values);
390 sfree(ci_prop);
391
392 sfree(ci->values);
393 sfree(ci);
394 sfree(qci->children[0].values);
395 sfree(qci->children[1].values);
396 sfree(qci->children);
397 sfree(qci->values);
398 sfree(qci);
399
400 redfish_query_t *query;
401 char *key;
402 c_avl_iterator_t *query_iter = c_avl_get_iterator(queries);
403 while (c_avl_iterator_next(query_iter, (void **)&key, (void **)&query) == 0) {
404 for (llentry_t *llres = llist_head(query->resources); llres != NULL;
405 llres = llres->next) {
406 redfish_resource_t *resource = (redfish_resource_t *)llres->value;
407 for (llentry_t *llprop = llist_head(resource->properties); llprop != NULL;
408 llprop = llprop->next) {
409 redfish_property_t *property = (redfish_property_t *)llprop->value;
410 sfree(property->name);
411 sfree(property->plugin_inst);
412 sfree(property->type);
413 sfree(property->type_inst);
414 sfree(property);
415 }
416 llist_destroy(resource->properties);
417 sfree(resource->name);
418 sfree(resource);
419 }
420 llist_destroy(query->resources);
421 sfree(query->name);
422 sfree(query->endpoint);
423 }
424 sfree(query_iter);
425 c_avl_destroy(queries);
426 return 0;
427 }
428
429 DEF_TEST(config_service) {
430 oconfig_item_t *ci = calloc(1, sizeof(*ci));
431 assert(ci != NULL);
432 ci->key = "Service";
433 ci->values_num = 1;
434 ci->values = calloc(1, sizeof(*ci->values));
435 ci->values->type = OCONFIG_TYPE_STRING;
436 ci->values->value.string = "Server 5";
437 ci->children_num = 4;
438 ci->children = calloc(1, sizeof(*ci->children) * ci->children_num);
439 ci->children[0].key = "Host";
440 ci->children[0].values_num = 1;
441 ci->children[0].values = calloc(1, sizeof(*ci->children[0].values));
442 ci->children[0].values->type = OCONFIG_TYPE_STRING;
443 ci->children[0].values->value.string = "127.0.0.1:5000";
444 ci->children[1].key = "User";
445 ci->children[1].values_num = 1;
446 ci->children[1].values = calloc(1, sizeof(*ci->children[1].values));
447 ci->children[1].values->type = OCONFIG_TYPE_STRING;
448 ci->children[1].values->value.string = "user";
449 ci->children[2].key = "Passwd";
450 ci->children[2].values_num = 1;
451 ci->children[2].values = calloc(1, sizeof(*ci->children[2].values));
452 ci->children[2].values->type = OCONFIG_TYPE_STRING;
453 ci->children[2].values->value.string = "passwd";
454 ci->children[3].key = "Queries";
455 ci->children[3].values_num = 1;
456 ci->children[3].values = calloc(1, sizeof(*ci->children[2].values));
457 ci->children[3].values->type = OCONFIG_TYPE_STRING;
458 ci->children[3].values->value.string = "fans";
459
460 ctx.services = llist_create();
461
462 int ret = redfish_config_service(ci);
463
464 EXPECT_EQ_INT(0, ret);
465
466 for (llentry_t *llserv = llist_head(ctx.services); llserv != NULL;
467 llserv = llserv->next) {
468 redfish_service_t *serv = (redfish_service_t *)llserv->value;
469 sfree(serv->name);
470 sfree(serv->host);
471 sfree(serv->user);
472 sfree(serv->passwd);
473 for (int i = 0; i < serv->queries_num; i++)
474 sfree(serv->queries[i]);
475 sfree(serv->queries);
476 sfree(serv);
477 }
478 llist_destroy(ctx.services);
479
480 sfree(ci->children[3].values);
481 sfree(ci->children[2].values);
482 sfree(ci->children[1].values);
483 sfree(ci->children[0].values);
484 sfree(ci->children);
485 sfree(ci->values);
486 sfree(ci);
487 return 0;
488 }
489
490 DEF_TEST(process_payload_property) {
491 redfish_property_t property;
492 property.name = "Abc";
493 property.plugin_inst = "TestPluginInstance";
494 property.type = "MAGIC";
495 property.type_inst = "TestTypeInstance";
496
497 redfish_resource_t resource;
498 resource.name = "ResourceName";
499
500 redfish_service_t service;
501 service.name = "localhost";
502
503 const char *json_text = "["
504 " { \"Abc\": 4567 }"
505 "]";
506 json_error_t error;
507 json_t *root = json_loads(json_text, 0, &error);
508
509 if (!root) {
510 return -1;
511 }
512
513 redfish_process_payload_property(&property, root, &resource, &service);
514
515 json_decref(root);
516
517 value_list_t *v = redfish_test_get_last_dispatched_value_list();
518 EXPECT_EQ_INT(1, v->values_len);
519 EXPECT_EQ_STR("MAGIC", v->type);
520 EXPECT_EQ_INT(4567, v->values->derive);
521 EXPECT_EQ_STR("TestPluginInstance", v->plugin_instance);
522 EXPECT_EQ_STR("TestTypeInstance", v->type_instance);
523 EXPECT_EQ_STR("localhost", v->host);
524 EXPECT_EQ_STR("redfish", v->plugin);
525 return 0;
526 }
527
528 DEF_TEST(service_destroy) {
529 /* Check for memory leaks when a service is destroyed */
530 redfish_service_t *service = calloc(1, sizeof(*service));
531
532 service->name = strdup("Name");
533 service->host = strdup("http://localhost:1234");
534 service->user = strdup("User");
535 service->passwd = strdup("Password");
536 service->token = strdup("Token");
537
538 service->queries = calloc(2, sizeof(*service->queries));
539 service->queries[0] = strdup("Query1");
540 service->queries[1] = strdup("Query2");
541 service->queries_num = 2;
542
543 service->query_ptrs = llist_create();
544
545 service->flags |= REDFISH_FLAG_SERVICE_NO_VERSION_DOC;
546 service->auth.authCodes.userPass.username = service->user;
547 service->auth.authCodes.userPass.password = service->passwd;
548 service->redfish = createServiceEnumerator(service->host, NULL,
549 &service->auth, service->flags);
550
551 redfish_service_destroy(service);
552 return 0;
553 }
554
555 DEF_TEST(job_destroy) {
556 /* Check for memory leaks when a job is destroyed */
557 redfish_job_t *job = calloc(1, sizeof(*job));
558 redfish_job_destroy(job);
559 return 0;
560 }
561
562 DEF_TEST(json_get_string_1) {
563 const char *json_text = "{ \"MemberId\": \"1234\" }";
564
565 json_error_t error;
566 json_t *root = json_loads(json_text, 0, &error);
567
568 if (!root) {
569 return -1;
570 }
571
572 char str[20];
573 json_t *json = json_object_get(root, "MemberId");
574 redfish_json_get_string(str, sizeof(str), json);
575
576 json_decref(root);
577
578 EXPECT_EQ_STR("1234", str);
579 return 0;
580 }
581
582 DEF_TEST(json_get_string_2) {
583 const char *json_text = "{ \"MemberId\": 9876 }";
584
585 json_error_t error;
586 json_t *root = json_loads(json_text, 0, &error);
587
588 if (!root) {
589 return -1;
590 }
591
592 char str[20];
593 json_t *json = json_object_get(root, "MemberId");
594 redfish_json_get_string(str, sizeof(str), json);
595
596 json_decref(root);
597
598 EXPECT_EQ_STR("9876", str);
599 return 0;
600 }
601
602 int main(void) {
603 RUN_TEST(read_queries);
604 RUN_TEST(convert_val);
605 RUN_TEST(redfish_preconfig);
606 RUN_TEST(config_property);
607 RUN_TEST(config_resource);
608 RUN_TEST(config_query);
609 RUN_TEST(config_service);
610 RUN_TEST(process_payload_property);
611 RUN_TEST(service_destroy);
612 RUN_TEST(job_destroy);
613 RUN_TEST(json_get_string_1);
614 RUN_TEST(json_get_string_2);
615 END_TEST;
616 }