2 * collectd - src/tests/utils_cmds_test.c
3 * Copyright (C) 2016 Sebastian 'tokkee' Harl
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:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
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.
24 * Sebastian 'tokkee' Harl <sh at tokkee.org>
29 #include "utils_cmds.h"
31 static void error_cb (void *ud, cmd_status_t status,
32 const char *format, va_list ap)
37 printf ("ERROR[%d]: ", status);
45 cmd_status_t expected_status;
46 cmd_type_t expected_type;
48 /* Valid FLUSH commands. */
55 "FLUSH identifier=myhost/magic/MAGIC",
60 "FLUSH timeout=123 plugin=\"A\"",
64 /* Invalid FLUSH commands. */
66 /* Missing 'identifier' key. */
67 "FLUSH myhost/magic/MAGIC",
72 /* Invalid timeout. */
78 /* Invalid identifier. */
79 "FLUSH identifier=invalid",
85 "FLUSH invalid=option",
90 /* Valid GETVAL commands. */
92 "GETVAL myhost/magic/MAGIC",
97 /* Invalid GETVAL commands. */
109 /* Valid LISTVAL commands. */
116 /* Invalid LISTVAL commands. */
123 /* Valid PUTVAL commands. */
125 "PUTVAL myhost/magic/MAGIC N:42",
130 "PUTVAL myhost/magic/MAGIC 1234:42",
135 "PUTVAL myhost/magic/MAGIC 1234:42 2345:23",
140 "PUTVAL myhost/magic/MAGIC interval=2 1234:42",
145 "PUTVAL myhost/magic/MAGIC interval=2 1234:42 interval=5 2345:23",
150 /* Invalid PUTVAL commands. */
157 "PUTVAL invalid N:42",
162 "PUTVAL myhost/magic/MAGIC A:42",
167 "PUTVAL myhost/magic/MAGIC 1234:A",
172 "PUTVAL myhost/magic/MAGIC",
182 "PUTVAL myhost/magic/UNKNOWN 1234:42",
187 * As of collectd 5.x, PUTVAL accepts invalid options.
189 "PUTVAL myhost/magic/MAGIC invalid=2 1234:42",
195 /* Invalid commands. */
202 "INVALID interval=2",
210 cmd_error_handler_t err = { error_cb, NULL };
214 for (i = 0; i < STATIC_ARRAY_SIZE (parse_data); i++) {
215 char *input = strdup (parse_data[i].input);
217 char description[1024];
223 memset (&cmd, 0, sizeof (cmd));
225 status = cmd_parse (input, &cmd, &err);
226 snprintf (description, sizeof (description),
227 "cmd_parse (\"%s\") = %d (type=%d [%s]); want %d (type=%d [%s])",
228 parse_data[i].input, status,
229 cmd.type, CMD_TO_STRING (cmd.type),
230 parse_data[i].expected_status,
231 parse_data[i].expected_type,
232 CMD_TO_STRING (parse_data[i].expected_type));
233 result = (status == parse_data[i].expected_status)
234 && (cmd.type == parse_data[i].expected_type);
235 LOG (result, description);
237 /* Run all tests before failing. */
245 return (test_result);
248 int main (int argc, char **argv)