2013-09-05 12:54:14 -05:00
|
|
|
# Copyright 2012-2013 OpenStack Foundation
|
2013-01-24 12:00:30 -06:00
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
|
|
# not use this file except in compliance with the License. You may obtain
|
|
|
|
# a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
# License for the specific language governing permissions and limitations
|
|
|
|
# under the License.
|
|
|
|
#
|
2014-08-22 17:26:07 -05:00
|
|
|
import mock
|
2014-07-18 19:18:25 +02:00
|
|
|
from requests_mock.contrib import fixture
|
2014-08-22 17:26:07 -05:00
|
|
|
|
|
|
|
from keystoneclient.auth.identity import v2 as auth_v2
|
2014-07-18 19:18:25 +02:00
|
|
|
from keystoneclient.openstack.common import jsonutils
|
|
|
|
from keystoneclient import service_catalog
|
|
|
|
|
|
|
|
from openstackclient.api import auth
|
2012-05-02 17:02:08 -04:00
|
|
|
from openstackclient.common import clientmanager
|
2014-07-18 19:18:25 +02:00
|
|
|
from openstackclient.common import exceptions as exc
|
|
|
|
from openstackclient.tests import fakes
|
2013-06-30 23:01:17 -04:00
|
|
|
from openstackclient.tests import utils
|
2012-05-02 17:02:08 -04:00
|
|
|
|
|
|
|
|
2014-07-18 19:18:25 +02:00
|
|
|
API_VERSION = {"identity": "2.0"}
|
2014-08-08 17:38:44 -05:00
|
|
|
|
2014-07-18 19:18:25 +02:00
|
|
|
AUTH_REF = {'version': 'v2.0'}
|
|
|
|
AUTH_REF.update(fakes.TEST_RESPONSE_DICT['access'])
|
|
|
|
SERVICE_CATALOG = service_catalog.ServiceCatalogV2(AUTH_REF)
|
2013-09-05 12:54:14 -05:00
|
|
|
|
|
|
|
|
2012-05-02 17:02:08 -04:00
|
|
|
class Container(object):
|
2013-01-24 12:33:17 -06:00
|
|
|
attr = clientmanager.ClientCache(lambda x: object())
|
2012-05-02 17:02:08 -04:00
|
|
|
|
2013-01-24 12:33:17 -06:00
|
|
|
def __init__(self):
|
|
|
|
pass
|
2012-05-02 17:02:08 -04:00
|
|
|
|
|
|
|
|
2014-07-18 19:18:25 +02:00
|
|
|
class FakeOptions(object):
|
|
|
|
def __init__(self, **kwargs):
|
|
|
|
for option in auth.OPTIONS_LIST:
|
|
|
|
setattr(self, 'os_' + option.replace('-', '_'), None)
|
|
|
|
self.os_auth_plugin = None
|
|
|
|
self.os_identity_api_version = '2.0'
|
|
|
|
self.timing = None
|
|
|
|
self.os_region_name = None
|
|
|
|
self.os_url = None
|
|
|
|
self.__dict__.update(kwargs)
|
|
|
|
|
|
|
|
|
2014-08-08 17:38:44 -05:00
|
|
|
class TestClientCache(utils.TestCase):
|
|
|
|
|
|
|
|
def test_singleton(self):
|
|
|
|
# NOTE(dtroyer): Verify that the ClientCache descriptor only invokes
|
|
|
|
# the factory one time and always returns the same value after that.
|
|
|
|
c = Container()
|
|
|
|
self.assertEqual(c.attr, c.attr)
|
|
|
|
|
|
|
|
|
2013-01-24 12:33:17 -06:00
|
|
|
class TestClientManager(utils.TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
super(TestClientManager, self).setUp()
|
2014-07-18 19:18:25 +02:00
|
|
|
self.mock = mock.Mock()
|
|
|
|
self.requests = self.useFixture(fixture.Fixture())
|
|
|
|
# fake v2password token retrieval
|
|
|
|
self.stub_auth(json=fakes.TEST_RESPONSE_DICT)
|
|
|
|
# fake v3password token retrieval
|
|
|
|
self.stub_auth(json=fakes.TEST_RESPONSE_DICT_V3,
|
|
|
|
url='/'.join([fakes.AUTH_URL, 'auth/tokens']))
|
|
|
|
# fake password version endpoint discovery
|
|
|
|
self.stub_auth(json=fakes.TEST_VERSIONS,
|
|
|
|
url=fakes.AUTH_URL,
|
|
|
|
verb='GET')
|
|
|
|
|
|
|
|
def test_client_manager_token(self):
|
2014-08-08 17:38:44 -05:00
|
|
|
|
|
|
|
client_manager = clientmanager.ClientManager(
|
2014-07-18 19:18:25 +02:00
|
|
|
auth_options=FakeOptions(os_token=fakes.AUTH_TOKEN,
|
|
|
|
os_auth_url=fakes.AUTH_URL,
|
|
|
|
os_auth_plugin='v2token'),
|
2014-08-22 17:26:07 -05:00
|
|
|
api_version=API_VERSION,
|
2014-07-18 19:18:25 +02:00
|
|
|
verify=True
|
2014-08-08 17:38:44 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
self.assertEqual(
|
2014-07-18 19:18:25 +02:00
|
|
|
fakes.AUTH_TOKEN,
|
2014-08-08 17:38:44 -05:00
|
|
|
client_manager._token,
|
|
|
|
)
|
|
|
|
self.assertEqual(
|
2014-07-18 19:18:25 +02:00
|
|
|
fakes.AUTH_URL,
|
|
|
|
client_manager._auth_url,
|
2014-08-08 17:38:44 -05:00
|
|
|
)
|
|
|
|
self.assertIsInstance(
|
2014-08-22 17:26:07 -05:00
|
|
|
client_manager.auth,
|
|
|
|
auth_v2.Token,
|
2014-08-08 17:38:44 -05:00
|
|
|
)
|
|
|
|
self.assertFalse(client_manager._insecure)
|
|
|
|
self.assertTrue(client_manager._verify)
|
|
|
|
|
2014-07-18 19:18:25 +02:00
|
|
|
def test_client_manager_password(self):
|
2014-08-08 17:38:44 -05:00
|
|
|
|
|
|
|
client_manager = clientmanager.ClientManager(
|
2014-07-18 19:18:25 +02:00
|
|
|
auth_options=FakeOptions(os_auth_url=fakes.AUTH_URL,
|
|
|
|
os_username=fakes.USERNAME,
|
|
|
|
os_password=fakes.PASSWORD),
|
2014-08-22 17:26:07 -05:00
|
|
|
api_version=API_VERSION,
|
2014-07-18 19:18:25 +02:00
|
|
|
verify=False,
|
2013-09-05 12:54:14 -05:00
|
|
|
)
|
|
|
|
|
2014-08-08 17:38:44 -05:00
|
|
|
self.assertEqual(
|
2014-07-18 19:18:25 +02:00
|
|
|
fakes.AUTH_URL,
|
2014-08-08 17:38:44 -05:00
|
|
|
client_manager._auth_url,
|
|
|
|
)
|
|
|
|
self.assertEqual(
|
2014-07-18 19:18:25 +02:00
|
|
|
fakes.USERNAME,
|
2014-08-08 17:38:44 -05:00
|
|
|
client_manager._username,
|
|
|
|
)
|
|
|
|
self.assertEqual(
|
2014-07-18 19:18:25 +02:00
|
|
|
fakes.PASSWORD,
|
2014-08-08 17:38:44 -05:00
|
|
|
client_manager._password,
|
|
|
|
)
|
|
|
|
self.assertIsInstance(
|
2014-08-22 17:26:07 -05:00
|
|
|
client_manager.auth,
|
|
|
|
auth_v2.Password,
|
2014-08-08 17:38:44 -05:00
|
|
|
)
|
|
|
|
self.assertTrue(client_manager._insecure)
|
|
|
|
self.assertFalse(client_manager._verify)
|
2013-09-05 12:54:14 -05:00
|
|
|
|
2014-07-18 19:18:25 +02:00
|
|
|
# These need to stick around until the old-style clients are gone
|
|
|
|
self.assertEqual(
|
|
|
|
AUTH_REF,
|
|
|
|
client_manager.auth_ref,
|
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
fakes.AUTH_TOKEN,
|
|
|
|
client_manager._token,
|
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
dir(SERVICE_CATALOG),
|
|
|
|
dir(client_manager._service_catalog),
|
|
|
|
)
|
|
|
|
|
|
|
|
def stub_auth(self, json=None, url=None, verb=None, **kwargs):
|
|
|
|
subject_token = fakes.AUTH_TOKEN
|
|
|
|
base_url = fakes.AUTH_URL
|
|
|
|
if json:
|
|
|
|
text = jsonutils.dumps(json)
|
|
|
|
headers = {'X-Subject-Token': subject_token,
|
|
|
|
'Content-Type': 'application/json'}
|
|
|
|
if not url:
|
|
|
|
url = '/'.join([base_url, 'tokens'])
|
|
|
|
url = url.replace("/?", "?")
|
|
|
|
if not verb:
|
|
|
|
verb = 'POST'
|
|
|
|
self.requests.register_uri(verb,
|
|
|
|
url,
|
|
|
|
headers=headers,
|
|
|
|
text=text)
|
|
|
|
|
|
|
|
def test_client_manager_password_verify_ca(self):
|
2014-08-08 17:38:44 -05:00
|
|
|
|
|
|
|
client_manager = clientmanager.ClientManager(
|
2014-07-18 19:18:25 +02:00
|
|
|
auth_options=FakeOptions(os_auth_url=fakes.AUTH_URL,
|
|
|
|
os_username=fakes.USERNAME,
|
|
|
|
os_password=fakes.PASSWORD,
|
|
|
|
os_auth_plugin='v2password'),
|
2014-08-22 17:26:07 -05:00
|
|
|
api_version=API_VERSION,
|
2014-07-18 19:18:25 +02:00
|
|
|
verify='cafile',
|
2014-08-08 17:38:44 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
self.assertFalse(client_manager._insecure)
|
|
|
|
self.assertTrue(client_manager._verify)
|
|
|
|
self.assertEqual('cafile', client_manager._cacert)
|
2014-07-18 19:18:25 +02:00
|
|
|
|
|
|
|
def _client_manager_guess_auth_plugin(self, auth_params,
|
|
|
|
api_version, auth_plugin):
|
|
|
|
auth_params['os_auth_plugin'] = auth_plugin
|
|
|
|
auth_params['os_identity_api_version'] = api_version
|
|
|
|
client_manager = clientmanager.ClientManager(
|
|
|
|
auth_options=FakeOptions(**auth_params),
|
|
|
|
api_version=API_VERSION,
|
|
|
|
verify=True
|
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
auth_plugin,
|
|
|
|
client_manager._auth_plugin,
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_client_manager_guess_auth_plugin(self):
|
|
|
|
# test token auth
|
|
|
|
params = dict(os_token=fakes.AUTH_TOKEN,
|
|
|
|
os_auth_url=fakes.AUTH_URL)
|
|
|
|
self._client_manager_guess_auth_plugin(params, '2.0', 'v2token')
|
|
|
|
self._client_manager_guess_auth_plugin(params, '3', 'v3token')
|
|
|
|
self._client_manager_guess_auth_plugin(params, 'XXX', 'token')
|
|
|
|
# test service auth
|
|
|
|
params = dict(os_token=fakes.AUTH_TOKEN, os_url='test')
|
|
|
|
self._client_manager_guess_auth_plugin(params, 'XXX', '')
|
|
|
|
# test password auth
|
|
|
|
params = dict(os_auth_url=fakes.AUTH_URL,
|
|
|
|
os_username=fakes.USERNAME,
|
|
|
|
os_password=fakes.PASSWORD)
|
|
|
|
self._client_manager_guess_auth_plugin(params, '2.0', 'v2password')
|
|
|
|
self._client_manager_guess_auth_plugin(params, '3', 'v3password')
|
|
|
|
self._client_manager_guess_auth_plugin(params, 'XXX', 'password')
|
|
|
|
|
|
|
|
def test_client_manager_guess_auth_plugin_failure(self):
|
|
|
|
self.assertRaises(exc.CommandError,
|
|
|
|
clientmanager.ClientManager,
|
|
|
|
auth_options=FakeOptions(os_auth_plugin=''),
|
|
|
|
api_version=API_VERSION,
|
|
|
|
verify=True)
|