feat(agent): toml config loader with defaults and validation
This commit is contained in:
parent
7ec38e0fd6
commit
e4db0beac6
3 changed files with 147 additions and 0 deletions
8
agent/test/fixtures/agent.toml
vendored
Normal file
8
agent/test/fixtures/agent.toml
vendored
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
server_url = "wss://monitor.example.com/socket/websocket"
|
||||
token = "test_token_123"
|
||||
host_id = "pve-test-01"
|
||||
|
||||
[intervals]
|
||||
fast_seconds = 15
|
||||
medium_seconds = 120
|
||||
slow_seconds = 600
|
||||
62
agent/test/proxmox_agent/config_test.exs
Normal file
62
agent/test/proxmox_agent/config_test.exs
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
defmodule ProxmoxAgent.ConfigTest do
|
||||
use ExUnit.Case, async: true
|
||||
|
||||
alias ProxmoxAgent.Config
|
||||
|
||||
@fixture Path.expand("../fixtures/agent.toml", __DIR__)
|
||||
|
||||
describe "load/1" do
|
||||
test "parses required fields" do
|
||||
assert {:ok, cfg} = Config.load(@fixture)
|
||||
assert cfg.server_url == "wss://monitor.example.com/socket/websocket"
|
||||
assert cfg.token == "test_token_123"
|
||||
assert cfg.host_id == "pve-test-01"
|
||||
assert cfg.fast_seconds == 15
|
||||
assert cfg.medium_seconds == 120
|
||||
assert cfg.slow_seconds == 600
|
||||
end
|
||||
|
||||
test "returns error for missing file" do
|
||||
assert {:error, {:file_read, _}} = Config.load("/does/not/exist.toml")
|
||||
end
|
||||
|
||||
test "defaults host_id to system hostname when absent" do
|
||||
tmp = Path.join(System.tmp_dir!(), "agent_nohost.toml")
|
||||
|
||||
File.write!(tmp, """
|
||||
server_url = "wss://x/socket/websocket"
|
||||
token = "t"
|
||||
""")
|
||||
|
||||
on_exit(fn -> File.rm(tmp) end)
|
||||
|
||||
assert {:ok, cfg} = Config.load(tmp)
|
||||
assert is_binary(cfg.host_id)
|
||||
assert cfg.host_id != ""
|
||||
end
|
||||
|
||||
test "applies default intervals when [intervals] is absent" do
|
||||
tmp = Path.join(System.tmp_dir!(), "agent_nointervals.toml")
|
||||
|
||||
File.write!(tmp, """
|
||||
server_url = "wss://x/socket/websocket"
|
||||
token = "t"
|
||||
host_id = "h"
|
||||
""")
|
||||
|
||||
on_exit(fn -> File.rm(tmp) end)
|
||||
|
||||
assert {:ok, cfg} = Config.load(tmp)
|
||||
assert cfg.fast_seconds == 30
|
||||
assert cfg.medium_seconds == 300
|
||||
assert cfg.slow_seconds == 1800
|
||||
end
|
||||
|
||||
test "returns error when required keys missing" do
|
||||
tmp = Path.join(System.tmp_dir!(), "agent_bad.toml")
|
||||
File.write!(tmp, "token = \"t\"\n")
|
||||
on_exit(fn -> File.rm(tmp) end)
|
||||
assert {:error, {:missing_key, :server_url}} = Config.load(tmp)
|
||||
end
|
||||
end
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue