diff --git a/agent/test/proxmox_agent/diagnostics_test.exs b/agent/test/proxmox_agent/diagnostics_test.exs index 867f358..000bfe2 100644 --- a/agent/test/proxmox_agent/diagnostics_test.exs +++ b/agent/test/proxmox_agent/diagnostics_test.exs @@ -61,4 +61,40 @@ defmodule ProxmoxAgent.DiagnosticsTest do 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 + end end