|
9 | 9 | from six import iteritems |
10 | 10 |
|
11 | 11 | from datadog_checks.base.utils.platform import Platform |
| 12 | +from datadog_checks.base.utils.timeout import TimeoutException |
12 | 13 | from datadog_checks.disk import Disk |
13 | 14 |
|
14 | 15 | from .common import DEFAULT_DEVICE_BASE_NAME, DEFAULT_DEVICE_NAME, DEFAULT_FILE_SYSTEM, DEFAULT_MOUNT_POINT |
15 | | -from .mocks import MockDiskMetrics, mock_blkid_output |
| 16 | +from .mocks import MockDiskMetrics, MockPart, mock_blkid_output |
16 | 17 |
|
17 | 18 |
|
18 | 19 | def test_default_options(): |
@@ -212,3 +213,61 @@ def test_blkid_cache_file_contains_no_labels( |
212 | 213 | c.check(instance_blkid_cache_file_no_label) |
213 | 214 | for metric in chain(gauge_metrics, rate_metrics): |
214 | 215 | aggregator.assert_metric(metric, tags=['device:/dev/sda1', 'device_name:sda1']) |
| 216 | + |
| 217 | + |
| 218 | +@pytest.mark.usefixtures('psutil_mocks') |
| 219 | +def test_timeout_config(aggregator, gauge_metrics, rate_metrics): |
| 220 | + """Test timeout configuration value is used on every timeout on the check.""" |
| 221 | + |
| 222 | + # Arbitrary value |
| 223 | + TIMEOUT_VALUE = 42 |
| 224 | + instance = {'timeout': TIMEOUT_VALUE} |
| 225 | + c = Disk('disk', {}, [instance]) |
| 226 | + |
| 227 | + # Mock timeout version |
| 228 | + def no_timeout(fun): |
| 229 | + return lambda *args: fun(args) |
| 230 | + |
| 231 | + with mock.patch('psutil.disk_partitions', return_value=[MockPart()]), mock.patch( |
| 232 | + 'datadog_checks.disk.disk.timeout', return_value=no_timeout |
| 233 | + ) as mock_timeout: |
| 234 | + c.check(instance) |
| 235 | + |
| 236 | + mock_timeout.assert_called_with(TIMEOUT_VALUE) |
| 237 | + |
| 238 | + |
| 239 | +@pytest.mark.usefixtures('psutil_mocks') |
| 240 | +def test_timeout_warning(aggregator, gauge_metrics, rate_metrics): |
| 241 | + """Test a warning is raised when there is a Timeout exception.""" |
| 242 | + |
| 243 | + # Raise exception for "/faulty" mountpoint |
| 244 | + def faulty_timeout(fun): |
| 245 | + def f(mountpoint): |
| 246 | + if mountpoint == "/faulty": |
| 247 | + raise TimeoutException |
| 248 | + else: |
| 249 | + return fun(mountpoint) |
| 250 | + |
| 251 | + return f |
| 252 | + |
| 253 | + c = Disk('disk', {}, [{}]) |
| 254 | + c.log = mock.MagicMock() |
| 255 | + m = MockDiskMetrics() |
| 256 | + m.total = 0 |
| 257 | + |
| 258 | + with mock.patch('psutil.disk_partitions', return_value=[MockPart(), MockPart(mountpoint="/faulty")]), mock.patch( |
| 259 | + 'psutil.disk_usage', return_value=m, __name__='disk_usage' |
| 260 | + ), mock.patch('datadog_checks.disk.disk.timeout', return_value=faulty_timeout): |
| 261 | + c.check({}) |
| 262 | + |
| 263 | + # Check that the warning is called once for the faulty disk |
| 264 | + c.log.warning.assert_called_once() |
| 265 | + |
| 266 | + for name in gauge_metrics: |
| 267 | + aggregator.assert_metric(name, count=0) |
| 268 | + |
| 269 | + for name in rate_metrics: |
| 270 | + aggregator.assert_metric_has_tag(name, 'device:{}'.format(DEFAULT_DEVICE_NAME)) |
| 271 | + aggregator.assert_metric_has_tag(name, 'device_name:{}'.format(DEFAULT_DEVICE_BASE_NAME)) |
| 272 | + |
| 273 | + aggregator.assert_all_metrics_covered() |
0 commit comments