From 87ccbfd731a4ec6b4ff0ba65b19051b04cb6fe1b Mon Sep 17 00:00:00 2001 From: Joe Talbott Date: Thu, 5 Feb 2015 11:08:53 -0500 Subject: [PATCH] boottest - Add exception handling for manifest fetching --- boottest.py | 8 +++++++- tests/test_boottest.py | 8 ++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/boottest.py b/boottest.py index 3feae36..174001c 100644 --- a/boottest.py +++ b/boottest.py @@ -79,7 +79,13 @@ class TouchManifest(object): "I: [%s] - Fetching manifest from %s" % ( time.asctime(), url)) print("I: [%s] - saving it to %s" % (time.asctime(), self.path)) - response = urllib.urlopen(url) + try: + response = urllib.urlopen(url) + except IOError as e: + print("W: [%s] - error connecting to %s" % ( + time.asctime(), self.path)) + return success # failure + # Only [re]create the manifest file if one was successfully downloaded # this allows for an existing image to be used if the download fails. if response.code == 200: diff --git a/tests/test_boottest.py b/tests/test_boottest.py index 01772e8..1f7acfb 100644 --- a/tests/test_boottest.py +++ b/tests/test_boottest.py @@ -89,6 +89,14 @@ class TestTouchManifest(unittest.TestCase): self.assertEqual(1, len(manifest._manifest)) self.assertIn('foo', manifest) + def test_fetch_excpetion(self): + _p = mock.patch('urllib.urlopen') + mocked_urlopen = _p.start() + self.addCleanup(_p.stop) + mocked_urlopen.side_effect = [IOError("connection refused")] + manifest = boottest.TouchManifest('not-real', 'not-real') + self.assertEqual(0, len(manifest._manifest)) + def test_simple(self): # Existing manifest file allows callsites to properly check presence. manifest_dir = os.path.join(self.imagesdir, 'ubuntu/vivid')