2014-03-17 19:19:46 +04:00
/*
* Copyright ( C ) 2014 Red Hat , Inc .
*
* 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 "testutils.h"
# include "qemu/qemu_capabilities.h"
# define VIR_FROM_THIS VIR_FROM_NONE
typedef struct _testQemuData testQemuData ;
typedef testQemuData * testQemuDataPtr ;
struct _testQemuData {
const char * base ;
2018-09-13 19:06:20 +03:00
const char * archName ;
2014-03-17 19:19:46 +04:00
} ;
static virQEMUCapsPtr
testQemuGetCaps ( char * caps )
{
virQEMUCapsPtr qemuCaps = NULL ;
xmlDocPtr xml ;
xmlXPathContextPtr ctxt = NULL ;
ssize_t i , n ;
xmlNodePtr * nodes = NULL ;
if ( ! ( xml = virXMLParseStringCtxt ( caps , " (test caps) " , & ctxt ) ) )
goto error ;
if ( ( n = virXPathNodeSet ( " /qemuCaps/flag " , ctxt , & nodes ) ) < 0 ) {
fprintf ( stderr , " failed to parse qemu capabilities flags " ) ;
goto error ;
}
if ( ! ( qemuCaps = virQEMUCapsNew ( ) ) )
goto error ;
for ( i = 0 ; i < n ; i + + ) {
char * str = virXMLPropString ( nodes [ i ] , " name " ) ;
if ( str ) {
int flag = virQEMUCapsTypeFromString ( str ) ;
if ( flag < 0 ) {
fprintf ( stderr , " Unknown qemu capabilities flag %s " , str ) ;
VIR_FREE ( str ) ;
goto error ;
}
VIR_FREE ( str ) ;
virQEMUCapsSet ( qemuCaps , flag ) ;
}
}
VIR_FREE ( nodes ) ;
xmlFreeDoc ( xml ) ;
xmlXPathFreeContext ( ctxt ) ;
return qemuCaps ;
error :
VIR_FREE ( nodes ) ;
virObjectUnref ( qemuCaps ) ;
xmlFreeDoc ( xml ) ;
xmlXPathFreeContext ( ctxt ) ;
return NULL ;
}
static virCapsPtr
testGetCaps ( char * capsData , const testQemuData * data )
{
virQEMUCapsPtr qemuCaps = NULL ;
virCapsPtr caps = NULL ;
2018-09-13 19:06:20 +03:00
virArch arch = virArchFromString ( data - > archName ) ;
char * binary = NULL ;
if ( virAsprintf ( & binary , " /usr/bin/qemu-system-%s " , data - > archName ) < 0 )
goto error ;
2014-03-17 19:19:46 +04:00
if ( ( qemuCaps = testQemuGetCaps ( capsData ) ) = = NULL ) {
fprintf ( stderr , " failed to parse qemu capabilities flags " ) ;
goto error ;
}
2018-09-13 19:06:20 +03:00
if ( ( caps = virCapabilitiesNew ( arch , false , false ) ) = = NULL ) {
2014-03-17 19:19:46 +04:00
fprintf ( stderr , " failed to create the fake capabilities " ) ;
goto error ;
}
if ( virQEMUCapsInitGuestFromBinary ( caps ,
2018-09-13 19:06:20 +03:00
binary ,
2014-03-17 19:19:46 +04:00
qemuCaps ,
2018-09-13 19:06:20 +03:00
arch ) < 0 ) {
2014-03-17 19:19:46 +04:00
fprintf ( stderr , " failed to create the capabilities from qemu " ) ;
goto error ;
}
2014-03-27 02:37:54 +04:00
virObjectUnref ( qemuCaps ) ;
2014-03-17 19:19:46 +04:00
return caps ;
error :
virObjectUnref ( qemuCaps ) ;
2015-09-08 14:12:52 +03:00
virObjectUnref ( caps ) ;
2018-09-13 19:06:20 +03:00
VIR_FREE ( binary ) ;
2014-03-17 19:19:46 +04:00
return NULL ;
}
static int
testQemuCapsXML ( const void * opaque )
{
int ret = - 1 ;
const testQemuData * data = opaque ;
char * capsFile = NULL , * xmlFile = NULL ;
2015-04-23 18:14:26 +03:00
char * capsData = NULL ;
2014-03-17 19:19:46 +04:00
char * capsXml = NULL ;
virCapsPtr capsProvided = NULL ;
2019-02-01 15:16:18 +03:00
if ( virAsprintf ( & xmlFile , " %s/qemucaps2xmloutdata/caps.%s.xml " ,
abs_srcdir , data - > archName ) < 0 )
2014-03-17 19:19:46 +04:00
goto cleanup ;
2018-09-13 19:06:20 +03:00
if ( virAsprintf ( & capsFile , " %s/qemucapabilitiesdata/%s.%s.xml " ,
abs_srcdir , data - > base , data - > archName ) < 0 )
2014-03-17 19:19:46 +04:00
goto cleanup ;
2016-05-26 18:01:52 +03:00
if ( virTestLoadFile ( capsFile , & capsData ) < 0 )
2014-03-17 19:19:46 +04:00
goto cleanup ;
if ( ! ( capsProvided = testGetCaps ( capsData , data ) ) )
goto cleanup ;
capsXml = virCapabilitiesFormatXML ( capsProvided ) ;
if ( ! capsXml )
goto cleanup ;
2016-05-26 18:01:53 +03:00
if ( virTestCompareToFile ( capsXml , xmlFile ) < 0 )
2015-04-23 18:14:26 +03:00
goto cleanup ;
2014-03-17 19:19:46 +04:00
2015-04-23 18:14:26 +03:00
ret = 0 ;
2014-03-17 19:19:46 +04:00
cleanup :
VIR_FREE ( xmlFile ) ;
VIR_FREE ( capsFile ) ;
VIR_FREE ( capsXml ) ;
VIR_FREE ( capsData ) ;
virObjectUnref ( capsProvided ) ;
return ret ;
}
static int
mymain ( void )
{
int ret = 0 ;
testQemuData data ;
2018-08-13 14:40:18 +03:00
# if !WITH_YAJL
2018-05-09 17:42:43 +03:00
fputs ( " libvirt not compiled with JSON support, skipping this test \n " , stderr ) ;
2014-03-17 19:19:46 +04:00
return EXIT_AM_SKIP ;
# endif
if ( virThreadInitialize ( ) < 0 )
return EXIT_FAILURE ;
virEventRegisterDefaultImpl ( ) ;
2018-09-13 19:06:20 +03:00
# define DO_TEST(arch, name) \
data . archName = arch ; \
2017-11-03 15:09:47 +03:00
data . base = name ; \
2018-09-13 19:06:20 +03:00
if ( virTestRun ( name " ( " arch " ) " , testQemuCapsXML , & data ) < 0 ) \
2014-03-17 19:19:46 +04:00
ret = - 1
2018-09-13 19:28:59 +03:00
/* Keep this in sync with qemucapabilitiestest */
DO_TEST ( " x86_64 " , " caps_1.5.3 " ) ;
2018-09-13 19:06:20 +03:00
DO_TEST ( " x86_64 " , " caps_1.6.0 " ) ;
2018-09-13 19:28:59 +03:00
DO_TEST ( " x86_64 " , " caps_1.7.0 " ) ;
DO_TEST ( " x86_64 " , " caps_2.1.1 " ) ;
DO_TEST ( " x86_64 " , " caps_2.4.0 " ) ;
DO_TEST ( " x86_64 " , " caps_2.5.0 " ) ;
DO_TEST ( " x86_64 " , " caps_2.6.0 " ) ;
DO_TEST ( " x86_64 " , " caps_2.7.0 " ) ;
DO_TEST ( " x86_64 " , " caps_2.8.0 " ) ;
DO_TEST ( " x86_64 " , " caps_2.9.0 " ) ;
DO_TEST ( " x86_64 " , " caps_2.10.0 " ) ;
DO_TEST ( " x86_64 " , " caps_2.11.0 " ) ;
DO_TEST ( " x86_64 " , " caps_2.12.0 " ) ;
DO_TEST ( " x86_64 " , " caps_3.0.0 " ) ;
2018-11-29 16:29:51 +03:00
DO_TEST ( " x86_64 " , " caps_3.1.0 " ) ;
2019-01-31 16:02:47 +03:00
DO_TEST ( " x86_64 " , " caps_4.0.0 " ) ;
2018-09-13 19:28:59 +03:00
DO_TEST ( " aarch64 " , " caps_2.6.0 " ) ;
DO_TEST ( " aarch64 " , " caps_2.10.0 " ) ;
DO_TEST ( " aarch64 " , " caps_2.12.0 " ) ;
DO_TEST ( " ppc64 " , " caps_2.6.0 " ) ;
DO_TEST ( " ppc64 " , " caps_2.9.0 " ) ;
DO_TEST ( " ppc64 " , " caps_2.10.0 " ) ;
DO_TEST ( " ppc64 " , " caps_2.12.0 " ) ;
DO_TEST ( " ppc64 " , " caps_3.0.0 " ) ;
2018-11-20 16:00:09 +03:00
DO_TEST ( " ppc64 " , " caps_3.1.0 " ) ;
2018-09-13 19:28:59 +03:00
DO_TEST ( " s390x " , " caps_2.7.0 " ) ;
DO_TEST ( " s390x " , " caps_2.8.0 " ) ;
DO_TEST ( " s390x " , " caps_2.9.0 " ) ;
DO_TEST ( " s390x " , " caps_2.10.0 " ) ;
DO_TEST ( " s390x " , " caps_2.11.0 " ) ;
DO_TEST ( " s390x " , " caps_2.12.0 " ) ;
2018-11-29 16:29:51 +03:00
DO_TEST ( " s390x " , " caps_3.0.0 " ) ;
2018-09-13 19:28:59 +03:00
DO_TEST ( " riscv32 " , " caps_3.0.0 " ) ;
2019-01-31 16:07:34 +03:00
DO_TEST ( " riscv32 " , " caps_4.0.0 " ) ;
2018-09-13 19:28:59 +03:00
DO_TEST ( " riscv64 " , " caps_3.0.0 " ) ;
2019-01-31 16:07:34 +03:00
DO_TEST ( " riscv64 " , " caps_4.0.0 " ) ;
2014-03-17 19:19:46 +04:00
return ( ret = = 0 ) ? EXIT_SUCCESS : EXIT_FAILURE ;
}
2017-03-29 17:45:42 +03:00
VIR_TEST_MAIN_PRELOAD ( mymain , abs_builddir " /.libs/qemucaps2xmlmock.so " )