Merge "Allow connection string to be just backend name"

This commit is contained in:
Jenkins
2014-03-07 08:23:36 +00:00
committed by Gerrit Code Review
3 changed files with 30 additions and 17 deletions

View File

@@ -15,8 +15,8 @@
# under the License.
import logging
import re
from six.moves import urllib_parse
from stevedore import driver
from taskflow import exceptions as exc
@@ -25,11 +25,22 @@ from taskflow import exceptions as exc
# NOTE(harlowja): this is the entrypoint namespace, not the module namespace.
BACKEND_NAMESPACE = 'taskflow.persistence'
# NOTE(imelnikov): regular expression to get scheme from URI,
# see RFC 3986 section 3.1
SCHEME_REGEX = re.compile(r"^([A-Za-z]{1}[A-Za-z0-9+.-]*):")
LOG = logging.getLogger(__name__)
def fetch(conf, namespace=BACKEND_NAMESPACE):
backend_name = urllib_parse.urlparse(conf['connection']).scheme
connection = conf['connection']
match = SCHEME_REGEX.match(connection)
if match:
backend_name = match.group(1)
else:
backend_name = connection
LOG.debug('Looking for %r backend driver in %r', backend_name, namespace)
try:
mgr = driver.DriverManager(namespace, backend_name,

View File

@@ -46,19 +46,16 @@ class DirPersistenceTest(test.TestCase, base.PersistenceTestMixin):
shutil.rmtree(self.path)
self.path = None
def test_dir_persistence_entry_point(self):
conf = {
'connection': 'dir:',
'path': self.path
}
backend = backends.fetch(conf)
self.assertIsInstance(backend, impl_dir.DirBackend)
backend.close()
def test_file_persistence_entry_point(self):
conf = {
'connection': 'file:',
'path': self.path
}
def _check_backend(self, conf):
with contextlib.closing(backends.fetch(conf)) as be:
self.assertIsInstance(be, impl_dir.DirBackend)
def test_dir_backend_entry_point(self):
self._check_backend(dict(connection='dir:', path=self.path))
def test_dir_backend_name(self):
self._check_backend(dict(connection='dir', # no colon
path=self.path))
def test_file_backend_entry_point(self):
self._check_backend(dict(connection='file:', path=self.path))

View File

@@ -36,7 +36,12 @@ class MemoryPersistenceTest(test.TestCase, base.PersistenceTestMixin):
self._backend = None
super(MemoryPersistenceTest, self).tearDown()
def test_memory_persistence_entry_point(self):
def test_memory_backend_entry_point(self):
conf = {'connection': 'memory:'}
with contextlib.closing(backends.fetch(conf)) as be:
self.assertIsInstance(be, impl_memory.MemoryBackend)
def test_memory_backend_fetch_by_name(self):
conf = {'connection': 'memory'} # note no colon
with contextlib.closing(backends.fetch(conf)) as be:
self.assertIsInstance(be, impl_memory.MemoryBackend)