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/>.
22 #include "testutils.h"
23 #include "testutilsqemu.h"
24 #include "qemumonitortestutils.h"
27 #define VIR_FROM_THIS VIR_FROM_NONE
29 typedef struct _testQemuData testQemuData;
30 typedef testQemuData *testQemuDataPtr;
31 struct _testQemuData {
32 virDomainXMLOptionPtr xmlopt;
37 static qemuMonitorTestPtr
38 testQemuFeedMonitor(char *replies,
39 virDomainXMLOptionPtr xmlopt)
41 qemuMonitorTestPtr test = NULL;
43 char *singleReply = tmp;
45 /* Our JSON parser expects replies to be separated by a newline character.
46 * Hence we must preprocess the file a bit. */
47 while ((tmp = strchr(tmp, '\n'))) {
48 /* It is safe to touch tmp[1] since all strings ends with '\0'. */
51 if (*(tmp + 1) != '\n') {
55 /* Cut off a single reply. */
59 if (qemuMonitorTestAddItem(test, NULL, singleReply) < 0)
62 /* Create new mocked monitor with our greeting */
63 if (!(test = qemuMonitorTestNew(true, xmlopt, NULL, NULL, singleReply)))
68 /* Move the @tmp and @singleReply. */
78 if (test && qemuMonitorTestAddItem(test, NULL, singleReply) < 0)
84 qemuMonitorTestFree(test);
89 testQemuGetCaps(char *caps)
91 virQEMUCapsPtr qemuCaps = NULL;
93 xmlXPathContextPtr ctxt = NULL;
95 xmlNodePtr *nodes = NULL;
97 if (!(xml = virXMLParseStringCtxt(caps, "(test caps)", &ctxt)))
100 if ((n = virXPathNodeSet("/qemuCaps/flag", ctxt, &nodes)) < 0) {
101 fprintf(stderr, "failed to parse qemu capabilities flags");
106 if (!(qemuCaps = virQEMUCapsNew()))
109 for (i = 0; i < n; i++) {
110 char *str = virXMLPropString(nodes[i], "name");
112 int flag = virQEMUCapsTypeFromString(str);
114 fprintf(stderr, "Unknown qemu capabilities flag %s", str);
119 virQEMUCapsSet(qemuCaps, flag);
126 xmlXPathFreeContext(ctxt);
131 virObjectUnref(qemuCaps);
133 xmlXPathFreeContext(ctxt);
138 testQemuCapsCompare(virQEMUCapsPtr capsProvided,
139 virQEMUCapsPtr capsComputed)
144 for (i = 0; i < QEMU_CAPS_LAST; i++) {
145 if (virQEMUCapsGet(capsProvided, i) &&
146 !virQEMUCapsGet(capsComputed, i)) {
147 fprintf(stderr, "Caps mismatch: capsComputed is missing %s\n",
148 virQEMUCapsTypeToString(i));
152 if (virQEMUCapsGet(capsComputed, i) &&
153 !virQEMUCapsGet(capsProvided, i)) {
154 fprintf(stderr, "Caps mismatch: capsProvided is missing %s\n",
155 virQEMUCapsTypeToString(i));
164 testQemuCaps(const void *opaque)
167 const testQemuData *data = opaque;
168 char *repliesFile = NULL, *capsFile = NULL;
169 char *replies = NULL, *caps = NULL;
170 qemuMonitorTestPtr mon = NULL;
171 virQEMUCapsPtr capsProvided = NULL, capsComputed = NULL;
173 if (virAsprintf(&repliesFile, "%s/qemucapabilitiesdata/%s.replies",
174 abs_srcdir, data->base) < 0 ||
175 virAsprintf(&capsFile, "%s/qemucapabilitiesdata/%s.caps",
176 abs_srcdir, data->base) < 0)
179 if (virtTestLoadFile(repliesFile, &replies) < 0 ||
180 virtTestLoadFile(capsFile, &caps) < 0)
183 if (!(mon = testQemuFeedMonitor(replies, data->xmlopt)))
186 if (!(capsProvided = testQemuGetCaps(caps)))
189 if (!(capsComputed = virQEMUCapsNew()))
192 if (virQEMUCapsInitQMPMonitor(capsComputed,
193 qemuMonitorTestGetMonitor(mon)) < 0)
196 /* So that our test does not depend on the contents of /proc, we
197 * hoisted the setting of ENABLE_FIPS to virQEMUCapsInitQMP. But
198 * we do want to test the effect of that flag. */
200 virQEMUCapsSet(capsComputed, QEMU_CAPS_ENABLE_FIPS);
202 if (testQemuCapsCompare(capsProvided, capsComputed) < 0)
207 VIR_FREE(repliesFile);
211 qemuMonitorTestFree(mon);
212 virObjectUnref(capsProvided);
213 virObjectUnref(capsComputed);
221 virDomainXMLOptionPtr xmlopt;
225 fputs("libvirt not compiled with yajl, skipping this test\n", stderr);
229 if (virThreadInitialize() < 0 ||
230 !(xmlopt = virQEMUDriverCreateXMLConf(NULL)))
233 virEventRegisterDefaultImpl();
235 data.xmlopt = xmlopt;
237 #define DO_TEST_FULL(name, use_fips) \
239 data.fips = use_fips; \
240 if (virtTestRun(name, testQemuCaps, &data) < 0) \
243 #define DO_TEST(name) DO_TEST_FULL(name, false)
245 DO_TEST_FULL("caps_1.2.2-1", true);
246 DO_TEST("caps_1.3.1-1");
247 DO_TEST("caps_1.4.2-1");
248 DO_TEST("caps_1.5.3-1");
249 DO_TEST_FULL("caps_1.6.0-1", true);
250 DO_TEST("caps_1.6.50-1");
252 virObjectUnref(xmlopt);
253 return (ret == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
256 VIRT_TEST_MAIN(mymain)