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/>.
26 #include <sys/types.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"
40 # include "testutilsxen.h"
42 # define VIR_FROM_THIS VIR_FROM_LIBXL
44 static virCapsPtr caps
;
47 testCompareXMLToDomConfig(const char *xmlfile
,
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()))
67 libxl_domain_config_init(&actualconfig
);
68 libxl_domain_config_init(&expectconfig
);
70 if (!(log
= (xentoollog_logger
*)xtl_createlogger_stdiostream(stderr
, XTL_DEBUG
, 0)))
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)
82 if (!(gports
= virPortAllocatorRangeNew("vnc", 5900, 6000)))
85 if (!(xmlopt
= libxlCreateXMLConf()))
88 if (!(vmdef
= virDomainDefParseFile(xmlfile
, caps
, xmlopt
,
89 NULL
, VIR_DOMAIN_XML_INACTIVE
)))
92 if (libxlBuildDomainConfig(gports
, vmdef
, cfg
, &actualconfig
) < 0)
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");
101 if (virTestLoadFile(jsonfile
, &tempjson
) < 0)
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");
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");
115 if (virTestCompareToString(expectjson
, actualjson
) < 0)
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
);
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
);
147 testCompareXMLToDomConfigHelper(const void *data
)
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)
160 ret
= testCompareXMLToDomConfig(xmlfile
, jsonfile
);
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) {
184 if ((caps
= testXLInitCaps()) == NULL
)
187 # define DO_TEST(name) \
189 static struct testInfo info = { \
192 if (virTestRun("LibXL XML-2-JSON " name, \
193 testCompareXMLToDomConfigHelper, &info) < 0) \
198 DO_TEST("basic-hvm");
200 DO_TEST("basic-pvh");
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");
211 DO_TEST("vnuma-hvm-legacy-nest");
212 DO_TEST("fullvirt-cpuid-legacy-nest");
215 # ifdef LIBXL_HAVE_BUILDINFO_GRANT_LIMITS
216 DO_TEST("max-gntframes-hvm");
219 unlink("libxl-driver.log");
221 return ret
== 0 ? EXIT_SUCCESS
: EXIT_FAILURE
;
224 VIR_TEST_MAIN_PRELOAD(mymain
, abs_builddir
"/.libs/libxlmock.so")
233 #endif /* WITH_LIBXL && WITH_YAJL */