summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 19828ad)
raw | patch | inline | side by side (parent: 19828ad)
author | Francesco Romani <fromani@redhat.com> | |
Wed, 23 Nov 2016 17:38:42 +0000 (18:38 +0100) | ||
committer | Francesco Romani <fromani@redhat.com> | |
Wed, 30 Nov 2016 09:27:00 +0000 (10:27 +0100) |
Bootstrap the unit tests for the virt plugin, starting with
the newly-added partition/tag support.
Signed-off-by: Francesco Romani <fromani@redhat.com>
the newly-added partition/tag support.
Signed-off-by: Francesco Romani <fromani@redhat.com>
src/Makefile.am | patch | blob | history | |
src/daemon/Makefile.am | patch | blob | history | |
src/daemon/plugin_mock.c | patch | blob | history | |
src/virt_test.c | [new file with mode: 0644] | patch | blob |
diff --git a/src/Makefile.am b/src/Makefile.am
index 3776389477485841b0c21f9842fb253b63241f79..0601ce9f7e7ab97262b34e11dcb88c136179e842 100644 (file)
--- a/src/Makefile.am
+++ b/src/Makefile.am
$(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 74af5196a25f07ca0811201b191ec488cd962e43..fbe4e30f2ddcc15caff95e028b310a3c807148c5 100644 (file)
--- a/src/daemon/Makefile.am
+++ b/src/daemon/Makefile.am
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
index ddfc78904f81faadb81f077806eec9958c9aac05..558921d5ebaf77bc41eb16edf1dd6f1086c288d0 100644 (file)
--- a/src/daemon/plugin_mock.c
+++ b/src/daemon/plugin_mock.c
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;
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
--- /dev/null
+++ b/src/virt_test.c
@@ -0,0 +1,207 @@
+/**
+ * collectd - src/virt_test.c
+ * Copyright (C) 2016 Francesco Romani <fromani at redhat.com>
+ * 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 <octo at collectd.org>
+ **/
+
+#include "virt.c" /* sic */
+#include "testing.h"
+
+#include <unistd.h>
+
+static const char minimal_xml[] =
+ ""
+ "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
+ "<domain type=\"kvm\" xmlns:ovirt=\"http://ovirt.org/vm/tune/1.0\">"
+ " <metadata/>"
+ "</domain>";
+
+static const char minimal_metadata_xml[] =
+ ""
+ "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
+ "<domain type=\"kvm\" xmlns:ovirt=\"http://ovirt.org/vm/tune/1.0\">"
+ " <metadata>"
+ " <ovirtmap:tag "
+ "xmlns:ovirtmap=\"http://ovirt.org/ovirtmap/tag/1.0\">virt-0</ovirtmap:tag>"
+ " </metadata>"
+ "</domain>";
+
+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 : */