summaryrefslogtreecommitdiffstats
path: root/custodia/store/sqlite.py
diff options
context:
space:
mode:
authorSimo Sorce <simo@redhat.com>2015-04-08 09:05:17 -0400
committerSimo Sorce <simo@redhat.com>2015-04-08 09:11:05 -0400
commite538bf2ae154588dd45a8f01fe1cc1c08441de03 (patch)
treedc2c96ca1d92a09a049197674e2b503b2755c8fb /custodia/store/sqlite.py
parentfe108073bb183b3c52676aaabfb2bff829310ff0 (diff)
downloadcustodia-e538bf2ae154588dd45a8f01fe1cc1c08441de03.tar.gz
custodia-e538bf2ae154588dd45a8f01fe1cc1c08441de03.tar.xz
custodia-e538bf2ae154588dd45a8f01fe1cc1c08441de03.zip
Always initialize db on store class instantiation
This will precreate the default tale if it doesn't exist and fail early if there are general database issues. Also change all CSStoreError events to report a 500 error and not silently transform them into innocuous 'no such data' errors.
Diffstat (limited to 'custodia/store/sqlite.py')
-rw-r--r--custodia/store/sqlite.py25
1 files changed, 15 insertions, 10 deletions
diff --git a/custodia/store/sqlite.py b/custodia/store/sqlite.py
index f321678..5308674 100644
--- a/custodia/store/sqlite.py
+++ b/custodia/store/sqlite.py
@@ -22,6 +22,17 @@ class SqliteStore(CSStore):
else:
self.table = "CustodiaSecrets"
+ # Initialize the DB by trying to create the default table
+ try:
+ conn = sqlite3.connect(self.dburi)
+ with conn:
+ c = conn.cursor()
+ self._create(c)
+ except sqlite3.Error as err:
+ log_error("Error creating table %s: [%r]" % (self.table,
+ repr(err)))
+ raise CSStoreError('Error occurred while trying to init db')
+
def get(self, key):
query = "SELECT value from %s WHERE key=?" % self.table
try:
@@ -110,18 +121,12 @@ class SqliteStoreTests(unittest.TestCase):
pass
def test_0_get_empty(self):
- with self.assertRaises(CSStoreError) as err:
- self.store.get('test')
-
- self.assertEqual(str(err.exception),
- 'Error occurred while trying to get key')
+ value = self.store.get('test')
+ self.assertEqual(value, None)
def test_1_list_empty(self):
- with self.assertRaises(CSStoreError) as err:
- self.store.list('test')
-
- self.assertEqual(str(err.exception),
- 'Error occurred while trying to list keys')
+ value = self.store.list('test')
+ self.assertEqual(value, None)
def test_2_set_key(self):
self.store.set('key', 'value')