2 * collectd - src/virt_test.c
3 * Copyright (C) 2016 Francesco Romani <fromani at redhat.com>
5 * collectd - src/ceph_test.c
6 * Copyright (C) 2015 Florian octo Forster
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; only version 2 of the License is applicable.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 * Florian octo Forster <octo at collectd.org>
26 #include "virt.c" /* sic */
30 static const char minimal_xml[] =
32 "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
33 "<domain type=\"kvm\" xmlns:ovirt=\"http://ovirt.org/vm/tune/1.0\">"
37 static const char minimal_metadata_xml[] =
39 "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
40 "<domain type=\"kvm\" xmlns:ovirt=\"http://ovirt.org/vm/tune/1.0\">"
43 "xmlns:ovirtmap=\"http://ovirt.org/ovirtmap/tag/1.0\">virt-0</ovirtmap:tag>"
49 xmlXPathContextPtr xpath_ctx;
50 xmlXPathObjectPtr xpath_obj;
51 char tag[PARTITION_TAG_MAX_LEN];
54 static int init_state(struct xml_state *st, const char *xml) {
55 memset(st, 0, sizeof(*st));
57 st->xml_doc = xmlReadDoc((const xmlChar *)xml, NULL, NULL, XML_PARSE_NONET);
58 if (st->xml_doc == NULL) {
61 st->xpath_ctx = xmlXPathNewContext(st->xml_doc);
62 if (st->xpath_ctx == NULL) {
68 static void fini_state(struct xml_state *st) {
70 xmlXPathFreeContext(st->xpath_ctx);
74 xmlFreeDoc(st->xml_doc);
81 DEF_TEST(lv_domain_get_tag_no_metadata_xml) {
84 err = init_state(&st, minimal_xml);
85 EXPECT_EQ_INT(0, err);
87 err = lv_domain_get_tag(st.xpath_ctx, "test", st.tag);
89 EXPECT_EQ_INT(0, err);
90 EXPECT_EQ_STR("", st.tag);
96 DEF_TEST(lv_domain_get_tag_valid_xml) {
99 err = init_state(&st, minimal_metadata_xml);
100 EXPECT_EQ_INT(0, err);
102 err = lv_domain_get_tag(st.xpath_ctx, "test", st.tag);
104 EXPECT_EQ_INT(0, err);
105 EXPECT_EQ_STR(TAG, st.tag);
110 DEF_TEST(lv_default_instance_include_domain_without_tag) {
111 struct lv_read_instance *inst = NULL;
114 ret = lv_init_instance(0, lv_read);
115 inst = &(lv_read_user_data[0].inst);
116 EXPECT_EQ_STR("virt-0", inst->tag);
118 ret = lv_instance_include_domain(inst, "testing", "");
119 EXPECT_EQ_INT(1, ret);
125 DEF_TEST(lv_regular_instance_skip_domain_without_tag) {
126 struct lv_read_instance *inst = NULL;
129 ret = lv_init_instance(1, lv_read);
130 inst = &(lv_read_user_data[1].inst);
131 EXPECT_EQ_STR("virt-1", inst->tag);
133 ret = lv_instance_include_domain(inst, "testing", "");
134 EXPECT_EQ_INT(0, ret);
140 DEF_TEST(lv_include_domain_matching_tags) {
141 struct lv_read_instance *inst = NULL;
144 ret = lv_init_instance(0, lv_read);
145 inst = &(lv_read_user_data[0].inst);
146 EXPECT_EQ_STR("virt-0", inst->tag);
148 ret = lv_instance_include_domain(inst, "testing", "virt-0");
149 EXPECT_EQ_INT(1, ret);
151 ret = lv_init_instance(1, lv_read);
152 inst = &(lv_read_user_data[1].inst);
153 EXPECT_EQ_STR("virt-1", inst->tag);
155 ret = lv_instance_include_domain(inst, "testing", "virt-1");
156 EXPECT_EQ_INT(1, ret);
163 DEF_TEST(lv_default_instance_include_domain_with_unknown_tag) {
164 struct lv_read_instance *inst = NULL;
167 ret = lv_init_instance(0, lv_read);
168 inst = &(lv_read_user_data[0].inst);
169 EXPECT_EQ_STR("virt-0", inst->tag);
171 ret = lv_instance_include_domain(inst, "testing", "unknownFormat-tag");
172 EXPECT_EQ_INT(1, ret);
178 DEF_TEST(lv_regular_instance_skip_domain_with_unknown_tag) {
179 struct lv_read_instance *inst = NULL;
182 ret = lv_init_instance(1, lv_read);
183 inst = &(lv_read_user_data[1].inst);
184 EXPECT_EQ_STR("virt-1", inst->tag);
186 ret = lv_instance_include_domain(inst, "testing", "unknownFormat-tag");
187 EXPECT_EQ_INT(0, ret);
195 RUN_TEST(lv_domain_get_tag_no_metadata_xml);
196 RUN_TEST(lv_domain_get_tag_valid_xml);
198 RUN_TEST(lv_default_instance_include_domain_without_tag);
199 RUN_TEST(lv_regular_instance_skip_domain_without_tag);
200 RUN_TEST(lv_include_domain_matching_tags);
201 RUN_TEST(lv_default_instance_include_domain_with_unknown_tag);
202 RUN_TEST(lv_regular_instance_skip_domain_with_unknown_tag);
207 /* vim: set sw=2 sts=2 et : */