mirror of
https://github.com/quay/quay.git
synced 2026-01-26 06:21:37 +03:00
* Add dev dependencies mypy and typing * Add makefile target `types-test`, not yet included in `test` target. * Generate stubs for imported modules to avoid mypy complaining about missing types. * Remove generated stubs as there are way too many and they cause tons of mess in the repo. Switched to ignoring untyped modules for now, to concentrate on Quay-only type checking. * mypy config changed to ignore missing imports * ignore property decorator as it is not supported by mypy * mypy annotations for many configuration variables * re-generate mypy_stubs directory as its necessary in some classes for base classes to prevent mypy errors * util/registry/queuefile referred to non existent definition of Empty class in multiprocessing.queues * ignore type checking for things like monkey patching and exported/re-imported objects that mypy does not allow. * Adjust mypy config to warn us about unreachable return paths and useless expressions. * Add the __annotations__ property to INTERNAL_ONLY_PROPERTIES so that it is not part of the config schema testing * Remove redundant dependencies `typing` and `typing-extensions` which are NOOP after Python 3.5 * Remove mypy-extensions which only provides a TypedDict implementation but has not been updated since 2019. * updated mypy to 0.910 which requires all types packages to be installed manually. * exclude local-dev from type checking until core team can suggest an outcome for __init__.py duplicate packages * re-add typing dependency which will be needed until Python 3.9 * ignore .mypy_cache * add mypy stub for features module to replace inline definitions * import annotations eager evaluation in billing.py as it was required to reference a class declared later in the module. * remove the type definition of V1ProtocolSteps/V2ProtocolSteps to make tox happy
157 lines
4.4 KiB
Python
157 lines
4.4 KiB
Python
import pytest
|
|
from typing import Dict, Any
|
|
from unittest.mock import patch, MagicMock
|
|
from rediscluster.nodemanager import NodeManager
|
|
|
|
from data.cache import (
|
|
InMemoryDataModelCache,
|
|
NoopDataModelCache,
|
|
MemcachedModelCache,
|
|
RedisDataModelCache,
|
|
)
|
|
from data.cache.cache_key import CacheKey
|
|
from data.cache.redis_cache import (
|
|
redis_cache_from_config,
|
|
REDIS_DRIVERS,
|
|
ReadEndpointSupportedRedis,
|
|
)
|
|
|
|
|
|
DATA: Dict[str, Any] = {}
|
|
|
|
TEST_CACHE_CONFIG = {
|
|
"repository_blob_cache_ttl": "240s",
|
|
"catalog_page_cache_ttl": "240s",
|
|
"namespace_geo_restrictions_cache_ttl": "240s",
|
|
"active_repo_tags_cache_ttl": "240s",
|
|
"appr_applications_list_cache_ttl": "3600s",
|
|
"appr_show_package_cache_ttl": "3600s",
|
|
}
|
|
|
|
|
|
class MockClient(object):
|
|
def __init__(self, **kwargs):
|
|
pass
|
|
|
|
def get(self, key, default=None):
|
|
return DATA.get(key, default)
|
|
|
|
def set(self, key, value, expire=None):
|
|
DATA[key] = value
|
|
|
|
def close(self):
|
|
pass
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"cache_type",
|
|
[
|
|
(NoopDataModelCache),
|
|
(InMemoryDataModelCache),
|
|
],
|
|
)
|
|
def test_caching(cache_type):
|
|
key = CacheKey("foo", "60m")
|
|
cache = cache_type(TEST_CACHE_CONFIG)
|
|
|
|
# Perform two retrievals, and make sure both return.
|
|
assert cache.retrieve(key, lambda: {"a": 1234}) == {"a": 1234}
|
|
assert cache.retrieve(key, lambda: {"a": 1234}) == {"a": 1234}
|
|
|
|
|
|
def test_memcache():
|
|
global DATA
|
|
DATA = {}
|
|
|
|
key = CacheKey("foo", "60m")
|
|
with patch("data.cache.impl.PooledClient", MockClient):
|
|
cache = MemcachedModelCache(TEST_CACHE_CONFIG, ("127.0.0.1", "-1"))
|
|
assert cache.retrieve(key, lambda: {"a": 1234}) == {"a": 1234}
|
|
assert cache.retrieve(key, lambda: {"a": 1234}) == {"a": 1234}
|
|
|
|
|
|
def test_memcache_should_cache():
|
|
global DATA
|
|
DATA = {}
|
|
|
|
key = CacheKey("foo", None)
|
|
|
|
def sc(value):
|
|
return value["a"] != 1234
|
|
|
|
with patch("data.cache.impl.PooledClient", MockClient):
|
|
cache = MemcachedModelCache(TEST_CACHE_CONFIG, ("127.0.0.1", "-1"))
|
|
assert cache.retrieve(key, lambda: {"a": 1234}, should_cache=sc) == {"a": 1234}
|
|
|
|
# Ensure not cached since it was `1234`.
|
|
assert cache._get_client_pool().get(key.key) is None
|
|
|
|
# Ensure cached.
|
|
assert cache.retrieve(key, lambda: {"a": 2345}, should_cache=sc) == {"a": 2345}
|
|
assert cache._get_client_pool().get(key.key) is not None
|
|
assert cache.retrieve(key, lambda: {"a": 2345}, should_cache=sc) == {"a": 2345}
|
|
|
|
|
|
def test_redis_cache():
|
|
global DATA
|
|
DATA = {}
|
|
|
|
key = CacheKey("foo", "60m")
|
|
cache = RedisDataModelCache(TEST_CACHE_CONFIG, MockClient())
|
|
|
|
assert cache.retrieve(key, lambda: {"a": 1234}) == {"a": 1234}
|
|
assert cache.retrieve(key, lambda: {"a": 1234}) == {"a": 1234}
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"cache_config, expected_exception",
|
|
[
|
|
pytest.param(
|
|
{
|
|
"engine": "rediscluster",
|
|
"redis_config": {
|
|
"startup_nodes": [{"host": "127.0.0.1", "port": "6379"}],
|
|
"password": "redisPassword",
|
|
},
|
|
},
|
|
None,
|
|
id="rediscluster",
|
|
),
|
|
pytest.param(
|
|
{
|
|
"engine": "redis",
|
|
"redis_config": {
|
|
"primary": {"host": "127.0.0.1", "password": "redisPassword"},
|
|
},
|
|
},
|
|
None,
|
|
id="redis",
|
|
),
|
|
pytest.param(
|
|
{
|
|
"engine": "memcached",
|
|
"endpoint": "127.0.0.1",
|
|
},
|
|
(ValueError, "Invalid Redis driver for cache model"),
|
|
id="invalid engine for redis",
|
|
),
|
|
pytest.param(
|
|
{
|
|
"engine": "redis",
|
|
"redis_config": {},
|
|
},
|
|
(ValueError, "Invalid Redis config for redis"),
|
|
id="invalid config for redis",
|
|
),
|
|
],
|
|
)
|
|
def test_redis_cache_config(cache_config, expected_exception):
|
|
with patch("rediscluster.nodemanager.NodeManager.initialize", MagicMock):
|
|
if expected_exception is not None:
|
|
with pytest.raises(expected_exception[0]) as e:
|
|
rc = redis_cache_from_config(cache_config)
|
|
assert str(e.value) == expected_exception[1]
|
|
else:
|
|
rc = redis_cache_from_config(cache_config)
|
|
assert isinstance(rc, REDIS_DRIVERS[cache_config["engine"]])
|