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
This commit is contained in:
Olga Kopylova 2015-04-01 15:27:29 +03:00
parent c6484daca1
commit afeaec8f43
2 changed files with 54 additions and 4 deletions

View File

@ -15,10 +15,6 @@
import os 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 oslo_config import cfg
from rally.common.i18n import _ from rally.common.i18n import _
@ -58,12 +54,15 @@ def cached(func):
def create_keystone_client(args): def create_keystone_client(args):
from keystoneclient import discover as keystone_discover
discover = keystone_discover.Discover(**args) discover = keystone_discover.Discover(**args)
for version_data in discover.version_data(): for version_data in discover.version_data():
version = version_data["version"] version = version_data["version"]
if version[0] <= 2: if version[0] <= 2:
from keystoneclient.v2_0 import client as keystone_v2
return keystone_v2.Client(**args) return keystone_v2.Client(**args)
elif version[0] == 3: elif version[0] == 3:
from keystoneclient.v3 import client as keystone_v3
return keystone_v3.Client(**args) return keystone_v3.Client(**args)
raise exceptions.RallyException( raise exceptions.RallyException(
"Failed to discover keystone version for url %(auth_url)s.", **args) "Failed to discover keystone version for url %(auth_url)s.", **args)
@ -110,6 +109,7 @@ class Clients(object):
:returns: Keystone Client :returns: Keystone Client
""" """
from keystoneclient import exceptions as keystone_exceptions
try: try:
# Ensure that user is admin # Ensure that user is admin
client = self.keystone() client = self.keystone()

View File

@ -26,6 +26,56 @@ from tests.unit import fakes
from tests.unit import test 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): class OSClientsTestCase(test.TestCase):
def setUp(self): def setUp(self):