Merge "fix WarningsFixture for public consumption"

This commit is contained in:
Zuul 2019-04-11 01:38:58 +00:00 committed by Gerrit Code Review
commit e599f4a02a
5 changed files with 48 additions and 19 deletions

View File

@ -11,6 +11,7 @@
# under the License.
import copy
import warnings
import fixtures
import mock
@ -308,3 +309,27 @@ class OpenFixture(fixtures.Fixture):
new=replacement_open)
self._patch.start()
self.addCleanup(self._patch.stop)
class WarningsFixture(fixtures.Fixture):
"""Filters out warnings during test runs."""
warning_types = (
DeprecationWarning, PendingDeprecationWarning, ImportWarning
)
def __init__(self, module_re=None):
"""Create a new WarningsFixture.
:param module_re: A list of regular expression strings that will be
used with filterwarnings. Multiple expressions are or'd together.
"""
self._modules = ['^neutron_lib\\.']
if module_re:
self._modules.extend(module_re)
def _setUp(self):
self.addCleanup(warnings.resetwarnings)
for wtype in self.warning_types:
warnings.filterwarnings(
"always", category=wtype, module='|'.join(self._modules))

View File

@ -32,7 +32,6 @@ from neutron_lib import exceptions
from neutron_lib import fixture
from neutron_lib.tests import _post_mortem_debug as post_mortem_debug
from neutron_lib.tests import tools
CONF = cfg.CONF
@ -139,7 +138,7 @@ class BaseTestCase(testtools.TestCase):
debugger))
# Make sure we see all relevant deprecation warnings when running tests
self.useFixture(tools.WarningsFixture())
self.useFixture(fixture.WarningsFixture())
if bool_from_env('OS_DEBUG'):
_level = std_logging.DEBUG

View File

@ -17,9 +17,7 @@ import os
import platform
import random
import time
import warnings
import fixtures
import netaddr
from neutron_lib.utils import helpers
@ -39,20 +37,6 @@ class UnorderedList(list):
return not self == other
class WarningsFixture(fixtures.Fixture):
"""Filters out warnings during test runs."""
warning_types = (
DeprecationWarning, PendingDeprecationWarning, ImportWarning
)
def _setUp(self):
self.addCleanup(warnings.resetwarnings)
for wtype in self.warning_types:
warnings.filterwarnings(
"always", category=wtype, module='^neutron_lib\\.')
def is_bsd():
"""Return True on BSD-based systems."""

View File

@ -10,8 +10,9 @@
# License for the specific language governing permissions and limitations
# under the License.
import mock
import re
import mock
from oslo_config import cfg
from oslo_db import options
from oslotest import base
@ -178,3 +179,19 @@ class DBResourceExtendFixtureTestCase(base.BaseTestCase):
db_fixture.cleanUp()
self.assertEqual(
orig_methods, resource_extend._resource_extend_functions)
class WarningsFixture(base.BaseTestCase):
@mock.patch.object(fixture.warnings, 'filterwarnings')
def test_fixture_regex(self, mock_filterwarnings):
module_re = ['^neutron\\.']
warn_fixture = fixture.WarningsFixture(module_re=module_re)
warn_fixture.setUp()
call_re = mock_filterwarnings.mock_calls[0][2]['module']
self.assertEqual('^neutron_lib\\.|^neutron\\.', call_re)
self.assertIsNotNone(re.compile(call_re))
self.assertIsNotNone(re.search(call_re, 'neutron.db.blah'))
self.assertIsNotNone(re.search(call_re, 'neutron_lib.db.blah'))
self.assertIsNone(re.search(
call_re, 'neutron_dynamic_routing.db.blah'))

View File

@ -0,0 +1,4 @@
---
features:
- The ``WarningsFixture`` is now available in ``neutron_lib.fixture`` and
its constructor accepts additional module's to use with filterwarnings.