tests: Use the new DO_TEST_CAPS_*() macros
[libvirt/ericb.git] / tests / libxlxml2domconfigtest.c
blob048e6c0431dfee1a6171e641185695a522e25e60
1 /*
2 * libxlxml2domconfigtest.c: test conversion of domXML to
3 * libxl_domain_config structure.
5 * Copyright (C) 2017 SUSE LINUX Products GmbH, Nuernberg, Germany.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library. If not, see
19 * <http://www.gnu.org/licenses/>.
22 #include <config.h>
24 #include <unistd.h>
26 #include <sys/types.h>
27 #include <fcntl.h>
29 #include "testutils.h"
31 #if defined(WITH_LIBXL) && defined(WITH_YAJL)
33 # include "internal.h"
34 # include "viralloc.h"
35 # include "libxl/libxl_conf.h"
36 # include "datatypes.h"
37 # include "virstring.h"
38 # include "virmock.h"
39 # include "virjson.h"
40 # include "testutilsxen.h"
42 # define VIR_FROM_THIS VIR_FROM_LIBXL
44 static virCapsPtr caps;
46 static int
47 testCompareXMLToDomConfig(const char *xmlfile,
48 const char *jsonfile)
50 int ret = -1;
51 libxl_domain_config actualconfig;
52 libxl_domain_config expectconfig;
53 libxlDriverConfigPtr cfg;
54 xentoollog_logger *log = NULL;
55 virPortAllocatorRangePtr gports = NULL;
56 virDomainXMLOptionPtr xmlopt = NULL;
57 virDomainDefPtr vmdef = NULL;
58 char *actualjson = NULL;
59 char *tempjson = NULL;
60 char *expectjson = NULL;
62 if (!(cfg = libxlDriverConfigNew()))
63 return -1;
65 cfg->caps = caps;
67 libxl_domain_config_init(&actualconfig);
68 libxl_domain_config_init(&expectconfig);
70 if (!(log = (xentoollog_logger *)xtl_createlogger_stdiostream(stderr, XTL_DEBUG, 0)))
71 goto cleanup;
73 /* for testing nested HVM */
74 cfg->nested_hvm = true;
76 /* replace logger with stderr one */
77 libxl_ctx_free(cfg->ctx);
79 if (libxl_ctx_alloc(&cfg->ctx, LIBXL_VERSION, 0, log) < 0)
80 goto cleanup;
82 if (!(gports = virPortAllocatorRangeNew("vnc", 5900, 6000)))
83 goto cleanup;
85 if (!(xmlopt = libxlCreateXMLConf()))
86 goto cleanup;
88 if (!(vmdef = virDomainDefParseFile(xmlfile, caps, xmlopt,
89 NULL, VIR_DOMAIN_XML_INACTIVE)))
90 goto cleanup;
92 if (libxlBuildDomainConfig(gports, vmdef, cfg, &actualconfig) < 0)
93 goto cleanup;
95 if (!(actualjson = libxl_domain_config_to_json(cfg->ctx, &actualconfig))) {
96 virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
97 "Failed to retrieve JSON doc for libxl_domain_config");
98 goto cleanup;
101 if (virTestLoadFile(jsonfile, &tempjson) < 0)
102 goto cleanup;
104 if (libxl_domain_config_from_json(cfg->ctx, &expectconfig, tempjson) != 0) {
105 virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
106 "Failed to create libxl_domain_config from JSON doc");
107 goto cleanup;
109 if (!(expectjson = libxl_domain_config_to_json(cfg->ctx, &expectconfig))) {
110 virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
111 "Failed to retrieve JSON doc for libxl_domain_config");
112 goto cleanup;
115 if (virTestCompareToString(expectjson, actualjson) < 0)
116 goto cleanup;
118 ret = 0;
120 cleanup:
121 if (vmdef &&
122 vmdef->ngraphics == 1 &&
123 vmdef->graphics[0]->type == VIR_DOMAIN_GRAPHICS_TYPE_VNC)
124 virPortAllocatorRelease(vmdef->graphics[0]->data.vnc.port);
126 VIR_FREE(expectjson);
127 VIR_FREE(actualjson);
128 VIR_FREE(tempjson);
129 virDomainDefFree(vmdef);
130 virPortAllocatorRangeFree(gports);
131 virObjectUnref(xmlopt);
132 libxl_domain_config_dispose(&actualconfig);
133 libxl_domain_config_dispose(&expectconfig);
134 xtl_logger_destroy(log);
135 cfg->caps = NULL;
136 virObjectUnref(cfg);
137 return ret;
141 struct testInfo {
142 const char *name;
146 static int
147 testCompareXMLToDomConfigHelper(const void *data)
149 int ret = -1;
150 const struct testInfo *info = data;
151 char *xmlfile = NULL;
152 char *jsonfile = NULL;
154 if (virAsprintf(&xmlfile, "%s/libxlxml2domconfigdata/%s.xml",
155 abs_srcdir, info->name) < 0 ||
156 virAsprintf(&jsonfile, "%s/libxlxml2domconfigdata/%s.json",
157 abs_srcdir, info->name) < 0)
158 goto cleanup;
160 ret = testCompareXMLToDomConfig(xmlfile, jsonfile);
162 cleanup:
163 VIR_FREE(xmlfile);
164 VIR_FREE(jsonfile);
165 return ret;
169 static int
170 mymain(void)
172 int ret = 0;
174 /* Set the timezone because we are mocking the time() function.
175 * If we don't do that, then localtime() may return unpredictable
176 * results. In order to detect things that just work by a blind
177 * chance, we need to set an virtual timezone that no libvirt
178 * developer resides in. */
179 if (setenv("TZ", "VIR00:30", 1) < 0) {
180 perror("setenv");
181 return EXIT_FAILURE;
184 if ((caps = testXLInitCaps()) == NULL)
185 return EXIT_FAILURE;
187 # define DO_TEST(name) \
188 do { \
189 static struct testInfo info = { \
190 name, \
191 }; \
192 if (virTestRun("LibXL XML-2-JSON " name, \
193 testCompareXMLToDomConfigHelper, &info) < 0) \
194 ret = -1; \
195 } while (0)
197 DO_TEST("basic-pv");
198 DO_TEST("basic-hvm");
199 # ifdef HAVE_XEN_PVH
200 DO_TEST("basic-pvh");
201 # endif
202 DO_TEST("cpu-shares-hvm");
203 DO_TEST("variable-clock-hvm");
204 DO_TEST("moredevs-hvm");
205 DO_TEST("multiple-ip");
207 # ifdef LIBXL_HAVE_BUILDINFO_NESTED_HVM
208 DO_TEST("vnuma-hvm");
209 DO_TEST("fullvirt-cpuid");
210 # else
211 DO_TEST("vnuma-hvm-legacy-nest");
212 DO_TEST("fullvirt-cpuid-legacy-nest");
213 # endif
215 # ifdef LIBXL_HAVE_BUILDINFO_GRANT_LIMITS
216 DO_TEST("max-gntframes-hvm");
217 # endif
219 unlink("libxl-driver.log");
221 return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
224 VIR_TEST_MAIN_PRELOAD(mymain, abs_builddir "/.libs/libxlmock.so")
226 #else
228 int main(void)
230 return EXIT_AM_SKIP;
233 #endif /* WITH_LIBXL && WITH_YAJL */