2013-10-27 21:59:46 +01:00
# Copyright (C) 2013 Red Hat, Inc.
2013-03-17 17: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-27 21:59:47 +01:00
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
2013-03-17 17: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 logging
2018-01-05 15:49:53 -05:00
import os
2018-01-02 14:58:57 -05:00
import sys
2018-01-05 15:49:53 -05:00
import time
2016-04-07 17:13:17 -04:00
import traceback
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
from virtinst import Guest
2013-08-08 21:42:44 -04:00
from virtinst import urlfetcher
2015-09-06 14:26:50 -04:00
from virtinst import util
2018-01-02 14:58:57 -05:00
from virtinst . urlfetcher import ALTLinuxDistro
2013-08-08 21:42:44 -04:00
from virtinst . urlfetcher import CentOSDistro
2018-01-02 14:58:57 -05:00
from virtinst . urlfetcher import DebianDistro
from virtinst . urlfetcher import FedoraDistro
from virtinst . urlfetcher import GenericDistro
from virtinst . urlfetcher import MandrivaDistro
from virtinst . urlfetcher import RHELDistro
2013-08-08 21:42:44 -04:00
from virtinst . urlfetcher import SLDistro
2018-01-02 14:58:57 -05:00
from virtinst . urlfetcher import SuseDistro
2013-08-08 21:42:44 -04:00
from virtinst . urlfetcher import UbuntuDistro
2013-09-25 18:52:41 -04:00
2013-09-26 14:47:08 -04:00
2013-09-26 10:24:28 -04:00
class _DistroURL ( object ) :
2018-01-02 14:58:57 -05:00
def __init__ ( self , name , url , detectdistro ,
testxen , testbootiso , testshortcircuit ) :
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 ( )
self . distroclass = self . _distroclass_for_name ( self . name )
self . testxen = testxen
self . testbootiso = testbootiso
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 _distroclass_for_name ( self , name ) :
# Map the test case name to the expected urlfetcher distro
# class we should be detecting
if " fedora " in name :
return FedoraDistro
if " centos " in name :
return CentOSDistro
if " rhel " in name :
return RHELDistro
if " suse " in name :
return SuseDistro
if " debian " in name :
return DebianDistro
if name . startswith ( " sl- " ) :
return SLDistro
if " ubuntu " in name :
return UbuntuDistro
if " mageia " in name :
return MandrivaDistro
if " altlinux " in name :
return ALTLinuxDistro
if " generic " in name :
return GenericDistro
raise RuntimeError ( " name= %s didn ' t map to any distro class. Extend "
" _distroclass_for_name " % name )
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
2013-07-05 08:59:58 -04:00
testconn = utils . open_testdefault ( )
2013-09-26 13:04:28 -04:00
hvmguest = Guest ( testconn )
hvmguest . os . os_type = " hvm "
xenguest = Guest ( testconn )
xenguest . os . os_type = " xen "
2015-09-06 14:26:50 -04:00
meter = util . make_meter ( quiet = not utils . get_debug ( ) )
2013-03-17 17:06:52 -04:00
2013-04-13 14:34:52 -04:00
2013-09-26 13:04:28 -04:00
def _storeForDistro ( fetcher , guest ) :
2013-09-26 11:49:16 -04:00
"""
Helper to lookup the Distro store object , basically detecting the
URL . Handle occasional proxy errors
"""
for ignore in range ( 0 , 10 ) :
2013-03-17 17:06:52 -04:00
try :
2013-09-26 13:04:28 -04:00
return urlfetcher . getDistroStore ( guest , fetcher )
2017-05-05 12:47:21 -04:00
except Exception as e :
2013-09-26 11:49:16 -04:00
if str ( e ) . count ( " 502 " ) :
logging . debug ( " Caught proxy error: %s " , str ( e ) )
time . sleep ( .5 )
2013-03-17 17:06:52 -04:00
continue
2013-09-26 11:49:16 -04:00
raise
2016-04-18 16:42:12 -04:00
raise # pylint: disable=misplaced-bare-raise
2013-03-17 17:06:52 -04:00
2018-01-02 14:58:57 -05:00
def _testURL ( fetcher , distroobj ) :
2013-09-26 11:49:16 -04:00
"""
Test that our URL detection logic works for grabbing kernel , xen
kernel , and boot . iso
"""
2018-01-27 16:12:11 -05:00
distname = distroobj . name
2017-08-29 11:04:57 -04:00
arch = distroobj . arch
2013-09-26 13:04:28 -04:00
hvmguest . os . arch = arch
xenguest . os . arch = arch
2013-09-26 17:44:30 -04:00
if distroobj . testshortcircuit :
hvmguest . os_variant = distroobj . detectdistro
xenguest . os_variant = distroobj . detectdistro
2018-01-02 14:58:57 -05:00
else :
hvmguest . os_variant = None
xenguest . os_variant = None
2013-03-17 17:06:52 -04:00
2016-04-07 17:13:17 -04:00
try :
hvmstore = _storeForDistro ( fetcher , hvmguest )
xenstore = None
2018-01-02 14:58:57 -05:00
if distroobj . testxen :
2016-04-07 17:13:17 -04:00
xenstore = _storeForDistro ( fetcher , xenguest )
2017-07-24 09:26:48 +01:00
except Exception :
2016-04-07 17:13:17 -04:00
raise AssertionError ( " \n Failed to detect URLDistro class: \n "
" name = %s \n "
" url = %s \n \n %s " %
( distname , fetcher . location , " " . join ( traceback . format_exc ( ) ) ) )
2013-03-17 17:06:52 -04:00
2013-09-26 11:49:16 -04:00
for s in [ hvmstore , xenstore ] :
2013-09-26 18:32:50 -04:00
if ( s and distroobj . distroclass and
not isinstance ( s , distroobj . distroclass ) ) :
2016-03-24 14:55:27 -04:00
raise AssertionError ( " Unexpected URLDistro class: \n "
" found = %s \n "
" expect = %s \n "
" name = %s \n "
" url = %s " %
( s . __class__ , distroobj . distroclass , distname ,
fetcher . location ) )
2013-03-17 17:06:52 -04:00
# Make sure the stores are reporting correct distro name/variant
2013-09-26 18:32:50 -04:00
if ( s and distroobj . detectdistro and
2018-01-02 14:58:57 -05:00
distroobj . detectdistro != s . get_osdict_info ( ) ) :
2015-11-24 21:59:26 -05:00
raise AssertionError (
2016-03-24 14:55:27 -04:00
" Detected OS did not match expected values: \n "
" found = %s \n "
" expect = %s \n "
" name = %s \n "
" url = %s \n "
" store = %s " %
( s . os_variant , distroobj . detectdistro ,
distname , fetcher . location , distroobj . distroclass ) )
2013-09-26 11:49:16 -04:00
# Do this only after the distro detection, since we actually need
# to fetch files for that part
2013-09-26 13:04:28 -04:00
def fakeAcquireFile ( filename ) :
2013-09-26 11:49:16 -04:00
logging . debug ( " Fake acquiring %s " , filename )
return fetcher . hasFile ( filename )
fetcher . acquireFile = fakeAcquireFile
# Fetch boot iso
2018-01-02 14:58:57 -05:00
if distroobj . testbootiso :
2013-09-26 13:04:28 -04:00
boot = hvmstore . acquireBootDisk ( hvmguest )
2013-09-26 11:49:16 -04:00
logging . debug ( " acquireBootDisk: %s " , str ( boot ) )
if boot is not True :
raise AssertionError ( " %s - %s : bootiso fetching failed " %
( distname , arch ) )
# Fetch regular kernel
2013-09-26 13:04:28 -04:00
kern = hvmstore . acquireKernel ( hvmguest )
2013-09-26 11:49:16 -04:00
logging . debug ( " acquireKernel (hvm): %s " , str ( kern ) )
if kern [ 0 ] is not True or kern [ 1 ] is not True :
AssertionError ( " %s - %s : hvm kernel fetching failed " %
( distname , arch ) )
# Fetch xen kernel
2018-01-02 14:58:57 -05:00
if xenstore :
2013-09-26 13:04:28 -04:00
kern = xenstore . acquireKernel ( xenguest )
2013-09-26 11:49:16 -04:00
logging . debug ( " acquireKernel (xen): %s " , str ( kern ) )
if kern [ 0 ] is not True or kern [ 1 ] is not True :
raise AssertionError ( " %s - %s : xen kernel fetching " %
( distname , arch ) )
2018-01-02 14:58:57 -05:00
def _testURLWrapper ( distroobj ) :
2018-01-27 14:49:43 -05:00
os . environ . pop ( " VIRTINST_TEST_SUITE " , None )
logging . debug ( " Testing for media arch= %s distroclass= %s " ,
distroobj . arch , distroobj . distroclass )
2018-01-27 16:12:11 -05:00
sys . stdout . write ( " \n Testing %-25s " % distroobj . name )
2018-01-27 14:49:43 -05:00
sys . stdout . flush ( )
2018-01-02 14:58:57 -05:00
fetcher = urlfetcher . fetcherForURI ( distroobj . url , " /tmp " , meter )
2013-09-26 11:49:16 -04:00
try :
fetcher . prepareLocation ( )
2018-01-02 14:58:57 -05:00
return _testURL ( fetcher , distroobj )
2013-09-26 11:49:16 -04:00
finally :
fetcher . cleanupLocation ( )
# Register tests to be picked up by unittest
class URLTests ( unittest . TestCase ) :
pass
def _make_tests ( ) :
2018-01-02 14:58:57 -05:00
import ConfigParser
cfg = ConfigParser . ConfigParser ( )
cfg . read ( " tests/test_urls.ini " )
2018-01-05 15:49:53 -05:00
manualpath = " tests/test_urls_manual.ini "
cfg . read ( manualpath )
if not os . path . exists ( manualpath ) :
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 ) )
d = _DistroURL ( name , vals [ " url " ] ,
vals . get ( " distro " , None ) ,
vals . get ( " testxen " , " 0 " ) == " 1 " ,
vals . get ( " testbootiso " , " 0 " ) == " 1 " ,
vals . get ( " testshortcircuit " , " 0 " ) == " 1 " )
urls [ d . name ] = d
2013-09-26 18:32:50 -04:00
keys = urls . keys ( )
keys . sort ( )
for key in keys :
distroobj = urls [ key ]
2018-01-02 14:58:57 -05:00
def _make_wrapper ( d ) :
return lambda _self : _testURLWrapper ( d )
setattr ( URLTests , " testURL %s " % key . replace ( " - " , " _ " ) ,
_make_wrapper ( distroobj ) )
2013-03-17 17:06:52 -04:00
2013-09-26 11:49:16 -04:00
_make_tests ( )