From 2634f6e8cea9b4c9394d8537b299295b73878cf3 Mon Sep 17 00:00:00 2001 From: yanyanhu Date: Tue, 24 Nov 2015 01:04:08 -0500 Subject: [PATCH] Reimplement authenticate method in sdk driver This patch reimplements authenticate method in senlin sdk driver to use keystoneauth session interface to get access information. Change-Id: I88912a8f868fb40ec6562ed94ad7dbe11c2a8f0e --- senlin/drivers/openstack/keystone_v3.py | 6 ++-- senlin/drivers/openstack/sdk.py | 11 +++--- senlin/tests/unit/drivers/test_keystone_v3.py | 8 ++--- senlin/tests/unit/drivers/test_sdk.py | 35 +++++-------------- 4 files changed, 21 insertions(+), 39 deletions(-) diff --git a/senlin/drivers/openstack/keystone_v3.py b/senlin/drivers/openstack/keystone_v3.py index 4d3025aaa..3dc03c5a1 100644 --- a/senlin/drivers/openstack/keystone_v3.py +++ b/senlin/drivers/openstack/keystone_v3.py @@ -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): diff --git a/senlin/drivers/openstack/sdk.py b/senlin/drivers/openstack/sdk.py index b608f822a..56f02803a 100644 --- a/senlin/drivers/openstack/sdk.py +++ b/senlin/drivers/openstack/sdk.py @@ -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 diff --git a/senlin/tests/unit/drivers/test_keystone_v3.py b/senlin/tests/unit/drivers/test_keystone_v3.py index 4da23cbd7..dda1a34a4 100644 --- a/senlin/tests/unit/drivers/test_keystone_v3.py +++ b/senlin/tests/unit/drivers/test_keystone_v3.py @@ -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', diff --git a/senlin/tests/unit/drivers/test_sdk.py b/senlin/tests/unit/drivers/test_sdk.py index 7544d85fd..7ec3b3b58 100644 --- a/senlin/tests/unit/drivers/test_sdk.py +++ b/senlin/tests/unit/drivers/test_sdk.py @@ -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)