Separate keystone monkey patching from base openstack test case

Change-Id: I6d428e7a3e8fbb071d3f3caf2eed516e7df6f1d2
This commit is contained in:
Federico Ressi 2019-07-19 10:32:21 +02:00
parent f493b9b1b5
commit ffcf6ebfe3
4 changed files with 128 additions and 60 deletions

View File

@ -14,63 +14,8 @@
# under the License. # under the License.
from __future__ import absolute_import from __future__ import absolute_import
from keystoneclient import discover from tobiko.tests.unit.openstack import _case
from keystoneclient.v3 import Client from tobiko.tests.unit.openstack import _keystone
from oslo_log import log
import mock
from tobiko.openstack import keystone OpenstackTest = _case.OpenstackTest
from tobiko.tests import unit DefaultKeystoneCredentialsPatch = _keystone.DefaultKeystoneCredentialsPatch
LOG = log.getLogger(__name__)
class KeystoneDiscoverMock(object):
def __init__(self, session, **kwargs):
self.session = session
self.kwargs = kwargs
def create_client(self, version, unstable):
LOG.debug("Create a mock keystone client for version {!r} "
"(unestable = {!r})", version, unstable)
return Client(session=self.session)
class OpenstackTest(unit.TobikoUnitTest):
default_keystone_credentials = keystone.keystone_credentials(
auth_url='http://127.0.0.1:5000/v3',
username='default',
project_name='default',
password='this is a secret')
def setUp(self):
super(OpenstackTest, self).setUp()
from tobiko import config
self.patch(config.CONF.tobiko, 'keystone',
self.default_keystone_credentials)
self.patch(discover, 'Discover', KeystoneDiscoverMock)
def patch_get_heat_client(self, *args, **kwargs):
from heatclient import client
from tobiko.openstack import heat
from tobiko.openstack.heat import _client
kwargs.setdefault('return_value', mock.MagicMock(specs=client.Client))
get_heat_client = self.patch(_client, 'get_heat_client', *args,
**kwargs)
self.patch(heat, 'get_heat_client', get_heat_client)
return get_heat_client
def patch_get_neutron_client(self, *args, **kwargs):
from neutronclient.v2_0 import client
from tobiko.openstack import neutron
from tobiko.openstack.neutron import _client
kwargs.setdefault('return_value', mock.MagicMock(specs=client.Client))
get_neutron_client = self.patch(_client, 'get_neutron_client', *args,
**kwargs)
self.patch(neutron, 'get_neutron_client', get_neutron_client)
return get_neutron_client

View File

@ -0,0 +1,54 @@
# 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 mock
from tobiko.tests.unit.openstack import _keystone
from tobiko.tests import unit
class OpenstackTest(unit.TobikoUnitTest):
default_credentials = None
def setUp(self):
super(OpenstackTest, self).setUp()
patch_credentials = _keystone.DefaultKeystoneCredentialsPatch(
credentials=self.default_credentials)
self.useFixture(patch_credentials)
def patch_get_heat_client(self, *args, **kwargs):
from heatclient import client
from tobiko.openstack import heat
from tobiko.openstack.heat import _client
kwargs.setdefault('return_value', mock.MagicMock(specs=client.Client))
get_heat_client = self.patch(_client, 'get_heat_client', *args,
**kwargs)
self.patch(heat, 'get_heat_client', get_heat_client)
return get_heat_client
def patch_get_neutron_client(self, *args, **kwargs):
from neutronclient.v2_0 import client
from tobiko.openstack import neutron
from tobiko.openstack.neutron import _client
kwargs.setdefault('return_value', mock.MagicMock(specs=client.Client))
get_neutron_client = self.patch(_client, 'get_neutron_client', *args,
**kwargs)
self.patch(neutron, 'get_neutron_client', get_neutron_client)
return get_neutron_client

View File

@ -0,0 +1,43 @@
# 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
from oslo_log import log
import tobiko
from tobiko import config
from tobiko.openstack import keystone
from tobiko.tests import unit
LOG = log.getLogger(__name__)
class DefaultKeystoneCredentialsPatch(unit.PatchFixture):
credentials = keystone.keystone_credentials(
auth_url='http://127.0.0.1:5000/v3',
username='default',
project_name='default',
password='this is a secret')
def __init__(self, credentials=None):
if credentials:
self.credentials = credentials
tobiko.check_valid_type(self.credentials,
keystone.KeystoneCredentials)
def setup_fixture(self):
self.patch(config.CONF.tobiko, 'keystone', self.credentials)

View File

@ -13,25 +13,47 @@
# under the License. # under the License.
from __future__ import absolute_import from __future__ import absolute_import
from keystoneclient import discover
from keystoneclient.v2_0 import client as client_v2 from keystoneclient.v2_0 import client as client_v2
from keystoneclient.v3 import client as client_v3 from keystoneclient.v3 import client as client_v3
from oslo_log import log
from tobiko.openstack import keystone from tobiko.openstack import keystone
from tobiko.tests.unit import openstack from tobiko.tests.unit import openstack
from tobiko.tests.unit.openstack import test_client from tobiko.tests.unit.openstack import test_client
LOG = log.getLogger(__name__)
KEYSTONE_CLIENTS = client_v2.Client, client_v3.Client KEYSTONE_CLIENTS = client_v2.Client, client_v3.Client
class DiscoverMock(object):
def __init__(self, session, **kwargs):
self.session = session
self.kwargs = kwargs
def create_client(self, version, unstable):
LOG.debug("Create a mock keystone client for version %r "
"(unestable=%r)", version, unstable)
return client_v3.Client(session=self.session)
class KeystoneClientFixtureTest(test_client.OpenstackClientFixtureTest): class KeystoneClientFixtureTest(test_client.OpenstackClientFixtureTest):
def setUp(self):
super(KeystoneClientFixtureTest, self).setUp()
self.patch(discover, 'Discover', DiscoverMock)
def create_client(self, session=None): def create_client(self, session=None):
return keystone.KeystoneClientFixture(session=session) return keystone.KeystoneClientFixture(session=session)
class GetKeystoneClientTest(openstack.OpenstackTest): class GetKeystoneClientTest(openstack.OpenstackTest):
def setUp(self):
super(GetKeystoneClientTest, self).setUp()
self.patch(discover, 'Discover', DiscoverMock)
def test_get_keystone_client(self, session=None, shared=True): def test_get_keystone_client(self, session=None, shared=True):
client1 = keystone.get_keystone_client(session=session, shared=shared) client1 = keystone.get_keystone_client(session=session, shared=shared)
client2 = keystone.get_keystone_client(session=session, shared=shared) client2 = keystone.get_keystone_client(session=session, shared=shared)
@ -52,6 +74,10 @@ class GetKeystoneClientTest(openstack.OpenstackTest):
class KeystoneClientTest(openstack.OpenstackTest): class KeystoneClientTest(openstack.OpenstackTest):
def setUp(self):
super(KeystoneClientTest, self).setUp()
self.patch(discover, 'Discover', DiscoverMock)
def test_keystone_client_with_none(self): def test_keystone_client_with_none(self):
default_client = keystone.get_keystone_client() default_client = keystone.get_keystone_client()
client = keystone.keystone_client(None) client = keystone.keystone_client(None)