From: Florian Forster Date: Wed, 20 Dec 2023 19:59:52 +0000 (+0100) Subject: cmds: Add a test for `cmd_parse_putval`. X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=c975e94419e00b05d701db1cadd50c0fdde2dd19;p=thirdparty%2Fcollectd.git cmds: Add a test for `cmd_parse_putval`. --- diff --git a/Makefile.am b/Makefile.am index 456eca6ef..2d1cb823d 100644 --- a/Makefile.am +++ b/Makefile.am @@ -157,6 +157,7 @@ check_PROGRAMS = \ test_meta_data \ test_utils_avltree \ test_utils_cmds \ + test_utils_cmds_putval \ test_utils_heap \ test_utils_latency \ test_utils_message_parser \ @@ -524,6 +525,13 @@ test_utils_cmds_LDADD = \ libcmds.la \ libplugin_mock.la +test_utils_cmds_putval_SOURCES = \ + src/utils/cmds/putval_test.c \ + src/testing.h +test_utils_cmds_putval_LDADD = \ + libcmds.la \ + libplugin_mock.la + liblookup_la_SOURCES = \ src/utils/lookup/vl_lookup.c \ src/utils/lookup/vl_lookup.h diff --git a/src/utils/cmds/putval_test.c b/src/utils/cmds/putval_test.c new file mode 100644 index 000000000..742a5eb50 --- /dev/null +++ b/src/utils/cmds/putval_test.c @@ -0,0 +1,146 @@ +/** + * collectd - src/utils/cmds/putval_test.c + * Copyright (C) 2023 Florian octo Forster + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + * + * Authors: + * Florian octo Forster + */ + +#include "collectd.h" + +#include "daemon/plugin.h" +#include "testing.h" +#include "utils/cmds/cmds.h" +#include "utils/cmds/putval.h" +#include "utils/common/common.h" + +static int value_list_compare(value_list_t const *want, + value_list_t const *got) { + EXPECT_EQ_STR(want->host, got->host); + EXPECT_EQ_STR(want->plugin, got->plugin); + EXPECT_EQ_STR(want->plugin_instance, got->plugin_instance); + EXPECT_EQ_STR(want->type, got->type); + EXPECT_EQ_STR(want->type_instance, got->type_instance); + + EXPECT_EQ_UINT64(want->time, got->time); + EXPECT_EQ_UINT64(want->interval, got->interval); + EXPECT_EQ_UINT64(want->values_len, got->values_len); + + for (size_t i = 0; i < want->values_len; i++) { + EXPECT_EQ_UINT64(want->values[i].derive, got->values[i].derive); + } + + return 0; +} + +static void err_callback(void *ud, cmd_status_t status, const char *format, + va_list ap) { + vprintf(format, ap); + puts(""); +} + +DEF_TEST(cmd_parse_putval) { + struct { + int argc; + char **argv; + size_t want_num; + value_list_t *want; + } cases[] = { + { + .argc = 3, + .argv = + (char *[]){ + "/MAGIC", + "interval=1", + "1685945973:281000", + }, + .want_num = 1, + .want = + &(value_list_t){ + .host = "example.com", + .type = "MAGIC", + .time = TIME_T_TO_CDTIME_T_STATIC(1685945973), + .interval = TIME_T_TO_CDTIME_T_STATIC(1), + .values_len = 1, + .values = &(value_t){.derive = 281000}, + }, + }, + { + .argc = 4, + .argv = + (char *[]){ + "/MAGIC", + "interval=1", + "1685945973:281000", + "1685945974:562000", + }, + .want_num = 2, + .want = + (value_list_t[]){ + { + .host = "example.com", + .type = "MAGIC", + .time = TIME_T_TO_CDTIME_T_STATIC(1685945973), + .interval = TIME_T_TO_CDTIME_T_STATIC(1), + .values_len = 1, + .values = &(value_t){.derive = 281000}, + }, + { + .host = "example.com", + .type = "MAGIC", + .time = TIME_T_TO_CDTIME_T_STATIC(1685945974), + .interval = TIME_T_TO_CDTIME_T_STATIC(1), + .values_len = 1, + .values = &(value_t){.derive = 562000}, + }, + }, + }, + }; + + for (size_t i = 0; i < STATIC_ARRAY_SIZE(cases); i++) { + cmd_putval_t got = {0}; + + cmd_options_t opts = { + .identifier_default_host = "example.com", + }; + + cmd_error_handler_t err_hndl = { + .cb = err_callback, + }; + EXPECT_EQ_INT(CMD_OK, cmd_parse_putval(cases[i].argc, cases[i].argv, &got, + &opts, &err_hndl)); + + EXPECT_EQ_UINT64(cases[i].want_num, got.vl_num); + for (size_t j = 0; j < cases[i].want_num; j++) { + value_list_compare(&cases[i].want[j], &got.vl[j]); + } + + cmd_destroy_putval(&got); + } + + return 0; +} + +int main(void) { + RUN_TEST(cmd_parse_putval); + + END_TEST; +}