backup: Wire up qemu full pull backup commands over QMP
[libvirt/ericb.git] / tests / qemudomainsnapshotxml2xmltest.c
blobc84ee0bf7d1793f3261bb58c4e993bc9f530112c
1 #include <config.h>
3 #include <unistd.h>
5 #include <sys/types.h>
6 #include <fcntl.h>
8 #include "testutils.h"
10 #ifdef WITH_QEMU
12 # include "internal.h"
13 # include "qemu/qemu_conf.h"
14 # include "qemu/qemu_domain.h"
15 # include "testutilsqemu.h"
16 # include "virstring.h"
18 # define VIR_FROM_THIS VIR_FROM_NONE
20 static virQEMUDriver driver;
22 enum {
23 TEST_INTERNAL = 1 << 0, /* Test use of INTERNAL parse/format flag */
24 TEST_REDEFINE = 1 << 1, /* Test use of REDEFINE parse flag */
25 TEST_RUNNING = 1 << 2, /* Set snapshot state to running after parse */
28 static int
29 testCompareXMLToXMLFiles(const char *inxml,
30 const char *outxml,
31 const char *uuid,
32 unsigned int flags)
34 char *inXmlData = NULL;
35 char *outXmlData = NULL;
36 char *actual = NULL;
37 int ret = -1;
38 unsigned int parseflags = VIR_DOMAIN_SNAPSHOT_PARSE_DISKS;
39 unsigned int formatflags = VIR_DOMAIN_SNAPSHOT_FORMAT_SECURE;
40 bool cur = false;
41 VIR_AUTOUNREF(virDomainSnapshotDefPtr) def = NULL;
43 if (flags & TEST_INTERNAL) {
44 parseflags |= VIR_DOMAIN_SNAPSHOT_PARSE_INTERNAL;
45 formatflags |= VIR_DOMAIN_SNAPSHOT_FORMAT_INTERNAL;
48 if (flags & TEST_REDEFINE)
49 parseflags |= VIR_DOMAIN_SNAPSHOT_PARSE_REDEFINE;
51 if (virTestLoadFile(inxml, &inXmlData) < 0)
52 goto cleanup;
54 if (virTestLoadFile(outxml, &outXmlData) < 0)
55 goto cleanup;
57 if (!(def = virDomainSnapshotDefParseString(inXmlData, driver.caps,
58 driver.xmlopt, NULL, &cur,
59 parseflags)))
60 goto cleanup;
61 if (cur) {
62 if (!(flags & TEST_INTERNAL))
63 goto cleanup;
64 formatflags |= VIR_DOMAIN_SNAPSHOT_FORMAT_CURRENT;
66 if (flags & TEST_RUNNING) {
67 if (def->state)
68 goto cleanup;
69 def->state = VIR_DOMAIN_RUNNING;
72 if (!(actual = virDomainSnapshotDefFormat(uuid, def, driver.caps,
73 driver.xmlopt,
74 formatflags)))
75 goto cleanup;
77 if (STRNEQ(outXmlData, actual)) {
78 virTestDifferenceFull(stderr, outXmlData, outxml, actual, inxml);
79 goto cleanup;
82 ret = 0;
84 cleanup:
85 VIR_FREE(inXmlData);
86 VIR_FREE(outXmlData);
87 VIR_FREE(actual);
88 return ret;
91 struct testInfo {
92 const char *inxml;
93 const char *outxml;
94 const char *uuid;
95 long long creationTime;
96 unsigned int flags;
98 static long long mocktime;
100 static int
101 testSnapshotPostParse(virDomainMomentDefPtr def)
103 if (!mocktime)
104 return 0;
105 if (def->creationTime)
106 return -1;
107 def->creationTime = mocktime;
108 if (!def->name &&
109 virAsprintf(&def->name, "%lld", def->creationTime) < 0)
110 return -1;
111 return 0;
114 static int
115 testCompareXMLToXMLHelper(const void *data)
117 const struct testInfo *info = data;
119 mocktime = info->creationTime;
120 return testCompareXMLToXMLFiles(info->inxml, info->outxml, info->uuid,
121 info->flags);
125 static int
126 mymain(void)
128 int ret = 0;
130 if (qemuTestDriverInit(&driver) < 0)
131 return EXIT_FAILURE;
133 virDomainXMLOptionSetMomentPostParse(driver.xmlopt,
134 testSnapshotPostParse);
136 # define DO_TEST(prefix, name, inpath, outpath, uuid, time, flags) \
137 do { \
138 const struct testInfo info = {abs_srcdir "/" inpath "/" name ".xml", \
139 abs_srcdir "/" outpath "/" name ".xml", \
140 uuid, time, flags}; \
141 if (virTestRun("SNAPSHOT XML-2-XML " prefix " " name, \
142 testCompareXMLToXMLHelper, &info) < 0) \
143 ret = -1; \
144 } while (0)
146 # define DO_TEST_IN(name, uuid) DO_TEST("in->in", name, \
147 "qemudomainsnapshotxml2xmlin", \
148 "qemudomainsnapshotxml2xmlin", \
149 uuid, 0, 0)
151 # define DO_TEST_OUT(name, uuid, internal) \
152 DO_TEST("out->out", name, "qemudomainsnapshotxml2xmlout", \
153 "qemudomainsnapshotxml2xmlout", uuid, 0, internal | TEST_REDEFINE)
155 # define DO_TEST_INOUT(name, uuid, time, flags) \
156 DO_TEST("in->out", name, \
157 "qemudomainsnapshotxml2xmlin",\
158 "qemudomainsnapshotxml2xmlout",\
159 uuid, time, flags)
161 /* Unset or set all envvars here that are copied in qemudBuildCommandLine
162 * using ADD_ENV_COPY, otherwise these tests may fail due to unexpected
163 * values for these envvars */
164 setenv("PATH", "/bin", 1);
166 DO_TEST_OUT("all_parameters", "9d37b878-a7cc-9f9a-b78f-49b3abad25a8",
167 TEST_INTERNAL);
168 DO_TEST_OUT("disk_snapshot_redefine", "c7a5fdbd-edaf-9455-926a-d65c16db1809",
169 TEST_INTERNAL);
170 DO_TEST_OUT("full_domain", "c7a5fdbd-edaf-9455-926a-d65c16db1809",
171 TEST_INTERNAL);
172 DO_TEST_OUT("noparent_nodescription_noactive", NULL, 0);
173 DO_TEST_OUT("noparent_nodescription", NULL, TEST_INTERNAL);
174 DO_TEST_OUT("noparent", "9d37b878-a7cc-9f9a-b78f-49b3abad25a8", 0);
175 DO_TEST_OUT("metadata", "c7a5fdbd-edaf-9455-926a-d65c16db1809", 0);
176 DO_TEST_OUT("external_vm_redefine", "c7a5fdbd-edaf-9455-926a-d65c16db1809",
179 DO_TEST_INOUT("empty", "9d37b878-a7cc-9f9a-b78f-49b3abad25a8",
180 1386166249, 0);
181 DO_TEST_INOUT("noparent", "9d37b878-a7cc-9f9a-b78f-49b3abad25a8",
182 1272917631, TEST_RUNNING);
183 DO_TEST_INOUT("external_vm", NULL, 1555419243, 0);
184 DO_TEST_INOUT("disk_snapshot", NULL, 1555419243, 0);
185 DO_TEST_INOUT("disk_driver_name_null", NULL, 1555419243, 0);
186 DO_TEST_INOUT("disk-seclabel", "9d37b878-a7cc-9f9a-b78f-49b3abad25a8", 581484660, 0);
188 DO_TEST_IN("name_and_description", NULL);
189 DO_TEST_IN("description_only", NULL);
190 DO_TEST_IN("name_only", NULL);
192 qemuTestDriverFree(&driver);
194 return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
197 VIR_TEST_MAIN(mymain)
199 #else
202 main(void)
204 return EXIT_AM_SKIP;
207 #endif /* WITH_QEMU */