Fix obscure error message when no template given to OSC

This is an internal-only fix, as it does not change any external APIs. A
bit of obfuscation was occurring in the
'template_utils.process_template_path' function call. First we try to
retrieve the template from a urllib call. If that fails, then we try to
make a request with the custom request_object function. If the
request_object function failed to retrieve the template as well, it
would report back 'ERROR: None'. I left both of the calls in this
function, but I made the first exception bubble up back to the caller,
since it is more helpful to the user.

The new error message should report back the following when a template
cannot be found:

<urlopen error [Errno 2] No such file or directory:
'/root/python-heatclient/test.yaml'>

Closed Bug: #1570229

Change-Id: I29e19172322c18559aa476744f7ff3530e689bd0
This commit is contained in:
Paul Breaux
2016-05-01 21:46:27 -06:00
parent 0586d59b5b
commit f5d4721e5f
2 changed files with 28 additions and 5 deletions

View File

@@ -31,15 +31,26 @@ def process_template_path(template_path, object_request=None, existing=False):
"""Read template from template path. """Read template from template path.
Attempt to read template first as a file or url. If that is unsuccessful, Attempt to read template first as a file or url. If that is unsuccessful,
try again to assuming path is to a template object. try again assuming path is to a template object.
:param template_path: local or uri path to template
:param object_request: custom object request function used to get template
if local or uri path fails
:param existing: if the current stack's template should be used
:returns: get_file dict and template contents
:raises: error.URLError
""" """
try: try:
return get_template_contents(template_file=template_path, return get_template_contents(template_file=template_path,
existing=existing) existing=existing)
except error.URLError: except error.URLError as template_file_exc:
return get_template_contents(template_object=template_path, try:
object_request=object_request, return get_template_contents(template_object=template_path,
existing=existing) object_request=object_request,
existing=existing)
except exc.HTTPNotFound:
# The initial exception gives the user better failure context.
raise template_file_exc
def get_template_contents(template_file=None, template_url=None, def get_template_contents(template_file=None, template_url=None,

View File

@@ -15,6 +15,7 @@ import base64
import json import json
from mox3 import mox from mox3 import mox
import six import six
from six.moves.urllib import error
from six.moves.urllib import request from six.moves.urllib import request
import tempfile import tempfile
import testtools import testtools
@@ -566,6 +567,17 @@ class TestGetTemplateContents(testtools.TestCase):
'Could not fetch template from file://%s' % tmpl_file.name, 'Could not fetch template from file://%s' % tmpl_file.name,
str(ex)) str(ex))
def test_get_template_file_nonextant(self):
nonextant_file = '/template/dummy/file/path/and/name.yaml'
ex = self.assertRaises(
error.URLError,
template_utils.get_template_contents,
nonextant_file)
self.assertEqual(
"<urlopen error [Errno 2] No such file or directory: '%s'>"
% nonextant_file,
str(ex))
def test_get_template_contents_file_none(self): def test_get_template_contents_file_none(self):
ex = self.assertRaises( ex = self.assertRaises(
exc.CommandError, exc.CommandError,