99 lines
2.7 KiB
Elixir
99 lines
2.7 KiB
Elixir
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
|
|
assert cfg.dump_dir == "/tmp/proxmox-monitor-test"
|
|
end
|
|
|
|
test "parses [debug] dump_dir when present" do
|
|
assert {:ok, cfg} = Config.load(@fixture)
|
|
assert cfg.dump_dir == "/tmp/proxmox-monitor-test"
|
|
end
|
|
|
|
test "dump_dir is nil when [debug] is absent" do
|
|
tmp = Path.join(System.tmp_dir!(), "agent_nodebug.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 cfg.dump_dir == nil
|
|
end
|
|
|
|
test "dump_dir is nil when set to empty string" do
|
|
tmp = Path.join(System.tmp_dir!(), "agent_emptydebug.toml")
|
|
|
|
File.write!(tmp, """
|
|
server_url = "wss://x/socket/websocket"
|
|
token = "t"
|
|
|
|
[debug]
|
|
dump_dir = ""
|
|
""")
|
|
|
|
on_exit(fn -> File.rm(tmp) end)
|
|
|
|
assert {:ok, cfg} = Config.load(tmp)
|
|
assert cfg.dump_dir == nil
|
|
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
|