Merge "Revert "Make Rally cope with unversioned keystone URL""

This commit is contained in:
Jenkins 2016-02-18 21:50:17 +00:00 committed by Gerrit Code Review
commit 3b766659ff
2 changed files with 62 additions and 87 deletions

View File

@ -144,12 +144,13 @@ class OSClient(plugin.Plugin):
return keystone(*args, **kwargs)
def _get_session(self, auth=None, endpoint=None):
endpoint = endpoint or self._get_endpoint()
from keystoneclient.auth import token_endpoint
from keystoneclient import session as ks_session
kc = self.keystone()
if auth is None:
endpoint = endpoint or self._get_endpoint()
kc = self.keystone()
auth = token_endpoint.Token(endpoint, kc.auth_token)
return ks_session.Session(auth=auth, verify=self.credential.insecure,
@ -213,22 +214,20 @@ class Keystone(OSClient):
raise exceptions.RallyException(_("Method 'keystone' is restricted "
"for keystoneclient. :)"))
def _create_keystone_client(self, args):
from keystoneclient.auth import identity
from keystoneclient import client
auth_arg_list = [
"username", "project_name", "tenant_name", "auth_url",
"password",
]
# NOTE(bigjools): If forcing a v2.0 URL then you cannot specify
# domain-related info, or the service discovery will fail.
if "v2.0" not in args["auth_url"]:
auth_arg_list.extend(
["user_domain_name", "domain_name", "project_domain_name"])
auth_args = {key: args.get(key) for key in auth_arg_list}
auth = identity.Password(**auth_args)
args["session"] = self._get_session(auth=auth)
return client.Client(**args)
@staticmethod
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)
def create_client(self):
"""Return keystone client."""

View File

@ -78,78 +78,54 @@ class CachedTestCase(test.TestCase):
class TestCreateKeystoneClient(test.TestCase):
def make_auth_args(self):
auth_kwargs = {
"auth_url": "http://auth_url", "username": "user",
"password": "password", "tenant_name": "tenant",
"domain_name": "domain", "project_name": "project_name",
"project_domain_name": "project_domain_name",
"user_domain_name": "user_domain_name",
}
kwargs = {"https_insecure": False, "https_cacert": None}
kwargs.update(auth_kwargs)
return auth_kwargs, kwargs
def setUp(self):
super(TestCreateKeystoneClient, self).setUp()
self.kwargs = {"auth_url": "http://auth_url", "username": "user",
"password": "password", "tenant_name": "tenant",
"https_insecure": False, "https_cacert": None}
def set_up_keystone_mocks(self):
self.ksc_module = mock.MagicMock()
self.ksc_client = mock.MagicMock()
self.ksc_identity = mock.MagicMock()
self.ksc_password = mock.MagicMock()
self.ksc_session = mock.MagicMock()
self.ksc_auth = mock.MagicMock()
self.patcher = mock.patch.dict("sys.modules",
{"keystoneclient": self.ksc_module,
"keystoneclient.auth": self.ksc_auth})
self.patcher.start()
self.addCleanup(self.patcher.stop)
self.ksc_module.client = self.ksc_client
self.ksc_auth.identity = self.ksc_identity
self.ksc_auth.identity.Password = self.ksc_password
self.ksc_module.session = self.ksc_session
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.Keystone._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(self):
# NOTE(bigjools): This is a very poor testing strategy as it
# tightly couples the test implementation to the tested
# function's implementation. Ideally, we'd use a fake keystone
# but all that's happening here is that it's checking the right
# parameters were passed to the various parts that create a
# client. Maybe one day we'll get a test double from the
# keystone guys.
self.set_up_keystone_mocks()
auth_kwargs, all_kwargs = self.make_auth_args()
keystone = osclients.Keystone(
mock.MagicMock(), mock.sentinel, mock.sentinel)
client = keystone._create_keystone_client(all_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.Keystone._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)
self.ksc_password.assert_called_once_with(**auth_kwargs)
self.ksc_session.Session.assert_called_once_with(
auth=self.ksc_identity.Password(), timeout=mock.ANY,
verify=mock.ANY)
self.ksc_client.Client.assert_called_once_with(**all_kwargs)
self.assertIs(client, self.ksc_client.Client())
def test_create_keystone_client_with_v2_url_omits_domain(self):
# NOTE(bigjools): Test that domain-related info is not present
# when forcing a v2 URL, because it breaks keystoneclient's
# service discovery.
self.set_up_keystone_mocks()
auth_kwargs, all_kwargs = self.make_auth_args()
all_kwargs["auth_url"] = "http://auth_url/v2.0"
auth_kwargs["auth_url"] = all_kwargs["auth_url"]
keystone = osclients.Keystone(
mock.MagicMock(), mock.sentinel, mock.sentinel)
client = keystone._create_keystone_client(all_kwargs)
auth_kwargs.pop("user_domain_name")
auth_kwargs.pop("project_domain_name")
auth_kwargs.pop("domain_name")
self.ksc_password.assert_called_once_with(**auth_kwargs)
self.ksc_session.Session.assert_called_once_with(
auth=self.ksc_identity.Password(), timeout=mock.ANY,
verify=mock.ANY)
self.ksc_client.Client.assert_called_once_with(**all_kwargs)
self.assertIs(client, self.ksc_client.Client())
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.Keystone._create_keystone_client,
self.kwargs)
mock_discover.version_data.assert_called_once_with()
@ddt.ddt