Implement base classes for Openstack clients

Change-Id: I766f952ec19660bf24d6ee37fe76b15a94324206
changes/17/644817/8
Federico Ressi 4 years ago
parent eae2951725
commit c5bddd49bb

@ -0,0 +1,90 @@
# Copyright 2019 Red Hat
#
# 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.
from __future__ import absolute_import
import abc
import fixtures
from oslo_log import log
import tobiko
from tobiko.openstack import keystone
LOG = log.getLogger(__name__)
class OpenstackClientFixture(tobiko.SharedFixture):
client = None
session = None
session_fixture = None
def __init__(self, session=None):
super(OpenstackClientFixture, self).__init__()
if session:
if tobiko.is_fixture(session):
self.session_fixture = session
else:
self.session = session
def setup_fixture(self):
self.setup_session()
self.setup_client()
def setup_session(self):
session_fixture = self.session_fixture
if session_fixture:
if not isinstance(session_fixture, fixtures.Fixture):
session_fixture = tobiko.get_fixture(session_fixture)
session_fixture.setUp()
self.session = session_fixture.session
elif not self.session:
self.session = keystone.get_keystone_session()
def setup_client(self):
self.client = self.init_client(session=self.session)
@abc.abstractmethod
def init_client(self, session):
raise NotImplementedError
class OpenstackClientManager(object):
def __init__(self, init_client=None):
self._clients = {}
self.init_client = init_client
def get_client(self, session=None, shared=True, init_client=None):
if shared:
if session and tobiko.is_fixture(session):
key = tobiko.get_fixture_name(session)
else:
key = session
client = self._clients.get(key)
if client:
return client
session = session or keystone.get_keystone_session()
init_client = init_client or self.init_client
assert callable(init_client)
LOG.debug('Initialize OpenStack client: %r(session=%r)',
init_client, session)
client = init_client(session=session)
if shared:
self._clients[key] = client
return client

@ -0,0 +1,149 @@
# Copyright (c) 2019 Red Hat
# All Rights Reserved.
#
# 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.
from __future__ import absolute_import
import inspect
import mock
import tobiko
from tobiko.openstack import client as _client
from tobiko.openstack import keystone
from tobiko.tests.openstack import base
CLIENT = object()
class ClientFixture(_client.OpenstackClientFixture):
def init_client(self, session=None):
return CLIENT
SESSION = object()
DEFAULT_SESSION = object()
class SessionFixture(tobiko.SharedFixture):
session = None
def setup_fixture(self):
self.session = SESSION
class OpenstackClientFixtureTest(base.OpenstackTest):
def create_client(self, session=None):
return ClientFixture(session=session)
def test_init(self, session=None):
client = self.create_client(session=session)
self.check_client_session(client=client, session=session)
def test_init_with_credentials(self):
self.test_init(session=SESSION)
def test_init_with_credentials_fixture(self):
self.test_init(session=keystone.KeystoneSessionFixture())
def test_init_with_credentials_fixture_type(self):
self.test_init(session=keystone.KeystoneSessionFixture)
def test_setup(self, session=None):
client = self.create_client(session=session)
client.setUp()
if session:
if tobiko.is_fixture(session):
if inspect.isclass(session):
session = tobiko.get_fixture(session)
self.assertIs(session.session, client.session)
else:
self.assertIs(session, client.session)
else:
self.assertIs(keystone.get_keystone_session(), client.session)
def test_setup_with_session(self):
self.test_setup(session=SESSION)
def test_setup_with_session_fixture(self):
self.test_setup(session=SessionFixture())
def test_setup_with_session_fixture_type(self):
self.test_setup(session=SessionFixture)
def check_client_session(self, client, session):
if session:
if tobiko.is_fixture(session):
self.assertIsNone(client.session)
self.assertIs(session, client.session_fixture)
else:
self.assertIs(session, client.session)
self.assertIsNone(client.session_fixture)
else:
self.assertIsNone(client.session)
self.assertIsNone(client.session_fixture)
class OpenstackClientManagerTest(base.OpenstackTest):
def setUp(self):
super(OpenstackClientManagerTest, self).setUp()
self.patch('tobiko.openstack.keystone.get_keystone_session',
return_value=DEFAULT_SESSION)
def test_init(self, init_client=None):
manager = _client.OpenstackClientManager(init_client=init_client)
self.assertIs(init_client, manager.init_client)
def test_init_with_init_client(self):
self.test_init(init_client=ClientFixture)
def test_get_client(self, session=None, shared=True):
default_init_client = mock.MagicMock(side_effect=ClientFixture)
manager = _client.OpenstackClientManager(
init_client=default_init_client)
client1 = manager.get_client(session=session, shared=shared)
client2 = manager.get_client(session=session, shared=shared)
if shared:
self.assertIs(client1, client2)
default_init_client.assert_called_once_with(
session=(session or DEFAULT_SESSION))
else:
self.assertIsNot(client1, client2)
default_init_client.assert_has_calls(
[mock.call(session=(session or DEFAULT_SESSION))] * 2)
def test_get_client_with_not_shared(self):
self.test_get_client(shared=False)
def test_get_client_with_session(self):
self.test_get_client(session=SESSION)
def test_get_client_with_session_fixture(self):
self.test_get_client(session=SessionFixture())
def test_get_client_with_session_fixture_type(self):
self.test_get_client(session=SessionFixture)
def test_get_client_with_init_client(self):
init_client = mock.MagicMock(return_value=CLIENT)
manager = _client.OpenstackClientManager()
client = manager.get_client(session=SESSION,
init_client=init_client)
self.assertIs(CLIENT, client)
init_client.assert_called_once_with(session=SESSION)
Loading…
Cancel
Save