Add a 'dsn' type for matching sqlalchemy-ish dsns.

Change-Id: I48f8fddd7c1add19719c475d9427650d38a4d150
This commit is contained in:
Clint Byrum 2013-06-19 01:48:25 -07:00
parent ba51abbdf1
commit 9d9dfa69d0
3 changed files with 35 additions and 1 deletions

View File

@ -186,7 +186,7 @@ def parse_opts(argv):
parser.add_argument('--type', default='default',
help='exit with error if the specified --key does not'
' match type. Valid types are'
' <int|default|netaddress|raw>')
' <int|default|netaddress|dsn|raw>')
parser.add_argument('--key-default',
help='This option only affects running with --key.'
' Print this if key is not found. This value is'

View File

@ -67,3 +67,32 @@ class ValueTypeTestCase(testtools.TestCase):
def test_net_address_bad(self):
self.assertRaises(config_exception.ConfigException,
value_types.ensure_type, "192.0.2.1;DROP TABLE foo")
def test_dsn_nopass(self):
test_dsn = 'mysql://user@host/db'
self.assertEqual(test_dsn, value_types.ensure_type(test_dsn, 'dsn'))
def test_dsn(self):
test_dsn = 'mysql://user:pass@host/db'
self.assertEqual(test_dsn, value_types.ensure_type(test_dsn, 'dsn'))
def test_dsn_set_variables(self):
test_dsn = 'mysql://user:pass@host/db?charset=utf8'
self.assertEqual(test_dsn, value_types.ensure_type(test_dsn, 'dsn'))
def test_dsn_sqlite_memory(self):
test_dsn = 'sqlite://'
self.assertEqual(test_dsn, value_types.ensure_type(test_dsn, 'dsn'))
def test_dsn_sqlite_file(self):
test_dsn = 'sqlite:///tmp/foo.db'
self.assertEqual(test_dsn, value_types.ensure_type(test_dsn, 'dsn'))
def test_dsn_bad(self):
self.assertRaises(config_exception.ConfigException,
value_types.ensure_type,
"mysql:/user:pass@host/db?charset=utf8", 'dsn')
self.assertRaises(config_exception.ConfigException,
value_types.ensure_type,
"mysql://user:pass@host/db?charset=utf8;DROP TABLE "
"foo", 'dsn')

View File

@ -21,6 +21,11 @@ TYPES = {
"int": "^[0-9]+$",
"default": "^[A-Za-z0-9_]*$",
"netaddress": "^[A-Za-z0-9/.:-]+$",
"dsn": "(?#driver)^[a-zA-Z0-9]+://"
"(?#username[:password])([a-zA-Z0-9+_-]+(:[^@]+)?)?"
"(?#@host or file)(@?[a-zA-Z0-9/_.-]+)?"
"(?#/dbname)(/[a-zA-Z0-9_-]+)?"
"(?#?variable=value)(\?[a-zA-Z0-9=_-]+)?$",
"raw": ""
}