Ensured the global database instance initialized by rhnSQL is not
reused when there's a database connection error.

The bug emerged while running the whole test suite: all the tests
executed after `test_exceptions` failed because they weren't able to
connect to the database.
---
 backend/server/rhnSQL/__init__.py                       |  4 ++++
 backend/server/test/unit-test/rhnSQL/test_exceptions.py | 17 +++++++++++++++++
 2 files changed, 21 insertions(+)

diff --git a/backend/server/rhnSQL/__init__.py 
b/backend/server/rhnSQL/__init__.py
index 1a58d07..c910a67 100644
--- a/backend/server/rhnSQL/__init__.py
+++ b/backend/server/rhnSQL/__init__.py
@@ -111,6 +111,10 @@ def initDB(backend=None, host=None, port=None, 
username=None,
 #        raise  # pass on, we know those ones
 #    except (KeyboardInterrupt, SystemExit):
 #        raise
+    except SQLConnectError, e:
+        global __DB
+        del __DB
+        raise e
     except:
         raise
         #e_type, e_value = sys.exc_info()[:2]
diff --git a/backend/server/test/unit-test/rhnSQL/test_exceptions.py 
b/backend/server/test/unit-test/rhnSQL/test_exceptions.py
index 4d1b25d..13275fc 100644
--- a/backend/server/test/unit-test/rhnSQL/test_exceptions.py
+++ b/backend/server/test/unit-test/rhnSQL/test_exceptions.py
@@ -5,8 +5,14 @@
 #
 
 import unittest
+from spacewalk.common.rhnConfig import CFG, initCFG
 from spacewalk.server import rhnSQL
 
+import misc_functions
+
+DB_SETTINGS = misc_functions.db_settings("oracle")
+
+
 class ExceptionsTest(unittest.TestCase):
     def test_failed_connection(self):
         # Connect to localhost and look for db on a totally bogus port, this
@@ -28,5 +34,16 @@ class ExceptionsTest(unittest.TestCase):
             password
         )
 
+        try:
+            rhnSQL.initDB(
+                backend  = "oracle",
+                username = DB_SETTINGS["user"],
+                password = DB_SETTINGS["password"],
+                database = DB_SETTINGS["database"]
+            )
+        except:
+            self.fail("Exception raised while trying to connect to the db 
using proper settings. That's not expected to happen.")
+
+
 if __name__ == '__main__':
     unittest.main()
-- 
1.8.4

_______________________________________________
Spacewalk-devel mailing list
Spacewalk-devel@redhat.com
https://www.redhat.com/mailman/listinfo/spacewalk-devel

Reply via email to