From ad13c57dc58cf89378fab9a121134745dcba3e30 Mon Sep 17 00:00:00 2001 From: Cole Robinson Date: Thu, 29 Mar 2018 19:22:28 -0400 Subject: [PATCH] urlfetch: Formalize the failed hostname check This heuristic is only valid for http connections, everything else will fail way earlier in the process --- tests/test_urls.py | 28 ++++++++++++++++++++++------ virtinst/urldetect.py | 2 +- virtinst/urlfetcher.py | 9 +++++++++ 3 files changed, 32 insertions(+), 7 deletions(-) diff --git a/tests/test_urls.py b/tests/test_urls.py index ce614e13..7ff9fb4b 100644 --- a/tests/test_urls.py +++ b/tests/test_urls.py @@ -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(): diff --git a/virtinst/urldetect.py b/virtinst/urldetect.py index bcd4477f..0120931b 100644 --- a/virtinst/urldetect.py +++ b/virtinst/urldetect.py @@ -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?")) diff --git a/virtinst/urlfetcher.py b/virtinst/urlfetcher.py index f204a171..5a0b05ec 100644 --- a/virtinst/urlfetcher.py +++ b/virtinst/urlfetcher.py @@ -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