Ejemplo n.º 1
0
 def cache(self):
     LOG.debug(_(u'Loading proxy cache driver'))
     try:
         oslo_cache.register_oslo_configs(self.conf)
         mgr = oslo_cache.get_cache(self.conf.cache_url)
         return mgr
     except RuntimeError as exc:
         LOG.exception(exc)
         raise errors.InvalidDriver(exc)
Ejemplo n.º 2
0
 def cache(self):
     LOG.debug(u'Loading proxy cache driver')
     try:
         oslo_cache.register_oslo_configs(self.conf)
         mgr = oslo_cache.get_cache(self.conf.cache_url)
         return mgr
     except RuntimeError as exc:
         LOG.exception(exc)
         raise errors.InvalidDriver(exc)
    def test_cached(self):
        conf = cfg.ConfigOpts()
        oslo_cache.register_oslo_configs(conf)
        cache = oslo_cache.get_cache(conf.cache_url)

        sample_project = {
            u'name': u'Cats Abound',
            u'bits': b'\x80\x81\x82\x83\x84',
            b'key': u'Value. \x80',
        }

        def create_key(user, project=None):
            return user + ':' + str(project)

        class TestClass(object):

            def __init__(self, cache):
                self._cache = cache
                self.project_gets = 0
                self.project_dels = 0

            @decorators.caches(create_key, 60)
            def get_project(self, user, project=None):
                self.project_gets += 1
                return sample_project

            @get_project.purges
            def del_project(self, user, project=None):
                self.project_dels += 1

        instance = TestClass(cache)

        args = ('23', 'cats')

        project = instance.get_project(*args)
        self.assertEqual(project, sample_project)
        self.assertEqual(instance.project_gets, 1)

        # Should be in the cache now.
        project = msgpack.unpackb(cache.get(create_key(*args)),
                                  encoding='utf-8')
        self.assertEqual(project, sample_project)

        # Should read from the cache this time (counter will not
        # be incremented).
        project = instance.get_project(*args)
        self.assertEqual(project, sample_project)
        self.assertEqual(instance.project_gets, 1)

        # Use kwargs this time
        instance.del_project('23', project='cats')
        self.assertEqual(instance.project_dels, 1)

        # Should be a cache miss since we purged (above)
        project = instance.get_project(*args)
        self.assertEqual(instance.project_gets, 2)
Ejemplo n.º 4
0
    def test_cached(self):
        conf = cfg.ConfigOpts()
        oslo_cache.register_oslo_configs(conf)
        cache = oslo_cache.get_cache(conf.cache_url)

        sample_project = {
            u'name': u'Cats Abound',
            u'bits': b'\x80\x81\x82\x83\x84',
            b'key': u'Value. \x80',
        }

        def create_key(user, project=None):
            return user + ':' + str(project)

        class TestClass(object):
            def __init__(self, cache):
                self._cache = cache
                self.project_gets = 0
                self.project_dels = 0

            @decorators.caches(create_key, 60)
            def get_project(self, user, project=None):
                self.project_gets += 1
                return sample_project

            @get_project.purges
            def del_project(self, user, project=None):
                self.project_dels += 1

        instance = TestClass(cache)

        args = ('23', 'cats')

        project = instance.get_project(*args)
        self.assertEqual(project, sample_project)
        self.assertEqual(instance.project_gets, 1)

        # Should be in the cache now.
        project = msgpack.unpackb(cache.get(create_key(*args)),
                                  encoding='utf-8')
        self.assertEqual(project, sample_project)

        # Should read from the cache this time (counter will not
        # be incremented).
        project = instance.get_project(*args)
        self.assertEqual(project, sample_project)
        self.assertEqual(instance.project_gets, 1)

        # Use kwargs this time
        instance.del_project('23', project='cats')
        self.assertEqual(instance.project_dels, 1)

        # Should be a cache miss since we purged (above)
        project = instance.get_project(*args)
        self.assertEqual(instance.project_gets, 2)
    def test_cached_with_cond(self):
        conf = cfg.ConfigOpts()
        oslo_cache.register_oslo_configs(conf)
        cache = oslo_cache.get_cache(conf.cache_url)

        class TestClass(object):

            def __init__(self, cache):
                self._cache = cache
                self.user_gets = 0

            @decorators.caches(lambda x: x, 60, lambda v: v != 'kgriffs')
            def get_user(self, name):
                self.user_gets += 1
                return name

        instance = TestClass(cache)

        name = 'malini'

        user = instance.get_user(name)
        self.assertEqual(user, name)
        self.assertEqual(instance.user_gets, 1)

        # Should be in the cache now.
        user = msgpack.unpackb(cache.get(name), encoding='utf-8')
        self.assertEqual(user, name)

        # Should read from the cache this time (counter will not
        # be incremented).
        user = instance.get_user(name)
        self.assertEqual(user, name)
        self.assertEqual(instance.user_gets, 1)

        # Won't go into the cache because of cond
        name = 'kgriffs'
        for i in range(3):
            user = instance.get_user(name)

            self.assertEqual(cache.get(name), None)

            self.assertEqual(user, name)
            self.assertEqual(instance.user_gets, 2 + i)
Ejemplo n.º 6
0
    def test_cached_with_cond(self):
        conf = cfg.ConfigOpts()
        oslo_cache.register_oslo_configs(conf)
        cache = oslo_cache.get_cache(conf.cache_url)

        class TestClass(object):
            def __init__(self, cache):
                self._cache = cache
                self.user_gets = 0

            @decorators.caches(lambda x: x, 60, lambda v: v != 'kgriffs')
            def get_user(self, name):
                self.user_gets += 1
                return name

        instance = TestClass(cache)

        name = 'malini'

        user = instance.get_user(name)
        self.assertEqual(user, name)
        self.assertEqual(instance.user_gets, 1)

        # Should be in the cache now.
        user = msgpack.unpackb(cache.get(name), encoding='utf-8')
        self.assertEqual(user, name)

        # Should read from the cache this time (counter will not
        # be incremented).
        user = instance.get_user(name)
        self.assertEqual(user, name)
        self.assertEqual(instance.user_gets, 1)

        # Won't go into the cache because of cond
        name = 'kgriffs'
        for i in range(3):
            user = instance.get_user(name)

            self.assertEqual(cache.get(name), None)

            self.assertEqual(user, name)
            self.assertEqual(instance.user_gets, 2 + i)
Ejemplo n.º 7
0
    def setUp(self):
        super(ControllerBaseTest, self).setUp()

        if not self.driver_class:
            self.skipTest('No driver class specified')

        if not issubclass(self.controller_class, self.controller_base_class):
            self.skipTest('{0} is not an instance of {1}. '
                          'Tests not supported'.format(
                              self.controller_class,
                              self.controller_base_class))

        oslo_cache.register_oslo_configs(self.conf)
        cache = oslo_cache.get_cache(self.conf.cache_url)

        self.conf.register_opts(bootstrap._GENERAL_OPTIONS)
        pooling = 'pooling' in self.conf and self.conf.pooling
        if pooling and not self.control_driver_class:
            self.skipTest("Pooling is enabled, "
                          "but control driver class is not specified")

        if not pooling:
            self.driver = self.driver_class(self.conf, cache)
        else:
            control = self.control_driver_class(self.conf, cache)
            uri = "sqlite:///:memory:"
            for i in range(4):
                control.pools_controller.create(six.text_type(i), 100, uri)
            self.driver = self.driver_class(self.conf, cache, control)

        self._prepare_conf()

        self.addCleanup(self._purge_databases)

        if not pooling:
            self.controller = self.controller_class(self.driver)
        else:
            self.controller = self.controller_class(self.driver._pool_catalog)
Ejemplo n.º 8
0
    def setUp(self):
        super(ControllerBaseTest, self).setUp()

        if not self.driver_class:
            self.skipTest('No driver class specified')

        if not issubclass(self.controller_class, self.controller_base_class):
            self.skipTest('{0} is not an instance of {1}. '
                          'Tests not supported'.format(
                              self.controller_class,
                              self.controller_base_class))

        oslo_cache.register_oslo_configs(self.conf)
        cache = oslo_cache.get_cache(self.conf.cache_url)

        # pylint: disable=not-callable
        self.driver = self.driver_class(self.conf, cache)
        self._prepare_conf()

        self.addCleanup(self._purge_databases)

        # pylint: disable=not-callable
        self.controller = self.controller_class(self.driver)
Ejemplo n.º 9
0
    def setUp(self):
        super(ControllerBaseTest, self).setUp()

        if not self.driver_class:
            self.skipTest('No driver class specified')

        if not issubclass(self.controller_class, self.controller_base_class):
            self.skipTest('{0} is not an instance of {1}. '
                          'Tests not supported'.format(
                              self.controller_class,
                              self.controller_base_class))

        oslo_cache.register_oslo_configs(self.conf)
        cache = oslo_cache.get_cache(self.conf.cache_url)

        # pylint: disable=not-callable
        self.driver = self.driver_class(self.conf, cache)
        self._prepare_conf()

        self.addCleanup(self._purge_databases)

        # pylint: disable=not-callable
        self.controller = self.controller_class(self.driver)