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);
43 static cmd_options_t default_host_opts = {
44 /* identifier_default_host = */ "dummy-host",
50 cmd_status_t expected_status;
51 cmd_type_t expected_type;
53 /* Valid FLUSH commands. */
61 "FLUSH identifier=myhost/magic/MAGIC",
67 "FLUSH identifier=magic/MAGIC",
73 "FLUSH timeout=123 plugin=\"A\"",
78 /* Invalid FLUSH commands. */
80 /* Missing hostname; no default. */
81 "FLUSH identifier=magic/MAGIC",
87 /* Missing 'identifier' key. */
88 "FLUSH myhost/magic/MAGIC",
94 /* Invalid timeout. */
101 /* Invalid identifier. */
102 "FLUSH identifier=invalid",
108 /* Invalid option. */
109 "FLUSH invalid=option",
115 /* Valid GETVAL commands. */
117 "GETVAL myhost/magic/MAGIC",
123 "GETVAL magic/MAGIC",
129 /* Invalid GETVAL commands. */
131 "GETVAL magic/MAGIC",
149 /* Valid LISTVAL commands. */
157 /* Invalid LISTVAL commands. */
165 /* Valid PUTVAL commands. */
167 "PUTVAL magic/MAGIC N:42",
173 "PUTVAL myhost/magic/MAGIC N:42",
179 "PUTVAL myhost/magic/MAGIC 1234:42",
185 "PUTVAL myhost/magic/MAGIC 1234:42 2345:23",
191 "PUTVAL myhost/magic/MAGIC interval=2 1234:42",
197 "PUTVAL myhost/magic/MAGIC interval=2 1234:42 interval=5 2345:23",
203 /* Invalid PUTVAL commands. */
205 "PUTVAL magic/MAGIC N:42",
217 "PUTVAL invalid N:42",
223 "PUTVAL myhost/magic/MAGIC A:42",
229 "PUTVAL myhost/magic/MAGIC 1234:A",
235 "PUTVAL myhost/magic/MAGIC",
247 "PUTVAL myhost/magic/UNKNOWN 1234:42",
253 * As of collectd 5.x, PUTVAL accepts invalid options.
255 "PUTVAL myhost/magic/MAGIC invalid=2 1234:42",
262 /* Invalid commands. */
270 "INVALID interval=2",
279 cmd_error_handler_t err = { error_cb, NULL };
283 for (i = 0; i < STATIC_ARRAY_SIZE (parse_data); i++) {
284 char *input = strdup (parse_data[i].input);
286 char description[1024];
292 memset (&cmd, 0, sizeof (cmd));
294 status = cmd_parse (input, &cmd, parse_data[i].opts, &err);
295 snprintf (description, sizeof (description),
296 "cmd_parse (\"%s\", opts=%p) = %d (type=%d [%s]); want %d (type=%d [%s])",
297 parse_data[i].input, parse_data[i].opts, status,
298 cmd.type, CMD_TO_STRING (cmd.type),
299 parse_data[i].expected_status,
300 parse_data[i].expected_type,
301 CMD_TO_STRING (parse_data[i].expected_type));
302 result = (status == parse_data[i].expected_status)
303 && (cmd.type == parse_data[i].expected_type);
304 LOG (result, description);
306 /* Run all tests before failing. */
314 return (test_result);
317 int main (int argc, char **argv)