From 25b3e6796dbdd5d53d2ac8aa34185c7cca138085 Mon Sep 17 00:00:00 2001 From: Stephen Finucane Date: Tue, 6 Sep 2022 16:13:10 +0100 Subject: tests: Add a WarningsFixture We will use this in the future to prepare for SQLAlchemy 2.0. For now, we're simply using it to filter out some of the more annoying warnings and to highlight general SQLAlchemy issues we need to address. Change-Id: I7c26c20e4b36c4f3b98873939677b966ec6186a5 Signed-off-by: Stephen Finucane --- ironic/tests/base.py | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++++ tox.ini | 1 - 2 files changed, 81 insertions(+), 1 deletion(-) diff --git a/ironic/tests/base.py b/ironic/tests/base.py index 4b34ef0a4..348f15c20 100644 --- a/ironic/tests/base.py +++ b/ironic/tests/base.py @@ -27,6 +27,7 @@ import subprocess import sys import tempfile from unittest import mock +import warnings import eventlet eventlet.monkey_patch(os=False) @@ -38,6 +39,7 @@ from oslo_log import log as logging from oslo_serialization import jsonutils from oslo_utils import uuidutils from oslotest import base as oslo_test_base +from sqlalchemy import exc as sqla_exc from ironic.common import config as ironic_config from ironic.common import context as ironic_context @@ -70,6 +72,84 @@ def _patch_mock_callable(obj): return False +class WarningsFixture(fixtures.Fixture): + """Filters out warnings during test runs.""" + + def setUp(self): + super().setUp() + + self._original_warning_filters = warnings.filters[:] + + # NOTE(sdague): Make deprecation warnings only happen once. Otherwise + # this gets kind of crazy given the way that upstream python libs use + # this. + warnings.simplefilter('once', DeprecationWarning) + + # NOTE(stephenfin): We get way too many of these. Silence them. + warnings.filterwarnings( + 'ignore', + message=( + 'Policy enforcement is depending on the value of .*. ' + 'This key is deprecated. Please update your policy ' + 'file to use the standard policy values.' + ), + ) + + # NOTE(mriedem): Ignore scope check UserWarnings from oslo.policy. + warnings.filterwarnings( + 'ignore', + message='Policy .* failed scope check', + category=UserWarning, + ) + + # Enable deprecation warnings to capture upcoming SQLAlchemy changes + + warnings.filterwarnings( + 'ignore', + category=sqla_exc.SADeprecationWarning, + ) + + warnings.filterwarnings( + 'error', + module='ironic', + category=sqla_exc.SADeprecationWarning, + ) + + # Enable general SQLAlchemy warnings also to ensure we're not doing + # silly stuff. It's possible that we'll need to filter things out here + # with future SQLAlchemy versions, but that's a good thing + + warnings.filterwarnings( + 'error', + module='ironic', + category=sqla_exc.SAWarning, + ) + + # ...but filter everything out until we get around to fixing them + # TODO(stephenfin): Fix all of these + + warnings.filterwarnings( + 'ignore', + module='ironic', + message='SELECT statement has a cartesian product ', + category=sqla_exc.SAWarning, + ) + + # FIXME(stephenfin): We can remove this once oslo.db is fixed + # https://review.opendev.org/c/openstack/oslo.db/+/856453 + warnings.filterwarnings( + 'ignore', + module='ironic', + message='TypeDecorator .* will not produce a cache key', + category=sqla_exc.SAWarning, + ) + + self.addCleanup(self._reset_warning_filters) + + def _reset_warning_filters(self): + warnings.filters[:] = self._original_warning_filters + + class ReplaceModule(fixtures.Fixture): """Replace a module with a fake module.""" @@ -113,6 +193,7 @@ class TestCase(oslo_test_base.BaseTestCase): self.addCleanup(hash_ring.HashRingManager().reset) self.useFixture(fixtures.EnvironmentVariable('http_proxy')) self.policy = self.useFixture(policy_fixture.PolicyFixture()) + self.useFixture(WarningsFixture()) driver_factory.HardwareTypesFactory._extension_manager = None for factory in driver_factory._INTERFACE_LOADERS.values(): diff --git a/tox.ini b/tox.ini index cffaa8f1d..247e819a4 100644 --- a/tox.ini +++ b/tox.ini @@ -11,7 +11,6 @@ setenv = VIRTUAL_ENV={envdir} PYTHONDONTWRITEBYTECODE = 1 LANGUAGE=en_US LC_ALL=en_US.UTF-8 - PYTHONWARNINGS=default::DeprecationWarning deps = -c{env:TOX_CONSTRAINTS_FILE:https://releases.openstack.org/constraints/upper/master} -r{toxinidir}/requirements.txt -- cgit v1.2.1