Merge "Reimplement authenticate method in sdk driver"
This commit is contained in:
commit
e8276124c0
@ -141,8 +141,7 @@ class KeystoneClient(base.DriverBase):
|
||||
'''Get token using given credential'''
|
||||
|
||||
access_info = sdk.authenticate(**creds)
|
||||
token = access_info.auth_token
|
||||
return token
|
||||
return access_info['token']
|
||||
|
||||
@classmethod
|
||||
@sdk.translate_exception
|
||||
@ -150,8 +149,7 @@ class KeystoneClient(base.DriverBase):
|
||||
'''Get ID of the user with given creddential'''
|
||||
|
||||
access_info = sdk.authenticate(**creds)
|
||||
user_id = access_info.user_id
|
||||
return user_id
|
||||
return access_info['user_id']
|
||||
|
||||
@classmethod
|
||||
def get_service_credentials(cls, **kwargs):
|
||||
|
@ -110,10 +110,11 @@ def create_connection(params=None):
|
||||
def authenticate(**kwargs):
|
||||
'''Authenticate using openstack sdk based on user credential'''
|
||||
|
||||
try:
|
||||
conn = create_connection(kwargs)
|
||||
access_info = conn.session.authenticator.authorize(conn.transport)
|
||||
except Exception as ex:
|
||||
raise parse_exception(ex)
|
||||
conn = create_connection(kwargs)
|
||||
access_info = {
|
||||
'token': conn.session.get_token(),
|
||||
'user_id': conn.session.get_user_id(),
|
||||
'project_id': conn.session.get_project_id()
|
||||
}
|
||||
|
||||
return access_info
|
||||
|
@ -223,23 +223,23 @@ class TestKeystoneV3(base.SenlinTestCase):
|
||||
|
||||
@mock.patch.object(sdk, 'authenticate')
|
||||
def test_get_token(self, mock_auth, mock_create):
|
||||
access_info = mock.Mock()
|
||||
access_info = {'token': '123', 'user_id': 'abc', 'project_id': 'xyz'}
|
||||
mock_auth.return_value = access_info
|
||||
|
||||
token = kv3.KeystoneClient.get_token(key='value')
|
||||
|
||||
mock_auth.assert_called_once_with(key='value')
|
||||
self.assertEqual(access_info.auth_token, token)
|
||||
self.assertEqual('123', token)
|
||||
|
||||
@mock.patch.object(sdk, 'authenticate')
|
||||
def test_get_user_id(self, mock_auth, mock_create):
|
||||
access_info = mock.Mock()
|
||||
access_info = {'token': '123', 'user_id': 'abc', 'project_id': 'xyz'}
|
||||
mock_auth.return_value = access_info
|
||||
|
||||
user_id = kv3.KeystoneClient.get_user_id(key='value')
|
||||
|
||||
mock_auth.assert_called_once_with(key='value')
|
||||
self.assertEqual(access_info.user_id, user_id)
|
||||
self.assertEqual('abc', user_id)
|
||||
|
||||
def test_get_service_credentials(self, mock_create):
|
||||
cfg.CONF.set_override('auth_url', 'FAKE_URL', group='authentication',
|
||||
|
@ -186,33 +186,16 @@ class OpenStackSDKTest(base.SenlinTestCase):
|
||||
@mock.patch.object(sdk, 'create_connection')
|
||||
def test_authenticate(self, mock_conn):
|
||||
x_conn = mock_conn.return_value
|
||||
x_transport = mock.Mock()
|
||||
x_conn.transport = x_transport
|
||||
x_access_info = mock.Mock()
|
||||
mock_authorize = mock.Mock(return_value=x_access_info)
|
||||
x_conn.session.authenticator.authorize = mock_authorize
|
||||
x_conn.session.get_token.return_value = 'TOKEN'
|
||||
x_conn.session.get_user_id.return_value = 'test-user-id'
|
||||
x_conn.session.get_project_id.return_value = 'test-project-id'
|
||||
access_info = {
|
||||
'token': 'TOKEN',
|
||||
'user_id': 'test-user-id',
|
||||
'project_id': 'test-project-id'
|
||||
}
|
||||
|
||||
res = sdk.authenticate(foo='bar')
|
||||
|
||||
self.assertEqual(x_access_info, res)
|
||||
self.assertEqual(access_info, res)
|
||||
mock_conn.assert_called_once_with({'foo': 'bar'})
|
||||
mock_authorize.assert_called_once_with(x_transport)
|
||||
|
||||
@mock.patch.object(sdk, 'create_connection')
|
||||
@mock.patch.object(sdk, 'parse_exception')
|
||||
def test_authenticate_with_exception(self, mock_parse, mock_conn):
|
||||
x_conn = mock_conn.return_value
|
||||
x_transport = mock.Mock()
|
||||
x_conn.transport = x_transport
|
||||
error = Exception('test exception')
|
||||
x_conn.session.authenticator.authorize.side_effect = error
|
||||
mock_parse.side_effect = senlin_exc.InternalError(code=123,
|
||||
message='BOOM')
|
||||
|
||||
ex = self.assertRaises(senlin_exc.InternalError,
|
||||
sdk.authenticate, foo='bar')
|
||||
|
||||
mock_conn.assert_called_once_with({'foo': 'bar'})
|
||||
mock_parse.assert_called_once_with(error)
|
||||
self.assertEqual(123, ex.code)
|
||||
self.assertEqual('BOOM', ex.message)
|
||||
|
Loading…
Reference in New Issue
Block a user