proxMon/agent/test/proxmox_agent/diagnostics_test.exs
Carsten 3367b95b91 chore(agent): log /proc reads, log diagnostics enable, comment trap_exit
Addresses final code review:
- Host collector's /proc reads now go through Diagnostics.log_read/3,
  appearing in commands.log formatted as `$ cat /proc/loadavg`
- configure/1 logs an info line on successful enable so the operator
  has a breadcrumb in the journal
- Writer.init/1 documents the deliberate trap_exit omission
2026-04-22 22:29:26 +02:00

110 lines
3.8 KiB
Elixir

defmodule ProxmoxAgent.DiagnosticsTest do
use ExUnit.Case, async: false
alias ProxmoxAgent.Diagnostics
setup do
# Isolate tests: clear the env key before and after each test.
Application.delete_env(:agent, :dump_dir)
on_exit(fn -> Application.delete_env(:agent, :dump_dir) end)
:ok
end
describe "configure/1 and enabled?/0" do
test "nil disables and returns :ok" do
assert :ok = Diagnostics.configure(nil)
refute Diagnostics.enabled?()
end
test "empty string disables and returns :ok" do
assert :ok = Diagnostics.configure("")
refute Diagnostics.enabled?()
end
test "valid path creates the directory and enables" do
dir = Path.join(System.tmp_dir!(), "diag-#{System.unique_integer([:positive])}")
on_exit(fn -> File.rm_rf(dir) end)
assert :ok = Diagnostics.configure(dir)
assert Diagnostics.enabled?()
assert File.dir?(dir)
assert Application.get_env(:agent, :dump_dir) == dir
end
test "unreachable path disables (does not crash)" do
# Point at a path under a non-directory file to force mkdir_p failure.
parent = Path.join(System.tmp_dir!(), "diag-parent-#{System.unique_integer([:positive])}")
File.write!(parent, "not a directory")
dir = Path.join(parent, "child")
on_exit(fn -> File.rm_rf(parent) end)
assert :ok = Diagnostics.configure(dir)
refute Diagnostics.enabled?()
end
end
describe "log_command/4 and log_sample/2 (no writer running)" do
test "log_command/4 no-ops and returns :ok when disabled" do
assert :ok = Diagnostics.log_command("zpool", ["list"], {:ok, "body"}, 1_234)
end
test "log_sample/2 no-ops and returns :ok when disabled" do
assert :ok = Diagnostics.log_sample("fast", %{foo: "bar"})
end
test "log_command/4 no-ops when enabled but writer is not running" do
dir = Path.join(System.tmp_dir!(), "diag-#{System.unique_integer([:positive])}")
on_exit(fn -> File.rm_rf(dir) end)
:ok = Diagnostics.configure(dir)
# Writer is not started in this test — log should still be safe.
assert :ok = Diagnostics.log_command("zpool", ["list"], {:ok, "body"}, 1_234)
end
end
describe "log_command/4 and log_sample/2 with writer running" do
setup do
dir = Path.join(System.tmp_dir!(), "diag-e2e-#{System.unique_integer([:positive])}")
File.mkdir_p!(dir)
:ok = Diagnostics.configure(dir)
{:ok, pid} = ProxmoxAgent.Diagnostics.Writer.start_link(dir: dir)
on_exit(fn ->
if Process.alive?(pid), do: GenServer.stop(pid)
File.rm_rf(dir)
end)
%{dir: dir}
end
test "log_command/4 writes to commands.log", %{dir: dir} do
assert :ok = Diagnostics.log_command("zpool", ["list", "-j"], {:ok, "body"}, 2_500)
:ok = GenServer.call(ProxmoxAgent.Diagnostics.Writer, :flush)
body = File.read!(Path.join(dir, "commands.log"))
assert body =~ "$ zpool list -j"
assert body =~ "exit=0"
assert body =~ "size=4"
end
test "log_sample/2 writes to samples.log", %{dir: dir} do
assert :ok = Diagnostics.log_sample("fast", %{"zfs_pools" => %{"pools" => []}})
:ok = GenServer.call(ProxmoxAgent.Diagnostics.Writer, :flush)
body = File.read!(Path.join(dir, "samples.log"))
assert body =~ "kind=fast"
assert body =~ "\"zfs_pools\""
end
test "log_read/3 writes to commands.log formatted as cat", %{dir: dir} do
assert :ok = Diagnostics.log_read("/proc/loadavg", {:ok, "0.12 0.34 0.56 1/200 3000"}, 150)
:ok = GenServer.call(ProxmoxAgent.Diagnostics.Writer, :flush)
body = File.read!(Path.join(dir, "commands.log"))
assert body =~ "$ cat /proc/loadavg"
assert body =~ "0.12 0.34 0.56"
assert body =~ "exit=0"
end
end
end