Create directory for database if it doesn't exist

Change-Id: Iea46947af8057c4c90f870c3b3b5725740b93b34
This commit is contained in:
Dmitry Tantsur 2015-01-20 10:13:34 +01:00
parent 80ea6712e5
commit 947dcc1dcf
2 changed files with 11 additions and 0 deletions

View File

@ -15,6 +15,7 @@
import json
import logging
import os
import sqlite3
import sys
import time
@ -104,6 +105,9 @@ def init():
LOG.critical('Configuration option discoverd.database should be set')
sys.exit(1)
db_dir = os.path.dirname(_DB_NAME)
if db_dir and not os.path.exists(db_dir):
os.makedirs(db_dir)
sqlite3.connect(_DB_NAME).executescript(_SCHEMA)

View File

@ -11,6 +11,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import os
import tempfile
import time
import unittest
@ -268,6 +269,12 @@ class TestInit(unittest.TestCase):
# Verify that table exists
node_cache._db().execute("select * from nodes")
def test_create_dir(self):
temp = tempfile.mkdtemp()
conf.CONF.set('discoverd', 'database',
os.path.join(temp, 'dir', 'file'))
node_cache.init()
def test_no_database(self):
self.assertRaises(SystemExit, node_cache.init)