urlfetch: Formalize the failed hostname check

This heuristic is only valid for http connections, everything
else will fail way earlier in the process
This commit is contained in:
Cole Robinson 2018-03-29 19:22:28 -04:00
parent 70820790ec
commit ad13c57dc5
3 changed files with 32 additions and 7 deletions

View File

@ -229,6 +229,15 @@ def _testURL(fetcher, testdata):
(distname, arch))
def _fetchWrapper(url, cb):
fetcher = urlfetcher.fetcherForURI(url, "/tmp", meter)
try:
fetcher.prepareLocation()
return cb(fetcher)
finally:
fetcher.cleanupLocation()
def _testURLWrapper(testdata):
os.environ.pop("VIRTINST_TEST_SUITE", None)
@ -238,17 +247,24 @@ def _testURLWrapper(testdata):
sys.stdout.write("\nTesting %-25s " % testdata.name)
sys.stdout.flush()
fetcher = urlfetcher.fetcherForURI(testdata.url, "/tmp", meter)
try:
fetcher.prepareLocation()
def cb(fetcher):
return _testURL(fetcher, testdata)
finally:
fetcher.cleanupLocation()
return _fetchWrapper(testdata.url, cb)
# Register tests to be picked up by unittest
class URLTests(unittest.TestCase):
pass
def test001BadURL(self):
badurl = "http://aksdkakskdfa-idontexist.com/foo/tree"
def cb(fetcher):
return _storeForDistro(fetcher, hvmguest)
try:
_fetchWrapper(badurl, cb)
raise AssertionError("Expected URL failure")
except ValueError as e:
self.assertTrue("maybe you mistyped" in str(e))
def _make_tests():

View File

@ -234,7 +234,7 @@ def getDistroStore(guest, fetcher):
# be true since some webservers don't allow directory listing.
# http://www.redhat.com/archives/virt-tools-list/2014-December/msg00048.html
extramsg = ""
if not fetcher.hasFile(""):
if not fetcher.can_access():
extramsg = (": " +
_("The URL could not be accessed, maybe you mistyped?"))

View File

@ -114,6 +114,12 @@ class _URLFetcher(object):
"""
pass
def can_access(self):
"""
Return True if the location URL seems to be valid
"""
return True
def _hasFile(self, url):
raise NotImplementedError("Must be implemented in subclass")
@ -169,6 +175,9 @@ class _HTTPURLFetcher(_URLFetcher):
logging.debug("Error closing requests.session", exc_info=True)
self._session = None
def can_access(self):
return self.hasFile("")
def _hasFile(self, url):
"""
We just do a HEAD request to see if the file exists