Imported Upstream version 1.2.4
[archive/platform/upstream/libvirt.git] / tests / domainsnapshotxml2xmltest.c
1 #include <config.h>
2
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <unistd.h>
6 #include <string.h>
7
8 #include <sys/types.h>
9 #include <fcntl.h>
10
11 #include <regex.h>
12
13 #include "testutils.h"
14
15 #ifdef WITH_QEMU
16
17 # include "internal.h"
18 # include "qemu/qemu_conf.h"
19 # include "qemu/qemu_domain.h"
20 # include "testutilsqemu.h"
21 # include "virstring.h"
22
23 # define VIR_FROM_THIS VIR_FROM_NONE
24
25 static virQEMUDriver driver;
26
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
32  */
33 static const char *testSnapshotXMLVariableLineRegexStr =
34     "(<(name|creationTime)>[0-9]+</(name|creationTime)>|"
35     "<state>nostate</state>)";
36
37 regex_t *testSnapshotXMLVariableLineRegex = NULL;
38
39 static char *
40 testFilterXML(char *xml)
41 {
42     virBuffer buf = VIR_BUFFER_INITIALIZER;
43     char **xmlLines = NULL;
44     char **xmlLine;
45     char *ret = NULL;
46
47     if (!(xmlLines = virStringSplit(xml, "\n", 0))) {
48         VIR_FREE(xml);
49         goto cleanup;
50     }
51     VIR_FREE(xml);
52
53     for (xmlLine = xmlLines; *xmlLine; xmlLine++) {
54         if (regexec(testSnapshotXMLVariableLineRegex,
55                     *xmlLine, 0, NULL, 0) == 0)
56             continue;
57
58         virBufferStrcat(&buf, *xmlLine, "\n", NULL);
59     }
60
61     if (virBufferError(&buf)) {
62         virReportOOMError();
63         goto cleanup;
64     }
65
66     ret = virBufferContentAndReset(&buf);
67
68  cleanup:
69     virBufferFreeAndReset(&buf);
70     virStringFreeList(xmlLines);
71     return ret;
72 }
73
74 static int
75 testCompareXMLToXMLFiles(const char *inxml,
76                          const char *outxml,
77                          const char *uuid,
78                          bool internal,
79                          bool redefine)
80 {
81     char *inXmlData = NULL;
82     char *outXmlData = NULL;
83     char *actual = NULL;
84     int ret = -1;
85     virDomainSnapshotDefPtr def = NULL;
86     unsigned int flags = VIR_DOMAIN_SNAPSHOT_PARSE_DISKS;
87
88     if (internal)
89         flags |= VIR_DOMAIN_SNAPSHOT_PARSE_INTERNAL;
90
91     if (redefine)
92         flags |= VIR_DOMAIN_SNAPSHOT_PARSE_REDEFINE;
93
94     if (virtTestLoadFile(inxml, &inXmlData) < 0)
95         goto cleanup;
96
97     if (virtTestLoadFile(outxml, &outXmlData) < 0)
98         goto cleanup;
99
100     if (!(def = virDomainSnapshotDefParseString(inXmlData, driver.caps,
101                                                 driver.xmlopt,
102                                                 QEMU_EXPECTED_VIRT_TYPES,
103                                                 flags)))
104         goto cleanup;
105
106     if (!(actual = virDomainSnapshotDefFormat(uuid, def,
107                                               VIR_DOMAIN_XML_SECURE,
108                                               internal)))
109         goto cleanup;
110
111     if (!redefine) {
112         if (!(actual = testFilterXML(actual)))
113             goto cleanup;
114
115         if (!(outXmlData = testFilterXML(outXmlData)))
116             goto cleanup;
117     }
118
119     if (STRNEQ(outXmlData, actual)) {
120         virtTestDifference(stderr, outXmlData, actual);
121         goto cleanup;
122     }
123
124     ret = 0;
125
126  cleanup:
127     VIR_FREE(inXmlData);
128     VIR_FREE(outXmlData);
129     VIR_FREE(actual);
130     virDomainSnapshotDefFree(def);
131     return ret;
132 }
133
134 struct testInfo {
135     const char *inxml;
136     const char *outxml;
137     const char *uuid;
138     bool internal;
139     bool redefine;
140 };
141
142
143 static int
144 testCompareXMLToXMLHelper(const void *data)
145 {
146     const struct testInfo *info = data;
147
148     return testCompareXMLToXMLFiles(info->inxml, info->outxml, info->uuid,
149                                     info->internal, info->redefine);
150 }
151
152
153 static int
154 mymain(void)
155 {
156     int ret = 0;
157
158     if ((driver.caps = testQemuCapsInit()) == NULL)
159         return EXIT_FAILURE;
160
161     if (!(driver.xmlopt = virQEMUDriverCreateXMLConf(&driver))) {
162         virObjectUnref(driver.caps);
163         return EXIT_FAILURE;
164     }
165
166     if (VIR_ALLOC(testSnapshotXMLVariableLineRegex) < 0)
167         goto cleanup;
168
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");
174         goto cleanup;
175     }
176
177
178 # define DO_TEST(prefix, name, inpath, outpath, uuid, internal, redefine)     \
179     do {                                                                      \
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)                \
185             ret = -1;                                                         \
186     } while (0)
187
188 # define DO_TEST_IN(name, uuid) DO_TEST("in->in", name,\
189                                         "domainsnapshotxml2xmlin",\
190                                         "domainsnapshotxml2xmlin",\
191                                         uuid, false, false)
192
193 # define DO_TEST_OUT(name, uuid, internal) DO_TEST("out->out", name,\
194                                                    "domainsnapshotxml2xmlout",\
195                                                    "domainsnapshotxml2xmlout",\
196                                                    uuid, internal, true)
197
198 # define DO_TEST_INOUT(name, uuid, internal, redefine) \
199     DO_TEST("in->out", name,\
200             "domainsnapshotxml2xmlin",\
201             "domainsnapshotxml2xmlout",\
202             uuid, internal, redefine)
203
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);
208
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);
217
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);
224
225     DO_TEST_IN("name_and_description", NULL);
226     DO_TEST_IN("description_only", NULL);
227     DO_TEST_IN("name_only", NULL);
228
229  cleanup:
230     if (testSnapshotXMLVariableLineRegex)
231         regfree(testSnapshotXMLVariableLineRegex);
232     VIR_FREE(testSnapshotXMLVariableLineRegex);
233     virObjectUnref(driver.caps);
234     virObjectUnref(driver.xmlopt);
235
236     return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
237 }
238
239 VIRT_TEST_MAIN(mymain)
240
241 #else
242
243 int
244 main(void)
245 {
246     return EXIT_AM_SKIP;
247 }
248
249 #endif /* WITH_QEMU */