|
| 1 | +"""Tests for Redis health check.""" |
| 2 | + |
| 3 | +import os |
| 4 | +from unittest import mock |
| 5 | + |
| 6 | +import pytest |
| 7 | + |
| 8 | +pytest.importorskip("redis") |
| 9 | + |
| 10 | +from redis.exceptions import ConnectionError as RedisConnectionError |
| 11 | +from redis.exceptions import TimeoutError as RedisTimeoutError |
| 12 | + |
| 13 | +from health_check.contrib.redis import Redis as RedisHealthCheck |
| 14 | +from health_check.exceptions import ServiceUnavailable |
| 15 | + |
| 16 | + |
| 17 | +class TestRedis: |
| 18 | + """Test Redis health check.""" |
| 19 | + |
| 20 | + def test_redis__ok(self): |
| 21 | + """Ping Redis successfully when using client parameter.""" |
| 22 | + mock_client = mock.MagicMock() |
| 23 | + mock_client.ping.return_value = True |
| 24 | + |
| 25 | + check = RedisHealthCheck(client=mock_client) |
| 26 | + check.check_status() |
| 27 | + assert check.errors == [] |
| 28 | + mock_client.ping.assert_called_once() |
| 29 | + |
| 30 | + def test_redis__connection_refused(self): |
| 31 | + """Raise ServiceUnavailable when connection is refused.""" |
| 32 | + mock_client = mock.MagicMock() |
| 33 | + mock_client.ping.side_effect = ConnectionRefusedError("refused") |
| 34 | + |
| 35 | + check = RedisHealthCheck(client=mock_client) |
| 36 | + with pytest.raises(ServiceUnavailable): |
| 37 | + check.check_status() |
| 38 | + |
| 39 | + def test_redis__timeout(self): |
| 40 | + """Raise ServiceUnavailable when connection times out.""" |
| 41 | + mock_client = mock.MagicMock() |
| 42 | + mock_client.ping.side_effect = RedisTimeoutError("timeout") |
| 43 | + |
| 44 | + check = RedisHealthCheck(client=mock_client) |
| 45 | + with pytest.raises(ServiceUnavailable): |
| 46 | + check.check_status() |
| 47 | + |
| 48 | + def test_redis__connection_error(self): |
| 49 | + """Raise ServiceUnavailable when connection fails.""" |
| 50 | + mock_client = mock.MagicMock() |
| 51 | + mock_client.ping.side_effect = RedisConnectionError("connection error") |
| 52 | + |
| 53 | + check = RedisHealthCheck(client=mock_client) |
| 54 | + with pytest.raises(ServiceUnavailable): |
| 55 | + check.check_status() |
| 56 | + |
| 57 | + def test_redis__unknown_error(self): |
| 58 | + """Raise ServiceUnavailable for unexpected exceptions.""" |
| 59 | + mock_client = mock.MagicMock() |
| 60 | + mock_client.ping.side_effect = RuntimeError("unexpected") |
| 61 | + |
| 62 | + check = RedisHealthCheck(client=mock_client) |
| 63 | + with pytest.raises(ServiceUnavailable): |
| 64 | + check.check_status() |
| 65 | + |
| 66 | + @pytest.mark.integration |
| 67 | + def test_redis__deprecated_url(self): |
| 68 | + """Create client from URL when redis_url is provided.""" |
| 69 | + redis_url = os.getenv("REDIS_URL") |
| 70 | + if not redis_url: |
| 71 | + pytest.skip("REDIS_URL not set; skipping integration test") |
| 72 | + |
| 73 | + with pytest.warns(DeprecationWarning, match="redis_url.*deprecated"): |
| 74 | + check = RedisHealthCheck(redis_url="redis://localhost:6379") |
| 75 | + check.check_status() |
| 76 | + assert check.errors == [] |
| 77 | + |
| 78 | + @pytest.mark.integration |
| 79 | + def test_redis__deprecated_url_with_options(self): |
| 80 | + """Pass options when creating client from URL.""" |
| 81 | + redis_url = os.getenv("REDIS_URL") |
| 82 | + if not redis_url: |
| 83 | + pytest.skip("REDIS_URL not set; skipping integration test") |
| 84 | + |
| 85 | + options = {"socket_connect_timeout": 5} |
| 86 | + with pytest.warns(DeprecationWarning): |
| 87 | + check = RedisHealthCheck(redis_url="redis://localhost:6379", redis_url_options=options) |
| 88 | + check.check_status() |
| 89 | + assert check.errors == [] |
| 90 | + |
| 91 | + @pytest.mark.integration |
| 92 | + def test_redis__real_connection(self): |
| 93 | + """Ping real Redis server when REDIS_URL is configured.""" |
| 94 | + redis_url = os.getenv("REDIS_URL") |
| 95 | + if not redis_url: |
| 96 | + pytest.skip("REDIS_URL not set; skipping integration test") |
| 97 | + |
| 98 | + from redis import Redis as RedisClient |
| 99 | + |
| 100 | + client = RedisClient.from_url(redis_url) |
| 101 | + check = RedisHealthCheck(client=client) |
| 102 | + check.check_status() |
| 103 | + assert check.errors == [] |
| 104 | + client.close() |
| 105 | + |
| 106 | + @pytest.mark.integration |
| 107 | + def test_redis__real_sentinel(self): |
| 108 | + """Ping real Redis Sentinel when configured.""" |
| 109 | + sentinel_url = os.getenv("REDIS_SENTINEL_URL") |
| 110 | + if not sentinel_url: |
| 111 | + pytest.skip("REDIS_SENTINEL_URL not set; skipping integration test") |
| 112 | + |
| 113 | + from redis.sentinel import Sentinel |
| 114 | + |
| 115 | + # Parse sentinel configuration from environment |
| 116 | + sentinel_nodes = os.getenv("REDIS_SENTINEL_NODES", "localhost:26379") |
| 117 | + service_name = os.getenv("REDIS_SENTINEL_SERVICE_NAME", "mymaster") |
| 118 | + |
| 119 | + # Parse sentinel nodes from comma-separated list |
| 120 | + sentinels = [] |
| 121 | + for node in sentinel_nodes.split(","): |
| 122 | + host, port = node.strip().split(":") |
| 123 | + sentinels.append((host, int(port))) |
| 124 | + |
| 125 | + # Create Sentinel and get master client |
| 126 | + sentinel = Sentinel(sentinels) |
| 127 | + master = sentinel.master_for(service_name) |
| 128 | + |
| 129 | + # Use the unified Redis check with the master client |
| 130 | + check = RedisHealthCheck(client=master) |
| 131 | + check.check_status() |
| 132 | + assert check.errors == [] |
0 commit comments