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
import logging
2018-01-05 23:49:53 +03:00
import os
2018-03-28 21:56:52 +03:00
import re
2018-01-02 22:58:57 +03:00
import sys
2016-04-08 00:13:17 +03:00
import traceback
2018-01-05 23:49:53 +03:00
import unittest
2013-09-26 02:52:41 +04:00
2013-04-11 03:48:07 +04:00
from tests import utils
2013-09-26 02:52:41 +04:00
2019-01-31 20:54:50 +03:00
from virtinst import Installer
2013-09-26 02:52:41 +04:00
from virtinst import Guest
2015-09-06 21:26:50 +03:00
from virtinst import util
2013-09-26 02:52:41 +04:00
2013-09-26 22:47:08 +04:00
2018-03-28 21:39:40 +03:00
class _URLTestData ( object ) :
"""
Class that tracks all data needed for a single URL test case .
Data is stored in test_urls . ini
"""
2018-01-02 22:58:57 +03:00
def __init__ ( self , name , url , detectdistro ,
2018-10-18 20:01:59 +03:00
testxen , testshortcircuit , kernelarg , kernelregex ) :
2018-01-02 22:58:57 +03:00
self . name = name
2017-08-29 18:04:57 +03:00
self . url = url
2013-09-26 18:24:28 +04:00
self . detectdistro = detectdistro
2018-01-02 22:58:57 +03:00
self . arch = self . _find_arch ( )
2018-04-02 23:50:41 +03:00
self . kernelarg = kernelarg
2018-10-18 20:01:59 +03:00
self . kernelregex = kernelregex
2018-01-02 22:58:57 +03:00
self . testxen = testxen
2013-09-26 18:24:28 +04:00
2013-09-27 01:44:30 +04:00
# If True, pass in the expected distro value to getDistroStore
2018-01-02 22:58:57 +03:00
# so it can short circuit the lookup checks. Speeds up the tests
# and exercises the shortcircuit infrastructure
2013-09-27 01:44:30 +04:00
self . testshortcircuit = testshortcircuit
2018-01-02 22:58:57 +03: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-18 01:06:52 +04:00
2018-02-22 22:57:10 +03:00
testconn = utils . URIs . open_testdefault_cached ( )
2013-09-26 21:04:28 +04:00
hvmguest = Guest ( testconn )
hvmguest . os . os_type = " hvm "
xenguest = Guest ( testconn )
xenguest . os . os_type = " xen "
2018-02-22 21:46:24 +03:00
meter = util . make_meter ( quiet = not utils . clistate . debug )
2013-03-18 01:06:52 +04:00
2013-04-13 22:34:52 +04:00
2018-03-28 21:56:52 +03:00
def _sanitize_osdict_name ( detectdistro ) :
2018-10-18 20:43:12 +03:00
if detectdistro in [ " none " , " None " , None ] :
return None
2018-03-28 21:56:52 +03:00
return detectdistro
2019-01-31 20:54:50 +03:00
def _testGuest ( testdata , guest ) :
2018-03-28 21:39:40 +03:00
distname = testdata . name
arch = testdata . arch
2019-01-31 20:54:50 +03:00
url = testdata . url
checkdistro = testdata . detectdistro
2018-03-28 21:56:52 +03:00
2019-01-31 20:54:50 +03:00
guest . os . arch = arch
2018-03-28 21:39:40 +03:00
if testdata . testshortcircuit :
2019-01-31 20:54:50 +03:00
guest . set_os_name ( checkdistro )
2013-03-18 01:06:52 +04:00
2019-01-31 20:54:50 +03:00
installer = Installer ( guest . conn , location = url )
2016-04-08 00:13:17 +03:00
try :
2019-01-31 20:54:50 +03:00
detected_distro = installer . detect_distro ( guest )
2017-07-24 11:26:48 +03:00
except Exception :
2019-01-31 20:54:50 +03:00
raise AssertionError ( " \n Failed in installer detect_distro(): \n "
2016-04-08 00:13:17 +03:00
" name = %s \n "
" url = %s \n \n %s " %
2019-01-31 20:54:50 +03:00
( distname , url , " " . join ( traceback . format_exc ( ) ) ) )
# 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 19:49:16 +04:00
2019-01-31 21:42:02 +03: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 ) :
logging . debug ( " Fake acquiring %s " , filename )
return filename
fetcher . acquireFile = fakeAcquireFile
2013-09-26 19:49:16 +04:00
# Fetch regular kernel
2019-01-31 21:42:02 +03:00
kernel , initrd , kernelargs = treemedia . prepare ( guest , meter )
2019-01-30 00:05:27 +03:00
dummy = initrd
2018-10-18 20:01:59 +03: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 23:50:41 +03: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 21:42:02 +03:00
if testdata . kernelarg != str ( kernelargs ) . split ( " = " ) [ 0 ] :
2018-04-02 23:50:41 +03:00
raise AssertionError ( " kernelargs= ' %s ' but testdata.kernelarg= ' %s ' "
% ( kernelargs , testdata . kernelarg ) )
2013-09-26 19:49:16 +04:00
2019-01-31 20:54:50 +03:00
def _testURL ( testdata ) :
"""
Test that our URL detection logic works for grabbing kernels
"""
testdata . detectdistro = _sanitize_osdict_name ( testdata . detectdistro )
_testGuest ( testdata , hvmguest )
if testdata . testxen :
_testGuest ( testdata , xenguest )
2018-03-30 02:22:28 +03:00
2018-03-28 21:39:40 +03:00
def _testURLWrapper ( testdata ) :
2018-01-27 22:49:43 +03:00
os . environ . pop ( " VIRTINST_TEST_SUITE " , None )
2018-03-28 21:39:40 +03:00
sys . stdout . write ( " \n Testing %-25s " % testdata . name )
2018-01-27 22:49:43 +03:00
sys . stdout . flush ( )
2019-01-31 20:54:50 +03:00
return _testURL ( testdata )
2013-09-26 19:49:16 +04:00
# Register tests to be picked up by unittest
class URLTests ( unittest . TestCase ) :
2018-03-30 02:22:28 +03:00
def test001BadURL ( self ) :
badurl = " http://aksdkakskdfa-idontexist.com/foo/tree "
try :
2019-01-31 20:54:50 +03:00
installer = Installer ( hvmguest . conn , location = badurl )
installer . detect_distro ( hvmguest )
2018-03-30 02:22:28 +03:00
raise AssertionError ( " Expected URL failure " )
except ValueError as e :
self . assertTrue ( " maybe you mistyped " in str ( e ) )
2019-01-31 21:01:33 +03:00
# Non-existent cdrom fails
try :
installer = Installer ( hvmguest . conn , cdrom = " /i/dont/exist/foobar " )
self . assertEqual ( None , installer . detect_distro ( hvmguest ) )
raise AssertionError ( " Expected cdrom failure " )
except ValueError as e :
self . assertTrue ( " non-existent path " in str ( e ) )
# 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 19:49:16 +04:00
def _make_tests ( ) :
2017-10-11 14:35:39 +03:00
import configparser
cfg = configparser . ConfigParser ( )
2018-01-02 22:58:57 +03:00
cfg . read ( " tests/test_urls.ini " )
2018-04-03 22:29:03 +03:00
manualpath = " ~/.config/virt-manager/test_urls_manual.ini "
2018-04-16 18:36:39 +03:00
cfg . read ( os . path . expanduser ( manualpath ) )
2018-04-03 22:29:03 +03:00
if not os . path . exists ( os . path . expanduser ( manualpath ) ) :
2018-01-05 23:49:53 +03:00
print ( " NOTE: Pass in manual data with %s " % manualpath )
2018-01-02 22:58:57 +03:00
urls = { }
for name in cfg . sections ( ) :
vals = dict ( cfg . items ( name ) )
2018-03-28 21:39:40 +03:00
d = _URLTestData ( name , vals [ " url " ] ,
vals . get ( " distro " , None ) ,
vals . get ( " testxen " , " 0 " ) == " 1 " ,
2018-04-02 23:50:41 +03:00
vals . get ( " testshortcircuit " , " 0 " ) == " 1 " ,
2018-10-18 20:01:59 +03:00
vals . get ( " kernelarg " , None ) ,
vals . get ( " kernelregex " , None ) )
2018-01-02 22:58:57 +03:00
urls [ d . name ] = d
2013-09-27 02:32:50 +04:00
2018-12-18 16:44:51 +03:00
for key , testdata in sorted ( urls . items ( ) ) :
2018-01-02 22:58:57 +03:00
def _make_wrapper ( d ) :
return lambda _self : _testURLWrapper ( d )
setattr ( URLTests , " testURL %s " % key . replace ( " - " , " _ " ) ,
2018-03-28 21:39:40 +03:00
_make_wrapper ( testdata ) )
2013-03-18 01:06:52 +04:00
2013-09-26 19:49:16 +04:00
_make_tests ( )