| 1 |
/** |
| 2 |
* collectd - src/utils_format_json_test.c |
| 3 |
* Copyright (C) 2015 Florian octo 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 octo Forster <octo at collectd.org> |
| 25 |
*/ |
| 26 |
|
| 27 |
/* Workaround for Solaris 10 defining label_t |
| 28 |
* Issue #1301 |
| 29 |
*/ |
| 30 |
|
| 31 |
#include "config.h" |
| 32 |
#if KERNEL_SOLARIS |
| 33 |
#ifndef _POSIX_C_SOURCE |
| 34 |
#define _POSIX_C_SOURCE 200112L |
| 35 |
#endif |
| 36 |
#undef __EXTENSIONS__ |
| 37 |
#endif |
| 38 |
|
| 39 |
#include "collectd.h" |
| 40 |
|
| 41 |
#include "testing.h" |
| 42 |
#include "utils/common/common.h" /* for STATIC_ARRAY_SIZE */ |
| 43 |
#include "utils/format_json/format_json.h" |
| 44 |
|
| 45 |
#include <yajl/yajl_common.h> |
| 46 |
#include <yajl/yajl_parse.h> |
| 47 |
#if HAVE_YAJL_YAJL_VERSION_H |
| 48 |
#include <yajl/yajl_version.h> |
| 49 |
#endif |
| 50 |
#if YAJL_MAJOR > 1 |
| 51 |
#define HAVE_YAJL_V2 1 |
| 52 |
#endif |
| 53 |
|
| 54 |
typedef struct { |
| 55 |
char const *key; |
| 56 |
char const *value; |
| 57 |
} label_t; |
| 58 |
|
| 59 |
typedef struct { |
| 60 |
label_t *expected_labels; |
| 61 |
size_t expected_labels_num; |
| 62 |
|
| 63 |
label_t *current_label; |
| 64 |
} test_case_t; |
| 65 |
|
| 66 |
#if HAVE_YAJL_V2 |
| 67 |
static int test_map_key(void *ctx, unsigned char const *key, size_t key_len) |
| 68 |
#else |
| 69 |
static int test_map_key(void *ctx, unsigned char const *key, |
| 70 |
unsigned int key_len) |
| 71 |
#endif |
| 72 |
{ |
| 73 |
test_case_t *c = ctx; |
| 74 |
size_t i; |
| 75 |
|
| 76 |
c->current_label = NULL; |
| 77 |
for (i = 0; i < c->expected_labels_num; i++) { |
| 78 |
label_t *l = c->expected_labels + i; |
| 79 |
if ((strlen(l->key) == key_len) && |
| 80 |
(strncmp(l->key, (char const *)key, key_len) == 0)) { |
| 81 |
c->current_label = l; |
| 82 |
break; |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
return 1; /* continue */ |
| 87 |
} |
| 88 |
|
| 89 |
static int expect_label(char const *name, char const *got, char const *want) { |
| 90 |
bool ok = (strcmp(got, want) == 0); |
| 91 |
char msg[1024]; |
| 92 |
|
| 93 |
if (ok) |
| 94 |
snprintf(msg, sizeof(msg), "label[\"%s\"] = \"%s\"", name, got); |
| 95 |
else |
| 96 |
snprintf(msg, sizeof(msg), "label[\"%s\"] = \"%s\", want \"%s\"", name, got, |
| 97 |
want); |
| 98 |
|
| 99 |
OK1(ok, msg); |
| 100 |
return 0; |
| 101 |
} |
| 102 |
|
| 103 |
#if HAVE_YAJL_V2 |
| 104 |
static int test_string(void *ctx, unsigned char const *value, size_t value_len) |
| 105 |
#else |
| 106 |
static int test_string(void *ctx, unsigned char const *value, |
| 107 |
unsigned int value_len) |
| 108 |
#endif |
| 109 |
{ |
| 110 |
test_case_t *c = ctx; |
| 111 |
|
| 112 |
if (c->current_label != NULL) { |
| 113 |
label_t *l = c->current_label; |
| 114 |
char *got; |
| 115 |
int status; |
| 116 |
|
| 117 |
got = malloc(value_len + 1); |
| 118 |
memmove(got, value, value_len); |
| 119 |
got[value_len] = 0; |
| 120 |
|
| 121 |
status = expect_label(l->key, got, l->value); |
| 122 |
|
| 123 |
free(got); |
| 124 |
|
| 125 |
if (status != 0) |
| 126 |
return 0; /* abort */ |
| 127 |
} |
| 128 |
|
| 129 |
return 1; /* continue */ |
| 130 |
} |
| 131 |
|
| 132 |
static int expect_json_labels(char *json, label_t *labels, size_t labels_num) { |
| 133 |
yajl_callbacks funcs = { |
| 134 |
.yajl_string = test_string, |
| 135 |
.yajl_map_key = test_map_key, |
| 136 |
}; |
| 137 |
|
| 138 |
test_case_t c = {labels, labels_num, NULL}; |
| 139 |
|
| 140 |
yajl_handle hndl; |
| 141 |
#if HAVE_YAJL_V2 |
| 142 |
CHECK_NOT_NULL(hndl = yajl_alloc(&funcs, /* alloc = */ NULL, &c)); |
| 143 |
#else |
| 144 |
CHECK_NOT_NULL( |
| 145 |
hndl = yajl_alloc(&funcs, /* config = */ NULL, /* alloc = */ NULL, &c)); |
| 146 |
#endif |
| 147 |
OK(yajl_parse(hndl, (unsigned char *)json, strlen(json)) == yajl_status_ok); |
| 148 |
|
| 149 |
yajl_free(hndl); |
| 150 |
return 0; |
| 151 |
} |
| 152 |
|
| 153 |
DEF_TEST(notification) { |
| 154 |
label_t labels[] = { |
| 155 |
{"summary", "this is a message"}, |
| 156 |
{"alertname", "collectd_unit_test"}, |
| 157 |
{"instance", "example.com"}, |
| 158 |
{"service", "collectd"}, |
| 159 |
{"unit", "case"}, |
| 160 |
}; |
| 161 |
|
| 162 |
/* 1448284606.125 ^= 1555083754651779072 */ |
| 163 |
notification_t n = {NOTIF_WARNING, |
| 164 |
1555083754651779072ULL, |
| 165 |
"this is a message", |
| 166 |
"example.com", |
| 167 |
"unit", |
| 168 |
"", |
| 169 |
"test", |
| 170 |
"case", |
| 171 |
NULL}; |
| 172 |
|
| 173 |
char got[1024]; |
| 174 |
CHECK_ZERO(format_json_notification(got, sizeof(got), &n)); |
| 175 |
// printf ("got = \"%s\";\n", got); |
| 176 |
|
| 177 |
return expect_json_labels(got, labels, STATIC_ARRAY_SIZE(labels)); |
| 178 |
} |
| 179 |
|
| 180 |
int main(void) { |
| 181 |
RUN_TEST(notification); |
| 182 |
|
| 183 |
END_TEST; |
| 184 |
} |