Work in progress for thor#1 phase 1 (PR 1 of 6). Not yet pushed: CI workflow and deploy files still to add. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
42 lines
1.8 KiB
Python
42 lines
1.8 KiB
Python
import json
|
|
|
|
from thor import config
|
|
|
|
|
|
def test_parse_env_file_handles_comments_quotes_and_junk():
|
|
text = '# comment\n\nA=1\nB = "two words"\nC=\'x=y\'\nnot a line\n=nokey\n'
|
|
assert config.parse_env_file(text) == {'A': '1', 'B': 'two words', 'C': 'x=y'}
|
|
|
|
|
|
def test_defaults_without_any_files(tmp_path):
|
|
s = config.load(str(tmp_path / 'missing.json'), str(tmp_path / 'nodir'), environ={})
|
|
assert s.prometheus_url == 'http://10.10.10.48:9090'
|
|
assert s.cycle_seconds == 120
|
|
assert s.catchup_hours == 24
|
|
assert s.stale_after_s == 360
|
|
assert not s.database.configured
|
|
assert s.proxmox_nodes[0] == '10.10.10.11'
|
|
|
|
|
|
def test_json_and_env_files_and_environment_layer_in_order(tmp_path):
|
|
(tmp_path / 'thor.json').write_text(json.dumps({
|
|
'prometheus_url': 'http://prom:9090/', 'cycle_seconds': 60, 'checks': {'memory': 'live', 'x': 'bogus'},
|
|
'proxmox_nodes': ['10.0.0.1'], 'proxmox_verify_tls': 'yes'}))
|
|
env = tmp_path / 'env'
|
|
env.mkdir()
|
|
(env / 'db.env').write_text('DB_HOST=10.10.10.50\nDB_PASSWORD=from-file\n')
|
|
(env / 'pve.env').write_text('PVE_TOKEN_ID=thor@pve!monitor\nPVE_TOKEN_SECRET=s3cret\n')
|
|
(env / 'ignored.txt').write_text('DB_HOST=wrong\n')
|
|
s = config.load(str(tmp_path / 'thor.json'), str(env), environ={'DB_PASSWORD': 'from-env', 'lower': 'x'})
|
|
assert s.prometheus_url == 'http://prom:9090'
|
|
assert s.cycle_seconds == 60
|
|
assert s.proxmox_nodes == ['10.0.0.1']
|
|
assert s.proxmox_verify_tls is True
|
|
assert s.database.host == '10.10.10.50'
|
|
assert s.database.password == 'from-env'
|
|
assert s.database.configured
|
|
assert s.proxmox_token_id == 'thor@pve!monitor'
|
|
assert s.check_mode('memory') == 'live'
|
|
assert s.check_mode('x') == 'shadow' # unknown mode falls back to shadow
|
|
assert s.check_mode('never-set') == 'shadow' # every check starts in shadow
|