tests: Add warning fixture

Copied almost wholesale from Nova. We're going to use this as part of
the SQLAlchemy 2.0 preparation. We enable those warnings and clean up
the tox file in preparation.

Change-Id: I869d9b110264d69301bf43ae579a4749adeba4f6
Signed-off-by: Stephen Finucane <stephenfin@redhat.com>
This commit is contained in:
Stephen Finucane 2024-04-12 14:51:27 +01:00
parent 8a69520298
commit 6bd15d6736
3 changed files with 103 additions and 30 deletions

View File

@ -28,6 +28,7 @@ import testtools
from freezer_api.common import config
from freezer_api import policy
from freezer_api.tests.unit import fixtures as freezer_fixtures
CONF = cfg.CONF
@ -697,6 +698,8 @@ class FreezerBaseTestCase(testtools.TestCase):
elif self.REGISTER_POLICY:
raise Exception('You need to register config to register policy')
self.useFixture(freezer_fixtures.WarningsFixture())
self.test_dir = self.useFixture(fixtures.TempDir()).path
self.conf_dir = os.path.join(self.test_dir, 'etc')
os.makedirs(self.conf_dir)

View File

@ -0,0 +1,56 @@
# 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[:]
warnings.simplefilter("once", DeprecationWarning)
# Enable SQLAlchemy deprecation warnings to capture upcoming changes to
# that library
warnings.filterwarnings(
'ignore',
category=sqla_exc.SADeprecationWarning,
)
warnings.filterwarnings(
'error',
module='freezer_api',
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='freezer_api',
category=sqla_exc.SAWarning,
)
self.addCleanup(self._reset_warning_filters)
def _reset_warning_filters(self):
warnings.filters[:] = self._original_warning_filters

74
tox.ini
View File

@ -1,28 +1,58 @@
[tox]
minversion = 3.18.0
envlist = py3,pep8,pylint,docs
ignore_basepython_conflict = True
[testenv]
basepython = python3
usedevelop = True
allowlist_externals = rm
usedevelop = true
allowlist_externals =
rm
deps =
-c{env:UPPER_CONSTRAINTS_FILE:https://releases.openstack.org/constraints/upper/master}
-r{toxinidir}/requirements.txt
-r{toxinidir}/test-requirements.txt
passenv = HTTP_PROXY, HTTPS_PROXY, NO_PROXY, OS_DEBUG, GENERATE_HASHES
commands = stestr run {posargs}
-c{env:UPPER_CONSTRAINTS_FILE:https://releases.openstack.org/constraints/upper/master}
-r{toxinidir}/requirements.txt
-r{toxinidir}/test-requirements.txt
setenv =
PYTHONDONTWRITEBYTECODE=1
# TODO(stephenfin): Remove once we bump our upper-constraint to SQLAlchemy 2.0
SQLALCHEMY_WARN_20=1
passenv =
HTTP_PROXY
HTTPS_PROXY
NO_PROXY
OS_DEBUG
GENERATE_HASHES
commands =
stestr run {posargs}
[testenv:venv]
commands = {posargs}
commands =
{posargs}
[testenv:pylint]
commands = pylint --rcfile .pylintrc freezer_api
commands =
pylint --rcfile .pylintrc freezer_api
[testenv:pep8]
commands = flake8 freezer_api
commands =
flake8 freezer_api
[testenv:docs]
commands =
rm -rf api-ref/build
# sphinx-build -W --keep-going -b html api-ref/source api-ref/build/html
sphinx-build -W --keep-going -b html doc/source doc/build/html
[testenv:api-ref]
commands =
rm -rf api-ref/build
sphinx-build -W -a -E --keep-going -b html -d api-ref/build/doctrees api-ref/source api-ref/build/html
[testenv:releasenotes]
commands =
sphinx-build -W -a -E -d releasenotes/build/doctrees --keep-going -b html releasenotes/source releasenotes/build/html
[testenv:genpolicy]
commands =
oslopolicy-sample-generator --config-file etc/freezer/freezer-policy-generator.conf
[flake8]
# Ignored hackings:
@ -31,21 +61,5 @@ commands = flake8 freezer_api
# H404 -> Multi line docstrings should start without a leading new line.
# H405 -> Multi line docstrings should start with a one line summary followed by an empty line.
ignore = H202,H401,H404,H405
show-source = True
show-source = true
exclude = .venv,.tox,dist,doc,*egg,specs,build,*/source/conf.py
[testenv:docs]
commands =
rm -rf api-ref/build
# sphinx-build -W --keep-going -b html api-ref/source api-ref/build/html
sphinx-build -W --keep-going -b html doc/source doc/build/html
[testenv:api-ref]
commands =
rm -rf api-ref/build
sphinx-build -W -a -E --keep-going -b html -d api-ref/build/doctrees api-ref/source api-ref/build/html
[testenv:releasenotes]
commands = sphinx-build -W -a -E -d releasenotes/build/doctrees --keep-going -b html releasenotes/source releasenotes/build/html
[testenv:genpolicy]
commands = oslopolicy-sample-generator --config-file etc/freezer/freezer-policy-generator.conf