diff --git a/rally/osclients.py b/rally/osclients.py index 76956182d0..610af32061 100644 --- a/rally/osclients.py +++ b/rally/osclients.py @@ -15,10 +15,6 @@ import os -from keystoneclient import discover as keystone_discover -from keystoneclient import exceptions as keystone_exceptions -from keystoneclient.v2_0 import client as keystone_v2 -from keystoneclient.v3 import client as keystone_v3 from oslo_config import cfg from rally.common.i18n import _ @@ -58,12 +54,15 @@ def cached(func): def create_keystone_client(args): + from keystoneclient import discover as keystone_discover discover = keystone_discover.Discover(**args) for version_data in discover.version_data(): version = version_data["version"] if version[0] <= 2: + from keystoneclient.v2_0 import client as keystone_v2 return keystone_v2.Client(**args) elif version[0] == 3: + from keystoneclient.v3 import client as keystone_v3 return keystone_v3.Client(**args) raise exceptions.RallyException( "Failed to discover keystone version for url %(auth_url)s.", **args) @@ -110,6 +109,7 @@ class Clients(object): :returns: Keystone Client """ + from keystoneclient import exceptions as keystone_exceptions try: # Ensure that user is admin client = self.keystone() diff --git a/tests/unit/test_osclients.py b/tests/unit/test_osclients.py index 3b52d3cb89..69ef324aaf 100644 --- a/tests/unit/test_osclients.py +++ b/tests/unit/test_osclients.py @@ -26,6 +26,56 @@ from tests.unit import fakes from tests.unit import test +class TestCreateKeystoneClient(test.TestCase): + + def setUp(self): + super(TestCreateKeystoneClient, self).setUp() + self.kwargs = {"auth_url": "http://auth_url", "username": "user", + "password": "password", "tenant_name": "tenant"} + + def test_create_keystone_client_v2(self): + mock_keystone = mock.MagicMock() + fake_keystoneclient = mock.MagicMock() + mock_keystone.v2_0.client.Client.return_value = fake_keystoneclient + mock_discover = mock.MagicMock( + version_data=mock.MagicMock(return_value=[{"version": [2]}])) + mock_keystone.discover.Discover.return_value = mock_discover + with mock.patch.dict("sys.modules", + {"keystoneclient": mock_keystone, + "keystoneclient.v2_0": mock_keystone.v2_0}): + client = osclients.create_keystone_client(self.kwargs) + mock_discover.version_data.assert_called_once_with() + self.assertEqual(fake_keystoneclient, client) + mock_keystone.v2_0.client.Client.assert_called_once_with( + **self.kwargs) + + def test_create_keystone_client_v3(self): + mock_keystone = mock.MagicMock() + fake_keystoneclient = mock.MagicMock() + mock_keystone.v3.client.Client.return_value = fake_keystoneclient + mock_discover = mock.MagicMock( + version_data=mock.MagicMock(return_value=[{"version": [3]}])) + mock_keystone.discover.Discover.return_value = mock_discover + with mock.patch.dict("sys.modules", + {"keystoneclient": mock_keystone, + "keystoneclient.v3": mock_keystone.v3}): + client = osclients.create_keystone_client(self.kwargs) + mock_discover.version_data.assert_called_once_with() + self.assertEqual(fake_keystoneclient, client) + mock_keystone.v3.client.Client.assert_called_once_with( + **self.kwargs) + + def test_create_keystone_client_version_not_found(self): + mock_keystone = mock.MagicMock() + mock_discover = mock.MagicMock( + version_data=mock.MagicMock(return_value=[{"version": [100500]}])) + mock_keystone.discover.Discover.return_value = mock_discover + with mock.patch.dict("sys.modules", {"keystoneclient": mock_keystone}): + self.assertRaises(exceptions.RallyException, + osclients.create_keystone_client, self.kwargs) + mock_discover.version_data.assert_called_once_with() + + class OSClientsTestCase(test.TestCase): def setUp(self):