2017-08-01 16:17:51 +03:00
/*
* libxlxml2domconfigtest . c : test conversion of domXML to
* libxl_domain_config structure .
*
* Copyright ( C ) 2017 SUSE LINUX Products GmbH , Nuernberg , Germany .
*
* This library is free software ; you can redistribute it and / or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation ; either
* version 2.1 of the License , or ( at your option ) any later version .
*
* This library is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the GNU
* Lesser General Public License for more details .
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library . If not , see
* < http : //www.gnu.org/licenses/>.
*/
# include <config.h>
# include <unistd.h>
# include <sys/types.h>
# include <fcntl.h>
# include "testutils.h"
2018-09-14 22:33:04 +03:00
# if defined(WITH_LIBXL) && defined(WITH_YAJL)
2017-08-01 16:17:51 +03:00
# include "internal.h"
# include "libxl / libxl_conf.h"
# include "testutilsxen.h"
# define VIR_FROM_THIS VIR_FROM_LIBXL
2021-03-11 10:16:13 +03:00
static libxlDriverPrivate * driver ;
2017-08-01 16:17:51 +03:00
static int
testCompareXMLToDomConfig ( const char * xmlfile ,
const char * jsonfile )
{
int ret = - 1 ;
libxl_domain_config actualconfig ;
libxl_domain_config expectconfig ;
xentoollog_logger * log = NULL ;
2021-03-11 10:16:13 +03:00
virPortAllocatorRange * gports = NULL ;
2021-09-04 22:50:02 +03:00
g_autoptr ( virDomainDef ) vmdef = NULL ;
2021-09-04 23:36:12 +03:00
g_autofree char * actualjson = NULL ;
g_autofree char * tempjson = NULL ;
g_autofree char * expectjson = NULL ;
2019-12-03 13:49:49 +03:00
g_autoptr ( libxlDriverConfig ) cfg = libxlDriverConfigGet ( driver ) ;
2018-04-12 04:03:20 +03:00
2018-09-28 02:09:44 +03:00
libxl_domain_config_init ( & actualconfig ) ;
libxl_domain_config_init ( & expectconfig ) ;
2017-08-01 16:17:51 +03:00
if ( ! ( log = ( xentoollog_logger * ) xtl_createlogger_stdiostream ( stderr , XTL_DEBUG , 0 ) ) )
goto cleanup ;
2018-04-12 04:03:22 +03:00
/* for testing nested HVM */
cfg - > nested_hvm = true ;
2018-04-12 04:03:20 +03:00
/* replace logger with stderr one */
libxl_ctx_free ( cfg - > ctx ) ;
if ( libxl_ctx_alloc ( & cfg - > ctx , LIBXL_VERSION , 0 , log ) < 0 )
2017-08-01 16:17:51 +03:00
goto cleanup ;
2018-02-06 12:09:09 +03:00
if ( ! ( gports = virPortAllocatorRangeNew ( " vnc " , 5900 , 6000 ) ) )
2017-08-01 16:17:51 +03:00
goto cleanup ;
2019-11-27 15:29:21 +03:00
if ( ! ( vmdef = virDomainDefParseFile ( xmlfile , driver - > xmlopt ,
2017-08-01 16:17:51 +03:00
NULL , VIR_DOMAIN_XML_INACTIVE ) ) )
goto cleanup ;
2018-04-12 04:03:20 +03:00
if ( libxlBuildDomainConfig ( gports , vmdef , cfg , & actualconfig ) < 0 )
2017-08-01 16:17:51 +03:00
goto cleanup ;
2018-04-12 04:03:20 +03:00
if ( ! ( actualjson = libxl_domain_config_to_json ( cfg - > ctx , & actualconfig ) ) ) {
2017-08-01 16:17:51 +03:00
virReportError ( VIR_ERR_INTERNAL_ERROR , " %s " ,
" Failed to retrieve JSON doc for libxl_domain_config " ) ;
goto cleanup ;
}
2018-02-14 15:12:35 +03:00
if ( virTestLoadFile ( jsonfile , & tempjson ) < 0 )
goto cleanup ;
2018-04-12 04:03:20 +03:00
if ( libxl_domain_config_from_json ( cfg - > ctx , & expectconfig , tempjson ) ! = 0 ) {
2017-08-01 16:17:51 +03:00
virReportError ( VIR_ERR_INTERNAL_ERROR , " %s " ,
" Failed to create libxl_domain_config from JSON doc " ) ;
goto cleanup ;
}
2020-09-24 06:29:42 +03:00
/*
* In order to have common test files between Xen 4.9 and newer Xen versions ,
* tweak the expected libxl_domain_config object before getting a json
* representation .
*/
# ifndef LIBXL_HAVE_BUILDINFO_APIC
if ( expectconfig . c_info . type = = LIBXL_DOMAIN_TYPE_HVM ) {
libxl_defbool_unset ( & expectconfig . b_info . acpi ) ;
libxl_defbool_set ( & expectconfig . b_info . u . hvm . apic , true ) ;
libxl_defbool_set ( & expectconfig . b_info . u . hvm . acpi , true ) ;
}
# endif
2018-04-12 04:03:20 +03:00
if ( ! ( expectjson = libxl_domain_config_to_json ( cfg - > ctx , & expectconfig ) ) ) {
2017-08-01 16:17:51 +03:00
virReportError ( VIR_ERR_INTERNAL_ERROR , " %s " ,
" Failed to retrieve JSON doc for libxl_domain_config " ) ;
goto cleanup ;
}
if ( virTestCompareToString ( expectjson , actualjson ) < 0 )
goto cleanup ;
ret = 0 ;
cleanup :
2018-02-06 12:09:06 +03:00
if ( vmdef & &
vmdef - > ngraphics = = 1 & &
2018-02-06 12:09:10 +03:00
vmdef - > graphics [ 0 ] - > type = = VIR_DOMAIN_GRAPHICS_TYPE_VNC )
virPortAllocatorRelease ( vmdef - > graphics [ 0 ] - > data . vnc . port ) ;
2018-02-06 12:09:06 +03:00
virPortAllocatorRangeFree ( gports ) ;
2017-08-01 16:17:51 +03:00
libxl_domain_config_dispose ( & actualconfig ) ;
libxl_domain_config_dispose ( & expectconfig ) ;
xtl_logger_destroy ( log ) ;
return ret ;
}
struct testInfo {
const char * name ;
} ;
static int
testCompareXMLToDomConfigHelper ( const void * data )
{
int ret = - 1 ;
const struct testInfo * info = data ;
2021-09-04 23:36:12 +03:00
g_autofree char * xmlfile = NULL ;
g_autofree char * jsonfile = NULL ;
2017-08-01 16:17:51 +03:00
2019-10-22 16:26:14 +03:00
xmlfile = g_strdup_printf ( " %s/libxlxml2domconfigdata/%s.xml " , abs_srcdir , info - > name ) ;
jsonfile = g_strdup_printf ( " %s/libxlxml2domconfigdata/%s.json " , abs_srcdir , info - > name ) ;
2017-08-01 16:17:51 +03:00
ret = testCompareXMLToDomConfig ( xmlfile , jsonfile ) ;
return ret ;
}
static int
mymain ( void )
{
int ret = 0 ;
/* Set the timezone because we are mocking the time() function.
* If we don ' t do that , then localtime ( ) may return unpredictable
* results . In order to detect things that just work by a blind
* chance , we need to set an virtual timezone that no libvirt
* developer resides in . */
2019-12-18 20:16:19 +03:00
if ( g_setenv ( " TZ " , " VIR00:30 " , TRUE ) = = FALSE ) {
perror ( " g_setenv " ) ;
2017-08-01 16:17:51 +03:00
return EXIT_FAILURE ;
}
2019-12-03 13:49:49 +03:00
if ( ( driver = testXLInitDriver ( ) ) = = NULL )
2017-08-01 16:17:51 +03:00
return EXIT_FAILURE ;
2017-11-03 15:09:47 +03:00
# define DO_TEST(name) \
do { \
static struct testInfo info = { \
name , \
} ; \
if ( virTestRun ( " LibXL XML-2-JSON " name , \
testCompareXMLToDomConfigHelper , & info ) < 0 ) \
ret = - 1 ; \
2017-08-01 16:17:51 +03:00
} while ( 0 )
DO_TEST ( " basic-pv " ) ;
DO_TEST ( " basic-hvm " ) ;
2023-02-11 00:22:19 +03:00
DO_TEST ( " efi-hvm " ) ;
2020-09-01 14:27:44 +03:00
# ifdef WITH_XEN_PVH
2018-11-26 22:34:38 +03:00
DO_TEST ( " basic-pvh " ) ;
# endif
2018-02-22 21:52:56 +03:00
DO_TEST ( " cpu-shares-hvm " ) ;
2018-02-21 02:51:27 +03:00
DO_TEST ( " variable-clock-hvm " ) ;
2017-08-01 16:17:51 +03:00
DO_TEST ( " moredevs-hvm " ) ;
2017-12-07 05:27:47 +03:00
DO_TEST ( " multiple-ip " ) ;
2018-09-25 15:48:35 +03:00
# ifdef LIBXL_HAVE_BUILDINFO_NESTED_HVM
DO_TEST ( " vnuma-hvm " ) ;
2018-04-12 04:03:25 +03:00
DO_TEST ( " fullvirt-cpuid " ) ;
2018-09-26 20:31:19 +03:00
# else
DO_TEST ( " vnuma-hvm-legacy-nest " ) ;
DO_TEST ( " fullvirt-cpuid-legacy-nest " ) ;
2018-09-25 15:48:35 +03:00
# endif
2019-09-15 22:43:23 +03:00
DO_TEST ( " fullvirt-acpi-slic " ) ;
2019-03-08 01:16:09 +03:00
# ifdef LIBXL_HAVE_BUILDINFO_GRANT_LIMITS
DO_TEST ( " max-gntframes-hvm " ) ;
# endif
2017-08-01 16:17:51 +03:00
2020-04-08 02:15:04 +03:00
DO_TEST ( " max-eventchannels-hvm " ) ;
2018-05-03 16:26:08 +03:00
unlink ( " libxl-driver.log " ) ;
2019-12-03 13:49:49 +03:00
testXLFreeDriver ( driver ) ;
2017-08-01 16:17:51 +03:00
return ret = = 0 ? EXIT_SUCCESS : EXIT_FAILURE ;
}
2019-08-21 19:13:16 +03:00
VIR_TEST_MAIN_PRELOAD ( mymain , VIR_TEST_MOCK ( " xl " ) )
2017-08-01 16:17:51 +03:00
# else
int main ( void )
{
return EXIT_AM_SKIP ;
}
2018-09-14 22:33:04 +03:00
# endif /* WITH_LIBXL && WITH_YAJL */