2012-12-14 19:08:25 +04:00
/*
* sysinfotest . c : Testcase ( s ) for virSysinfoRead
*
2013-11-20 02:50:56 +04:00
* Copyright ( C ) 2013 Red Hat , Inc .
2012-12-14 19:08:25 +04:00
* Copyright IBM Corp . 2012
*
* 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 "internal.h"
2012-12-04 16:04:07 +04:00
# include "virbuffer.h"
2012-12-13 19:31:53 +04:00
# include "virsysinfo.h"
2012-12-14 19:08:25 +04:00
# include "testutils.h"
# include "virfile.h"
2013-04-03 14:36:23 +04:00
# include "virstring.h"
2012-12-14 19:08:25 +04:00
2018-12-13 17:53:50 +03:00
# define LIBVIRT_VIRSYSINFOPRIV_H_ALLOW
2017-03-06 17:25:07 +03:00
# include "virsysinfopriv.h"
2020-06-08 13:51:07 +03:00
# define LIBVIRT_VIRCOMMANDPRIV_H_ALLOW
# include "vircommandpriv.h"
2013-06-07 19:10:28 +04:00
# define VIR_FROM_THIS VIR_FROM_NONE
2012-12-14 19:08:25 +04:00
struct testSysinfoData {
2020-06-08 13:32:52 +03:00
const char * name ; /* test name, also base name for result files */
2017-03-06 17:56:46 +03:00
virSysinfoDefPtr ( * func ) ( void ) ; /* sysinfo gathering function */
2012-12-14 19:08:25 +04:00
} ;
2020-06-08 13:51:07 +03:00
static void
testDMIDecodeDryRun ( const char * const * args G_GNUC_UNUSED ,
const char * const * env G_GNUC_UNUSED ,
const char * input G_GNUC_UNUSED ,
char * * output ,
char * * error ,
int * status ,
void * opaque )
{
const char * sysinfo = opaque ;
2020-06-02 11:15:38 +03:00
if ( STREQ_NULLABLE ( args [ 1 ] , " --dump " ) & &
STREQ_NULLABLE ( args [ 2 ] , " --oem-string " ) ) {
if ( ! args [ 3 ] ) {
* error = g_strdup ( " dmidecode: option '--oem-string' requires an argument " ) ;
* status = EXIT_FAILURE ;
return ;
}
if ( STREQ ( args [ 3 ] , " 3 " ) ) {
* output = g_strdup ( " Ha ha ha try parsing \\ n \n "
" String 3: this correctly \n "
" String 4:then " ) ;
} else {
* error = g_strdup_printf ( " No OEM string number %s " , args [ 3 ] ) ;
* status = EXIT_FAILURE ;
return ;
}
} else {
if ( virFileReadAll ( sysinfo , 10 * 1024 * 1024 , output ) < 0 ) {
* error = g_strdup ( virGetLastErrorMessage ( ) ) ;
* status = EXIT_FAILURE ;
return ;
}
2020-06-08 13:51:07 +03:00
}
* error = g_strdup ( " " ) ;
* status = 0 ;
}
2012-12-14 19:08:25 +04:00
static int
testSysinfo ( const void * data )
{
2020-06-08 13:32:52 +03:00
const struct testSysinfoData * testdata = data ;
2012-12-14 19:08:25 +04:00
const char * sysfsActualData ;
2020-06-08 12:56:45 +03:00
g_auto ( virSysinfoDefPtr ) ret = NULL ;
g_auto ( virBuffer ) buf = VIR_BUFFER_INITIALIZER ;
2020-06-08 13:32:52 +03:00
g_autofree char * sysinfo = NULL ;
g_autofree char * cpuinfo = NULL ;
g_autofree char * expected = NULL ;
sysinfo = g_strdup_printf ( " %s/sysinfodata/%ssysinfo.data " , abs_srcdir , testdata - > name ) ;
cpuinfo = g_strdup_printf ( " %s/sysinfodata/%scpuinfo.data " , abs_srcdir , testdata - > name ) ;
expected = g_strdup_printf ( " %s/sysinfodata/%ssysinfo.expect " , abs_srcdir , testdata - > name ) ;
2020-06-08 13:51:07 +03:00
virCommandSetDryRun ( NULL , testDMIDecodeDryRun , sysinfo ) ;
2020-06-08 13:53:04 +03:00
virSysinfoSetup ( sysinfo , cpuinfo ) ;
2012-12-14 19:08:25 +04:00
2020-06-08 13:51:07 +03:00
ret = testdata - > func ( ) ;
virCommandSetDryRun ( NULL , NULL , NULL ) ;
2012-12-14 19:08:25 +04:00
2020-06-08 13:51:07 +03:00
if ( ! ret )
2020-06-08 12:56:45 +03:00
return - 1 ;
2012-12-14 19:08:25 +04:00
2013-11-20 02:50:56 +04:00
if ( virSysinfoFormat ( & buf , ret ) < 0 )
2020-06-08 12:56:45 +03:00
return - 1 ;
2012-12-14 19:08:25 +04:00
if ( ! ( sysfsActualData = virBufferCurrentContent ( & buf ) ) )
2020-06-08 12:56:45 +03:00
return - 1 ;
2012-12-14 19:08:25 +04:00
2020-06-08 13:32:52 +03:00
return virTestCompareToFile ( sysfsActualData , expected ) ;
2012-12-14 19:08:25 +04:00
}
2020-06-08 13:51:07 +03:00
# define TEST(name, func) \
2020-06-08 13:32:52 +03:00
do { \
2020-06-08 13:51:07 +03:00
struct testSysinfoData data = { name , func } ; \
2020-06-08 13:32:52 +03:00
if ( virTestRun ( name " sysinfo " , testSysinfo , & data ) < 0 ) \
ret = EXIT_FAILURE ; \
} while ( 0 )
2012-12-14 19:08:25 +04:00
2013-04-03 21:58:50 +04:00
static int
2017-03-06 17:56:46 +03:00
mymain ( void )
2013-04-03 21:58:50 +04:00
{
2015-05-12 19:21:18 +03:00
int ret = EXIT_SUCCESS ;
2017-03-06 17:56:46 +03:00
TEST ( " s390 " , virSysinfoReadS390 ) ;
2018-01-12 14:38:02 +03:00
TEST ( " s390-freq " , virSysinfoReadS390 ) ;
2017-03-06 17:56:46 +03:00
TEST ( " ppc " , virSysinfoReadPPC ) ;
2020-06-08 13:51:07 +03:00
TEST ( " x86 " , virSysinfoReadDMI ) ;
2017-03-06 17:56:46 +03:00
TEST ( " arm " , virSysinfoReadARM ) ;
TEST ( " arm-rpi2 " , virSysinfoReadARM ) ;
TEST ( " aarch64 " , virSysinfoReadARM ) ;
2017-03-06 18:20:25 +03:00
TEST ( " aarch64-moonshot " , virSysinfoReadARM ) ;
2020-06-08 13:51:07 +03:00
TEST ( " aarch64-gigabyte " , virSysinfoReadARM ) ;
2015-05-12 19:21:18 +03:00
return ret ;
2013-04-03 21:58:50 +04:00
}
2017-03-06 17:56:46 +03:00
# undef TEST
# undef TEST_FULL
2013-10-08 17:49:09 +04:00
2017-03-29 17:45:42 +03:00
VIR_TEST_MAIN ( mymain )