Browse Source
This duplicates what exists in nova and various other projects. The important difference between this and what we're doing currently is that it *restores*, rather than reset, the warning filters. There are more various warning filters pre-configured in a typical Python environment, including a few from third-party libraries such as requests [1][2] and urllib3 [3] as well as stdlib [4]. By calling 'warnings.resetwarnings', we *reset* all the warning filters [5]. This is clearly not something we want to do, and resulted in tests puking warnings after the initial test run. [1] https://github.com/psf/requests/blob/v2.26.0/requests/__init__.py#L127 [2] https://github.com/psf/requests/blob/v2.26.0/requests/__init__.py#L152 [3] https://github.com/urllib3/urllib3/blob/1.26.7/src/urllib3/__init__.py#L68-L78 [4] https://docs.python.org/3.8/library/warnings.html#default-warning-filter [5] https://docs.python.org/3.8/library/warnings.html#warnings.resetwarnings Change-Id: Ia2046dc32e3ac270b1dbcf2fe540104c1a8d95d8 Signed-off-by: Stephen Finucane <stephenfin@redhat.com>changes/59/823659/1
3 changed files with 57 additions and 12 deletions
@ -0,0 +1,54 @@
|
||||
# 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. |
||||
|
||||
import warnings |
||||
|
||||
import fixtures |
||||
from sqlalchemy import exc as sqla_exc |
||||
|
||||
|
||||
class WarningsFixture(fixtures.Fixture): |
||||
"""Filters out warnings during test runs.""" |
||||
|
||||
def setUp(self): |
||||
super().setUp() |
||||
|
||||
self._original_warning_filters = warnings.filters[:] |
||||
|
||||
# NOTE(stephenfin): 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) |
||||
|
||||
warnings.filterwarnings( |
||||
'error', |
||||
category=DeprecationWarning, |
||||
module='^keystone\\.', |
||||
) |
||||
|
||||
# TODO(stephenfin): This will be fixed once we drop sqlalchemy-migrate |
||||
warnings.filterwarnings( |
||||
'ignore', |
||||
category=DeprecationWarning, |
||||
message=r"Using function/method 'db_version\(\)' is deprecated", |
||||
) |
||||
|
||||
# TODO(stephenfin): We should filter on the specific RemovedIn20Warning |
||||
# warnings that affect us, so that we can slowly start addressing them |
||||
warnings.simplefilter('error', sqla_exc.SAWarning) |
||||
if hasattr(sqla_exc, 'RemovedIn20Warning'): |
||||
warnings.simplefilter('ignore', sqla_exc.RemovedIn20Warning) |
||||
|
||||
self.addCleanup(self._reset_warning_filters) |
||||
|
||||
def _reset_warning_filters(self): |
||||
warnings.filters[:] = self._original_warning_filters |
Loading…
Reference in new issue