tests: Enable SQLAlchemy 2.0 deprecation warnings

Well, sort of. We enable them but immediately filter out the ones we're
actually seeing, the rationale being that we can address these in a
piecemeal fashion without the risk of introducing new issues.

There's a lot more to be done here. However, the work done in oslo.db
[1], nova [2], cinder [2] etc. should provide a guide for how to resolve
the outstanding issues.

[1] https://review.opendev.org/q/topic:sqlalchemy-20+project:openstack/oslo.db
[2] https://review.opendev.org/q/topic:sqlalchemy-20+project:openstack/nova
[3] https://review.opendev.org/q/topic:sqlalchemy-20+project:openstack/cinder

Change-Id: If13c14f1d968f1ef968ae440087227691cf966b0
Signed-off-by: Stephen Finucane <stephenfin@redhat.com>
This commit is contained in:
Stephen Finucane
2023-07-14 11:31:14 +01:00
parent df3fdb4f4e
commit 88950e33ba
6 changed files with 147 additions and 82 deletions

View File

@@ -726,6 +726,8 @@ def vmove_delete(context, vmove_uuid):
class DeleteFromSelect(sa_sql.expression.UpdateBase):
inherit_cache = False
def __init__(self, table, select, column):
self.table = table
self.select = select

View File

@@ -91,6 +91,8 @@ class TestCase(testtools.TestCase):
else:
self.useFixture(masakari_fixtures.DatabasePoisonFixture())
self.useFixture(masakari_fixtures.WarningsFixture())
def stub_out(self, old, new):
"""Replace a function for the duration of the test.

View File

@@ -1,22 +0,0 @@
# -*- coding: utf-8 -*-
# Copyright (c) 2016 NTT Data.
#
# 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.
from oslotest import base
class TestCase(base.BaseTestCase):
"""Test case base class for all unit tests."""

View File

@@ -14,8 +14,11 @@
"""Fixtures for Masakari tests."""
import warnings
import fixtures
from oslo_config import cfg
from sqlalchemy import exc as sqla_exc
from masakari.db import migration
from masakari.db.sqlalchemy import api as session
@@ -139,3 +142,77 @@ class Database(fixtures.Fixture):
super(Database, self).setUp()
self.reset()
self.addCleanup(self.cleanup)
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)
# The UUIDFields emits a warning if the value is not a valid UUID.
# Let's escalate that to an exception in the test to prevent adding
# violations.
# warnings.filterwarnings('error', message='.*invalid UUID.*')
# Enable deprecation warnings for nova itself to capture upcoming
# SQLAlchemy changes
warnings.filterwarnings(
'ignore',
category=sqla_exc.SADeprecationWarning,
)
warnings.filterwarnings(
'error',
module='masakari',
category=sqla_exc.SADeprecationWarning,
)
warnings.filterwarnings(
'ignore',
message='The current statement is being autocommitted',
module='masakari',
category=sqla_exc.SADeprecationWarning,
)
warnings.filterwarnings(
'ignore',
message='Using strings to indicate column or relationship paths',
module='masakari',
category=sqla_exc.SADeprecationWarning,
)
warnings.filterwarnings(
'ignore',
message=r'Passing a string to Connection.execute\(\)',
module='masakari',
category=sqla_exc.SADeprecationWarning,
)
warnings.filterwarnings(
'ignore',
message=r'The legacy calling style of select\(\)',
module='masakari',
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='masakari',
category=sqla_exc.SAWarning,
)
self.addCleanup(self._reset_warning_filters)
def _reset_warning_filters(self):
warnings.filters[:] = self._original_warning_filters

View File

@@ -19,9 +19,7 @@ import sys
import openstack.config
from openstack import connection
from masakari.tests import base
from oslotest import base
#: Defines the OpenStack Client Config (OCC) cloud key in your OCC config
#: file, typically in /etc/openstack/clouds.yaml. That configuration
@@ -30,7 +28,7 @@ from masakari.tests import base
TEST_CLOUD_NAME = os.getenv('OS_CLOUD', 'devstack-admin')
class BaseFunctionalTest(base.TestCase):
class BaseFunctionalTest(base.BaseTestCase):
def setUp(self, ha_api_version="1.0"):
super(BaseFunctionalTest, self).setUp()

34
tox.ini
View File

@@ -1,32 +1,40 @@
[tox]
minversion = 3.1.1
minversion = 3.18.0
envlist = pep8,py3
ignore_basepython_conflict = True
[testenv]
basepython = python3
usedevelop = True
setenv = VIRTUAL_ENV={envdir}
setenv =
VIRTUAL_ENV={envdir}
LANGUAGE=en_US
LC_ALL=en_US.utf-8
# TODO(stephenfin): Remove once we bump our upper-constraint to SQLAlchemy 2.0
SQLALCHEMY_WARN_20=1
deps =
-c{env:TOX_CONSTRAINTS_FILE:https://releases.openstack.org/constraints/upper/master}
-r{toxinidir}/requirements.txt
-r{toxinidir}/test-requirements.txt
commands =
stestr run {posargs}
passenv = HTTP_PROXY, HTTPS_PROXY, NO_PROXY, OS_DEBUG, GENERATE_HASHES
passenv =
HTTP_PROXY
HTTPS_PROXY
NO_PROXY
OS_DEBUG
GENERATE_HASHES
[testenv:functional]
commands =
stestr --test-path=./masakari/tests/functional run --concurrency=1 --slowest {posargs}
[testenv:genconfig]
commands = oslo-config-generator --config-file=etc/masakari/masakari-config-generator.conf
commands =
oslo-config-generator --config-file=etc/masakari/masakari-config-generator.conf
oslo-config-generator --config-file=etc/masakari/masakari-customized-recovery-flow-config-generator.conf
[testenv:genpolicy]
commands = oslopolicy-sample-generator --config-file=etc/masakari/masakari-policy-generator.conf
commands =
oslopolicy-sample-generator --config-file=etc/masakari/masakari-policy-generator.conf
[testenv:linters]
skip_install = True
@@ -84,7 +92,8 @@ commands =
# NOTE(elod.illes): requirements.txt is needed because otherwise
# dependencies are installed during 'develop-inst' tox phase without
# constraints which could cause failures in stable branches.
deps = -c{env:TOX_CONSTRAINTS_FILE:https://releases.openstack.org/constraints/upper/master}
deps =
-c{env:TOX_CONSTRAINTS_FILE:https://releases.openstack.org/constraints/upper/master}
-r{toxinidir}/requirements.txt
-r{toxinidir}/doc/requirements.txt
commands =
@@ -100,12 +109,13 @@ commands =
[testenv:releasenotes]
skip_install = True
deps = -c{env:TOX_CONSTRAINTS_FILE:https://releases.openstack.org/constraints/upper/master}
deps =
-c{env:TOX_CONSTRAINTS_FILE:https://releases.openstack.org/constraints/upper/master}
-r{toxinidir}/doc/requirements.txt
allowlist_externals =
rm
commands =
rm -fr releasenotes/build
rm -rf releasenotes/build
sphinx-build -a -E -W -d releasenotes/build/doctrees -b html releasenotes/source releasenotes/build/html
[testenv:debug]
@@ -122,10 +132,8 @@ commands =
sphinx-build -W -b html -d api-ref/build/doctrees api-ref/source api-ref/build/html
[flake8]
# E123, E125 skipped as they are invalid PEP-8.
show-source = True
# E123, E125 skipped as they are invalid PEP-8.
# The below hacking rules by default are disabled should be enabled:
# [H106] Don't put vim configuration in source files.
# [H203] Use assertIs(Not)None to check for None.