deb-oslo.db/oslo_db/tests/sqlalchemy/test_fixtures.py
Mike Bayer af27831a9e Display full reason for backend not available
This adds to the provisioning system the ability
to preserve and re-display the full reason for a
particular DB backend not available, and propagates
this all the way through the skip() emitted
by the OpportunisticTestCase and DbFixture.

As part of the change, the database type requested
is added to the messages themselves, which
also appears in the messages emitted by the
consuming is_backend_avail() function, which
has tests that check for these explicit messages.
New tests specific to the Backend object are
added which are not dependent on is_backend_avail();
as this function is deprecated, we will be
able to remove its tests at the same time
as the function itself with no loss in coverage.

Change-Id: I7c00e0770b02aa751e184edfecdec4306de6340c
2016-08-08 14:25:58 -04:00

84 lines
2.5 KiB
Python

# 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 mock
from oslo_db.sqlalchemy import provision
from oslo_db.sqlalchemy import test_base
from oslotest import base as oslo_test_base
class BackendSkipTest(oslo_test_base.BaseTestCase):
def test_skip_no_dbapi(self):
class FakeDatabaseOpportunisticFixture(test_base.DbFixture):
DRIVER = 'postgresql'
class SomeTest(test_base.DbTestCase):
FIXTURE = FakeDatabaseOpportunisticFixture
def runTest(self):
pass
st = SomeTest()
# patch in replacement lookup dictionaries to avoid
# leaking from/to other tests
with mock.patch(
"oslo_db.sqlalchemy.provision."
"Backend.backends_by_database_type", {
"postgresql":
provision.Backend("postgresql", "postgresql://")}):
st._database_resources = {}
st._db_not_available = {}
st._schema_resources = {}
with mock.patch(
"sqlalchemy.create_engine",
mock.Mock(side_effect=ImportError())):
self.assertEqual([], st.resources)
ex = self.assertRaises(
self.skipException,
st.setUp
)
self.assertEqual(
"Backend 'postgresql' is unavailable: No DBAPI installed",
str(ex)
)
def test_skip_no_such_backend(self):
class FakeDatabaseOpportunisticFixture(test_base.DbFixture):
DRIVER = 'postgresql+nosuchdbapi'
class SomeTest(test_base.DbTestCase):
FIXTURE = FakeDatabaseOpportunisticFixture
def runTest(self):
pass
st = SomeTest()
ex = self.assertRaises(
self.skipException,
st.setUp
)
self.assertEqual(
"Backend 'postgresql+nosuchdbapi' is unavailable: No such backend",
str(ex)
)