Allow skipping test cases when missing networking extensions

Change-Id: Iaaedcbd6f351ccb9dc2ba0813e3316dd424a5385
changes/39/651739/6
Federico Ressi 4 years ago
parent f40fefc2d7
commit dff46a36b2

@ -15,7 +15,7 @@ from __future__ import absolute_import
from heatclient import client as heatclient
from tobiko.openstack import client as _client
from tobiko.openstack import _client
class HeatClientFixture(_client.OpenstackClientFixture):

@ -14,6 +14,14 @@
from __future__ import absolute_import
from tobiko.openstack.neutron import _client
from tobiko.openstack.neutron import _extension
get_neutron_client = _client.get_neutron_client
NeutronClientFixture = _client.NeutronClientFixture
get_networking_extensions = _extension.get_networking_extensions
missing_networking_extensions = _extension.missing_networking_extensions
has_networking_extensions = _extension.has_networking_extensions
skip_if_missing_networking_extensions = (
_extension.skip_if_missing_networking_extensions)

@ -15,7 +15,7 @@ from __future__ import absolute_import
from neutronclient.v2_0 import client as neutronclient
from tobiko.openstack import client as _client
from tobiko.openstack import _client
class NeutronClientFixture(_client.OpenstackClientFixture):

@ -0,0 +1,58 @@
# 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 tobiko
from tobiko.openstack.neutron import _client
class NetworkingExtensionsFixture(tobiko.SharedFixture):
client = None
extensions = None
def setup_fixture(self):
self.setup_client()
self.get_networking_extensions()
def setup_client(self):
self.client = _client.get_neutron_client()
def get_networking_extensions(self):
extensions = self.client.list_extensions()['extensions']
self.extensions = frozenset(e['alias'] for e in extensions)
def get_networking_extensions():
return tobiko.setup_fixture(NetworkingExtensionsFixture).extensions
def missing_networking_extensions(*extensions):
return sorted(frozenset(extensions) - get_networking_extensions())
def has_networking_extensions(*extensions):
return not missing_networking_extensions(*extensions)
def skip_if_missing_networking_extensions(*extensions):
return tobiko.skip_if('missing networking extensions: {return_value!r}',
missing_networking_extensions, *extensions)
if __name__ == '__main__':
import sys
sys.stdout.write('\n'.join(sorted(get_networking_extensions())))
sys.stdout.flush()

@ -15,7 +15,7 @@ from __future__ import absolute_import
from novaclient import client as novaclient
from tobiko.openstack import client as _client
from tobiko.openstack import _client
class NovaClientFixture(_client.OpenstackClientFixture):

@ -18,7 +18,6 @@ import mock
from tobiko.config import CONF
from tobiko.openstack import keystone
from tobiko.openstack import heat
from tobiko.tests import unit
@ -38,4 +37,17 @@ class OpenstackTest(unit.TobikoUnitTest):
def patch_get_heat_client(self, *args, **kwargs):
from heatclient import client
kwargs.setdefault('return_value', mock.MagicMock(specs=client.Client))
return self.patch_object(heat, 'get_heat_client', *args, **kwargs)
get_heat_client = self.patch(
'tobiko.openstack.heat._client.get_heat_client', *args, **kwargs)
self.patch('tobiko.openstack.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
kwargs.setdefault('return_value', mock.MagicMock(specs=client.Client))
get_neutron_client = self.patch(
'tobiko.openstack.neutron._client.get_neutron_client', *args,
**kwargs)
self.patch('tobiko.openstack.neutron.get_neutron_client',
get_neutron_client)
return get_neutron_client

@ -0,0 +1,61 @@
# 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
from tobiko.openstack import neutron
from tobiko.tests.openstack import base
class NeutronExtensionTest(base.OpenstackTest):
extensions = ['ext-1', 'ext-2', 'ext-3']
def setUp(self):
super(NeutronExtensionTest, self).setUp()
self.client = self.patch_get_neutron_client().return_value
self.client.list_extensions.return_value = {
'extensions': [{'alias': e} for e in self.extensions]}
def test_get_networking_extensions(self):
result = neutron.get_networking_extensions()
self.assertEqual(frozenset(self.extensions), result)
def test_missing_networking_extensions(self):
result = neutron.missing_networking_extensions(
'ext-2', 'ext-4', 'ext-0')
self.assertEqual(['ext-0', 'ext-4'], result)
def test_has_networking_extensions(self):
self.assertTrue(neutron.has_networking_extensions(*self.extensions))
self.assertFalse(neutron.has_networking_extensions('ext-1', 'ext-4'))
def test_skip_if_networking_extensions_when_missing(self):
ex = self.assertRaises(
self.skipException,
self._test_skip_if_networking_extensions_when_missing)
self.assertEqual("missing networking extensions: ['ext-4']", str(ex))
@neutron.skip_if_missing_networking_extensions('ext-1', 'ext-4')
def _test_skip_if_networking_extensions_when_missing(self):
self.fail('Not skipped')
def test_skip_if_networking_extensions_when_not_missing(self):
ex = self.assertRaises(
self.failureException,
self._test_skip_if_networking_extensions_when_not_missing)
self.assertEqual('OK', str(ex))
@neutron.skip_if_missing_networking_extensions('ext-1', 'ext-2')
def _test_skip_if_networking_extensions_when_not_missing(self):
self.fail('OK')

@ -19,7 +19,7 @@ import inspect
import mock
import tobiko
from tobiko.openstack import client as _client
from tobiko.openstack import _client
from tobiko.openstack import keystone
from tobiko.tests.openstack import base

Loading…
Cancel
Save