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 ,
2015-02-17 18:57:02 +03:00
int imgformat ,
unsigned long parse_flags )
2013-02-18 16:43:28 +04:00
{
char * actualCmdline = NULL ;
int ret = - 1 ;
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 ;
2015-04-23 18:10:15 +03:00
if ( ! ( pool = virStoragePoolDefParseFile ( poolxml ) ) )
2013-02-18 16:43:28 +04:00
goto cleanup ;
poolobj . def = pool ;
2013-07-22 17:55:40 +04:00
if ( inputpoolxml ) {
2015-04-23 18:10:15 +03:00
if ( ! ( inputpool = virStoragePoolDefParseFile ( inputpoolxml ) ) )
2013-07-22 17:55:40 +04:00
goto cleanup ;
}
2015-02-17 18:54:53 +03:00
if ( inputvolxml )
parse_flags | = VIR_VOL_XML_PARSE_NO_CAPACITY ;
2015-04-23 18:10:15 +03:00
if ( ! ( vol = virStorageVolDefParseFile ( pool , volxml , parse_flags ) ) )
2013-02-18 16:43:28 +04:00
goto cleanup ;
if ( inputvolxml & &
2015-04-23 18:10:15 +03:00
! ( inputvol = virStorageVolDefParseFile ( inputpool , inputvolxml , 0 ) ) )
2013-02-18 16:43:28 +04:00
goto cleanup ;
2013-07-22 17:55:40 +04:00
testSetVolumeType ( vol , pool ) ;
testSetVolumeType ( inputvol , inputpool ) ;
2014-07-31 20:08:33 +04:00
cmd = virStorageBackendCreateQemuImgCmdFromVol ( conn , & poolobj , vol ,
inputvol , flags ,
2016-06-02 18:33:47 +03:00
create_tool , imgformat ,
NULL ) ;
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 ;
2016-05-26 18:01:53 +03:00
if ( virTestCompareToFile ( actualCmdline , cmdline ) < 0 )
2013-02-18 16:43:28 +04:00
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 ) ;
2013-02-27 13:11:52 +04:00
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 ;
2015-02-17 18:57:02 +03:00
unsigned long parseflags ;
2013-02-18 16:43:28 +04:00
} ;
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 ,
2015-02-17 18:57:02 +03:00
info - > imgformat , info - > parseflags ) ;
2013-02-18 16:43:28 +04:00
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 {
2016-05-31 17:53:18 +03:00
FMT_OPTIONS = 0 ,
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 ;
2015-02-17 18:57:02 +03:00
# define DO_TEST_FULL(shouldFail, parseflags, 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 , \
2015-02-17 18:57:02 +03:00
cmdline , flags , imgformat , parseflags } ; \
2016-05-26 18:01:50 +03:00
if ( virTestRun ( " Storage Vol XML-2-argv " cmdline , \
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, ...) \
2015-02-17 18:57:02 +03:00
DO_TEST_FULL ( false , 0 , pool , __VA_ARGS__ )
2013-07-22 11:11:50 +04:00
# define DO_TEST_FAIL(pool, ...) \
2015-02-17 18:57:02 +03:00
DO_TEST_FULL ( true , 0 , pool , __VA_ARGS__ )
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 " , 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-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 ) ;
2015-02-17 18:54:53 +03:00
DO_TEST ( " pool-dir " , " vol-qcow2-nocapacity " ,
" pool-dir " , " vol-file " ,
" qcow2-nocapacity-convert-prealloc " , flags , FMT_OPTIONS ) ;
2015-06-30 23:19:04 +03:00
DO_TEST ( " pool-dir " , " vol-qcow2-zerocapacity " ,
NULL , NULL ,
" qcow2-zerocapacity " , 0 , FMT_COMPAT ) ;
2015-02-17 18:57:02 +03:00
DO_TEST_FULL ( false , VIR_VOL_XML_PARSE_OPT_CAPACITY ,
" pool-dir " , " vol-qcow2-nocapacity-backing " , NULL , NULL ,
" qcow2-nocapacity " , 0 , FMT_OPTIONS ) ;
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 )