More info when fail to get api version

When tempest verify-config incorrectly guesses the unversioned
endpoint (for example when keystone is accepting requests on a
subpath) it might get a response that's not JSON, in which point
all that it prints out is that the response isn't JSON. It would
be more useful if the URL that was requested was also printed so
that the user could see what was wrong.

Change-Id: Idb5f8ad9290569cf559bd2141b82075e6e0d6091
This commit is contained in:
Brant Knudson 2016-03-18 13:07:00 -05:00
parent e2644c0ee8
commit 5a59f8735d
2 changed files with 30 additions and 1 deletions

View File

@ -97,7 +97,14 @@ def _get_api_versions(os, service):
ca_certs=ca_certs)
__, body = raw_http.request(endpoint, 'GET')
client_dict[service].reset_path()
body = json.loads(body)
try:
body = json.loads(body)
except ValueError:
LOG.error(
'Failed to get a JSON response from unversioned endpoint %s '
'(versioned endpoint was %s). Response is:\n%s',
endpoint, client_dict[service].base_url, body[:100])
raise
if service == 'keystone':
versions = map(lambda x: x['id'], body['versions']['values'])
else:

View File

@ -82,6 +82,28 @@ class TestDiscovery(base.TestCase):
self.assertIn('v2.0', versions)
self.assertIn('v3.0', versions)
def test_get_versions_invalid_response(self):
# When the response doesn't contain a JSON response, an error is
# logged.
mock_log_error = self.useFixture(mockpatch.PatchObject(
verify_tempest_config.LOG, 'error')).mock
self.useFixture(mockpatch.PatchObject(
verify_tempest_config, '_get_unversioned_endpoint'))
# Simulated response is not JSON.
sample_body = (
'<html><head>Sample Response</head><body>This is the sample page '
'for the web server. Why are you requesting it?</body></html>')
self.useFixture(mockpatch.Patch('httplib2.Http.request',
return_value=(None, sample_body)))
# service value doesn't matter, just needs to match what
# _get_api_versions puts in its client_dict.
self.assertRaises(ValueError, verify_tempest_config._get_api_versions,
os=mock.MagicMock(), service='keystone')
self.assertTrue(mock_log_error.called)
def test_verify_api_versions(self):
api_services = ['cinder', 'glance', 'keystone']
fake_os = mock.MagicMock()