From: Francesco Romani Date: Wed, 23 Nov 2016 17:38:42 +0000 (+0100) Subject: virt plugin: Add tests for the domain tags X-Git-Tag: collectd-5.8.0~290^2~3 X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=98e38add333bfcb893fcde408ecf3a0c43d99e31;p=collectd.git virt plugin: Add tests for the domain tags Bootstrap the unit tests for the virt plugin, starting with the newly-added partition/tag support. Signed-off-by: Francesco Romani --- diff --git a/src/Makefile.am b/src/Makefile.am index 37763894..0601ce9f 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1277,6 +1277,16 @@ virt_la_CFLAGS = $(AM_CFLAGS) \ $(BUILD_WITH_LIBVIRT_CFLAGS) $(BUILD_WITH_LIBXML2_CFLAGS) virt_la_LIBADD = $(BUILD_WITH_LIBVIRT_LIBS) $(BUILD_WITH_LIBXML2_LIBS) virt_la_LDFLAGS = $(PLUGIN_LDFLAGS) + +test_plugin_virt_SOURCES = virt_test.c +test_plugin_virt_CPPFLAGS = $(AM_CPPFLAGS) \ + $(BUILD_WITH_LIBVIRT_CFLAGS) $(BUILD_WITH_LIBXML2_CFLAGS) +test_plugin_virt_LDFLAGS = $(PLUGIN_LDFLAGS) +test_plugin_virt_LDADD = daemon/libplugin_mock.la \ + $(BUILD_WITH_LIBVIRT_LIBS) $(BUILD_WITH_LIBXML2_LIBS) +check_PROGRAMS += test_plugin_virt +TESTS += test_plugin_virt + endif if BUILD_PLUGIN_VMEM diff --git a/src/daemon/Makefile.am b/src/daemon/Makefile.am index 74af5196..fbe4e30f 100644 --- a/src/daemon/Makefile.am +++ b/src/daemon/Makefile.am @@ -47,6 +47,8 @@ libheap_la_SOURCES = utils_heap.c utils_heap.h libmetadata_la_SOURCES = meta_data.c meta_data.h libplugin_mock_la_SOURCES = plugin_mock.c utils_cache_mock.c \ + utils_complain.c utils_complain.h \ + utils_ignorelist.c utils_ignorelist.h \ utils_time.c utils_time.h libplugin_mock_la_CPPFLAGS = $(AM_CPPFLAGS) -DMOCK_TIME libplugin_mock_la_LIBADD = $(COMMON_LIBS) libcommon.la diff --git a/src/daemon/plugin_mock.c b/src/daemon/plugin_mock.c index ddfc7890..558921d5 100644 --- a/src/daemon/plugin_mock.c +++ b/src/daemon/plugin_mock.c @@ -32,6 +32,12 @@ kstat_ctl_t *kc = NULL; char hostname_g[] = "example.com"; +int plugin_register_config(const char *name, + int (*callback)(const char *key, const char *val), + const char **keys, int keys_num) { + return ENOTSUP; +} + int plugin_register_complex_config(const char *type, int (*callback)(oconfig_item_t *)) { return ENOTSUP; @@ -45,6 +51,13 @@ int plugin_register_read(const char *name, int (*callback)(void)) { return ENOTSUP; } +int plugin_register_complex_read(const char *group, const char *name, + int (*callback)(user_data_t *), + cdtime_t interval, + user_data_t const *user_data) { + return ENOTSUP; +} + int plugin_register_shutdown(const char *name, int (*callback)(void)) { return ENOTSUP; } diff --git a/src/virt_test.c b/src/virt_test.c new file mode 100644 index 00000000..cb3cc254 --- /dev/null +++ b/src/virt_test.c @@ -0,0 +1,207 @@ +/** + * collectd - src/virt_test.c + * Copyright (C) 2016 Francesco Romani + * Based on + * collectd - src/ceph_test.c + * Copyright (C) 2015 Florian octo Forster + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; only version 2 of the License is applicable. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + * Authors: + * Florian octo Forster + **/ + +#include "virt.c" /* sic */ +#include "testing.h" + +#include + +static const char minimal_xml[] = + "" + "" + "" + " " + ""; + +static const char minimal_metadata_xml[] = + "" + "" + "" + " " + " virt-0" + " " + ""; + +struct xml_state { + xmlDocPtr xml_doc; + xmlXPathContextPtr xpath_ctx; + xmlXPathObjectPtr xpath_obj; + char tag[PARTITION_TAG_MAX_LEN]; +}; + +static int init_state(struct xml_state *st, const char *xml) { + memset(st, 0, sizeof(*st)); + + st->xml_doc = xmlReadDoc((const xmlChar *)xml, NULL, NULL, XML_PARSE_NONET); + if (st->xml_doc == NULL) { + return -1; + } + st->xpath_ctx = xmlXPathNewContext(st->xml_doc); + if (st->xpath_ctx == NULL) { + return -1; + } + return 0; +} + +static void fini_state(struct xml_state *st) { + if (st->xpath_ctx) { + xmlXPathFreeContext(st->xpath_ctx); + st->xpath_ctx = NULL; + } + if (st->xml_doc) { + xmlFreeDoc(st->xml_doc); + st->xml_doc = NULL; + } +} + +#define TAG "virt-0" + +DEF_TEST(lv_domain_get_tag_no_metadata_xml) { + int err; + struct xml_state st; + err = init_state(&st, minimal_xml); + EXPECT_EQ_INT(0, err); + + err = lv_domain_get_tag(st.xpath_ctx, "test", st.tag); + + EXPECT_EQ_INT(0, err); + EXPECT_EQ_STR("", st.tag); + + fini_state(&st); + return 0; +} + +DEF_TEST(lv_domain_get_tag_valid_xml) { + int err; + struct xml_state st; + err = init_state(&st, minimal_metadata_xml); + EXPECT_EQ_INT(0, err); + + err = lv_domain_get_tag(st.xpath_ctx, "test", st.tag); + + EXPECT_EQ_INT(0, err); + EXPECT_EQ_STR(TAG, st.tag); + + return 0; +} + +DEF_TEST(lv_default_instance_include_domain_without_tag) { + struct lv_read_instance *inst = NULL; + int ret; + + ret = lv_init_instance(0, lv_read); + inst = &(lv_read_user_data[0].inst); + EXPECT_EQ_STR("virt-0", inst->tag); + + ret = lv_instance_include_domain(inst, "testing", ""); + EXPECT_EQ_INT(1, ret); + + lv_fini_instance(0); + return 0; +} + +DEF_TEST(lv_regular_instance_skip_domain_without_tag) { + struct lv_read_instance *inst = NULL; + int ret; + + ret = lv_init_instance(1, lv_read); + inst = &(lv_read_user_data[1].inst); + EXPECT_EQ_STR("virt-1", inst->tag); + + ret = lv_instance_include_domain(inst, "testing", ""); + EXPECT_EQ_INT(0, ret); + + lv_fini_instance(0); + return 0; +} + +DEF_TEST(lv_include_domain_matching_tags) { + struct lv_read_instance *inst = NULL; + int ret; + + ret = lv_init_instance(0, lv_read); + inst = &(lv_read_user_data[0].inst); + EXPECT_EQ_STR("virt-0", inst->tag); + + ret = lv_instance_include_domain(inst, "testing", "virt-0"); + EXPECT_EQ_INT(1, ret); + + ret = lv_init_instance(1, lv_read); + inst = &(lv_read_user_data[1].inst); + EXPECT_EQ_STR("virt-1", inst->tag); + + ret = lv_instance_include_domain(inst, "testing", "virt-1"); + EXPECT_EQ_INT(1, ret); + + lv_fini_instance(0); + lv_fini_instance(1); + return 0; +} + +DEF_TEST(lv_default_instance_include_domain_with_unknown_tag) { + struct lv_read_instance *inst = NULL; + int ret; + + ret = lv_init_instance(0, lv_read); + inst = &(lv_read_user_data[0].inst); + EXPECT_EQ_STR("virt-0", inst->tag); + + ret = lv_instance_include_domain(inst, "testing", "unknownFormat-tag"); + EXPECT_EQ_INT(1, ret); + + lv_fini_instance(0); + return 0; +} + +DEF_TEST(lv_regular_instance_skip_domain_with_unknown_tag) { + struct lv_read_instance *inst = NULL; + int ret; + + ret = lv_init_instance(1, lv_read); + inst = &(lv_read_user_data[1].inst); + EXPECT_EQ_STR("virt-1", inst->tag); + + ret = lv_instance_include_domain(inst, "testing", "unknownFormat-tag"); + EXPECT_EQ_INT(0, ret); + + lv_fini_instance(0); + return 0; +} +#undef TAG + +int main(void) { + RUN_TEST(lv_domain_get_tag_no_metadata_xml); + RUN_TEST(lv_domain_get_tag_valid_xml); + + RUN_TEST(lv_default_instance_include_domain_without_tag); + RUN_TEST(lv_regular_instance_skip_domain_without_tag); + RUN_TEST(lv_include_domain_matching_tags); + RUN_TEST(lv_default_instance_include_domain_with_unknown_tag); + RUN_TEST(lv_regular_instance_skip_domain_with_unknown_tag); + + END_TEST; +} + +/* vim: set sw=2 sts=2 et : */