From 6c14d8e55de0c3045ee37e7d37c963cc35ade21d Mon Sep 17 00:00:00 2001 From: Liang Chen Date: Wed, 21 Aug 2013 23:24:27 +0800 Subject: [PATCH] Process request exceptions while fetching template requests raises subclass of requests.exception.RequestException, not IOError. And the code explicitly calls raise_for_status to avoid HTTP errors being silently ignored. Fixes bug #1214429 Change-Id: Ib3ea7e4fea459a231f0bb7f113e3d66983b006c2 --- heat/common/urlfetch.py | 8 +++++++- heat/tests/test_urlfetch.py | 15 ++++++++++++++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/heat/common/urlfetch.py b/heat/common/urlfetch.py index 05ef8bb2..623ce148 100644 --- a/heat/common/urlfetch.py +++ b/heat/common/urlfetch.py @@ -18,6 +18,7 @@ Utility for fetching a resource (e.g. a template) from a URL. ''' import requests +from requests import exceptions import urlparse from heat.openstack.common import log as logging @@ -40,4 +41,9 @@ def get(url): if components.scheme not in ('http', 'https'): raise IOError('Invalid URL scheme %s' % components.scheme) - return requests.get(url).text + try: + resp = requests.get(url) + resp.raise_for_status() + return resp.text + except exceptions.RequestException as ex: + raise IOError('Failed to retrieve template: %s' % str(ex)) diff --git a/heat/tests/test_urlfetch.py b/heat/tests/test_urlfetch.py index 7cf29533..776d8d8f 100644 --- a/heat/tests/test_urlfetch.py +++ b/heat/tests/test_urlfetch.py @@ -14,6 +14,7 @@ # under the License. import requests +from requests import exceptions from heat.common import urlfetch from heat.tests.common import HeatTestCase @@ -27,6 +28,9 @@ class Response: def text(self): return self._text + def raise_for_status(self): + pass + class UrlFetchTest(HeatTestCase): def setUp(self): @@ -61,7 +65,16 @@ class UrlFetchTest(HeatTestCase): def test_http_error(self): url = 'http://example.com/template' - requests.get(url).AndRaise(IOError('fubar')) + requests.get(url).AndRaise(exceptions.HTTPError()) + self.m.ReplayAll() + + self.assertRaises(IOError, urlfetch.get, url) + self.m.VerifyAll() + + def test_non_exist_url(self): + url = 'http://non-exist.com/template' + + requests.get(url).AndRaise(exceptions.Timeout()) self.m.ReplayAll() self.assertRaises(IOError, urlfetch.get, url) -- 2.45.2