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
2017-10-11 14:35:50 +03:00
import io
2014-02-06 04:09:26 +04:00
import os
import unittest
2013-03-18 01:06:52 +04:00
2018-09-03 22:21:11 +03:00
from virtinst import Installer
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 ) :
2018-02-20 22:21:53 +03:00
def _convert_helper ( self , in_path , out_path , in_type , disk_format ) :
2018-01-27 22:19:12 +03:00
outbuf = io . StringIO ( )
2017-10-11 14:36:00 +03:00
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
2018-02-22 22:57:10 +03:00
conn = utils . URIs . open_kvm ( )
2018-02-20 22:21:53 +03:00
converter = VirtConverter ( conn , in_path , 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 " %
2018-02-20 22:21:53 +03:00
( in_path , 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 ( )
2018-09-03 22:21:11 +03:00
installer = Installer ( guest . conn )
ignore , out_xml = installer . start_install ( guest , return_xml = True )
2014-02-06 04:09:26 +04:00
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 " )
2018-02-20 22:21:53 +03:00
utils . diff_compare ( out_expect , out_path )
2017-07-19 00:00:01 +03:00
utils . test_create ( conn , out_xml )
2013-03-18 01:06:52 +04:00
2018-02-20 22:21:53 +03:00
def _compare ( self , in_path , disk_format = None ) :
in_type = " ovf "
if " vmx " in in_path :
in_type = " vmx "
in_path = os . path . join ( base_dir , in_path )
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
2018-02-23 03:25:05 +03:00
self . _convert_helper ( in_path , out_path , in_type , disk_format )
2014-02-06 04:09:26 +04:00
def testOVF2Libvirt ( self ) :
2018-02-20 22:21:53 +03:00
self . _compare ( " ovf_input/test1.ovf " )
self . _compare ( " ovf_input/test2.ovf " )
self . _compare ( " ovf_input/test_gzip.ovf " )
self . _compare ( " ovf_input/ovf_directory " )
2014-02-06 04:09:26 +04:00
def testVMX2Libvirt ( self ) :
2018-02-20 22:21:53 +03:00
self . _compare ( " vmx_input/test1.vmx " )
self . _compare ( " vmx_input/test-nodisks.vmx " )
self . _compare ( " vmx_input/test-vmx-zip.zip " )
self . _compare ( " vmx_input/vmx-dir " )
2014-02-06 04:09:26 +04:00
def testDiskConvert ( self ) :
2018-02-20 22:21:53 +03:00
self . _compare ( " ovf_input/test1.ovf " , disk_format = " qcow2 " )
self . _compare ( " vmx_input/test1.vmx " , disk_format = " raw " )
self . _compare ( " ovf_input/test_gzip.ovf " , disk_format = " raw " )