backup: Wire up qemu full pull backup commands over QMP
[libvirt/ericb.git] / tests / virschematest.c
blob330c42b0106a468fdbe6efeda91c1d2ad26a12d9
1 /*
2 * Copyright (C) 2016 Red Hat, Inc.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library. If not, see
16 * <http://www.gnu.org/licenses/>.
19 #include <config.h>
22 #include "testutils.h"
24 #include "virerror.h"
25 #include "viralloc.h"
26 #include "virlog.h"
27 #include "virxml.h"
29 #define VIR_FROM_THIS VIR_FROM_NONE
31 VIR_LOG_INIT("tests.schematest");
33 struct testSchemaData {
34 virXMLValidatorPtr validator;
35 const char *schema;
36 const char *xml_path;
40 static int
41 testSchemaFile(const void *args)
43 const struct testSchemaData *data = args;
44 bool shouldFail = virStringHasSuffix(data->xml_path, "-invalid.xml");
45 xmlDocPtr xml = NULL;
46 int ret = -1;
48 if (!(xml = virXMLParseFile(data->xml_path)))
49 return -1;
51 if (virXMLValidatorValidate(data->validator, xml) < 0) {
52 if (!shouldFail)
53 goto cleanup;
54 } else {
55 if (shouldFail)
56 goto cleanup;
59 ret = 0;
60 cleanup:
61 xmlFreeDoc(xml);
62 return ret;
66 static int
67 testSchemaDir(const char *schema,
68 virXMLValidatorPtr validator,
69 const char *dir_path)
71 DIR *dir = NULL;
72 struct dirent *ent;
73 int ret = 0;
74 int rc;
75 char *test_name = NULL;
76 char *xml_path = NULL;
77 struct testSchemaData data = {
78 .validator = validator,
81 if (virDirOpen(&dir, dir_path) < 0)
82 return -1;
84 while ((rc = virDirRead(dir, &ent, dir_path)) > 0) {
85 if (!virStringHasSuffix(ent->d_name, ".xml"))
86 continue;
87 if (ent->d_name[0] == '.')
88 continue;
90 if (virAsprintf(&xml_path, "%s/%s", dir_path, ent->d_name) < 0)
91 goto cleanup;
93 if (virAsprintf(&test_name, "Checking %s against %s",
94 ent->d_name, schema) < 0)
95 goto cleanup;
97 data.xml_path = xml_path;
98 if (virTestRun(test_name, testSchemaFile, &data) < 0)
99 ret = -1;
101 VIR_FREE(test_name);
102 VIR_FREE(xml_path);
105 if (rc < 0)
106 ret = -1;
108 cleanup:
109 VIR_FREE(test_name);
110 VIR_FREE(xml_path);
111 VIR_DIR_CLOSE(dir);
112 return ret;
116 static int
117 testSchemaDirs(const char *schema, virXMLValidatorPtr validator, ...)
119 va_list args;
120 int ret = 0;
121 char *dir_path = NULL;
122 const char *dir;
124 va_start(args, validator);
126 while ((dir = va_arg(args, char *))) {
127 if (virAsprintf(&dir_path, "%s/%s", abs_srcdir, dir) < 0) {
128 ret = -1;
129 goto cleanup;
131 if (testSchemaDir(schema, validator, dir_path) < 0)
132 ret = -1;
133 VIR_FREE(dir_path);
136 cleanup:
137 VIR_FREE(dir_path);
138 va_end(args);
139 return ret;
143 static int
144 testSchemaGrammar(const void *opaque)
146 struct testSchemaData *data = (struct testSchemaData *) opaque;
147 char *schema_path;
148 int ret = -1;
150 if (virAsprintf(&schema_path, "%s/docs/schemas/%s",
151 abs_top_srcdir, data->schema) < 0)
152 return -1;
154 if (!(data->validator = virXMLValidatorInit(schema_path)))
155 goto cleanup;
157 ret = 0;
159 cleanup:
160 VIR_FREE(schema_path);
161 return ret;
165 static int
166 mymain(void)
168 int ret = 0;
169 struct testSchemaData data;
171 memset(&data, 0, sizeof(data));
173 #define DO_TEST_DIR(sch, ...) \
174 do { \
175 data.schema = sch; \
176 if (virTestRun("test schema grammar file: " sch, \
177 testSchemaGrammar, &data) == 0) { \
178 /* initialize the validator even if the schema test \
179 * was skipped because of VIR_TEST_RANGE */ \
180 if (!data.validator && testSchemaGrammar(&data) < 0) { \
181 ret = -1; \
182 break; \
184 if (testSchemaDirs(sch, data.validator, __VA_ARGS__, NULL) < 0) \
185 ret = -1; \
187 virXMLValidatorFree(data.validator); \
188 data.validator = NULL; \
189 } else { \
190 ret = -1; \
192 } while (0)
194 #define DO_TEST_FILE(sch, xmlfile) \
195 do { \
196 data.schema = sch; \
197 data.xml_path = abs_srcdir "/" xmlfile; \
198 if (virTestRun("test schema grammar file: " sch, \
199 testSchemaGrammar, &data) == 0) { \
200 /* initialize the validator even if the schema test \
201 * was skipped because of VIR_TEST_RANGE */ \
202 if (!data.validator && testSchemaGrammar(&data) < 0) { \
203 ret = -1; \
204 break; \
206 if (virTestRun("Checking " xmlfile " against " sch, \
207 testSchemaFile, &data) < 0) \
208 ret = -1; \
210 virXMLValidatorFree(data.validator); \
211 data.validator = NULL; \
212 } else { \
213 ret = -1; \
215 } while (0)
217 DO_TEST_DIR("capability.rng", "capabilityschemadata", "vircaps2xmldata");
218 DO_TEST_DIR("domain.rng", "domainschemadata",
219 "qemuxml2argvdata", "xmconfigdata",
220 "qemuxml2xmloutdata", "lxcxml2xmldata",
221 "lxcxml2xmloutdata", "bhyvexml2argvdata", "genericxml2xmlindata",
222 "genericxml2xmloutdata", "xlconfigdata", "libxlxml2domconfigdata",
223 "qemuhotplugtestdomains");
224 DO_TEST_DIR("domainbackup.rng", "domainbackupxml2xmlin",
225 "domainbackupxml2xmlout");
226 DO_TEST_DIR("domaincaps.rng", "domaincapsschemadata");
227 DO_TEST_DIR("domaincheckpoint.rng", "qemudomaincheckpointxml2xmlin",
228 "qemudomaincheckpointxml2xmlout");
229 DO_TEST_DIR("domainsnapshot.rng", "qemudomainsnapshotxml2xmlin",
230 "qemudomainsnapshotxml2xmlout");
231 DO_TEST_DIR("interface.rng", "interfaceschemadata");
232 DO_TEST_DIR("network.rng", "../src/network", "networkxml2xmlin",
233 "networkxml2xmlout", "networkxml2confdata");
234 DO_TEST_DIR("networkport.rng", "virnetworkportxml2xmldata");
235 DO_TEST_DIR("nodedev.rng", "nodedevschemadata");
236 DO_TEST_DIR("nwfilter.rng", "nwfilterxml2xmlout", "../src/nwfilter");
237 DO_TEST_DIR("nwfilterbinding.rng", "virnwfilterbindingxml2xmldata");
238 DO_TEST_DIR("secret.rng", "secretxml2xmlin");
239 DO_TEST_DIR("storagepoolcaps.rng", "storagepoolcapsschemadata");
240 DO_TEST_DIR("storagepool.rng", "storagepoolxml2xmlin", "storagepoolxml2xmlout",
241 "storagepoolschemadata");
242 DO_TEST_DIR("storagevol.rng", "storagevolxml2xmlin", "storagevolxml2xmlout",
243 "storagevolschemadata");
245 DO_TEST_FILE("../news.rng", "../docs/news.xml");
247 return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
250 VIR_TEST_MAIN(mymain)