2013-10-28 00:59:46 +04:00
# Copyright (C) 2013 Red Hat, Inc.
2013-03-18 01:06:52 +04:00
#
2018-04-04 16:35:41 +03:00
# This work is licensed under the GNU GPLv2 or later.
2018-03-20 22:00:02 +03:00
# See the COPYING file in the top-level directory.
2013-03-18 01:06:52 +04:00
import os
import unittest
2013-09-20 04:18:12 +04:00
from virtinst import StoragePool , StorageVolume
2019-06-17 04:12:39 +03:00
from virtinst import log
2013-03-18 01:06:52 +04:00
2013-07-05 16:59:58 +04:00
from tests import utils
2013-03-18 01:06:52 +04:00
2014-04-03 02:39:43 +04:00
# pylint: disable=protected-access
2013-04-12 00:32:00 +04:00
# Access to protected member, needed to unittest stuff
2020-01-27 02:11:43 +03:00
basepath = os . path . join ( os . getcwd ( ) , " tests " , " data " , " storage " )
2013-03-18 01:06:52 +04:00
2013-04-13 22:34:52 +04:00
2013-03-18 01:06:52 +04:00
def createPool ( conn , ptype , poolname = None , fmt = None , target_path = None ,
2017-12-14 20:42:08 +03:00
source_path = None , source_name = None , iqn = None ) :
2013-03-18 01:06:52 +04:00
if poolname is None :
2017-07-19 02:09:58 +03:00
poolname = StoragePool . find_free_name ( conn , " %s -pool " % ptype )
2013-03-18 01:06:52 +04:00
2013-09-20 04:18:12 +04:00
pool_inst = StoragePool ( conn )
pool_inst . name = poolname
pool_inst . type = ptype
2013-03-18 01:06:52 +04:00
2019-07-03 00:03:39 +03:00
if pool_inst . supports_hosts ( ) :
2018-02-08 01:27:56 +03:00
hostobj = pool_inst . hosts . add_new ( )
hostobj . name = " some.random.hostname "
2019-07-03 00:03:39 +03:00
if pool_inst . supports_source_path ( ) :
2013-03-18 01:06:52 +04:00
pool_inst . source_path = source_path or " /some/source/path "
2019-07-03 00:03:39 +03:00
if pool_inst . supports_target_path ( ) :
2019-07-03 03:03:45 +03:00
pool_inst . target_path = ( target_path or
pool_inst . default_target_path ( ) )
2019-07-03 00:03:39 +03:00
if fmt and pool_inst . supports_format ( ) :
2013-03-18 01:06:52 +04:00
pool_inst . format = fmt
2019-07-03 02:55:54 +03:00
if pool_inst . supports_source_name ( ) :
pool_inst . source_name = ( source_name or
pool_inst . default_source_name ( ) )
2019-07-03 00:03:39 +03:00
if iqn and pool_inst . supports_iqn ( ) :
2013-03-18 01:06:52 +04:00
pool_inst . iqn = iqn
return poolCompare ( pool_inst )
2013-04-13 22:34:52 +04:00
2018-02-23 19:42:40 +03:00
def removePool ( poolobj ) :
poolobj . destroy ( )
poolobj . undefine ( )
2013-03-18 01:06:52 +04:00
def poolCompare ( pool_inst ) :
2018-09-02 21:47:34 +03:00
pool_inst . validate ( )
2013-03-18 01:06:52 +04:00
filename = os . path . join ( basepath , pool_inst . name + " .xml " )
2018-08-31 23:52:02 +03:00
out_expect = pool_inst . get_xml ( )
2013-03-18 01:06:52 +04:00
if not os . path . exists ( filename ) :
open ( filename , " w " ) . write ( out_expect )
utils . diff_compare ( out_expect , filename )
return pool_inst . install ( build = True , meter = None , create = True )
2013-04-13 22:34:52 +04:00
2013-07-06 19:20:28 +04:00
def createVol ( conn , poolobj , volname = None , input_vol = None , clone_vol = None ) :
2013-04-13 22:34:52 +04:00
if volname is None :
2013-03-18 01:06:52 +04:00
volname = poolobj . name ( ) + " -vol "
2013-12-02 22:41:52 +04:00
# Format here depends on libvirt-1.2.0 and later
if clone_vol and conn . local_libvirt_version ( ) < 1002000 :
2019-06-17 04:12:39 +03:00
log . debug ( " skip clone compare " )
2013-12-02 22:41:52 +04:00
return
2013-03-18 01:06:52 +04:00
alloc = 5 * 1024 * 1024 * 1024
cap = 10 * 1024 * 1024 * 1024
2013-09-20 04:18:12 +04:00
vol_inst = StorageVolume ( conn )
vol_inst . pool = poolobj
vol_inst . name = volname
vol_inst . capacity = cap
vol_inst . allocation = alloc
2013-03-18 01:06:52 +04:00
2013-09-20 04:18:12 +04:00
vol_inst . permissions . mode = " 0700 "
vol_inst . permissions . owner = " 10736 "
vol_inst . permissions . group = " 10736 "
2013-03-18 01:06:52 +04:00
if input_vol :
vol_inst . input_vol = input_vol
2013-09-20 04:18:12 +04:00
vol_inst . sync_input_vol ( )
2013-03-18 01:06:52 +04:00
elif clone_vol :
2013-09-20 04:18:12 +04:00
vol_inst = StorageVolume ( conn , parsexml = clone_vol . XMLDesc ( 0 ) )
vol_inst . input_vol = clone_vol
vol_inst . sync_input_vol ( )
vol_inst . name = volname
2013-03-18 01:06:52 +04:00
2013-09-20 04:18:12 +04:00
vol_inst . validate ( )
2013-03-18 01:06:52 +04:00
filename = os . path . join ( basepath , vol_inst . name + " .xml " )
2018-08-31 23:52:02 +03:00
utils . diff_compare ( vol_inst . get_xml ( ) , filename )
2013-03-18 01:06:52 +04:00
return vol_inst . install ( meter = False )
2013-04-13 22:34:52 +04:00
2013-03-18 01:06:52 +04:00
class TestStorage ( unittest . TestCase ) :
2018-02-22 22:57:10 +03:00
@property
def conn ( self ) :
return utils . URIs . open_testdefault_cached ( )
2013-03-18 01:06:52 +04:00
def testDirPool ( self ) :
2013-08-09 05:42:44 +04:00
poolobj = createPool ( self . conn ,
2013-09-20 04:18:12 +04:00
StoragePool . TYPE_DIR , " pool-dir " )
2013-07-06 19:20:28 +04:00
invol = createVol ( self . conn , poolobj )
createVol ( self . conn , poolobj ,
volname = invol . name ( ) + " input " , input_vol = invol )
createVol ( self . conn , poolobj ,
volname = invol . name ( ) + " clone " , clone_vol = invol )
2018-02-23 19:42:40 +03:00
removePool ( poolobj )
2013-03-18 01:06:52 +04:00
def testFSPool ( self ) :
2013-08-09 05:42:44 +04:00
poolobj = createPool ( self . conn ,
2013-09-20 04:18:12 +04:00
StoragePool . TYPE_FS , " pool-fs " )
2013-07-06 19:20:28 +04:00
invol = createVol ( self . conn , poolobj )
createVol ( self . conn , poolobj ,
volname = invol . name ( ) + " input " , input_vol = invol )
createVol ( self . conn , poolobj ,
volname = invol . name ( ) + " clone " , clone_vol = invol )
2018-02-23 19:42:40 +03:00
removePool ( poolobj )
2013-03-18 01:06:52 +04:00
def testNetFSPool ( self ) :
2013-08-09 05:42:44 +04:00
poolobj = createPool ( self . conn ,
2013-09-20 04:18:12 +04:00
StoragePool . TYPE_NETFS , " pool-netfs " )
2013-07-06 19:20:28 +04:00
invol = createVol ( self . conn , poolobj )
createVol ( self . conn , poolobj ,
volname = invol . name ( ) + " input " , input_vol = invol )
createVol ( self . conn , poolobj ,
volname = invol . name ( ) + " clone " , clone_vol = invol )
2018-02-23 19:42:40 +03:00
removePool ( poolobj )
2013-03-18 01:06:52 +04:00
def testLVPool ( self ) :
2013-08-09 05:42:44 +04:00
poolobj = createPool ( self . conn ,
2013-09-20 04:18:12 +04:00
StoragePool . TYPE_LOGICAL ,
2013-07-26 06:06:28 +04:00
" pool-logical " ,
2019-06-17 03:10:37 +03:00
source_name = " pool-logical " )
2013-07-06 19:20:28 +04:00
invol = createVol ( self . conn , poolobj )
createVol ( self . conn , poolobj ,
volname = invol . name ( ) + " input " , input_vol = invol )
createVol ( self . conn ,
poolobj , volname = invol . name ( ) + " clone " , clone_vol = invol )
2018-02-23 19:42:40 +03:00
removePool ( poolobj )
2013-03-18 01:06:52 +04:00
# Test parsing source name for target path
2018-02-23 19:42:40 +03:00
poolobj = createPool ( self . conn , StoragePool . TYPE_LOGICAL ,
2013-03-18 01:06:52 +04:00
" pool-logical-target-srcname " ,
target_path = " /dev/vgfoobar " )
2018-02-23 19:42:40 +03:00
removePool ( poolobj )
2013-03-18 01:06:52 +04:00
# Test with source name
2018-02-23 19:42:40 +03:00
poolobj = createPool ( self . conn ,
2013-09-20 04:18:12 +04:00
StoragePool . TYPE_LOGICAL , " pool-logical-srcname " ,
2013-03-18 01:06:52 +04:00
source_name = " vgname " )
2018-02-23 19:42:40 +03:00
removePool ( poolobj )
2013-03-18 01:06:52 +04:00
def testDiskPool ( self ) :
2013-08-09 05:42:44 +04:00
poolobj = createPool ( self . conn ,
2013-09-20 04:18:12 +04:00
StoragePool . TYPE_DISK ,
2019-07-03 03:03:45 +03:00
" pool-disk " , fmt = " auto " ,
target_path = " /some/target/path " )
2013-07-06 19:20:28 +04:00
invol = createVol ( self . conn , poolobj )
createVol ( self . conn , poolobj ,
volname = invol . name ( ) + " input " , input_vol = invol )
createVol ( self . conn , poolobj ,
volname = invol . name ( ) + " clone " , clone_vol = invol )
2018-02-23 19:42:40 +03:00
removePool ( poolobj )
2013-03-18 01:06:52 +04:00
def testISCSIPool ( self ) :
2018-02-23 19:42:40 +03:00
poolobj = createPool ( self . conn ,
2013-09-20 04:18:12 +04:00
StoragePool . TYPE_ISCSI , " pool-iscsi " ,
2013-03-18 01:06:52 +04:00
iqn = " foo.bar.baz.iqn " )
2018-02-23 19:42:40 +03:00
removePool ( poolobj )
2013-03-18 01:06:52 +04:00
def testSCSIPool ( self ) :
2018-02-23 19:42:40 +03:00
poolobj = createPool ( self . conn , StoragePool . TYPE_SCSI , " pool-scsi " )
removePool ( poolobj )
2013-03-18 01:06:52 +04:00
def testMpathPool ( self ) :
2018-02-23 19:42:40 +03:00
poolobj = createPool ( self . conn , StoragePool . TYPE_MPATH , " pool-mpath " )
removePool ( poolobj )
2013-03-18 01:06:52 +04:00
2014-02-12 13:39:50 +04:00
def testGlusterPool ( self ) :
2018-02-23 19:42:40 +03:00
poolobj = createPool ( self . conn ,
StoragePool . TYPE_GLUSTER , " pool-gluster " )
removePool ( poolobj )
2014-02-12 13:39:50 +04:00
2019-07-03 02:55:54 +03:00
def testRBDPool ( self ) :
poolobj = createPool ( self . conn ,
StoragePool . TYPE_RBD , " pool-rbd " )
removePool ( poolobj )
2019-07-03 02:38:09 +03:00
def testMisc ( self ) :
# Misc coverage testing
vol = StorageVolume ( self . conn )
self . assertTrue ( vol . is_size_conflict ( ) [ 0 ] is False )
fullconn = utils . URIs . open_testdriver_cached ( )
glusterpool = fullconn . storagePoolLookupByName ( " gluster-pool " )
diskpool = fullconn . storagePoolLookupByName ( " disk-pool " )
glustervol = StorageVolume ( fullconn )
glustervol . pool = glusterpool
self . assertTrue ( glustervol . supports_format ( ) is True )
diskvol = StorageVolume ( fullconn )
diskvol . pool = diskpool
self . assertTrue ( diskvol . supports_format ( ) is False )
glusterpool . destroy ( )
StoragePool . ensure_pool_is_running ( glusterpool )
# Check pool collision detection
self . assertEqual (
StoragePool . find_free_name ( fullconn , " gluster-pool " ) ,
" gluster-pool-1 " )
2013-03-18 01:06:52 +04:00
def testEnumerateLogical ( self ) :
2013-09-20 04:18:12 +04:00
lst = StoragePool . pool_list_from_sources ( self . conn ,
StoragePool . TYPE_LOGICAL )
2019-07-04 00:55:01 +03:00
self . assertEqual ( lst , [ " testvg1 " , " testvg2 " ] )