2013-03-18 01:06:52 +04:00
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301 USA.
import unittest
import virtinst
import virtinst . cli
import virtinst . ImageParser
import os
2013-04-11 03:48:07 +04:00
from tests import utils
2013-03-18 01:06:52 +04:00
qemuuri = " __virtinst_test__test:///default,caps= %s /tests/capabilities-xml/capabilities-kvm.xml,qemu,predictable " % os . getcwd ( )
2013-04-12 00:32:00 +04:00
# pylint: disable=W0212
# Access to protected member, needed to unittest stuff
2013-03-18 01:06:52 +04:00
class TestImageParser ( unittest . TestCase ) :
basedir = " tests/image-xml/ "
2013-07-05 16:59:58 +04:00
conn = utils . open_testdefault ( )
qemuconn = virtinst . cli . getConnection ( qemuuri )
2013-03-18 01:06:52 +04:00
def testImageParsing ( self ) :
f = open ( os . path . join ( self . basedir , " image.xml " ) , " r " )
xml = f . read ( )
f . close ( )
img = virtinst . ImageParser . parse ( xml , " . " )
self . assertEqual ( " test-image " , img . name )
self . assertTrue ( img . domain )
self . assertEqual ( 5 , len ( img . storage ) )
self . assertEqual ( 2 , len ( img . domain . boots ) )
self . assertEqual ( 1 , img . domain . interface )
boot = img . domain . boots [ 0 ]
self . assertEqual ( " xvdb " , boot . drives [ 1 ] . target )
def testMultipleNics ( self ) :
f = open ( os . path . join ( self . basedir , " image2nics.xml " ) , " r " )
xml = f . read ( )
f . close ( )
img = virtinst . ImageParser . parse ( xml , " . " )
self . assertEqual ( 2 , img . domain . interface )
def testBadArch ( self ) :
""" Makes sure we sanitize i386->i686 """
image = virtinst . ImageParser . parse_file ( self . basedir +
" image-bad-arch.xml " )
2013-07-06 04:14:57 +04:00
virtinst . ImageInstaller ( self . conn , image , 0 )
2013-03-18 01:06:52 +04:00
self . assertTrue ( True )
def testStorageFormat ( self ) :
self . _image2XMLhelper ( " image-format.xml " , " image-format-out.xml " ,
qemu = True )
def _image2XMLhelper ( self , image_xml , output_xmls , qemu = False ) :
image2guestdir = self . basedir + " image2guest/ "
image = virtinst . ImageParser . parse_file ( self . basedir + image_xml )
if type ( output_xmls ) is not list :
output_xmls = [ output_xmls ]
conn = qemu and self . qemuconn or self . conn
gtype = qemu and " qemu " or " xen "
for idx in range ( len ( output_xmls ) ) :
fname = output_xmls [ idx ]
2013-07-06 04:14:57 +04:00
inst = virtinst . ImageInstaller ( conn , image , boot_index = idx )
2013-07-17 15:53:47 +04:00
capsguest , capsdomain = inst . get_caps_guest ( )
if capsguest . os_type == " hvm " :
2013-03-18 01:06:52 +04:00
g = utils . get_basic_fullyvirt_guest ( typ = gtype )
else :
g = utils . get_basic_paravirt_guest ( )
2013-07-17 15:53:47 +04:00
g . os . os_type = capsguest . os_type
g . type = capsdomain . hypervisor_type
g . os . arch = capsguest . arch
utils . set_conn ( conn )
2013-03-18 01:06:52 +04:00
g . installer = inst
g . _prepare_install ( None )
2013-07-24 19:32:30 +04:00
actual_out = g . get_install_xml ( install = False )
2013-03-18 01:06:52 +04:00
expect_file = os . path . join ( image2guestdir + fname )
expect_out = utils . read_file ( expect_file )
expect_out = expect_out . replace ( " REPLACEME " , os . getcwd ( ) )
utils . diff_compare ( actual_out ,
expect_file ,
expect_out = expect_out )
utils . reset_conn ( )
def testImage2XML ( self ) :
2013-07-05 16:59:58 +04:00
# Build guest XML from the image xml
2013-03-18 01:06:52 +04:00
self . _image2XMLhelper ( " image.xml " , [ " image-xenpv32.xml " ,
" image-xenfv32.xml " ] )
self . _image2XMLhelper ( " image-kernel.xml " , [ " image-xenpv32-kernel.xml " ] )
if __name__ == " __main__ " :
unittest . main ( )