13 #include "testutils.h"
17 # include "internal.h"
18 # include "qemu/qemu_conf.h"
19 # include "qemu/qemu_domain.h"
20 # include "testutilsqemu.h"
21 # include "virstring.h"
23 # define VIR_FROM_THIS VIR_FROM_NONE
25 static virQEMUDriver driver;
27 /* This regex will skip the following XML constructs in test files
28 * that are dynamically generated and thus problematic to test:
29 * <name>1234352345</name> if the snapshot has no name,
30 * <creationTime>23523452345</creationTime>,
31 * <state>nostate</state> as the backend code doesn't fill this
33 static const char *testSnapshotXMLVariableLineRegexStr =
34 "(<(name|creationTime)>[0-9]+</(name|creationTime)>|"
35 "<state>nostate</state>)";
37 regex_t *testSnapshotXMLVariableLineRegex = NULL;
40 testFilterXML(char *xml)
42 virBuffer buf = VIR_BUFFER_INITIALIZER;
43 char **xmlLines = NULL;
47 if (!(xmlLines = virStringSplit(xml, "\n", 0))) {
53 for (xmlLine = xmlLines; *xmlLine; xmlLine++) {
54 if (regexec(testSnapshotXMLVariableLineRegex,
55 *xmlLine, 0, NULL, 0) == 0)
58 virBufferStrcat(&buf, *xmlLine, "\n", NULL);
61 if (virBufferError(&buf)) {
66 ret = virBufferContentAndReset(&buf);
69 virBufferFreeAndReset(&buf);
70 virStringFreeList(xmlLines);
75 testCompareXMLToXMLFiles(const char *inxml,
81 char *inXmlData = NULL;
82 char *outXmlData = NULL;
85 virDomainSnapshotDefPtr def = NULL;
86 unsigned int flags = VIR_DOMAIN_SNAPSHOT_PARSE_DISKS;
89 flags |= VIR_DOMAIN_SNAPSHOT_PARSE_INTERNAL;
92 flags |= VIR_DOMAIN_SNAPSHOT_PARSE_REDEFINE;
94 if (virtTestLoadFile(inxml, &inXmlData) < 0)
97 if (virtTestLoadFile(outxml, &outXmlData) < 0)
100 if (!(def = virDomainSnapshotDefParseString(inXmlData, driver.caps,
102 QEMU_EXPECTED_VIRT_TYPES,
106 if (!(actual = virDomainSnapshotDefFormat(uuid, def,
107 VIR_DOMAIN_XML_SECURE,
112 if (!(actual = testFilterXML(actual)))
115 if (!(outXmlData = testFilterXML(outXmlData)))
119 if (STRNEQ(outXmlData, actual)) {
120 virtTestDifference(stderr, outXmlData, actual);
128 VIR_FREE(outXmlData);
130 virDomainSnapshotDefFree(def);
144 testCompareXMLToXMLHelper(const void *data)
146 const struct testInfo *info = data;
148 return testCompareXMLToXMLFiles(info->inxml, info->outxml, info->uuid,
149 info->internal, info->redefine);
158 if ((driver.caps = testQemuCapsInit()) == NULL)
161 if (!(driver.xmlopt = virQEMUDriverCreateXMLConf(&driver))) {
162 virObjectUnref(driver.caps);
166 if (VIR_ALLOC(testSnapshotXMLVariableLineRegex) < 0)
169 if (regcomp(testSnapshotXMLVariableLineRegex,
170 testSnapshotXMLVariableLineRegexStr,
171 REG_EXTENDED | REG_NOSUB) != 0) {
172 virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
173 "failed to compile test regex");
178 # define DO_TEST(prefix, name, inpath, outpath, uuid, internal, redefine) \
180 const struct testInfo info = {abs_srcdir "/" inpath "/" name ".xml", \
181 abs_srcdir "/" outpath "/" name ".xml", \
182 uuid, internal, redefine}; \
183 if (virtTestRun("SNAPSHOT XML-2-XML " prefix " " name, \
184 testCompareXMLToXMLHelper, &info) < 0) \
188 # define DO_TEST_IN(name, uuid) DO_TEST("in->in", name,\
189 "domainsnapshotxml2xmlin",\
190 "domainsnapshotxml2xmlin",\
193 # define DO_TEST_OUT(name, uuid, internal) DO_TEST("out->out", name,\
194 "domainsnapshotxml2xmlout",\
195 "domainsnapshotxml2xmlout",\
196 uuid, internal, true)
198 # define DO_TEST_INOUT(name, uuid, internal, redefine) \
199 DO_TEST("in->out", name,\
200 "domainsnapshotxml2xmlin",\
201 "domainsnapshotxml2xmlout",\
202 uuid, internal, redefine)
204 /* Unset or set all envvars here that are copied in qemudBuildCommandLine
205 * using ADD_ENV_COPY, otherwise these tests may fail due to unexpected
206 * values for these envvars */
207 setenv("PATH", "/bin", 1);
209 DO_TEST_OUT("all_parameters", "9d37b878-a7cc-9f9a-b78f-49b3abad25a8", true);
210 DO_TEST_OUT("disk_snapshot_redefine", "c7a5fdbd-edaf-9455-926a-d65c16db1809", true);
211 DO_TEST_OUT("full_domain", "c7a5fdbd-edaf-9455-926a-d65c16db1809", true);
212 DO_TEST_OUT("noparent_nodescription_noactive", NULL, false);
213 DO_TEST_OUT("noparent_nodescription", NULL, true);
214 DO_TEST_OUT("noparent", "9d37b878-a7cc-9f9a-b78f-49b3abad25a8", false);
215 DO_TEST_OUT("metadata", "c7a5fdbd-edaf-9455-926a-d65c16db1809", false);
216 DO_TEST_OUT("external_vm_redefine", "c7a5fdbd-edaf-9455-926a-d65c16db1809", false);
218 DO_TEST_INOUT("empty", "9d37b878-a7cc-9f9a-b78f-49b3abad25a8", false, false);
219 DO_TEST_INOUT("noparent", "9d37b878-a7cc-9f9a-b78f-49b3abad25a8", false, false);
220 DO_TEST_INOUT("external_vm", NULL, false, false);
221 DO_TEST_INOUT("noparent", "9d37b878-a7cc-9f9a-b78f-49b3abad25a8", false, false);
222 DO_TEST_INOUT("disk_snapshot", NULL, false, false);
223 DO_TEST_INOUT("disk_driver_name_null", NULL, false, false);
225 DO_TEST_IN("name_and_description", NULL);
226 DO_TEST_IN("description_only", NULL);
227 DO_TEST_IN("name_only", NULL);
230 if (testSnapshotXMLVariableLineRegex)
231 regfree(testSnapshotXMLVariableLineRegex);
232 VIR_FREE(testSnapshotXMLVariableLineRegex);
233 virObjectUnref(driver.caps);
234 virObjectUnref(driver.xmlopt);
236 return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
239 VIRT_TEST_MAIN(mymain)
249 #endif /* WITH_QEMU */