feat(agent): load config from environment with validation
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
import pytest
|
||||
|
||||
from agent.config import AgentConfig, ConfigError, load_config
|
||||
|
||||
FULL_ENV = {
|
||||
"DB_HOST": "db",
|
||||
"DB_PORT": "5433",
|
||||
"POSTGRES_USER": "firefly",
|
||||
"POSTGRES_PASSWORD": "s3cret",
|
||||
"AGENT_DB_NAME": "agentdb2",
|
||||
"FIREFLY_API_URL": "http://app:8080",
|
||||
"AGENT_FIREFLY_TOKEN": "tok123",
|
||||
"HEARTBEAT_INTERVAL_MINUTES": "15",
|
||||
}
|
||||
|
||||
MINIMAL_ENV = {
|
||||
"DB_HOST": "db",
|
||||
"POSTGRES_USER": "firefly",
|
||||
"POSTGRES_PASSWORD": "s3cret",
|
||||
"FIREFLY_API_URL": "http://app:8080",
|
||||
"AGENT_FIREFLY_TOKEN": "tok123",
|
||||
}
|
||||
|
||||
|
||||
def test_load_config_reads_all_vars():
|
||||
cfg = load_config(FULL_ENV)
|
||||
assert cfg == AgentConfig(
|
||||
db_host="db",
|
||||
db_port=5433,
|
||||
db_user="firefly",
|
||||
db_password="s3cret",
|
||||
agent_db_name="agentdb2",
|
||||
firefly_url="http://app:8080",
|
||||
firefly_token="tok123",
|
||||
heartbeat_interval_minutes=15,
|
||||
)
|
||||
|
||||
|
||||
def test_defaults_applied():
|
||||
cfg = load_config(MINIMAL_ENV)
|
||||
assert cfg.db_port == 5432
|
||||
assert cfg.agent_db_name == "agentdb"
|
||||
assert cfg.heartbeat_interval_minutes == 60
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"missing",
|
||||
["DB_HOST", "POSTGRES_USER", "POSTGRES_PASSWORD", "FIREFLY_API_URL", "AGENT_FIREFLY_TOKEN"],
|
||||
)
|
||||
def test_missing_required_var_names_the_var(missing):
|
||||
env = {k: v for k, v in MINIMAL_ENV.items() if k != missing}
|
||||
with pytest.raises(ConfigError, match=missing):
|
||||
load_config(env)
|
||||
Reference in New Issue
Block a user