From afeaec8f43c30fa06a47c19e6c926309f930a1a6 Mon Sep 17 00:00:00 2001 From: Olga Kopylova Date: Wed, 1 Apr 2015 15:27:29 +0300 Subject: [PATCH] Lazy import of keystoneclient in osclients * Move import from keystoneclient to create_keystone_client method in osclients. This is done to avoid loading keystoneclient on Rally start to make it starts faster. * Move import of exceptions from keystoneclient to verified_keystone method. * Add unit tests for function create_keystone_client Implements: blueprint lazy-imports Change-Id: Ie11618b380a9a51c5b6e237179b4da8b7178d16b --- rally/osclients.py | 8 +++--- tests/unit/test_osclients.py | 50 ++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 4 deletions(-) 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):