Add guard checks in _get_cpid_from_keystone.

Closes-Bug: #1502949

Change-Id: I8e4bea2b5ab0e60d92fc07a5f0cfa60a7f84732d
This commit is contained in:
david liu 2015-10-19 11:29:29 +08:00
parent 84e4ef114d
commit c39c3c7981
2 changed files with 73 additions and 3 deletions
refstack_client

@ -189,7 +189,15 @@ class RefstackClient:
token = client.auth_ref
for service in token['serviceCatalog']:
if service['type'] == 'identity':
return service['endpoints'][0]['id']
if 'endpoints' in service and \
len(service['endpoints']) > 0:
return service['endpoints'][0]['id']
else:
message = "Unable to retrieve CPID. " + \
"Identity service endpoint was " + \
"not found in Keystone v2 catalog."
self.logger.error(message)
raise RuntimeError(message)
elif auth_version == 'v3':
args['auth_url'] = conf_file.get('identity', 'uri_v3')
if conf_file.has_option('identity', 'domain_name'):
@ -203,8 +211,15 @@ class RefstackClient:
client = ksclient3.Client(**args)
token = client.auth_ref
for service in token['catalog']:
if service['type'] == 'identity':
return service['id']
if service['type'] == 'identity' and \
'id' in service and service['id'] is not None:
return service['id']
else:
message = "Unable to retrive CPID. " + \
"Identity service ID was not " + \
"found in Keystone v3 catalog."
self.logger.error(message)
raise RuntimeError(message)
else:
raise ValueError('Auth_version %s is unsupported'
'' % auth_version)

@ -93,6 +93,33 @@ class TestRefstackClient(unittest.TestCase):
return_value=self.mock_ks3_client
)
def mock_keystone_with_wrong_service(self):
"""
Mock the Keystone client methods.
"""
self.mock_identity_service_v2 = {'type': 'identity',
'endpoints': []}
self.mock_identity_service_v3 = {'type': 'identity',
'id': None}
self.mock_ks2_client = MagicMock(
name='ks_client',
**{'auth_ref':
{'serviceCatalog': [self.mock_identity_service_v2]}}
)
self.mock_ks3_client = MagicMock(
name='ks_client',
**{'auth_ref':
{'catalog': [self.mock_identity_service_v3]}}
)
self.ks2_client_builder = self.patch(
'refstack_client.refstack_client.ksclient2.Client',
return_value=self.mock_ks2_client
)
self.ks3_client_builder = self.patch(
'refstack_client.refstack_client.ksclient3.Client',
return_value=self.mock_ks3_client
)
def setUp(self):
"""
Test case setup
@ -315,6 +342,34 @@ class TestRefstackClient(unittest.TestCase):
)
self.assertEqual('test-id', cpid)
def test_get_cpid_from_keystone_v2_exits(self):
"""
Test getting the CPID from keystone API v2 exits with wrong service.
"""
argv = self.mock_argv()
args = rc.parse_cli_args(argv)
client = rc.RefstackClient(args)
client.tempest_dir = self.test_path
client._prep_test()
self.mock_keystone_with_wrong_service()
with self.assertRaises(RuntimeError):
client._get_cpid_from_keystone(client.conf)
def test_get_cpid_from_keystone_v3_exits(self):
"""
Test getting the CPID from keystone API v3 exits.
"""
args = rc.parse_cli_args(self.mock_argv())
client = rc.RefstackClient(args)
client.tempest_dir = self.test_path
client._prep_test()
client.conf.remove_option('identity', 'tenant_id')
client.conf.set('identity', 'tenant_name', 'tenant_name')
client.conf.set('identity-feature-enabled', 'api_v3', 'true')
self.mock_keystone_with_wrong_service()
with self.assertRaises(RuntimeError):
client._get_cpid_from_keystone(client.conf)
def test_form_result_content(self):
"""
Test that the request content is formed into the expected format.