|
| 1 | +# SPDX-FileCopyrightText: 2026 The Botanu Authors |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +"""Tests for BotanuConfig.""" |
| 5 | + |
| 6 | +from __future__ import annotations |
| 7 | + |
| 8 | +import os |
| 9 | +from unittest import mock |
| 10 | + |
| 11 | +import pytest |
| 12 | + |
| 13 | +from botanu.sdk.config import BotanuConfig, _interpolate_env_vars |
| 14 | + |
| 15 | + |
| 16 | +class TestInterpolateEnvVars: |
| 17 | + """Tests for environment variable interpolation.""" |
| 18 | + |
| 19 | + def test_interpolates_env_vars(self): |
| 20 | + with mock.patch.dict(os.environ, {"MY_VAR": "my_value"}): |
| 21 | + result = _interpolate_env_vars("endpoint: ${MY_VAR}") |
| 22 | + assert result == "endpoint: my_value" |
| 23 | + |
| 24 | + def test_preserves_unset_vars(self): |
| 25 | + result = _interpolate_env_vars("endpoint: ${UNSET_VAR}") |
| 26 | + assert result == "endpoint: ${UNSET_VAR}" |
| 27 | + |
| 28 | + def test_no_interpolation_needed(self): |
| 29 | + result = _interpolate_env_vars("endpoint: http://localhost") |
| 30 | + assert result == "endpoint: http://localhost" |
| 31 | + |
| 32 | + def test_default_value_when_unset(self): |
| 33 | + result = _interpolate_env_vars("endpoint: ${UNSET_VAR:-default_value}") |
| 34 | + assert result == "endpoint: default_value" |
| 35 | + |
| 36 | + def test_default_value_ignored_when_set(self): |
| 37 | + with mock.patch.dict(os.environ, {"MY_VAR": "actual_value"}): |
| 38 | + result = _interpolate_env_vars("endpoint: ${MY_VAR:-default_value}") |
| 39 | + assert result == "endpoint: actual_value" |
| 40 | + |
| 41 | + |
| 42 | +class TestBotanuConfigDefaults: |
| 43 | + """Tests for BotanuConfig defaults.""" |
| 44 | + |
| 45 | + def test_default_values(self): |
| 46 | + with mock.patch.dict(os.environ, {}, clear=True): |
| 47 | + # Clear relevant env vars |
| 48 | + for key in ["OTEL_SERVICE_NAME", "BOTANU_ENVIRONMENT", "OTEL_EXPORTER_OTLP_ENDPOINT"]: |
| 49 | + os.environ.pop(key, None) |
| 50 | + |
| 51 | + config = BotanuConfig() |
| 52 | + |
| 53 | + assert config.service_name == "unknown_service" |
| 54 | + assert config.deployment_environment == "production" |
| 55 | + assert config.propagation_mode == "lean" |
| 56 | + assert config.trace_sample_rate == 1.0 |
| 57 | + assert config.auto_detect_resources is True |
| 58 | + |
| 59 | + def test_env_var_service_name(self): |
| 60 | + with mock.patch.dict(os.environ, {"OTEL_SERVICE_NAME": "my-service"}): |
| 61 | + config = BotanuConfig() |
| 62 | + assert config.service_name == "my-service" |
| 63 | + |
| 64 | + def test_env_var_environment(self): |
| 65 | + with mock.patch.dict(os.environ, {"BOTANU_ENVIRONMENT": "staging"}): |
| 66 | + config = BotanuConfig() |
| 67 | + assert config.deployment_environment == "staging" |
| 68 | + |
| 69 | + def test_env_var_otlp_endpoint_base(self): |
| 70 | + """OTEL_EXPORTER_OTLP_ENDPOINT gets /v1/traces appended.""" |
| 71 | + with mock.patch.dict(os.environ, {"OTEL_EXPORTER_OTLP_ENDPOINT": "http://collector:4318"}): |
| 72 | + config = BotanuConfig() |
| 73 | + # Base endpoint gets /v1/traces appended |
| 74 | + assert config.otlp_endpoint == "http://collector:4318/v1/traces" |
| 75 | + |
| 76 | + def test_env_var_otlp_traces_endpoint_direct(self): |
| 77 | + """OTEL_EXPORTER_OTLP_TRACES_ENDPOINT is used directly without appending.""" |
| 78 | + with mock.patch.dict(os.environ, {"OTEL_EXPORTER_OTLP_TRACES_ENDPOINT": "http://collector:4318/v1/traces"}): |
| 79 | + config = BotanuConfig() |
| 80 | + # Direct traces endpoint is used as-is |
| 81 | + assert config.otlp_endpoint == "http://collector:4318/v1/traces" |
| 82 | + |
| 83 | + def test_explicit_values_override_env(self): |
| 84 | + with mock.patch.dict(os.environ, {"OTEL_SERVICE_NAME": "env-service"}): |
| 85 | + config = BotanuConfig(service_name="explicit-service") |
| 86 | + assert config.service_name == "explicit-service" |
| 87 | + |
| 88 | + def test_env_var_sample_rate(self): |
| 89 | + with mock.patch.dict(os.environ, {"BOTANU_TRACE_SAMPLE_RATE": "0.5"}): |
| 90 | + config = BotanuConfig() |
| 91 | + assert config.trace_sample_rate == 0.5 |
| 92 | + |
| 93 | + def test_env_var_propagation_mode(self): |
| 94 | + with mock.patch.dict(os.environ, {"BOTANU_PROPAGATION_MODE": "full"}): |
| 95 | + config = BotanuConfig() |
| 96 | + assert config.propagation_mode == "full" |
| 97 | + |
| 98 | + |
| 99 | +class TestBotanuConfigFromYaml: |
| 100 | + """Tests for loading config from YAML.""" |
| 101 | + |
| 102 | + def test_from_yaml_basic(self, tmp_path): |
| 103 | + yaml_content = """ |
| 104 | +service: |
| 105 | + name: yaml-service |
| 106 | + environment: production |
| 107 | +""" |
| 108 | + yaml_file = tmp_path / "config.yaml" |
| 109 | + yaml_file.write_text(yaml_content) |
| 110 | + |
| 111 | + config = BotanuConfig.from_yaml(str(yaml_file)) |
| 112 | + assert config.service_name == "yaml-service" |
| 113 | + assert config.deployment_environment == "production" |
| 114 | + |
| 115 | + def test_from_yaml_with_otlp(self, tmp_path): |
| 116 | + yaml_content = """ |
| 117 | +service: |
| 118 | + name: test-service |
| 119 | +otlp: |
| 120 | + endpoint: http://localhost:4318 |
| 121 | + headers: |
| 122 | + Authorization: Bearer token123 |
| 123 | +""" |
| 124 | + yaml_file = tmp_path / "config.yaml" |
| 125 | + yaml_file.write_text(yaml_content) |
| 126 | + |
| 127 | + config = BotanuConfig.from_yaml(str(yaml_file)) |
| 128 | + assert config.otlp_endpoint == "http://localhost:4318" |
| 129 | + assert config.otlp_headers == {"Authorization": "Bearer token123"} |
| 130 | + |
| 131 | + def test_from_yaml_file_not_found(self): |
| 132 | + with pytest.raises(FileNotFoundError): |
| 133 | + BotanuConfig.from_yaml("/nonexistent/path/config.yaml") |
| 134 | + |
| 135 | + def test_from_yaml_empty_file(self, tmp_path): |
| 136 | + yaml_file = tmp_path / "empty.yaml" |
| 137 | + yaml_file.write_text("") |
| 138 | + |
| 139 | + config = BotanuConfig.from_yaml(str(yaml_file)) |
| 140 | + # Should use defaults |
| 141 | + assert config.service_name is not None |
| 142 | + |
| 143 | + def test_from_yaml_env_interpolation(self, tmp_path): |
| 144 | + yaml_content = """ |
| 145 | +service: |
| 146 | + name: ${TEST_SERVICE_NAME} |
| 147 | +""" |
| 148 | + yaml_file = tmp_path / "config.yaml" |
| 149 | + yaml_file.write_text(yaml_content) |
| 150 | + |
| 151 | + with mock.patch.dict(os.environ, {"TEST_SERVICE_NAME": "interpolated-service"}): |
| 152 | + config = BotanuConfig.from_yaml(str(yaml_file)) |
| 153 | + assert config.service_name == "interpolated-service" |
| 154 | + |
| 155 | + |
| 156 | +class TestBotanuConfigFromFileOrEnv: |
| 157 | + """Tests for from_file_or_env method.""" |
| 158 | + |
| 159 | + def test_uses_env_when_no_file(self): |
| 160 | + with mock.patch.dict( |
| 161 | + os.environ, |
| 162 | + {"OTEL_SERVICE_NAME": "env-only-service"}, |
| 163 | + clear=False, |
| 164 | + ): |
| 165 | + # Ensure no config files exist in current directory |
| 166 | + config = BotanuConfig.from_file_or_env() |
| 167 | + # Should use env vars |
| 168 | + assert config.service_name == "env-only-service" |
| 169 | + |
| 170 | + def test_uses_specified_path(self, tmp_path): |
| 171 | + yaml_content = """ |
| 172 | +service: |
| 173 | + name: file-service |
| 174 | +""" |
| 175 | + yaml_file = tmp_path / "config.yaml" |
| 176 | + yaml_file.write_text(yaml_content) |
| 177 | + |
| 178 | + config = BotanuConfig.from_file_or_env(path=str(yaml_file)) |
| 179 | + assert config.service_name == "file-service" |
| 180 | + |
| 181 | + |
| 182 | +class TestBotanuConfigToDict: |
| 183 | + """Tests for config serialization.""" |
| 184 | + |
| 185 | + def test_to_dict(self): |
| 186 | + config = BotanuConfig( |
| 187 | + service_name="test-service", |
| 188 | + deployment_environment="staging", |
| 189 | + otlp_endpoint="http://localhost:4318", |
| 190 | + ) |
| 191 | + d = config.to_dict() |
| 192 | + |
| 193 | + assert d["service"]["name"] == "test-service" |
| 194 | + assert d["service"]["environment"] == "staging" |
| 195 | + assert d["otlp"]["endpoint"] == "http://localhost:4318" |
| 196 | + |
| 197 | + |
| 198 | +class TestBotanuConfigAutoInstrument: |
| 199 | + """Tests for auto-instrumentation configuration.""" |
| 200 | + |
| 201 | + def test_default_packages(self): |
| 202 | + config = BotanuConfig() |
| 203 | + packages = config.auto_instrument_packages |
| 204 | + |
| 205 | + assert "requests" in packages |
| 206 | + assert "httpx" in packages |
| 207 | + assert "fastapi" in packages |
| 208 | + assert "openai_v2" in packages |
| 209 | + assert "anthropic" in packages |
0 commit comments