2020-01-27 18:44:09 +03:00
# This work is licensed under the GNU GPLv2 or later.
# See the COPYING file in the top-level directory.
#
import ftplib
import os
import urllib
from urllib . request import Request
import requests
from virtinst import log
2020-07-18 03:51:54 +03:00
_URLPREFIX = " https://virtinst-testsuite.example/ "
_MOCK_TOPDIR = os . path . dirname ( __file__ ) + " /data/urldetect/ "
2020-01-27 18:44:09 +03:00
2020-07-18 03:51:54 +03:00
def make_mock_input_url ( distro ) :
return _URLPREFIX + distro
def _map_mock_url_to_file ( url ) :
if url . startswith ( _URLPREFIX ) :
path = url [ len ( _URLPREFIX ) : ]
fn = _MOCK_TOPDIR + path
if not os . path . exists ( os . path . dirname ( fn ) ) :
raise RuntimeError ( " Expected mock file not found: %s " % fn )
elif url . endswith ( " treeinfo " ) :
2020-01-27 18:44:09 +03:00
# If the url is requesting treeinfo, give a fake treeinfo from
# our testsuite data
2020-09-09 00:48:40 +03:00
fn = ( " %s /data/fakemedia/fakerhel6tree/.treeinfo " %
2020-01-27 18:44:09 +03:00
os . path . abspath ( os . path . dirname ( __file__ ) ) )
else :
# Otherwise just copy this file
2020-07-18 03:51:54 +03:00
fn = os . path . abspath ( __file__ )
2020-01-27 18:44:09 +03:00
2020-07-18 03:51:54 +03:00
return os . path . abspath ( fn )
2020-01-27 18:44:09 +03:00
class _MockRequestsResponse :
def __init__ ( self , url ) :
log . debug ( " mocking requests session for url= %s " , url )
2020-07-18 03:51:54 +03:00
fn = _map_mock_url_to_file ( url )
2020-01-27 18:44:09 +03:00
self . _content = open ( fn ) . read ( )
self . headers = { ' content-length ' : len ( self . _content ) }
def raise_for_status ( self ) :
pass
def iter_content ( self , * args , * * kwargs ) :
dummy = args
dummy = kwargs
return [ self . _content . encode ( " utf-8 " ) ]
class _MockRequestsSession :
def close ( self ) :
pass
def head ( self , url , * args , * * kwargs ) :
dummy = args
dummy = kwargs
return _MockRequestsResponse ( url )
def get ( self , url , * args , * * kwargs ) :
dummy = args
dummy = kwargs
if " testsuitefail " in url :
raise RuntimeError ( " testsuitefail seen, raising mock error " )
return _MockRequestsResponse ( url )
class _MockFTPSession :
def connect ( self , * args , * * kwargs ) :
pass
def login ( self , * args , * * kwargs ) :
pass
def voidcmd ( self , * args , * * kwargs ) :
pass
def quit ( self , * args , * * kwargs ) :
pass
def size ( self , url ) :
2020-07-18 03:51:54 +03:00
path = _map_mock_url_to_file ( url )
2020-01-27 18:44:09 +03:00
return os . path . getsize ( path )
def _MockUrllibRequest ( url ) :
2020-07-18 03:51:54 +03:00
url = _map_mock_url_to_file ( url )
url = " file:// " + url
2020-01-27 18:44:09 +03:00
return Request ( url )
def setup_mock ( ) :
requests . Session = _MockRequestsSession
ftplib . FTP = _MockFTPSession
urllib . request . Request = _MockUrllibRequest