2014-02-06 15:12:12 +04:00
# Copyright (C) 2013, 2014 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.
import difflib
import os
import virtinst
import virtinst . cli
2015-09-22 18:34:15 +03:00
import virtinst . uri
2013-03-18 01:06:52 +04:00
2014-01-31 22:44:50 +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
2018-01-09 01:05:55 +03:00
class _CLIState ( object ) :
"""
Class containing any bits passed in from setup . py
"""
def __init__ ( self ) :
self . regenerate_output = False
clistate = _CLIState ( )
2013-08-18 16:19:58 +04:00
_capsprefix = " ,caps= %s /tests/capabilities-xml/ " % os . getcwd ( )
2015-02-18 22:49:53 +03:00
_domcapsprefix = " ,domcaps= %s /tests/capabilities-xml/ " % os . getcwd ( )
2015-04-23 00:06:35 +03:00
uri_test_default = " __virtinst_test__test:///default,predictable "
uri_test = " __virtinst_test__test:/// %s /tests/testdriver.xml,predictable " % os . getcwd ( )
uri_test_remote = uri_test + " ,remote "
_uri_qemu = " %s ,qemu " % uri_test
2015-04-23 02:27:27 +03:00
_uri_kvm_domcaps = ( _uri_qemu + _domcapsprefix + " kvm-x86_64-domcaps.xml " )
2017-02-06 15:46:06 +03:00
_uri_kvm_domcaps_q35 = ( _uri_qemu + _domcapsprefix + " kvm-x86_64-domcaps-q35.xml " )
2016-06-10 18:42:07 +03:00
_uri_kvm_aarch64_domcaps = ( _uri_qemu + _domcapsprefix + " kvm-aarch64-domcaps.xml " )
2015-04-23 00:06:35 +03:00
uri_kvm_nodomcaps = ( _uri_qemu + _capsprefix + " kvm-x86_64.xml " )
2015-04-23 02:27:27 +03:00
uri_kvm_rhel = ( _uri_kvm_domcaps + _capsprefix + " kvm-x86_64-rhel7.xml " )
uri_kvm = ( _uri_kvm_domcaps + _capsprefix + " kvm-x86_64.xml " )
2017-02-06 15:46:06 +03:00
uri_kvm_q35 = ( _uri_kvm_domcaps_q35 + _capsprefix + " kvm-x86_64.xml " )
2015-08-10 19:46:47 +03:00
uri_kvm_session = uri_kvm + " ,session "
2015-04-23 02:27:27 +03:00
uri_kvm_armv7l = ( _uri_kvm_domcaps + _capsprefix + " kvm-armv7l.xml " )
2016-06-10 18:42:07 +03:00
uri_kvm_aarch64 = ( _uri_kvm_aarch64_domcaps + _capsprefix + " kvm-aarch64.xml " )
2015-04-23 02:27:27 +03:00
uri_kvm_ppc64le = ( _uri_kvm_domcaps + _capsprefix + " kvm-ppc64le.xml " )
2015-07-13 14:35:24 +03:00
uri_kvm_s390x = ( _uri_kvm_domcaps + _capsprefix + " kvm-s390x.xml " )
2015-11-04 09:30:23 +03:00
uri_kvm_s390x_KVMIBM = ( _uri_kvm_domcaps + _capsprefix + " kvm-s390x-KVMIBM.xml " )
2015-04-23 00:06:35 +03:00
uri_xen = uri_test + _capsprefix + " xen-rhel5.4.xml,xen "
uri_lxc = uri_test + _capsprefix + " lxc.xml,lxc "
2017-02-21 17:28:00 +03:00
uri_vz = uri_test + _capsprefix + " vz.xml,vz "
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 get_debug ( ) :
return ( " DEBUG_TESTS " in os . environ and
os . environ [ " DEBUG_TESTS " ] == " 1 " )
2013-04-13 22:34:52 +04:00
2013-03-18 01:06:52 +04:00
def _make_uri ( base , connver = None , libver = None ) :
if connver :
base + = " ,connver= %s " % connver
if libver :
base + = " ,libver= %s " % libver
return base
2013-04-13 22:34:52 +04:00
2013-09-29 05:03:03 +04:00
_conn_cache = { }
def openconn ( uri ) :
"""
Extra super caching to speed up the test suite . We basically
cache the first guest / pool / vol poll attempt for each URI , and save it
across multiple reopenings of that connection . We aren ' t caching
libvirt objects , just parsed XML objects . This works fine since
generally every test uses a fresh virConnect , or undoes the
persistent changes it makes .
"""
2014-09-20 19:37:23 +04:00
virtinst . util . register_libvirt_error_handler ( )
2013-09-29 05:03:03 +04:00
conn = virtinst . cli . getConnection ( uri )
2017-08-30 17:36:37 +03:00
# For the basic test:///default URI, skip this caching, so we have
# an option to test the stock code
if uri == uri_test_default :
return conn
2013-09-29 05:03:03 +04:00
if uri not in _conn_cache :
2017-07-19 22:53:11 +03:00
conn . fetch_all_guests ( )
conn . fetch_all_pools ( )
conn . fetch_all_vols ( )
conn . fetch_all_nodedevs ( )
2013-09-29 05:03:03 +04:00
_conn_cache [ uri ] = { }
2017-07-19 22:53:11 +03:00
for key , value in conn . _fetch_cache . items ( ) :
_conn_cache [ uri ] [ key ] = value [ : ]
# Prime the internal connection cache
for key , value in _conn_cache [ uri ] . items ( ) :
conn . _fetch_cache [ key ] = value [ : ]
2013-09-29 05:03:03 +04:00
2017-07-18 22:42:11 +03:00
def cb_cache_new_pool ( poolobj ) :
# Used by clonetest.py nvram-newpool test
if poolobj . name ( ) == " nvram-newpool " :
from virtinst import StorageVolume
vol = StorageVolume ( conn )
vol . pool = poolobj
vol . name = " clone-orig-vars.fd "
vol . capacity = 1024 * 1024
vol . install ( )
conn . _cache_new_pool_raw ( poolobj )
conn . cb_cache_new_pool = cb_cache_new_pool
2013-09-29 05:03:03 +04:00
return conn
2013-07-05 16:59:58 +04:00
def open_testdefault ( ) :
2015-04-23 00:06:35 +03:00
return openconn ( uri_test_default )
2013-07-05 16:59:58 +04:00
2013-03-18 01:06:52 +04:00
def open_testdriver ( ) :
2015-04-23 00:06:35 +03:00
return openconn ( uri_test )
2013-04-13 22:34:52 +04:00
2015-04-23 00:06:35 +03:00
def open_kvm ( connver = None , libver = None ) :
return openconn ( _make_uri ( uri_kvm , connver , libver ) )
2015-03-23 23:48:43 +03:00
2015-04-23 00:06:35 +03:00
def open_kvm_rhel ( connver = None ) :
return openconn ( _make_uri ( uri_kvm_rhel , connver ) )
2013-04-13 22:34:52 +04:00
2013-03-18 01:06:52 +04:00
def open_test_remote ( ) :
2015-04-23 00:06:35 +03:00
return openconn ( uri_test_remote )
2013-03-18 01:06:52 +04:00
2013-04-13 22:34:52 +04:00
2013-09-10 01:14:16 +04:00
def test_create ( testconn , xml , define_func = " defineXML " ) :
2015-09-22 18:34:15 +03:00
xml = virtinst . uri . sanitize_xml_for_test_define ( xml )
2013-03-18 01:06:52 +04:00
2013-06-14 22:58:52 +04:00
try :
2013-09-10 01:14:16 +04:00
func = getattr ( testconn , define_func )
obj = func ( xml )
2017-05-05 19:47:21 +03:00
except Exception as e :
2013-06-14 22:58:52 +04:00
raise RuntimeError ( str ( e ) + " \n " + xml )
2013-03-18 01:06:52 +04:00
try :
2013-09-10 01:14:16 +04:00
obj . create ( )
obj . destroy ( )
obj . undefine ( )
2017-07-24 11:26:48 +03:00
except Exception :
2013-03-18 01:06:52 +04:00
try :
2013-09-10 01:14:16 +04:00
obj . destroy ( )
2017-07-24 11:26:48 +03:00
except Exception :
2013-03-18 01:06:52 +04:00
pass
try :
2013-09-10 01:14:16 +04:00
obj . undefine ( )
2017-07-24 11:26:48 +03:00
except Exception :
2013-03-18 01:06:52 +04:00
pass
2013-04-13 22:34:52 +04:00
2013-03-18 01:06:52 +04:00
def read_file ( filename ) :
""" Helper function to read a files contents and return them """
f = open ( filename , " r " )
out = f . read ( )
f . close ( )
return out
2013-04-13 22:34:52 +04:00
2013-03-18 01:06:52 +04:00
def diff_compare ( actual_out , filename = None , expect_out = None ) :
""" Compare passed string output to contents of filename """
if not expect_out :
2018-01-09 01:05:55 +03:00
if not os . path . exists ( filename ) or clistate . regenerate_output :
2017-05-05 21:19:54 +03:00
open ( filename , " w " ) . write ( actual_out )
2013-03-18 01:06:52 +04:00
expect_out = read_file ( filename )
diff = " " . join ( difflib . unified_diff ( expect_out . splitlines ( 1 ) ,
actual_out . splitlines ( 1 ) ,
2017-10-11 14:35:54 +03:00
fromfile = filename or ' ' ,
2013-03-18 01:06:52 +04:00
tofile = " Generated Output " ) )
if diff :
raise AssertionError ( " Conversion outputs did not match. \n %s " % diff )