From 331071a3e2f1ee6783e6a908789af40835818eed Mon Sep 17 00:00:00 2001 From: Ivan Kolodyazhny Date: Mon, 4 Aug 2014 18:16:10 +0300 Subject: [PATCH] Catch DBDuplicateEntry instead of IntegrityError 'iscsi_target_create_safe' method didn't catch DB exceptions correct. Need to catch both db_exc.DBError and db_exc.DBDuplicateEntry due to current oslo.db limitations: it doesn't handle correct DBDuplicateEntry with different SQLite versions Related-Bug: #1187367 Change-Id: Ie412ecabd3cbcac301340c82b4275adb7a7206c3 --- cinder/db/sqlalchemy/api.py | 14 ++++++++------ cinder/tests/test_db_api.py | 8 ++++---- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/cinder/db/sqlalchemy/api.py b/cinder/db/sqlalchemy/api.py index a3244ea09..9a81acfaf 100644 --- a/cinder/db/sqlalchemy/api.py +++ b/cinder/db/sqlalchemy/api.py @@ -28,7 +28,6 @@ from oslo.config import cfg from oslo.db import exception as db_exc from oslo.db import options from oslo.db.sqlalchemy import session as db_session -from sqlalchemy.exc import IntegrityError from sqlalchemy import or_ from sqlalchemy.orm import joinedload, joinedload_all from sqlalchemy.orm import RelationshipProperty @@ -450,12 +449,15 @@ def iscsi_target_create_safe(context, values): for (key, value) in values.iteritems(): iscsi_target_ref[key] = value session = get_session() - with session.begin(): - try: - iscsi_target_ref.save(session) + + try: + with session.begin(): + session.add(iscsi_target_ref) return iscsi_target_ref - except IntegrityError: - return None + # TODO(e0ne): Remove check on db_exc.DBError, when + # Cinder will use oslo.db 0.4.0 or higher. + except (db_exc.DBError, db_exc.DBDuplicateEntry): + return None ################### diff --git a/cinder/tests/test_db_api.py b/cinder/tests/test_db_api.py index 61a46c0f5..35594a88b 100644 --- a/cinder/tests/test_db_api.py +++ b/cinder/tests/test_db_api.py @@ -1124,11 +1124,11 @@ class DBAPIIscsiTargetTestCase(BaseTest): self.assertEqual(db.iscsi_target_count_by_host(self.ctxt, 'fake_host'), 3) - @test.testtools.skip("bug 1187367") def test_integrity_error(self): - db.iscsi_target_create_safe(self.ctxt, self._get_base_values()) - self.assertFalse(db.iscsi_target_create_safe(self.ctxt, - self._get_base_values())) + values = self._get_base_values() + values['id'] = 1 + db.iscsi_target_create_safe(self.ctxt, values) + self.assertFalse(db.iscsi_target_create_safe(self.ctxt, values)) class DBAPIBackupTestCase(BaseTest): -- 2.45.2