fix WarningsFixture for public consumption
The neutron.tests.tools was recently made public and along with it the WarningsFixture. However the WarningsFixture as-is doesn't support filtering warnings from other modules. This patch address that by accepting an constructor arg to add other module regular expressions to filter. It also moves WarningsFixture to the fixture module where it seems to make the most sense. For a sample consumption patch see: https://review.openstack.org/#/c/651370/ Change-Id: I08c713916aef407ba108ed2c67c3e99892f5883a
This commit is contained in:
parent
bcb9c90072
commit
b4ecdc9945
@ -11,6 +11,7 @@
|
|||||||
# under the License.
|
# under the License.
|
||||||
|
|
||||||
import copy
|
import copy
|
||||||
|
import warnings
|
||||||
|
|
||||||
import fixtures
|
import fixtures
|
||||||
import mock
|
import mock
|
||||||
@ -308,3 +309,27 @@ class OpenFixture(fixtures.Fixture):
|
|||||||
new=replacement_open)
|
new=replacement_open)
|
||||||
self._patch.start()
|
self._patch.start()
|
||||||
self.addCleanup(self._patch.stop)
|
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))
|
||||||
|
@ -32,7 +32,6 @@ from neutron_lib import exceptions
|
|||||||
from neutron_lib import fixture
|
from neutron_lib import fixture
|
||||||
|
|
||||||
from neutron_lib.tests import _post_mortem_debug as post_mortem_debug
|
from neutron_lib.tests import _post_mortem_debug as post_mortem_debug
|
||||||
from neutron_lib.tests import tools
|
|
||||||
|
|
||||||
|
|
||||||
CONF = cfg.CONF
|
CONF = cfg.CONF
|
||||||
@ -139,7 +138,7 @@ class BaseTestCase(testtools.TestCase):
|
|||||||
debugger))
|
debugger))
|
||||||
|
|
||||||
# Make sure we see all relevant deprecation warnings when running tests
|
# 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'):
|
if bool_from_env('OS_DEBUG'):
|
||||||
_level = std_logging.DEBUG
|
_level = std_logging.DEBUG
|
||||||
|
@ -17,9 +17,7 @@ import os
|
|||||||
import platform
|
import platform
|
||||||
import random
|
import random
|
||||||
import time
|
import time
|
||||||
import warnings
|
|
||||||
|
|
||||||
import fixtures
|
|
||||||
import netaddr
|
import netaddr
|
||||||
|
|
||||||
from neutron_lib.utils import helpers
|
from neutron_lib.utils import helpers
|
||||||
@ -39,20 +37,6 @@ class UnorderedList(list):
|
|||||||
return not self == other
|
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():
|
def is_bsd():
|
||||||
"""Return True on BSD-based systems."""
|
"""Return True on BSD-based systems."""
|
||||||
|
|
||||||
|
@ -10,8 +10,9 @@
|
|||||||
# License for the specific language governing permissions and limitations
|
# License for the specific language governing permissions and limitations
|
||||||
# under the License.
|
# under the License.
|
||||||
|
|
||||||
import mock
|
import re
|
||||||
|
|
||||||
|
import mock
|
||||||
from oslo_config import cfg
|
from oslo_config import cfg
|
||||||
from oslo_db import options
|
from oslo_db import options
|
||||||
from oslotest import base
|
from oslotest import base
|
||||||
@ -178,3 +179,19 @@ class DBResourceExtendFixtureTestCase(base.BaseTestCase):
|
|||||||
db_fixture.cleanUp()
|
db_fixture.cleanUp()
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
orig_methods, resource_extend._resource_extend_functions)
|
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'))
|
||||||
|
4
releasenotes/notes/fix-warnfixture-c9457c50d0d5c5a7.yaml
Normal file
4
releasenotes/notes/fix-warnfixture-c9457c50d0d5c5a7.yaml
Normal 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.
|
Loading…
Reference in New Issue
Block a user