2013-02-18 16:43:28 +04:00
# include <config.h>
# include "internal.h"
# include "testutils.h"
# include "datatypes.h"
# include "storage/storage_backend.h"
# include "testutilsqemu.h"
2013-04-03 14:36:23 +04:00
# include "virstring.h"
2013-02-18 16:43:28 +04:00
2013-06-07 19:10:28 +04:00
# define VIR_FROM_THIS VIR_FROM_NONE
2013-02-18 16:43:28 +04:00
const char create_tool [ ] = " qemu-img " ;
2013-07-22 17:55:40 +04:00
/* createVol sets this on volume creation */
static void
testSetVolumeType ( virStorageVolDefPtr vol ,
virStoragePoolDefPtr pool )
{
2013-07-26 15:10:12 +04:00
if ( ! vol | | ! pool )
2013-07-22 17:55:40 +04:00
return ;
switch ( pool - > type ) {
case VIR_STORAGE_POOL_DIR :
case VIR_STORAGE_POOL_FS :
case VIR_STORAGE_POOL_NETFS :
vol - > type = VIR_STORAGE_VOL_FILE ;
return ;
case VIR_STORAGE_POOL_LOGICAL :
vol - > type = VIR_STORAGE_VOL_BLOCK ;
return ;
}
}
2013-02-18 16:43:28 +04:00
static int
testCompareXMLToArgvFiles ( bool shouldFail ,
const char * poolxml ,
const char * volxml ,
2013-07-22 17:55:40 +04:00
const char * inputpoolxml ,
2013-02-18 16:43:28 +04:00
const char * inputvolxml ,
const char * cmdline ,
unsigned int flags ,
int imgformat )
{
char * volXmlData = NULL ;
char * poolXmlData = NULL ;
2013-07-22 17:55:40 +04:00
char * inputpoolXmlData = NULL ;
2013-02-18 16:43:28 +04:00
char * inputvolXmlData = NULL ;
char * expectedCmdline = NULL ;
char * actualCmdline = NULL ;
int ret = - 1 ;
int len ;
virCommandPtr cmd = NULL ;
virConnectPtr conn ;
virStorageVolDefPtr vol = NULL , inputvol = NULL ;
virStoragePoolDefPtr pool = NULL ;
2013-07-22 17:55:40 +04:00
virStoragePoolDefPtr inputpool = NULL ;
2013-02-18 16:43:28 +04:00
virStoragePoolObj poolobj = { . def = NULL } ;
if ( ! ( conn = virGetConnect ( ) ) )
goto cleanup ;
if ( virtTestLoadFile ( poolxml , & poolXmlData ) < 0 )
goto cleanup ;
if ( virtTestLoadFile ( volxml , & volXmlData ) < 0 )
goto cleanup ;
if ( inputvolxml & &
virtTestLoadFile ( inputvolxml , & inputvolXmlData ) < 0 )
goto cleanup ;
if ( ! ( pool = virStoragePoolDefParseString ( poolXmlData ) ) )
goto cleanup ;
poolobj . def = pool ;
2013-07-22 17:55:40 +04:00
if ( inputpoolxml ) {
if ( virtTestLoadFile ( inputpoolxml , & inputpoolXmlData ) < 0 )
goto cleanup ;
if ( ! ( inputpool = virStoragePoolDefParseString ( inputpoolXmlData ) ) )
goto cleanup ;
}
2013-02-18 16:43:28 +04:00
if ( ! ( vol = virStorageVolDefParseString ( pool , volXmlData ) ) )
goto cleanup ;
if ( inputvolxml & &
2013-07-22 17:55:40 +04:00
! ( inputvol = virStorageVolDefParseString ( inputpool , inputvolXmlData ) ) )
2013-02-18 16:43:28 +04:00
goto cleanup ;
2013-07-22 17:55:40 +04:00
testSetVolumeType ( vol , pool ) ;
testSetVolumeType ( inputvol , inputpool ) ;
2013-02-18 16:43:28 +04:00
cmd = virStorageBackendCreateQemuImgCmd ( conn , & poolobj , vol , inputvol ,
flags , create_tool , imgformat ) ;
2013-06-05 12:49:15 +04:00
if ( ! cmd ) {
2013-02-18 16:43:28 +04:00
if ( shouldFail ) {
virResetLastError ( ) ;
ret = 0 ;
}
goto cleanup ;
}
2013-06-05 12:49:15 +04:00
if ( ! ( actualCmdline = virCommandToString ( cmd ) ) )
goto cleanup ;
2013-02-18 16:43:28 +04:00
len = virtTestLoadFile ( cmdline , & expectedCmdline ) ;
if ( len < 0 )
goto cleanup ;
if ( len & & expectedCmdline [ len - 1 ] = = ' \n ' )
expectedCmdline [ len - 1 ] = ' \0 ' ;
if ( STRNEQ_NULLABLE ( expectedCmdline , actualCmdline ) ) {
virtTestDifference ( stderr , expectedCmdline , actualCmdline ) ;
goto cleanup ;
}
ret = 0 ;
2014-03-25 10:53:44 +04:00
cleanup :
2013-02-18 16:43:28 +04:00
virStoragePoolDefFree ( pool ) ;
2013-07-22 17:55:40 +04:00
virStoragePoolDefFree ( inputpool ) ;
2013-02-18 16:43:28 +04:00
virStorageVolDefFree ( vol ) ;
virStorageVolDefFree ( inputvol ) ;
virCommandFree ( cmd ) ;
VIR_FREE ( actualCmdline ) ;
VIR_FREE ( expectedCmdline ) ;
2013-07-22 17:55:40 +04:00
VIR_FREE ( inputpoolXmlData ) ;
2013-02-27 13:11:52 +04:00
VIR_FREE ( poolXmlData ) ;
VIR_FREE ( volXmlData ) ;
VIR_FREE ( inputvolXmlData ) ;
virObjectUnref ( conn ) ;
2013-02-18 16:43:28 +04:00
return ret ;
}
struct testInfo {
bool shouldFail ;
const char * pool ;
const char * vol ;
2013-07-22 17:55:40 +04:00
const char * inputpool ;
2013-02-18 16:43:28 +04:00
const char * inputvol ;
const char * cmdline ;
unsigned int flags ;
int imgformat ;
} ;
static int
testCompareXMLToArgvHelper ( const void * data )
{
int result = - 1 ;
const struct testInfo * info = data ;
char * poolxml = NULL ;
2013-07-22 17:55:40 +04:00
char * inputpoolxml = NULL ;
2013-02-18 16:43:28 +04:00
char * volxml = NULL ;
char * inputvolxml = NULL ;
char * cmdline = NULL ;
if ( info - > inputvol & &
2013-07-22 17:44:06 +04:00
virAsprintf ( & inputvolxml , " %s/storagevolxml2xmlin/%s.xml " ,
2013-02-18 16:43:28 +04:00
abs_srcdir , info - > inputvol ) < 0 )
goto cleanup ;
2013-07-22 17:55:40 +04:00
if ( info - > inputpool & &
virAsprintf ( & inputpoolxml , " %s/storagepoolxml2xmlin/%s.xml " ,
abs_srcdir , info - > inputpool ) < 0 )
goto cleanup ;
2013-07-22 16:56:26 +04:00
if ( virAsprintf ( & poolxml , " %s/storagepoolxml2xmlin/%s.xml " ,
2013-02-18 16:43:28 +04:00
abs_srcdir , info - > pool ) < 0 | |
2013-07-22 17:44:06 +04:00
virAsprintf ( & volxml , " %s/storagevolxml2xmlin/%s.xml " ,
2013-02-18 16:43:28 +04:00
abs_srcdir , info - > vol ) < 0 ) {
goto cleanup ;
}
if ( virAsprintf ( & cmdline , " %s/storagevolxml2argvdata/%s.argv " ,
abs_srcdir , info - > cmdline ) < 0 & & ! info - > shouldFail )
goto cleanup ;
result = testCompareXMLToArgvFiles ( info - > shouldFail , poolxml , volxml ,
2013-07-22 17:55:40 +04:00
inputpoolxml , inputvolxml ,
cmdline , info - > flags ,
2013-02-18 16:43:28 +04:00
info - > imgformat ) ;
2014-03-25 10:53:44 +04:00
cleanup :
2013-02-18 16:43:28 +04:00
VIR_FREE ( poolxml ) ;
VIR_FREE ( volxml ) ;
VIR_FREE ( inputvolxml ) ;
2013-07-22 17:55:40 +04:00
VIR_FREE ( inputpoolxml ) ;
2013-02-18 16:43:28 +04:00
VIR_FREE ( cmdline ) ;
return result ;
}
enum {
FMT_NONE = 0 ,
FMT_FLAG ,
FMT_OPTIONS ,
2013-08-20 19:37:08 +04:00
FMT_COMPAT ,
2013-02-18 16:43:28 +04:00
} ;
static int
mymain ( void )
{
int ret = 0 ;
unsigned int flags = VIR_STORAGE_VOL_CREATE_PREALLOC_METADATA ;
2013-07-22 17:55:40 +04:00
# define DO_TEST_FULL(shouldFail, pool, vol, inputpool, inputvol, cmdline, \
flags , imgformat ) \
2013-07-22 11:11:50 +04:00
do { \
2013-07-22 17:55:40 +04:00
struct testInfo info = { shouldFail , pool , vol , inputpool , inputvol , \
cmdline , flags , imgformat } ; \
2013-07-22 11:11:50 +04:00
if ( virtTestRun ( " Storage Vol XML-2-argv " cmdline , \
2013-09-20 22:13:35 +04:00
testCompareXMLToArgvHelper , & info ) < 0 ) \
2013-07-22 11:11:50 +04:00
ret = - 1 ; \
} \
2013-02-18 16:43:28 +04:00
while ( 0 ) ;
2013-07-22 11:11:50 +04:00
# define DO_TEST(pool, ...) \
DO_TEST_FULL ( false , pool , __VA_ARGS__ )
# define DO_TEST_FAIL(pool, ...) \
DO_TEST_FULL ( true , pool , __VA_ARGS__ )
DO_TEST ( " pool-dir " , " vol-qcow2 " ,
2013-07-22 17:55:40 +04:00
NULL , NULL ,
2013-07-22 11:11:50 +04:00
" qcow2 " , 0 , FMT_OPTIONS ) ;
DO_TEST_FAIL ( " pool-dir " , " vol-qcow2 " ,
2013-07-22 17:55:40 +04:00
NULL , NULL ,
2013-07-22 11:11:50 +04:00
" qcow2-prealloc " , flags , FMT_OPTIONS ) ;
DO_TEST ( " pool-dir " , " vol-qcow2-nobacking " ,
2013-07-22 17:55:40 +04:00
NULL , NULL ,
2013-02-18 16:43:28 +04:00
" qcow2-nobacking-prealloc " , flags , FMT_OPTIONS ) ;
2013-07-22 11:11:50 +04:00
DO_TEST ( " pool-dir " , " vol-qcow2-nobacking " ,
2013-07-22 17:55:40 +04:00
" pool-dir " , " vol-file " ,
2013-02-18 16:43:28 +04:00
" qcow2-nobacking-convert-prealloc " , flags , FMT_OPTIONS ) ;
2014-08-01 17:24:20 +04:00
DO_TEST_FAIL ( " pool-dir " , " vol-qcow2 " ,
" pool-dir " , " vol-file " ,
" qcow2-convert-nobacking " , 0 , FMT_OPTIONS ) ;
2013-07-22 11:11:50 +04:00
DO_TEST_FAIL ( " pool-dir " , " vol-qcow2 " ,
2013-07-22 17:55:40 +04:00
" pool-dir " , " vol-file " ,
2013-07-22 11:11:50 +04:00
" qcow2-convert-prealloc " , flags , FMT_OPTIONS ) ;
DO_TEST ( " pool-dir " , " vol-qcow2 " ,
2013-07-22 17:55:40 +04:00
NULL , NULL ,
2013-07-22 11:11:50 +04:00
" qcow2-flag " , 0 , FMT_FLAG ) ;
DO_TEST ( " pool-dir " , " vol-qcow2-nobacking " ,
2013-07-22 17:55:40 +04:00
NULL , NULL ,
2013-02-18 16:43:28 +04:00
" qcow2-nobacking-flag " , 0 , FMT_FLAG ) ;
2013-07-22 11:11:50 +04:00
DO_TEST ( " pool-dir " , " vol-qcow2-nobacking " ,
2013-07-22 17:55:40 +04:00
" pool-dir " , " vol-file " ,
2013-02-18 16:43:28 +04:00
" qcow2-nobacking-convert-flag " , 0 , FMT_FLAG ) ;
2013-07-22 11:11:50 +04:00
DO_TEST ( " pool-dir " , " vol-qcow2 " ,
2013-07-22 17:55:40 +04:00
NULL , NULL ,
2013-07-22 11:11:50 +04:00
" qcow2-none " , 0 , FMT_NONE ) ;
DO_TEST ( " pool-dir " , " vol-qcow2-nobacking " ,
2013-07-22 17:55:40 +04:00
NULL , NULL ,
2013-02-18 16:43:28 +04:00
" qcow2-nobacking-none " , 0 , FMT_NONE ) ;
2013-07-22 11:11:50 +04:00
DO_TEST ( " pool-dir " , " vol-qcow2-nobacking " ,
2013-07-22 17:55:40 +04:00
" pool-dir " , " vol-file " ,
2013-02-18 16:43:28 +04:00
" qcow2-nobacking-convert-none " , 0 , FMT_NONE ) ;
2013-07-22 11:11:50 +04:00
DO_TEST ( " pool-dir " , " vol-qcow2-lazy " ,
2013-07-22 17:55:40 +04:00
NULL , NULL ,
2013-07-22 11:11:50 +04:00
" qcow2-lazy " , 0 , FMT_OPTIONS ) ;
DO_TEST ( " pool-dir " , " vol-qcow2-1.1 " ,
2013-07-22 17:55:40 +04:00
NULL , NULL ,
2013-07-22 11:11:50 +04:00
" qcow2-1.1 " , 0 , FMT_OPTIONS ) ;
DO_TEST_FAIL ( " pool-dir " , " vol-qcow2-0.10-lazy " ,
2013-07-22 17:55:40 +04:00
NULL , NULL ,
2013-07-22 11:11:50 +04:00
" qcow2-0.10-lazy " , 0 , FMT_OPTIONS ) ;
2013-07-22 17:55:40 +04:00
DO_TEST ( " pool-dir " , " vol-qcow2-nobacking " ,
" pool-logical " , " vol-logical " ,
" qcow2-from-logical " , 0 , FMT_OPTIONS ) ;
DO_TEST ( " pool-logical " , " vol-logical " ,
" pool-dir " , " vol-qcow2-nobacking " ,
" logical-from-qcow2 " , 0 , FMT_OPTIONS ) ;
2013-02-18 16:43:28 +04:00
2013-08-20 19:37:08 +04:00
DO_TEST ( " pool-dir " , " vol-qcow2 " ,
NULL , NULL ,
" qcow2-compat " , 0 , FMT_COMPAT ) ;
DO_TEST ( " pool-dir " , " vol-qcow2-nobacking " ,
NULL , NULL ,
" qcow2-nobacking-prealloc-compat " , flags , FMT_COMPAT ) ;
DO_TEST ( " pool-dir " , " vol-qcow2-nobacking " ,
" pool-dir " , " vol-file " ,
" qcow2-nobacking-convert-prealloc-compat " , flags , FMT_COMPAT ) ;
DO_TEST ( " pool-dir " , " vol-qcow2-lazy " ,
NULL , NULL ,
" qcow2-lazy " , 0 , FMT_COMPAT ) ;
DO_TEST ( " pool-dir " , " vol-qcow2-1.1 " ,
NULL , NULL ,
" qcow2-1.1 " , 0 , FMT_COMPAT ) ;
DO_TEST_FAIL ( " pool-dir " , " vol-qcow2-0.10-lazy " ,
NULL , NULL ,
" qcow2-0.10-lazy " , 0 , FMT_COMPAT ) ;
DO_TEST ( " pool-dir " , " vol-qcow2-nobacking " ,
" pool-logical " , " vol-logical " ,
" qcow2-from-logical-compat " , 0 , FMT_COMPAT ) ;
DO_TEST ( " pool-logical " , " vol-logical " ,
" pool-dir " , " vol-qcow2-nobacking " ,
" logical-from-qcow2 " , 0 , FMT_COMPAT ) ;
2014-07-15 12:49:47 +04:00
DO_TEST ( " pool-dir " , " vol-qcow2-nocow " ,
NULL , NULL ,
" qcow2-nocow " , 0 , FMT_OPTIONS ) ;
DO_TEST ( " pool-dir " , " vol-qcow2-nocow " ,
NULL , NULL ,
" qcow2-nocow-compat " , 0 , FMT_COMPAT ) ;
2013-08-20 19:37:08 +04:00
2014-03-17 13:38:38 +04:00
return ret = = 0 ? EXIT_SUCCESS : EXIT_FAILURE ;
2013-02-18 16:43:28 +04:00
}
VIRT_TEST_MAIN ( mymain )