backup: Wire up qemu full pull backup commands over QMP
[libvirt/ericb.git] / tests / qemumigparamstest.c
blobdcefde2dbc3fcfc1ca491dec57f81ff50cb753b1
1 /*
2 * Copyright (C) 2011-2013 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/>.
20 #include <config.h>
22 #include "virjson.h"
23 #include "virbuffer.h"
24 #include "virxml.h"
25 #include "testutils.h"
26 #include "testutilsqemu.h"
27 #include "qemumonitortestutils.h"
28 #include "qemu/qemu_migration_params.h"
29 #define LIBVIRT_QEMU_MIGRATION_PARAMSPRIV_H_ALLOW
30 #include "qemu/qemu_migration_paramspriv.h"
31 #include "qemu/qemu_monitor.h"
33 #define VIR_FROM_THIS VIR_FROM_NONE
35 typedef struct _qemuMigParamsData qemuMigParamsData;
36 struct _qemuMigParamsData {
37 virDomainXMLOptionPtr xmlopt;
38 const char *name;
42 static void
43 qemuMigParamsTestFormatXML(virBufferPtr buf,
44 qemuMigrationParamsPtr migParams)
46 virBufferAddLit(buf, "<test>\n");
47 virBufferAdjustIndent(buf, 2);
49 if (migParams)
50 qemuMigrationParamsFormat(buf, migParams);
52 virBufferAdjustIndent(buf, -2);
53 virBufferAddLit(buf, "</test>\n");
57 static int
58 qemuMigParamsTestXML2XML(const void *opaque)
60 const qemuMigParamsData *data = opaque;
61 virBuffer buf = VIR_BUFFER_INITIALIZER;
62 char *xmlFile = NULL;
63 xmlDocPtr doc = NULL;
64 xmlXPathContextPtr ctxt = NULL;
65 qemuMigrationParamsPtr migParams = NULL;
66 char *actualXML = NULL;
67 int ret = -1;
69 if (virAsprintf(&xmlFile, "%s/qemumigparamsdata/%s.xml",
70 abs_srcdir, data->name) < 0)
71 goto cleanup;
73 if (!(doc = virXMLParseFileCtxt(xmlFile, &ctxt)))
74 goto cleanup;
76 if (qemuMigrationParamsParse(ctxt, &migParams) < 0)
77 goto cleanup;
79 qemuMigParamsTestFormatXML(&buf, migParams);
81 if (!(actualXML = virBufferContentAndReset(&buf)))
82 goto cleanup;
84 if (virTestCompareToFile(actualXML, xmlFile) < 0)
85 goto cleanup;
87 ret = 0;
89 cleanup:
90 VIR_FREE(xmlFile);
91 VIR_FREE(actualXML);
92 qemuMigrationParamsFree(migParams);
93 virBufferFreeAndReset(&buf);
94 xmlXPathFreeContext(ctxt);
95 xmlFreeDoc(doc);
96 return ret;
100 static int
101 qemuMigParamsTestXML(const void *opaque)
103 const qemuMigParamsData *data = opaque;
104 virBuffer buf = VIR_BUFFER_INITIALIZER;
105 char *replyFile = NULL;
106 char *xmlFile = NULL;
107 qemuMonitorTestPtr mon = NULL;
108 virJSONValuePtr params = NULL;
109 qemuMigrationParamsPtr migParams = NULL;
110 char *actualXML = NULL;
111 int ret = -1;
113 if (virAsprintf(&replyFile, "%s/qemumigparamsdata/%s.reply",
114 abs_srcdir, data->name) < 0 ||
115 virAsprintf(&xmlFile, "%s/qemumigparamsdata/%s.xml",
116 abs_srcdir, data->name) < 0)
117 goto cleanup;
119 if (!(mon = qemuMonitorTestNewFromFile(replyFile, data->xmlopt, true)))
120 goto cleanup;
122 if (qemuMonitorGetMigrationParams(qemuMonitorTestGetMonitor(mon),
123 &params) < 0)
124 goto cleanup;
126 if (!(migParams = qemuMigrationParamsFromJSON(params)))
127 goto cleanup;
129 qemuMigParamsTestFormatXML(&buf, migParams);
131 if (!(actualXML = virBufferContentAndReset(&buf)))
132 goto cleanup;
134 if (virTestCompareToFile(actualXML, xmlFile) < 0)
135 goto cleanup;
137 ret = 0;
139 cleanup:
140 VIR_FREE(replyFile);
141 VIR_FREE(xmlFile);
142 VIR_FREE(actualXML);
143 virJSONValueFree(params);
144 qemuMigrationParamsFree(migParams);
145 virBufferFreeAndReset(&buf);
146 qemuMonitorTestFree(mon);
147 return ret;
151 static int
152 qemuMigParamsTestJSON(const void *opaque)
154 const qemuMigParamsData *data = opaque;
155 char *replyFile = NULL;
156 char *jsonFile = NULL;
157 qemuMonitorTestPtr mon = NULL;
158 virJSONValuePtr paramsIn = NULL;
159 virJSONValuePtr paramsOut = NULL;
160 qemuMigrationParamsPtr migParams = NULL;
161 char *actualJSON = NULL;
162 int ret = -1;
164 if (virAsprintf(&replyFile, "%s/qemumigparamsdata/%s.reply",
165 abs_srcdir, data->name) < 0 ||
166 virAsprintf(&jsonFile, "%s/qemumigparamsdata/%s.json",
167 abs_srcdir, data->name) < 0)
168 goto cleanup;
170 if (!(mon = qemuMonitorTestNewFromFile(replyFile, data->xmlopt, true)))
171 goto cleanup;
173 if (qemuMonitorGetMigrationParams(qemuMonitorTestGetMonitor(mon),
174 &paramsIn) < 0)
175 goto cleanup;
177 if (!(migParams = qemuMigrationParamsFromJSON(paramsIn)))
178 goto cleanup;
180 if (!(paramsOut = qemuMigrationParamsToJSON(migParams)) ||
181 !(actualJSON = virJSONValueToString(paramsOut, true)))
182 goto cleanup;
184 if (virTestCompareToFile(actualJSON, jsonFile) < 0)
185 goto cleanup;
187 ret = 0;
189 cleanup:
190 VIR_FREE(replyFile);
191 VIR_FREE(jsonFile);
192 VIR_FREE(actualJSON);
193 virJSONValueFree(paramsIn);
194 virJSONValueFree(paramsOut);
195 qemuMigrationParamsFree(migParams);
196 qemuMonitorTestFree(mon);
197 return ret;
201 static int
202 mymain(void)
204 virQEMUDriver driver;
205 int ret = 0;
207 #if !WITH_YAJL
208 fputs("libvirt not compiled with JSON support, skipping this test\n", stderr);
209 return EXIT_AM_SKIP;
210 #endif
212 if (virThreadInitialize() < 0 ||
213 qemuTestDriverInit(&driver) < 0)
214 return EXIT_FAILURE;
216 virEventRegisterDefaultImpl();
218 #define DO_TEST(name) \
219 do { \
220 qemuMigParamsData data = { \
221 driver.xmlopt, name \
222 }; \
223 if (virTestRun(name " (xml)", qemuMigParamsTestXML, &data) < 0) \
224 ret = -1; \
225 if (virTestRun(name " (json)", qemuMigParamsTestJSON, &data) < 0) \
226 ret = -1; \
227 if (virTestRun(name " (xml2xml)", qemuMigParamsTestXML2XML, &data) < 0) \
228 ret = -1; \
229 } while (0)
231 DO_TEST("unsupported");
232 DO_TEST("empty");
233 DO_TEST("basic");
234 DO_TEST("tls");
235 DO_TEST("tls-enabled");
236 DO_TEST("tls-hostname");
238 qemuTestDriverFree(&driver);
240 return (ret == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
243 VIR_TEST_MAIN(mymain)