From 61c79ccd8480df2a52158940727e41a80717ae77 Mon Sep 17 00:00:00 2001 From: Florian Forster Date: Fri, 25 Nov 2016 10:08:49 +0100 Subject: [PATCH] src/utils_format_graphite.[ch]: Add unit test. First step towards fixing issue #419. --- src/Makefile.am | 26 +++--- src/utils_format_graphite_test.c | 149 +++++++++++++++++++++++++++++++ 2 files changed, 165 insertions(+), 10 deletions(-) create mode 100644 src/utils_format_graphite_test.c diff --git a/src/Makefile.am b/src/Makefile.am index 18dddf77..b6b00a27 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -27,6 +27,15 @@ noinst_LTLIBRARIES = check_PROGRAMS = TESTS = +noinst_LTLIBRARIES += libformat_graphite.la +libformat_graphite_la_SOURCES = utils_format_graphite.c utils_format_graphite.h +libformat_graphite_la_CPPFLAGS = $(AM_CPPFLAGS) +libformat_graphite_la_LDFLAGS = $(AM_LDFLAGS) +check_PROGRAMS += test_format_graphite +TESTS += test_format_graphite +test_format_graphite_SOURCES = utils_format_graphite_test.c testing.h +test_format_graphite_LDADD = libformat_graphite.la daemon/libmetadata.la daemon/libplugin_mock.la -lm + noinst_LTLIBRARIES += libformat_json.la libformat_json_la_SOURCES = utils_format_json.c utils_format_json.h libformat_json_la_CPPFLAGS = $(AM_CPPFLAGS) @@ -171,11 +180,10 @@ pkglib_LTLIBRARIES += amqp.la amqp_la_SOURCES = amqp.c \ utils_cmds.c utils_cmds.h \ utils_cmd_putval.c utils_cmd_putval.h \ - utils_parse_option.c utils_parse_option.h \ - utils_format_graphite.c utils_format_graphite.h + utils_parse_option.c utils_parse_option.h amqp_la_LDFLAGS = $(PLUGIN_LDFLAGS) $(BUILD_WITH_LIBRABBITMQ_LDFLAGS) amqp_la_CPPFLAGS = $(AM_CPPFLAGS) $(BUILD_WITH_LIBRABBITMQ_CPPFLAGS) -amqp_la_LIBADD = $(BUILD_WITH_LIBRABBITMQ_LIBS) libformat_json.la +amqp_la_LIBADD = $(BUILD_WITH_LIBRABBITMQ_LIBS) libformat_graphite.la libformat_json.la endif if BUILD_PLUGIN_APACHE @@ -1288,9 +1296,9 @@ endif if BUILD_PLUGIN_WRITE_GRAPHITE pkglib_LTLIBRARIES += write_graphite.la -write_graphite_la_SOURCES = write_graphite.c \ - utils_format_graphite.c utils_format_graphite.h +write_graphite_la_SOURCES = write_graphite.c write_graphite_la_LDFLAGS = $(PLUGIN_LDFLAGS) +write_graphite_la_LIBADD = libformat_graphite.la endif if BUILD_PLUGIN_WRITE_HTTP @@ -1305,21 +1313,19 @@ endif if BUILD_PLUGIN_WRITE_KAFKA pkglib_LTLIBRARIES += write_kafka.la write_kafka_la_SOURCES = write_kafka.c \ - utils_format_graphite.c utils_format_graphite.h \ utils_cmds.c utils_cmds.h \ utils_cmd_putval.c utils_cmd_putval.h \ utils_crc32.c utils_crc32.h write_kafka_la_CPPFLAGS = $(AM_CPPFLAGS) $(BUILD_WITH_LIBRDKAFKA_CPPFLAGS) write_kafka_la_LDFLAGS = $(PLUGIN_LDFLAGS) $(BUILD_WITH_LIBRDKAFKA_LDFLAGS) -write_kafka_la_LIBADD = $(BUILD_WITH_LIBRDKAFKA_LIBS) libformat_json.la +write_kafka_la_LIBADD = $(BUILD_WITH_LIBRDKAFKA_LIBS) libformat_graphite.la libformat_json.la endif if BUILD_PLUGIN_WRITE_LOG pkglib_LTLIBRARIES += write_log.la -write_log_la_SOURCES = write_log.c \ - utils_format_graphite.c utils_format_graphite.h +write_log_la_SOURCES = write_log.c write_log_la_LDFLAGS = $(PLUGIN_LDFLAGS) -write_log_la_LIBADD = libformat_json.la +write_log_la_LIBADD = libformat_graphite.la libformat_json.la endif if BUILD_PLUGIN_WRITE_MONGODB diff --git a/src/utils_format_graphite_test.c b/src/utils_format_graphite_test.c new file mode 100644 index 00000000..30cdd7a4 --- /dev/null +++ b/src/utils_format_graphite_test.c @@ -0,0 +1,149 @@ +/** + * collectd - src/utils_format_graphite_test.c + * Copyright (C) 2016 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 "testing.h" +#include "utils_format_graphite.h" +#include "common.h" /* for STATIC_ARRAY_SIZE */ + +static data_set_t ds_single = { + .type = "single", + .ds_num = 1, + .ds = &(data_source_t){"value", DS_TYPE_GAUGE, NAN, NAN}, +}; + +/* +static data_set_t ds_double = { + .type = "double", + .ds_num = 2, + .ds = + (data_source_t[]){ + {"one", DS_TYPE_DERIVE, 0, NAN}, {"two", DS_TYPE_DERIVE, 0, NAN}, + }, +}; +*/ + +DEF_TEST(metric_name) +{ + struct { + char *plugin_instance; + char *type_instance; + char *prefix; + char *suffix; + unsigned int flags; + char *want_name; + } cases[] = { + { + .want_name = "example@com.test.single", + }, + /* plugin and type instances */ + { + .plugin_instance = "foo", + .type_instance = "bar", + .want_name = "example@com.test-foo.single-bar", + }, + { + .plugin_instance = NULL, + .type_instance = "bar", + .want_name = "example@com.test.single-bar", + }, + { + .plugin_instance = "foo", + .type_instance = NULL, + .want_name = "example@com.test-foo.single", + }, + /* special chars */ + { + .plugin_instance = "foo (test)", + .type_instance = "test: \"hello\"", + .want_name = "example@com.test-foo@@test@.single-test@@@hello@", + }, + /* flag GRAPHITE_SEPARATE_INSTANCES */ + { + .plugin_instance = "foo", + .type_instance = "bar", + .flags = GRAPHITE_SEPARATE_INSTANCES, + .want_name = "example@com.test.foo.single.bar", + }, + /* flag GRAPHITE_ALWAYS_APPEND_DS */ + { + .plugin_instance = "foo", + .type_instance = "bar", + .flags = GRAPHITE_ALWAYS_APPEND_DS, + .want_name = "example@com.test-foo.single-bar.value", + }, + /* prefix and suffix */ + { + .prefix = "foo.", + .suffix = ".bar", + .want_name = "foo.example@com.bar.test.single", + }, + { + .prefix = NULL, + .suffix = ".bar", + .want_name = "example@com.bar.test.single", + }, + { + .prefix = "foo.", + .suffix = NULL, + .want_name = "foo.example@com.test.single", + }, + }; + + for (size_t i = 0; i < STATIC_ARRAY_SIZE(cases); i++) { + value_list_t vl = { + .values = &(value_t){.gauge = 42}, + .values_len = 1, + .time = TIME_T_TO_CDTIME_T_STATIC(1480063672), + .interval = TIME_T_TO_CDTIME_T_STATIC(10), + .host = "example.com", + .plugin = "test", + .type = "single", + }; + + char want[1024]; + snprintf(want, sizeof(want), "%s 42 1480063672\r\n", cases[i].want_name); + + if (cases[i].plugin_instance != NULL) + sstrncpy (vl.plugin_instance, cases[i].plugin_instance, sizeof (vl.plugin_instance)); + if (cases[i].type_instance != NULL) + sstrncpy (vl.type_instance, cases[i].type_instance, sizeof (vl.type_instance)); + + char got[1024]; + EXPECT_EQ_INT(0, format_graphite(got, sizeof(got), &ds_single, &vl, cases[i].prefix, cases[i].suffix, '@', cases[i].flags)); + EXPECT_EQ_STR(want, got); + } + + return 0; +} + +int main (void) +{ + RUN_TEST(metric_name); + + END_TEST; +} -- 2.30.2