Esempio n. 1
0
 def test_can_be_enabled(self):
     options = {
         'image_cache_enabled': 'True',
         'image_cache_datadir': '/some/place'
     }
     cache = image_cache.ImageCache(options)
     self.assertEqual(cache.enabled, True)
Esempio n. 2
0
    def setUp(self):
        """
        Test to see if the pre-requisites for the image cache
        are working (python-sqlite3 installed)
        """
        super(TestImageCacheSqlite, self).setUp()

        if getattr(self, 'disable', False):
            return

        if not getattr(self, 'inited', False):
            try:
                import sqlite3
            except ImportError:
                self.inited = True
                self.disabled = True
                self.disabled_message = ("python-sqlite3 not installed.")
                return

        self.inited = True
        self.disabled = False
        self.cache_dir = os.path.join("/", "tmp", "test.cache.%d" %
                                      random.randint(0, 1000000))
        self.config(image_cache_dir=self.cache_dir,
                    image_cache_driver='sqlite',
                    image_cache_max_size=1024 * 5,
                    registry_host='127.0.0.1',
                    registry_port=9191)
        self.cache = image_cache.ImageCache()
Esempio n. 3
0
    def setUp(self):
        """
        Test to see if the pre-requisites for the image cache
        are working (python-sqlite3 installed)
        """
        if getattr(self, 'disable', False):
            return

        if not getattr(self, 'inited', False):
            try:
                import sqlite3
            except ImportError:
                self.inited = True
                self.disabled = True
                self.disabled_message = ("python-sqlite3 not installed.")
                return

        self.inited = True
        self.disabled = False
        self.cache_dir = os.path.join("/", "tmp", "test.cache.%d" %
                                      random.randint(0, 1000000))
        self.conf = test_utils.TestConfigOpts({
                'image_cache_dir': self.cache_dir,
                'image_cache_driver': 'sqlite',
                'image_cache_max_size': 1024 * 5,
                'registry_host': '0.0.0.0',
                'registry_port': 9191})
        self.cache = image_cache.ImageCache(self.conf)
Esempio n. 4
0
    def setUp(self):
        """
        Test to see if the pre-requisites for the image cache
        are working (python-xattr installed and xattr support on the
        filesystem)
        """
        super(TestImageCacheXattr, self).setUp()

        if getattr(self, 'disable', False):
            return

        self.cache_dir = self.useFixture(fixtures.TempDir()).path

        if not getattr(self, 'inited', False):
            try:
                import xattr  # noqa
            except ImportError:
                self.inited = True
                self.disabled = True
                self.disabled_message = ("python-xattr not installed.")
                return

        self.inited = True
        self.disabled = False
        self.config(image_cache_dir=self.cache_dir,
                    image_cache_driver='xattr',
                    image_cache_max_size=5 * units.Ki)
        self.cache = image_cache.ImageCache()

        if not xattr_writes_supported(self.cache_dir):
            self.inited = True
            self.disabled = True
            self.disabled_message = ("filesystem does not support xattr")
            return
Esempio n. 5
0
    def setUp(self):
        """
        Test to see if the pre-requisites for the image cache
        are working (python-sqlite3 installed)
        """
        super(TestImageCacheSqlite, self).setUp()

        if getattr(self, 'disable', False):
            return

        if not getattr(self, 'inited', False):
            try:
                import sqlite3  # noqa
            except ImportError:
                self.inited = True
                self.disabled = True
                self.disabled_message = ("python-sqlite3 not installed.")
                return

        self.inited = True
        self.disabled = False
        self.cache_dir = self.useFixture(fixtures.TempDir()).path
        self.config(image_cache_dir=self.cache_dir,
                    image_cache_driver='sqlite',
                    image_cache_max_size=5 * units.Ki)
        self.cache = image_cache.ImageCache()
Esempio n. 6
0
    def test_gate_caching_iter_bad_checksum(self):
        image = "12345678990abcdefghijklmnop"
        image_id = 123
        checksum = "foobar"  # bad.

        cache = image_cache.ImageCache()
        img_iter = cache.get_caching_iter(image_id, checksum, image)

        def reader():
            for chunk in img_iter:
                pass
        # checksum is invalid, caching will fail:
        self.assertFalse(cache.is_cached(image_id))
Esempio n. 7
0
    def test_gate_caching_iter_good_checksum(self):
        image = b"12345678990abcdefghijklmnop"
        image_id = 123

        md5 = hashlib.md5()
        md5.update(image)
        checksum = md5.hexdigest()

        cache = image_cache.ImageCache()
        img_iter = cache.get_caching_iter(image_id, checksum, [image])
        for chunk in img_iter:
            pass
        # checksum is valid, fake image should be cached:
        self.assertTrue(cache.is_cached(image_id))
Esempio n. 8
0
    def test_get_caching_iter_when_open_fails(self):
        class OpenFailingDriver(object):
            def is_cacheable(self, *args, **kwargs):
                return True

            @contextmanager
            def open_for_write(self, *args, **kwargs):
                raise IOError

        self.driver = OpenFailingDriver()
        cache = image_cache.ImageCache()
        data = [b'a', b'b', b'c', b'd', b'e', b'f']

        caching_iter = cache.get_caching_iter('dummy_id', None, iter(data))
        self.assertEqual(data, list(caching_iter))
    def test_get_caching_iter_when_open_fails(self):
        class OpenFailingDriver(object):
            def is_cacheable(self, *args, **kwargs):
                return True

            @contextmanager
            def open_for_write(self, *args, **kwargs):
                raise IOError

        self.driver = OpenFailingDriver()
        conf = cfg.ConfigOpts()
        cache = image_cache.ImageCache(conf)
        data = ['a', 'b', 'c', 'd', 'e', 'f']

        caching_iter = cache.get_caching_iter('dummy_id', iter(data))
        self.assertEqual(list(caching_iter), data)
Esempio n. 10
0
    def test_gate_caching_iter_s3_chunked_file(self):
        """Tests get_caching_iter when using an S3 ChunkedFile"""
        image_id = 123

        with tempfile.NamedTemporaryFile() as test_data_file:
            test_data_file.write(FIXTURE_DATA)
            test_data_file.seek(0)
            image = s3_store.ChunkedFile(test_data_file)
            md5 = hashlib.md5()
            md5.update(FIXTURE_DATA)
            checksum = md5.hexdigest()

            cache = image_cache.ImageCache()
            img_iter = cache.get_caching_iter(image_id, checksum, image)
            for chunk in img_iter:
                pass
            # checksum is valid, fake image should be cached:
            self.assertTrue(cache.is_cached(image_id))
Esempio n. 11
0
    def test_get_caching_iter_when_write_fails(self):
        class FailingFile(object):
            def write(self, data):
                if data == "Fail":
                    raise IOError

        class FailingFileDriver(object):
            def is_cacheable(self, *args, **kwargs):
                return True

            @contextmanager
            def open_for_write(self, *args, **kwargs):
                yield FailingFile()

        self.driver = FailingFileDriver()
        cache = image_cache.ImageCache()
        data = ['a', 'b', 'c', 'Fail', 'd', 'e', 'f']

        caching_iter = cache.get_caching_iter('dummy_id', None, iter(data))
        self.assertEqual(list(caching_iter), data)
Esempio n. 12
0
    def setUp(self):
        """
        Test to see if the pre-requisites for the image cache
        are working (python-xattr installed and xattr support on the
        filesystem)
        """
        super(TestImageCacheXattr, self).setUp()

        if getattr(self, 'disable', False):
            return

        self.cache_dir = os.path.join("/", "tmp", "test.cache.%d" %
                                      random.randint(0, 1000000))
        utils.safe_mkdirs(self.cache_dir)

        if not getattr(self, 'inited', False):
            try:
                import xattr
            except ImportError:
                self.inited = True
                self.disabled = True
                self.disabled_message = ("python-xattr not installed.")
                return

        self.inited = True
        self.disabled = False
        self.config(image_cache_dir=self.cache_dir,
                    image_cache_driver='xattr',
                    image_cache_max_size=1024 * 5,
                    registry_host='127.0.0.1',
                    registry_port=9191)
        self.cache = image_cache.ImageCache()

        if not xattr_writes_supported(self.cache_dir):
            self.inited = True
            self.disabled = True
            self.disabled_message = ("filesystem does not support xattr")
            return
    def setUp(self):
        """
        Test to see if the pre-requisites for the image cache
        are working (python-xattr installed and xattr support on the
        filesystem)
        """
        if getattr(self, 'disable', False):
            return

        self.cache_dir = os.path.join(
            "/", "tmp", "test.cache.%d" % random.randint(0, 1000000))
        utils.safe_mkdirs(self.cache_dir)

        if not getattr(self, 'inited', False):
            try:
                import xattr
            except ImportError:
                self.inited = True
                self.disabled = True
                self.disabled_message = ("python-xattr not installed.")
                return

        self.inited = True
        self.disabled = False
        self.conf = test_utils.TestConfigOpts({
            'image_cache_dir': self.cache_dir,
            'image_cache_driver': 'xattr',
            'image_cache_max_size': 1024 * 5,
            'registry_host': '0.0.0.0',
            'registry_port': 9191
        })
        self.cache = image_cache.ImageCache(self.conf)

        if not xattr_writes_supported(self.cache_dir):
            self.inited = True
            self.disabled = True
            self.disabled_message = ("filesystem does not support xattr")
            return
Esempio n. 14
0
 def __init__(self, app, options):
     self.options = options
     self.cache = image_cache.ImageCache(options)
     self.serializer = images.ImageSerializer()
     logger.info(_("Initialized image cache middleware"))
     super(CacheFilter, self).__init__(app)
Esempio n. 15
0
 def test_enabled_defaults_to_false(self):
     options = {}
     cache = image_cache.ImageCache(options)
     self.assertEqual(cache.enabled, False)
Esempio n. 16
0
 def __init__(self, options):
     self.options = options
     self.cache = image_cache.ImageCache(self.options)
Esempio n. 17
0
 def __init__(self, conf):
     self.conf = conf
     self.cache = image_cache.ImageCache(self.conf)
Esempio n. 18
0
 def __init__(self, app, conf, **local_conf):
     self.conf = conf
     self.cache = image_cache.ImageCache(conf)
     self.serializer = images.ImageSerializer()
     logger.info(_("Initialized image cache middleware"))
     super(CacheFilter, self).__init__(app)
Esempio n. 19
0
 def __init__(self, app):
     self.cache = image_cache.ImageCache()
     self.serializer = images.ImageSerializer()
     LOG.info(_("Initialized image cache middleware"))
     super(CacheFilter, self).__init__(app)
Esempio n. 20
0
 def __init__(self, app):
     self.cache = image_cache.ImageCache()
     self.policy = policy.Enforcer()
     LOG.info(_LI("Initialized image cache middleware"))
     super(CacheFilter, self).__init__(app)
Esempio n. 21
0
 def __init__(self):
     self.cache = image_cache.ImageCache()
     self.policy = policy.Enforcer()
Esempio n. 22
0
        def get_from_cache(image, cache):
            """Called if cache hit"""
            with cache.open(image, "rb") as cache_file:
                chunks = utils.chunkiter(cache_file)
                for chunk in chunks:
                    yield chunk

        def get_from_store_tee_into_cache(image, cache):
            """Called if cache miss"""
            with cache.open(image, "wb") as cache_file:
                chunks = get_from_store(image)
                for chunk in chunks:
                    cache_file.write(chunk)
                    yield chunk

        cache = image_cache.ImageCache(self.options)
        if cache.enabled:
            if cache.hit(id):
                # hit
                logger.debug(_("image '%s' is a cache HIT"), id)
                image_iterator = get_from_cache(image, cache)
            else:
                # miss
                logger.debug(_("image '%s' is a cache MISS"), id)

                # Make sure we're not already prefetching or caching the image
                # that just generated the miss
                if cache.is_image_currently_prefetching(id):
                    logger.debug(
                        _("image '%s' is already being prefetched,"
                          " not tee'ing into the cache"), id)
 def __init__(self, conf):
     self.conf = conf
     self.cache = image_cache.ImageCache(self.conf)
     self.policy = policy.Enforcer(conf)