2016-06-17 16:38:57 -04:00
#!/usr/bin/env python2
2014-02-06 12:12:12 +01:00
# Copyright (C) 2013, 2014 Red Hat, Inc.
2013-09-28 14:42:37 -04:00
2017-05-05 14:16:59 -04:00
from __future__ import print_function
2013-09-28 14:42:37 -04:00
import atexit
import os
2018-01-27 15:23:04 -05:00
import logging
2013-09-28 14:42:37 -04:00
import sys
import unittest
from tests import utils
from virtinst import Guest
from virtinst import urlfetcher
2015-09-06 14:26:50 -04:00
from virtinst import util
2016-06-16 17:22:35 -04:00
from virtinst . initrdinject import perform_initrd_injections
2013-09-28 14:42:37 -04:00
cleanup = [ ]
_alldistros = { }
testconn = utils . open_testdefault ( )
guest = Guest ( testconn )
guest . os . os_type = " hvm "
guest . os . arch = " x86_64 "
2015-09-06 14:26:50 -04:00
meter = util . make_meter ( quiet = False )
2013-09-28 14:42:37 -04:00
2016-06-17 18:41:50 -04:00
DEVFEDORA_URL = " http://dl.fedoraproject.org/pub/fedora/linux/development/ %s /Server/ %s /os/ "
2015-03-26 19:37:17 -04:00
FEDORA_URL = " http://dl.fedoraproject.org/pub/fedora/linux/releases/ %s /Server/ %s /os/ "
2013-09-28 14:42:37 -04:00
2014-09-20 15:21:03 -04:00
( WARN_RHEL4 ,
WARN_RHEL5 ,
WARN_LATEST ) = range ( 1 , 4 )
2013-09-28 14:42:37 -04:00
def prompt ( ) :
sys . stdout . write ( " (press enter to continue) " )
2018-01-27 15:23:04 -05:00
sys . stdout . flush ( )
2013-09-28 14:42:37 -04:00
return sys . stdin . readline ( )
class Distro ( object ) :
2014-09-20 15:21:03 -04:00
def __init__ ( self , name , url , warntype = WARN_LATEST ,
ks2 = False , virtio = True ) :
2013-09-28 14:42:37 -04:00
self . name = name
self . url = url
self . virtio = virtio
2014-09-20 15:21:03 -04:00
self . warntype = warntype
2013-09-28 14:42:37 -04:00
self . ks = " tests/inject-data/old-kickstart.ks "
if ks2 :
self . ks = " tests/inject-data/new-kickstart.ks "
self . kernel = None
self . initrd = None
def _add ( * args , * * kwargs ) :
_d = Distro ( * args , * * kwargs )
_alldistros [ _d . name ] = _d
_add ( " centos-4.9 " , " http://vault.centos.org/4.9/os/x86_64 " ,
2014-09-20 15:21:03 -04:00
warntype = WARN_RHEL4 , ks2 = True , virtio = False )
2017-06-07 12:40:46 -04:00
_add ( " centos-5.11 " , " http://vault.centos.org/5.11/os/x86_64/ " ,
2014-09-20 15:21:03 -04:00
warntype = WARN_RHEL5 )
_add ( " centos-6-latest " , " http://ftp.linux.ncsu.edu/pub/CentOS/6/os/x86_64/ " ,
warntype = WARN_RHEL5 )
_add ( " centos-7-latest " , " http://ftp.linux.ncsu.edu/pub/CentOS/7/os/x86_64/ " ,
ks2 = True )
2018-01-27 15:23:04 -05:00
_add ( " fedora-27 " , FEDORA_URL % ( " 27 " , " x86_64 " ) , ks2 = True )
2013-09-28 14:42:37 -04:00
def exit_cleanup ( ) :
for f in cleanup or [ ] :
try :
os . unlink ( f )
2017-07-24 09:26:48 +01:00
except Exception :
2013-09-28 14:42:37 -04:00
pass
atexit . register ( exit_cleanup )
def _fetch_distro ( distro ) :
2017-05-05 14:16:59 -04:00
print ( " Fetching distro= %s " % distro . name )
2013-09-28 14:42:37 -04:00
fetcher = urlfetcher . fetcherForURI ( distro . url , " /tmp " , meter )
2014-02-14 11:35:29 -05:00
origenv = os . environ . pop ( " VIRTINST_TEST_SUITE " )
2013-09-28 14:42:37 -04:00
try :
fetcher . prepareLocation ( )
store = urlfetcher . getDistroStore ( guest , fetcher )
kernel , initrd , ignore = store . acquireKernel ( guest )
cleanup . append ( kernel )
cleanup . append ( initrd )
distro . kernel = kernel
distro . initrd = initrd
2018-01-27 15:23:04 -05:00
except Exception :
logging . error ( " Fetching distro= %s failed " , distro . name , exc_info = True )
2013-09-28 14:42:37 -04:00
finally :
fetcher . cleanupLocation ( )
2014-02-14 11:35:29 -05:00
if origenv :
os . environ [ " VIRTINST_TEST_SUITE " ] = origenv
2013-09-28 14:42:37 -04:00
def _test_distro ( distro ) :
2014-09-20 15:21:03 -04:00
os . system ( " clear " )
2017-05-05 14:16:59 -04:00
print ( " \n " )
2014-09-20 15:21:03 -04:00
if distro . warntype == WARN_RHEL4 :
2017-05-05 14:16:59 -04:00
print ( " RHEL4: Makes its way to the text installer, then chokes " )
print ( " on our bogus URI http://HEY-THIS-IS-OUR-BAD-KICKSTART-URL.com/ " )
2014-09-20 15:21:03 -04:00
elif distro . warntype == WARN_RHEL5 :
2017-05-05 14:16:59 -04:00
print ( " RHEL5, RHEL6, Fedora < 17: You ' ll get an error about a " )
print ( " bogus bootproto ITREADTHEKICKSTART. This means anaconda " )
print ( " read our busted kickstart. " )
2014-09-20 15:21:03 -04:00
else :
2017-05-05 14:16:59 -04:00
print ( " RHEL7, Fedora >= 17: Chokes on the bogus URI in the early " )
print ( " console screen when fetching the installer squashfs image. " )
2014-09-20 15:21:03 -04:00
2013-09-28 14:42:37 -04:00
originitrd = distro . initrd
kernel = distro . kernel
newinitrd = originitrd + " .copy "
injectfile = distro . ks
os . system ( " cp -f %s %s " % ( originitrd , newinitrd ) )
cleanup . append ( newinitrd )
2016-06-16 17:22:35 -04:00
perform_initrd_injections ( newinitrd , [ injectfile ] , " . " )
2013-09-28 14:42:37 -04:00
nic = distro . virtio and " virtio " or " rtl8139 "
append = " -append \" ks=file:/ %s \" " % os . path . basename ( injectfile )
cmd = ( " sudo qemu-kvm -enable-kvm -name %s "
2016-06-17 18:41:50 -04:00
" -cpu host -m 1500 -display gtk "
2013-09-28 14:42:37 -04:00
" -net bridge,br=virbr0 -net nic,model= %s "
" -kernel %s -initrd %s %s " %
( distro . name , nic , kernel , newinitrd , append ) )
2017-05-05 14:16:59 -04:00
print ( " \n \n " + cmd )
2013-09-28 14:42:37 -04:00
os . system ( cmd )
_printinitrd = False
_printfetch = False
class FetchTests ( unittest . TestCase ) :
def setUp ( self ) :
2016-06-17 18:41:50 -04:00
self . failfast = True
2013-09-28 14:42:37 -04:00
global _printfetch
if _printfetch :
return
2018-01-27 16:22:29 -05:00
print ( """
2013-09-28 14:42:37 -04:00
This is an interactive test .
First step is we need to go and fetch a bunch of distro kernel / initrd
from public trees . This is going to take a while . Let it run then come
back later and we will be waiting to continue .
2017-05-05 14:16:59 -04:00
""" )
2013-09-28 14:42:37 -04:00
prompt ( )
_printfetch = True
class InjectTests ( unittest . TestCase ) :
def setUp ( self ) :
global _printinitrd
if _printinitrd :
return
2017-05-05 14:16:59 -04:00
print ( """
2013-09-28 14:42:37 -04:00
Okay , we have all the media . We are going to perform the initrd injection
2014-09-20 15:21:03 -04:00
of some broken kickstarts , then manually launch a qemu instance to verify
the kickstart is detected . How you know it ' s working depends on the distro.
When each test launches , we will print the manual verification instructions .
2013-09-28 14:42:37 -04:00
2017-05-05 14:16:59 -04:00
""" )
2013-09-28 14:42:37 -04:00
prompt ( )
_printinitrd = True
def _make_tests ( ) :
def _make_fetch_cb ( _d ) :
return lambda s : _fetch_distro ( _d )
def _make_check_cb ( _d ) :
return lambda s : _test_distro ( _d )
idx = 0
2016-06-17 18:41:50 -04:00
for dname , dobj in _alldistros . items ( ) :
2013-09-28 14:42:37 -04:00
idx + = 1
2016-06-17 18:41:50 -04:00
setattr ( FetchTests , " testFetch %.3d _ %s " %
( idx , dname . replace ( " - " , " _ " ) ) , _make_fetch_cb ( dobj ) )
setattr ( InjectTests , " testInitrd %.3d _ %s " %
( idx , dname . replace ( " - " , " _ " ) ) , _make_check_cb ( dobj ) )
2013-09-28 14:42:37 -04:00
_make_tests ( )