From 87ccbfd731a4ec6b4ff0ba65b19051b04cb6fe1b Mon Sep 17 00:00:00 2001 From: Joe Talbott Date: Thu, 5 Feb 2015 11:08:53 -0500 Subject: [PATCH 1/3] 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') From f0a55936f513b7e1b1022207e2d9206d9c261ca6 Mon Sep 17 00:00:00 2001 From: Joe Talbott Date: Thu, 5 Feb 2015 11:20:28 -0500 Subject: [PATCH 2/3] boottest - Add exception message to warning print and fix typo --- boottest.py | 4 ++-- tests/test_boottest.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/boottest.py b/boottest.py index 174001c..2f72e9e 100644 --- a/boottest.py +++ b/boottest.py @@ -82,8 +82,8 @@ class TouchManifest(object): try: response = urllib.urlopen(url) except IOError as e: - print("W: [%s] - error connecting to %s" % ( - time.asctime(), self.path)) + print("W: [%s] - error connecting to %s: %s" % ( + time.asctime(), self.path, e)) return success # failure # Only [re]create the manifest file if one was successfully downloaded diff --git a/tests/test_boottest.py b/tests/test_boottest.py index 1f7acfb..03d0ece 100644 --- a/tests/test_boottest.py +++ b/tests/test_boottest.py @@ -89,7 +89,7 @@ class TestTouchManifest(unittest.TestCase): self.assertEqual(1, len(manifest._manifest)) self.assertIn('foo', manifest) - def test_fetch_excpetion(self): + def test_fetch_exception(self): _p = mock.patch('urllib.urlopen') mocked_urlopen = _p.start() self.addCleanup(_p.stop) From 6269f50abccf31a78784cb0842a85b5d45f2de39 Mon Sep 17 00:00:00 2001 From: Joe Talbott Date: Thu, 5 Feb 2015 17:27:39 -0500 Subject: [PATCH 3/3] Don't bother creating another mocked urllib.urlopen. --- tests/test_boottest.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/tests/test_boottest.py b/tests/test_boottest.py index 03d0ece..3582415 100644 --- a/tests/test_boottest.py +++ b/tests/test_boottest.py @@ -90,10 +90,7 @@ class TestTouchManifest(unittest.TestCase): self.assertIn('foo', manifest) def test_fetch_exception(self): - _p = mock.patch('urllib.urlopen') - mocked_urlopen = _p.start() - self.addCleanup(_p.stop) - mocked_urlopen.side_effect = [IOError("connection refused")] + self.mocked_urlopen.side_effect = [IOError("connection refused")] manifest = boottest.TouchManifest('not-real', 'not-real') self.assertEqual(0, len(manifest._manifest))