Skip to content

Commit 5f7b80c

Browse files
pvitalGSVarsha
authored andcommitted
refactor: ConfigReader
Signed-off-by: Paulo Vital <[email protected]>
1 parent d4b2149 commit 5f7b80c

File tree

2 files changed

+73
-10
lines changed

2 files changed

+73
-10
lines changed

src/instana/util/config_reader.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,27 @@
11
# (c) Copyright IBM Corp. 2025
22

3-
from typing import Union
4-
from instana.log import logger
53
import yaml
64

5+
from instana.log import logger
6+
77

88
class ConfigReader:
9-
def __init__(self, file_path: Union[str]) -> None:
9+
def __init__(self, file_path: str) -> None:
1010
self.file_path = file_path
11-
self.data = None
12-
self.load_file()
11+
self.data = {}
12+
if file_path:
13+
self.load_file()
14+
else:
15+
logger.warning("ConfigReader: No configuration file specified")
1316

1417
def load_file(self) -> None:
1518
"""Loads and parses the YAML file"""
1619
try:
1720
with open(self.file_path, "r") as file:
1821
self.data = yaml.safe_load(file)
1922
except FileNotFoundError:
20-
logger.error(f"Configuration file has not found: {self.file_path}")
23+
logger.error(
24+
f"ConfigReader: Configuration file has not found: {self.file_path}"
25+
)
2126
except yaml.YAMLError as e:
22-
logger.error(f"Error parsing YAML file: {e}")
27+
logger.error(f"ConfigReader: Error parsing YAML file: {e}")

tests/util/test_config_reader.py

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,77 @@
22

33
import logging
44
import os
5+
from typing import TYPE_CHECKING, Generator
56

67
import pytest
8+
from yaml import YAMLError
79

810
from instana.util.config import (
911
get_disable_trace_configurations_from_yaml,
1012
parse_ignored_endpoints_from_yaml,
1113
)
14+
from instana.util.config_reader import ConfigReader
15+
16+
if TYPE_CHECKING:
17+
from pytest import LogCaptureFixture
18+
from pytest_mock import MockerFixture
1219

1320

1421
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"
1748
) -> 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:
1876
caplog.set_level(logging.DEBUG, logger="instana")
1977

2078
ignore_endpoints = parse_ignored_endpoints_from_yaml(
@@ -49,7 +107,7 @@ def test_load_configuration_with_tracing(
49107
not in caplog.messages
50108
)
51109

52-
def test_load_configuration_legacy(self, caplog: pytest.LogCaptureFixture) -> None:
110+
def test_load_configuration_legacy(self, caplog: "LogCaptureFixture") -> None:
53111
caplog.set_level(logging.DEBUG, logger="instana")
54112

55113
ignore_endpoints = parse_ignored_endpoints_from_yaml(

0 commit comments

Comments
 (0)