2013-10-27 21:59:46 +01:00
# Copyright (C) 2013 Red Hat, Inc.
2013-03-17 17:06:52 -04:00
#
2018-04-04 14:35:41 +01:00
# This work is licensed under the GNU GPLv2 or later.
2018-03-20 15:00:02 -04:00
# See the COPYING file in the top-level directory.
2013-03-17 17:06:52 -04:00
2018-01-05 15:49:53 -05:00
import os
2018-03-28 14:56:52 -04:00
import re
2018-01-02 14:58:57 -05:00
import sys
2018-01-05 15:49:53 -05:00
import unittest
2013-09-25 18:52:41 -04:00
2013-04-10 19:48:07 -04:00
from tests import utils
2013-09-25 18:52:41 -04:00
2019-06-07 17:32:51 -04:00
import virtinst . progress
2019-01-31 12:54:50 -05:00
from virtinst import Installer
2013-09-25 18:52:41 -04:00
from virtinst import Guest
2019-06-16 21:12:39 -04:00
from virtinst import log
2013-09-25 18:52:41 -04:00
2019-06-07 20:55:11 -04:00
# These are all functional tests
os . environ . pop ( " VIRTINST_TEST_SUITE " , None )
2013-09-26 14:47:08 -04:00
2018-03-28 14:39:40 -04:00
class _URLTestData ( object ) :
"""
Class that tracks all data needed for a single URL test case .
2020-01-26 18:11:43 -05:00
Data is stored in data / test_urls . ini
2018-03-28 14:39:40 -04:00
"""
2018-01-02 14:58:57 -05:00
def __init__ ( self , name , url , detectdistro ,
2019-03-24 11:22:50 -04:00
testxen , testshortcircuit , kernelarg , kernelregex ,
skip_libosinfo ) :
2018-01-02 14:58:57 -05:00
self . name = name
2017-08-29 11:04:57 -04:00
self . url = url
2013-09-26 10:24:28 -04:00
self . detectdistro = detectdistro
2018-01-02 14:58:57 -05:00
self . arch = self . _find_arch ( )
2018-04-02 16:50:41 -04:00
self . kernelarg = kernelarg
2018-10-18 13:01:59 -04:00
self . kernelregex = kernelregex
2019-03-24 11:22:50 -04:00
self . skip_libosinfo = skip_libosinfo
2018-01-02 14:58:57 -05:00
self . testxen = testxen
2013-09-26 10:24:28 -04:00
2013-09-26 17:44:30 -04:00
# If True, pass in the expected distro value to getDistroStore
2018-01-02 14:58:57 -05:00
# so it can short circuit the lookup checks. Speeds up the tests
# and exercises the shortcircuit infrastructure
2013-09-26 17:44:30 -04:00
self . testshortcircuit = testshortcircuit
2018-01-02 14:58:57 -05:00
def _find_arch ( self ) :
if ( " i686 " in self . url or
" i386 " in self . url or
" i586 " in self . url ) :
return " i686 "
if ( " arm64 " in self . url or
" aarch64 " in self . url ) :
return " aarch64 "
if ( " ppc64el " in self . url or
" ppc64le " in self . url ) :
return " ppc64le "
if " s390 " in self . url :
return " s390x "
if ( " x86_64 " in self . url or
" amd64 " in self . url ) :
return " x86_64 "
return " x86_64 "
2013-03-17 17:06:52 -04:00
2018-02-22 14:57:10 -05:00
testconn = utils . URIs . open_testdefault_cached ( )
2013-09-26 13:04:28 -04:00
hvmguest = Guest ( testconn )
hvmguest . os . os_type = " hvm "
xenguest = Guest ( testconn )
xenguest . os . os_type = " xen "
2020-07-17 16:02:45 -04:00
meter = virtinst . progress . make_meter ( quiet = not utils . TESTCONFIG . debug )
2013-03-17 17:06:52 -04:00
2020-07-17 16:02:45 -04:00
if utils . TESTCONFIG . url_skip_libosinfo :
2019-03-24 11:22:50 -04:00
os . environ [ " VIRTINST_TEST_SUITE_FORCE_LIBOSINFO " ] = " 0 "
2020-07-17 16:02:45 -04:00
elif utils . TESTCONFIG . url_force_libosinfo :
2019-03-24 11:22:50 -04:00
os . environ [ " VIRTINST_TEST_SUITE_FORCE_LIBOSINFO " ] = " 1 "
2013-04-13 14:34:52 -04:00
2018-03-28 14:56:52 -04:00
def _sanitize_osdict_name ( detectdistro ) :
2018-10-18 13:43:12 -04:00
if detectdistro in [ " none " , " None " , None ] :
return None
2018-03-28 14:56:52 -04:00
return detectdistro
2019-03-24 11:22:50 -04:00
def _skipmsg ( testdata ) :
is_iso = testdata . url . lower ( ) . endswith ( " .iso " )
distname = testdata . name
2020-07-17 16:02:45 -04:00
if utils . TESTCONFIG . url_iso_only and not is_iso :
2019-03-24 11:22:50 -04:00
return " skipping non-iso test "
2020-07-17 16:02:45 -04:00
elif utils . TESTCONFIG . url_only and is_iso :
2019-03-24 11:22:50 -04:00
return " skipping non-url test "
2020-07-17 16:02:45 -04:00
if not utils . TESTCONFIG . url_force_libosinfo :
2019-03-24 11:22:50 -04:00
return
if testdata . skip_libosinfo :
return " force-libosinfo requested but test has skip_libosinfo set "
if is_iso :
return
# If --force-libosinfo used, don't run tests that we know libosinfo
# can't detect, non-treeinfo URLs basically
if ( " ubuntu " in distname or
" debian " in distname or
" mageia " in distname or
" opensuse10 " in distname or
" opensuse11 " in distname or
" opensuse12 " in distname or
" opensuse13 " in distname or
" opensuseleap-42 " in distname or
2019-07-16 16:36:07 -04:00
" generic " in distname or
testdata . url . startswith ( " ftp:/ " ) ) :
2019-03-24 11:22:50 -04:00
return " skipping known busted libosinfo URL tests "
2019-01-31 12:54:50 -05:00
def _testGuest ( testdata , guest ) :
2018-03-28 14:39:40 -04:00
distname = testdata . name
arch = testdata . arch
2019-01-31 12:54:50 -05:00
url = testdata . url
checkdistro = testdata . detectdistro
2018-03-28 14:56:52 -04:00
2019-01-31 12:54:50 -05:00
guest . os . arch = arch
2019-11-12 15:23:10 -05:00
guest . set_os_name ( " generic " )
2018-03-28 14:39:40 -04:00
if testdata . testshortcircuit :
2019-01-31 12:54:50 -05:00
guest . set_os_name ( checkdistro )
2013-03-17 17:06:52 -04:00
2019-03-24 11:22:50 -04:00
msg = _skipmsg ( testdata )
if msg :
raise unittest . SkipTest ( msg )
2019-01-31 12:54:50 -05:00
installer = Installer ( guest . conn , location = url )
2016-04-07 17:13:17 -04:00
try :
2019-01-31 12:54:50 -05:00
detected_distro = installer . detect_distro ( guest )
2019-06-24 19:39:07 -04:00
except Exception as e :
msg = ( " \n Failed in installer detect_distro(): \n "
2016-04-07 17:13:17 -04:00
" name = %s \n "
2019-06-24 19:39:07 -04:00
" url = %s \n \n %s " % ( distname , url , str ( e ) ) )
raise type ( e ) ( msg ) . with_traceback ( sys . exc_info ( ) [ 2 ] ) from None
2019-01-31 12:54:50 -05:00
# Make sure the stores are reporting correct distro name/variant
if checkdistro != detected_distro :
raise AssertionError (
" Detected OS did not match expected values: \n "
" found = %s \n "
" expect = %s \n \n "
" testname = %s \n "
" url = %s \n " %
( detected_distro , checkdistro , distname , url ) )
if guest is xenguest :
return
2013-09-26 11:49:16 -04:00
2019-03-24 11:22:50 -04:00
2019-01-31 13:42:02 -05:00
# Do this only after the distro detection, since we actually need
# to fetch files for that part
treemedia = installer . _treemedia # pylint: disable=protected-access
fetcher = treemedia . _cached_fetcher # pylint: disable=protected-access
def fakeAcquireFile ( filename ) :
2019-06-16 21:12:39 -04:00
log . debug ( " Fake acquiring %s " , filename )
2019-01-31 13:42:02 -05:00
return filename
fetcher . acquireFile = fakeAcquireFile
2013-09-26 11:49:16 -04:00
# Fetch regular kernel
2019-06-09 19:10:19 -04:00
kernel , initrd , kernelargs = treemedia . prepare ( guest , meter , None )
2019-01-29 16:05:27 -05:00
dummy = initrd
2018-10-18 13:01:59 -04:00
if testdata . kernelregex and not re . match ( testdata . kernelregex , kernel ) :
raise AssertionError ( " kernel= %s but testdata.kernelregex= ' %s ' " %
( kernel , testdata . kernelregex ) )
2018-04-02 16:50:41 -04:00
if testdata . kernelarg == " None " :
if bool ( kernelargs ) :
raise AssertionError ( " kernelargs= ' %s ' but testdata.kernelarg= ' %s ' "
% ( kernelargs , testdata . kernelarg ) )
elif testdata . kernelarg :
2019-01-31 13:42:02 -05:00
if testdata . kernelarg != str ( kernelargs ) . split ( " = " ) [ 0 ] :
2018-04-02 16:50:41 -04:00
raise AssertionError ( " kernelargs= ' %s ' but testdata.kernelarg= ' %s ' "
% ( kernelargs , testdata . kernelarg ) )
2013-09-26 11:49:16 -04:00
2019-01-31 12:54:50 -05:00
def _testURL ( testdata ) :
"""
Test that our URL detection logic works for grabbing kernels
"""
2019-06-24 19:39:07 -04:00
sys . stdout . write ( " \n Testing %-25s " % testdata . name )
sys . stdout . flush ( )
2019-01-31 12:54:50 -05:00
testdata . detectdistro = _sanitize_osdict_name ( testdata . detectdistro )
_testGuest ( testdata , hvmguest )
if testdata . testxen :
_testGuest ( testdata , xenguest )
2018-03-29 19:22:28 -04:00
2013-09-26 11:49:16 -04:00
# Register tests to be picked up by unittest
class URLTests ( unittest . TestCase ) :
2018-03-29 19:22:28 -04:00
def test001BadURL ( self ) :
badurl = " http://aksdkakskdfa-idontexist.com/foo/tree "
2019-06-14 13:04:56 -04:00
with self . assertRaises ( ValueError ) as cm :
2019-01-31 12:54:50 -05:00
installer = Installer ( hvmguest . conn , location = badurl )
installer . detect_distro ( hvmguest )
2019-06-14 13:04:56 -04:00
self . assertTrue ( " maybe you mistyped " in str ( cm . exception ) )
2018-03-29 19:22:28 -04:00
2019-01-31 13:01:33 -05:00
# Non-existent cdrom fails
2019-06-14 13:04:56 -04:00
with self . assertRaises ( ValueError ) as cm :
2019-05-16 07:13:09 -04:00
installer = Installer ( hvmguest . conn , cdrom = " /not/exist/foobar " )
2019-01-31 13:01:33 -05:00
self . assertEqual ( None , installer . detect_distro ( hvmguest ) )
2019-06-14 13:04:56 -04:00
self . assertTrue ( " non-existent path " in str ( cm . exception ) )
2019-01-31 13:01:33 -05:00
# Ensure existing but non-distro file doesn't error
installer = Installer ( hvmguest . conn , cdrom = " /dev/null " )
self . assertEqual ( None , installer . detect_distro ( hvmguest ) )
2013-09-26 11:49:16 -04:00
def _make_tests ( ) :
2017-10-11 12:35:39 +01:00
import configparser
cfg = configparser . ConfigParser ( )
2020-01-26 18:11:43 -05:00
cfg . read ( " tests/data/test_urls.ini " )
2018-01-02 14:58:57 -05:00
2018-04-03 15:29:03 -04:00
manualpath = " ~/.config/virt-manager/test_urls_manual.ini "
2018-04-16 11:36:39 -04:00
cfg . read ( os . path . expanduser ( manualpath ) )
2018-04-03 15:29:03 -04:00
if not os . path . exists ( os . path . expanduser ( manualpath ) ) :
2018-01-05 15:49:53 -05:00
print ( " NOTE: Pass in manual data with %s " % manualpath )
2018-01-02 14:58:57 -05:00
urls = { }
for name in cfg . sections ( ) :
vals = dict ( cfg . items ( name ) )
2019-03-02 16:52:24 -05:00
url = vals [ " url " ]
if " distro " not in vals :
print ( " url needs an explicit distro= value: %s " % url )
sys . exit ( 1 )
d = _URLTestData ( name , url , vals [ " distro " ] ,
2018-03-28 14:39:40 -04:00
vals . get ( " testxen " , " 0 " ) == " 1 " ,
2018-04-02 16:50:41 -04:00
vals . get ( " testshortcircuit " , " 0 " ) == " 1 " ,
2018-10-18 13:01:59 -04:00
vals . get ( " kernelarg " , None ) ,
2019-03-24 11:22:50 -04:00
vals . get ( " kernelregex " , None ) ,
vals . get ( " skiplibosinfo " , " 0 " ) == " 1 " )
2018-01-02 14:58:57 -05:00
urls [ d . name ] = d
2013-09-26 18:32:50 -04:00
2018-12-18 14:44:51 +01:00
for key , testdata in sorted ( urls . items ( ) ) :
2018-01-02 14:58:57 -05:00
def _make_wrapper ( d ) :
2019-06-24 19:39:07 -04:00
return lambda _self : _testURL ( d )
methodname = " testURL %s " % key . replace ( " - " , " _ " )
setattr ( URLTests , methodname , _make_wrapper ( testdata ) )
2013-03-17 17:06:52 -04:00
2013-09-26 11:49:16 -04:00
_make_tests ( )