|
31 | 31 | from __future__ import absolute_import |
32 | 32 |
|
33 | 33 | import logging |
| 34 | +import os |
| 35 | +import platform |
| 36 | +import stat |
34 | 37 |
|
35 | 38 | import mock |
| 39 | +import pytest |
36 | 40 |
|
37 | 41 | from elasticapm.conf import ( |
38 | 42 | Config, |
| 43 | + ConfigurationError, |
| 44 | + FileIsReadableValidator, |
39 | 45 | RegexValidator, |
40 | 46 | _BoolConfigValue, |
41 | 47 | _ConfigBase, |
@@ -221,3 +227,35 @@ class MyConfig(_ConfigBase): |
221 | 227 |
|
222 | 228 | c = MyConfig({"CHAIN": "x"}) |
223 | 229 | assert c.chained == "XX" |
| 230 | + |
| 231 | + |
| 232 | +def test_file_is_readable_validator_not_exists(tmpdir): |
| 233 | + validator = FileIsReadableValidator() |
| 234 | + with pytest.raises(ConfigurationError) as e: |
| 235 | + validator(tmpdir.join("doesnotexist").strpath, "path") |
| 236 | + assert "does not exist" in e.value.args[0] |
| 237 | + |
| 238 | + |
| 239 | +def test_file_is_readable_validator_not_a_file(tmpdir): |
| 240 | + validator = FileIsReadableValidator() |
| 241 | + with pytest.raises(ConfigurationError) as e: |
| 242 | + validator(tmpdir.strpath, "path") |
| 243 | + assert "is not a file" in e.value.args[0] |
| 244 | + |
| 245 | + |
| 246 | +@pytest.mark.skipif(platform.system() == "Windows", reason="os.access() doesn't seem to work as we expect on Windows") |
| 247 | +def test_file_is_readable_validator_not_readable(tmpdir): |
| 248 | + p = tmpdir.join("nonreadable") |
| 249 | + p.write("") |
| 250 | + os.chmod(p.strpath, stat.S_IWRITE) |
| 251 | + validator = FileIsReadableValidator() |
| 252 | + with pytest.raises(ConfigurationError) as e: |
| 253 | + validator(p.strpath, "path") |
| 254 | + assert "is not readable" in e.value.args[0] |
| 255 | + |
| 256 | + |
| 257 | +def test_file_is_readable_validator_all_good(tmpdir): |
| 258 | + p = tmpdir.join("readable") |
| 259 | + p.write("") |
| 260 | + validator = FileIsReadableValidator() |
| 261 | + assert validator(p.strpath, "path") == p.strpath |
0 commit comments