|
| 1 | +# pylint: disable=import-outside-toplevel |
| 2 | + |
| 3 | +import importlib.util |
| 4 | +import os |
| 5 | + |
| 6 | +import pytest |
| 7 | + |
| 8 | +@pytest.fixture |
| 9 | +def require_redis() -> None: |
| 10 | + if not importlib.util.find_spec("redis"): |
| 11 | + pytest.skip("redis is missing, skipping") |
| 12 | + |
| 13 | + |
| 14 | +@pytest.mark.usefixtures("require_redis") |
| 15 | +@pytest.fixture |
| 16 | +def require_redis_server() -> None: |
| 17 | + should_test_with_redis = os.getenv("TEST_REDIS_SERVER", None) |
| 18 | + if not should_test_with_redis: |
| 19 | + pytest.skip("No Redis server (env TEST_REDIS_SERVER isn't True), skipping") |
| 20 | + |
| 21 | +@pytest.fixture |
| 22 | +def redis_host() -> str: |
| 23 | + return os.getenv("REDIS_HOST", "localhost") |
| 24 | + |
| 25 | +@pytest.mark.usefixtures("require_redis", "require_redis_server") |
| 26 | +def test_aioredis_health(redis_host: str) -> None: |
| 27 | + from redis.asyncio import Redis as AioRedis |
| 28 | + from pyctuator.health.health_provider import Status |
| 29 | + from pyctuator.health.aioredis_health_provider import AioRedisHealthProvider, RedisHealthStatus, RedisHealthDetails |
| 30 | + |
| 31 | + redis_instance = AioRedis(host=redis_host) |
| 32 | + |
| 33 | + health = AioRedisHealthProvider(redis_instance).get_health() |
| 34 | + assert health == RedisHealthStatus(Status.UP, RedisHealthDetails("5.0.3", "standalone")) |
| 35 | + |
| 36 | + |
| 37 | +@pytest.mark.usefixtures("require_redis", "require_redis_server") |
| 38 | +def test_aioredis_bad_password(redis_host: str) -> None: |
| 39 | + from redis.asyncio import Redis as AioRedis |
| 40 | + from pyctuator.health.health_provider import Status |
| 41 | + from pyctuator.health.aioredis_health_provider import AioRedisHealthProvider |
| 42 | + |
| 43 | + redis_instance = AioRedis(host=redis_host, password="blabla") |
| 44 | + |
| 45 | + health = AioRedisHealthProvider(redis_instance).get_health() |
| 46 | + assert health.status == Status.DOWN |
| 47 | + assert "Client sent AUTH, but no password is set" in str(health.details.failure) |
0 commit comments