|
3 | 3 | # Licensed under a 3-clause BSD style license (see LICENSE)
|
4 | 4 | import mock
|
5 | 5 | import pytest
|
| 6 | +from redis.exceptions import ResponseError |
6 | 7 |
|
7 | 8 | from datadog_checks.dev.utils import get_metadata_metrics
|
8 | 9 |
|
@@ -102,3 +103,36 @@ def test__check_total_commands_processed_present(check, aggregator, redis_instan
|
102 | 103 |
|
103 | 104 | # Assert that the `redis.net.commands` metric was sent
|
104 | 105 | aggregator.assert_metric('redis.net.commands', value=1000, tags=['test_total_commands_processed'])
|
| 106 | + |
| 107 | + |
| 108 | +def test_slowlog_quiet_failure(check, aggregator, redis_instance): |
| 109 | + """ |
| 110 | + The check should not fail if the slowlog command fails with redis.ResponseError |
| 111 | + """ |
| 112 | + redis_check = check(redis_instance) |
| 113 | + |
| 114 | + # Mock the connection object returned by _get_conn |
| 115 | + mock_conn = mock.MagicMock() |
| 116 | + mock_conn.slowlog_get.side_effect = ResponseError('ERR unknown command `SLOWLOG`') |
| 117 | + mock_conn.config_get.return_value = {'slowlog-max-len': '128'} |
| 118 | + |
| 119 | + with mock.patch.object(redis_check, '_get_conn', return_value=mock_conn): |
| 120 | + redis_check._check_slowlog() |
| 121 | + # Assert that no metrics were sent |
| 122 | + aggregator.assert_metric('redis.slowlog.micros', count=0) |
| 123 | + |
| 124 | + |
| 125 | +def test_slowlog_loud_failure(check, redis_instance): |
| 126 | + """ |
| 127 | + The check should fail if the slowlog command fails for any other reason |
| 128 | + """ |
| 129 | + redis_check = check(redis_instance) |
| 130 | + |
| 131 | + # Mock the connection object returned by _get_conn |
| 132 | + mock_conn = mock.MagicMock() |
| 133 | + mock_conn.slowlog_get.side_effect = RuntimeError('Some other error') |
| 134 | + mock_conn.config_get.return_value = {'slowlog-max-len': '128'} |
| 135 | + |
| 136 | + with mock.patch.object(redis_check, '_get_conn', return_value=mock_conn): |
| 137 | + with pytest.raises(RuntimeError, match='Some other error'): |
| 138 | + redis_check._check_slowlog() |
0 commit comments