summaryrefslogtreecommitdiff
path: root/oslo_db/tests/sqlalchemy/test_provision.py
diff options
context:
space:
mode:
Diffstat (limited to 'oslo_db/tests/sqlalchemy/test_provision.py')
-rw-r--r--oslo_db/tests/sqlalchemy/test_provision.py58
1 files changed, 58 insertions, 0 deletions
diff --git a/oslo_db/tests/sqlalchemy/test_provision.py b/oslo_db/tests/sqlalchemy/test_provision.py
index 1ad586d..53d2303 100644
--- a/oslo_db/tests/sqlalchemy/test_provision.py
+++ b/oslo_db/tests/sqlalchemy/test_provision.py
@@ -10,7 +10,9 @@
# License for the specific language governing permissions and limitations
# under the License.
+import mock
from oslotest import base as oslo_test_base
+from sqlalchemy import exc as sa_exc
from sqlalchemy import inspect
from sqlalchemy import schema
from sqlalchemy import types
@@ -73,6 +75,62 @@ class DropAllObjectsTest(test_base.DbTestCase):
)
+class BackendNotAvailableTest(oslo_test_base.BaseTestCase):
+ def test_no_dbapi(self):
+ backend = provision.Backend(
+ "postgresql", "postgresql+nosuchdbapi://hostname/dsn")
+
+ with mock.patch(
+ "sqlalchemy.create_engine",
+ mock.Mock(side_effect=ImportError("nosuchdbapi"))):
+
+ # NOTE(zzzeek): Call and test the _verify function twice, as it
+ # exercises a different code path on subsequent runs vs.
+ # the first run
+ ex = self.assertRaises(
+ exception.BackendNotAvailable,
+ backend._verify)
+ self.assertEqual(
+ "Backend 'postgresql+nosuchdbapi' is unavailable: "
+ "No DBAPI installed", str(ex))
+
+ ex = self.assertRaises(
+ exception.BackendNotAvailable,
+ backend._verify)
+ self.assertEqual(
+ "Backend 'postgresql+nosuchdbapi' is unavailable: "
+ "No DBAPI installed", str(ex))
+
+ def test_cant_connect(self):
+ backend = provision.Backend(
+ "postgresql", "postgresql+nosuchdbapi://hostname/dsn")
+
+ with mock.patch(
+ "sqlalchemy.create_engine",
+ mock.Mock(return_value=mock.Mock(connect=mock.Mock(
+ side_effect=sa_exc.OperationalError(
+ "can't connect", None, None))
+ ))
+ ):
+
+ # NOTE(zzzeek): Call and test the _verify function twice, as it
+ # exercises a different code path on subsequent runs vs.
+ # the first run
+ ex = self.assertRaises(
+ exception.BackendNotAvailable,
+ backend._verify)
+ self.assertEqual(
+ "Backend 'postgresql+nosuchdbapi' is unavailable: "
+ "Could not connect", str(ex))
+
+ ex = self.assertRaises(
+ exception.BackendNotAvailable,
+ backend._verify)
+ self.assertEqual(
+ "Backend 'postgresql+nosuchdbapi' is unavailable: "
+ "Could not connect", str(ex))
+
+
class MySQLDropAllObjectsTest(
DropAllObjectsTest, test_base.MySQLOpportunisticTestCase):
pass