|
2 | 2 |
|
3 | 3 | import logging |
4 | 4 | import os |
| 5 | +from typing import TYPE_CHECKING, Generator |
5 | 6 |
|
6 | 7 | import pytest |
| 8 | +from yaml import YAMLError |
7 | 9 |
|
8 | 10 | from instana.util.config import ( |
9 | 11 | get_disable_trace_configurations_from_yaml, |
10 | 12 | parse_ignored_endpoints_from_yaml, |
11 | 13 | ) |
| 14 | +from instana.util.config_reader import ConfigReader |
| 15 | + |
| 16 | +if TYPE_CHECKING: |
| 17 | + from pytest import LogCaptureFixture |
| 18 | + from pytest_mock import MockerFixture |
12 | 19 |
|
13 | 20 |
|
14 | 21 | class TestConfigReader: |
15 | | - def test_load_configuration_with_tracing( |
16 | | - self, caplog: pytest.LogCaptureFixture |
| 22 | + @pytest.fixture(autouse=True) |
| 23 | + def _resource( |
| 24 | + self, |
| 25 | + caplog: "LogCaptureFixture", |
| 26 | + ) -> Generator[None, None, None]: |
| 27 | + yield |
| 28 | + caplog.clear() |
| 29 | + if "INSTANA_CONFIG_PATH" in os.environ: |
| 30 | + os.environ.pop("INSTANA_CONFIG_PATH") |
| 31 | + |
| 32 | + def test_config_reader_null(self, caplog: "LogCaptureFixture") -> None: |
| 33 | + config_reader = ConfigReader(os.environ.get("INSTANA_CONFIG_PATH", "")) |
| 34 | + assert config_reader.file_path == "" |
| 35 | + assert config_reader.data == {} |
| 36 | + assert "ConfigReader: No configuration file specified" in caplog.messages |
| 37 | + |
| 38 | + def test_config_reader_default(self) -> None: |
| 39 | + filename = "tests/util/test_configuration-1.yaml" |
| 40 | + os.environ["INSTANA_CONFIG_PATH"] = filename |
| 41 | + config_reader = ConfigReader(os.environ.get("INSTANA_CONFIG_PATH", "")) |
| 42 | + assert config_reader.file_path == filename |
| 43 | + assert "tracing" in config_reader.data |
| 44 | + assert len(config_reader.data["tracing"]) == 2 |
| 45 | + |
| 46 | + def test_config_reader_file_not_found_error( |
| 47 | + self, caplog: "LogCaptureFixture" |
17 | 48 | ) -> None: |
| 49 | + filename = "tests/util/test_configuration-3.yaml" |
| 50 | + os.environ["INSTANA_CONFIG_PATH"] = filename |
| 51 | + config_reader = ConfigReader(os.environ.get("INSTANA_CONFIG_PATH", "")) |
| 52 | + assert config_reader.file_path == filename |
| 53 | + assert config_reader.data == {} |
| 54 | + assert ( |
| 55 | + f"ConfigReader: Configuration file has not found: {filename}" |
| 56 | + in caplog.messages |
| 57 | + ) |
| 58 | + |
| 59 | + def test_config_reader_yaml_error( |
| 60 | + self, caplog: "LogCaptureFixture", mocker: "MockerFixture" |
| 61 | + ) -> None: |
| 62 | + filename = "tests/util/test_configuration-1.yaml" |
| 63 | + exception_message = "BLAH" |
| 64 | + mocker.patch( |
| 65 | + "instana.util.config_reader.yaml.safe_load", |
| 66 | + side_effect=YAMLError(exception_message), |
| 67 | + ) |
| 68 | + |
| 69 | + config_reader = ConfigReader(filename) # noqa: F841 |
| 70 | + assert ( |
| 71 | + f"ConfigReader: Error parsing YAML file: {exception_message}" |
| 72 | + in caplog.messages |
| 73 | + ) |
| 74 | + |
| 75 | + def test_load_configuration_with_tracing(self, caplog: "LogCaptureFixture") -> None: |
18 | 76 | caplog.set_level(logging.DEBUG, logger="instana") |
19 | 77 |
|
20 | 78 | ignore_endpoints = parse_ignored_endpoints_from_yaml( |
@@ -49,7 +107,7 @@ def test_load_configuration_with_tracing( |
49 | 107 | not in caplog.messages |
50 | 108 | ) |
51 | 109 |
|
52 | | - def test_load_configuration_legacy(self, caplog: pytest.LogCaptureFixture) -> None: |
| 110 | + def test_load_configuration_legacy(self, caplog: "LogCaptureFixture") -> None: |
53 | 111 | caplog.set_level(logging.DEBUG, logger="instana") |
54 | 112 |
|
55 | 113 | ignore_endpoints = parse_ignored_endpoints_from_yaml( |
|
0 commit comments