2013-10-28 00:59:46 +04:00
# Copyright (C) 2013 Red Hat, Inc.
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
2013-10-28 00:59:47 +04:00
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
2013-03-18 01:06:52 +04:00
#
# 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.
2017-05-05 21:16:59 +03:00
from __future__ import print_function
2013-03-18 01:06:52 +04:00
import glob
2017-10-11 14:35:50 +03:00
import io
2014-02-06 04:09:26 +04:00
import os
2017-10-11 14:36:00 +03:00
import sys
2014-02-06 04:09:26 +04:00
import unittest
2013-03-18 01:06:52 +04:00
2014-02-06 04:09:26 +04:00
from virtconv import VirtConverter
2013-03-18 01:06:52 +04:00
2014-02-06 04:09:26 +04:00
from tests import utils
2013-03-18 01:06:52 +04:00
2014-02-06 04:09:26 +04:00
base_dir = os . getcwd ( ) + " /tests/virtconv-files/ "
out_dir = base_dir + " libvirt_output "
2013-03-18 01:06:52 +04:00
2013-04-13 22:34:52 +04:00
2013-03-18 01:06:52 +04:00
class TestVirtConv ( unittest . TestCase ) :
2014-02-06 04:09:26 +04:00
def _convert_helper ( self , infile , outfile , in_type , disk_format ) :
2017-10-11 14:36:00 +03:00
if sys . version_info [ 0 ] == 3 :
outbuf = io . StringIO ( )
else :
outbuf = io . BytesIO ( )
2014-02-06 04:09:26 +04:00
def print_cb ( msg ) :
2017-05-05 21:16:59 +03:00
print ( msg , file = outbuf )
2013-03-18 01:06:52 +04:00
2017-07-19 00:00:01 +03:00
conn = utils . open_kvm ( )
2014-02-06 04:09:26 +04:00
converter = VirtConverter ( conn , infile , print_cb = print_cb )
2013-03-18 01:06:52 +04:00
2014-02-06 04:09:26 +04:00
if converter . parser . name != in_type :
2013-03-18 01:06:52 +04:00
raise AssertionError ( " find_parser_by_file for ' %s ' returned "
" wrong parser type. \n "
" Expected: %s \n "
2013-04-13 22:34:52 +04:00
" Received: %s \n " %
2014-02-06 04:09:26 +04:00
( infile , in_type , converter . parser . name ) )
2013-03-18 01:06:52 +04:00
2014-02-06 04:09:26 +04:00
converter . convert_disks ( disk_format , dry = True )
guest = converter . get_guest ( )
ignore , out_xml = guest . start_install ( return_xml = True )
out_expect = out_xml
if outbuf . getvalue ( ) :
2016-01-15 10:27:45 +03:00
out_expect + = ( " \n \n " + outbuf . getvalue ( ) . replace ( base_dir , " " ) )
2013-03-18 01:06:52 +04:00
2015-05-20 20:59:48 +03:00
if not conn . check_support ( conn . SUPPORT_CONN_VMPORT ) :
self . skipTest ( " Not comparing XML because vmport isn ' t supported " )
2013-03-18 01:06:52 +04:00
utils . diff_compare ( out_expect , outfile )
2017-07-19 00:00:01 +03:00
utils . test_create ( conn , out_xml )
2013-03-18 01:06:52 +04:00
2014-02-06 04:09:26 +04:00
def _compare_single_file ( self , in_path , in_type , disk_format = None ) :
2013-03-18 01:06:52 +04:00
cwd = os . getcwd ( )
2014-02-06 04:09:26 +04:00
base = in_type + " 2libvirt "
in_base = os . path . basename ( in_path ) . rsplit ( " . " , 1 ) [ 0 ]
out_path = " %s / %s _ %s . %s " % ( out_dir , base , in_base , " libvirt " )
if disk_format :
out_path + = " .disk_ %s " % disk_format
try :
os . chdir ( os . path . dirname ( in_path ) )
self . _convert_helper ( in_path , out_path , in_type , disk_format )
finally :
os . chdir ( cwd )
def _compare_files ( self , in_type ) :
in_dir = base_dir + in_type + " _input "
if not os . path . exists ( in_dir ) :
raise RuntimeError ( " Directory does not exist: %s " % in_dir )
for in_path in glob . glob ( os . path . join ( in_dir , " * " ) ) :
self . _compare_single_file ( in_path , in_type )
def testOVF2Libvirt ( self ) :
self . _compare_files ( " ovf " )
def testVMX2Libvirt ( self ) :
self . _compare_files ( " vmx " )
def testDiskConvert ( self ) :
self . _compare_single_file (
base_dir + " ovf_input/test1.ovf " , " ovf " , disk_format = " qcow2 " )
self . _compare_single_file (
base_dir + " vmx_input/test1.vmx " , " vmx " , disk_format = " raw " )
2016-03-11 12:31:36 +03:00
self . _compare_single_file (
base_dir + " ovf_input/test_gzip.ovf " , " ovf " , disk_format = " raw " )