proxMon/agent/test/proxmox_agent/shell_test.exs

40 lines
1.3 KiB
Elixir

defmodule ProxmoxAgent.ShellTest do
use ExUnit.Case, async: false
alias ProxmoxAgent.Shell
test "run/2 returns {:ok, output} on zero exit" do
assert {:ok, output} = Shell.run("/bin/echo", ["hello"])
assert String.trim(output) == "hello"
end
test "run/2 returns {:error, {:nonzero_exit, code, output}} on non-zero exit" do
assert {:error, {:nonzero_exit, code, _}} = Shell.run("/bin/sh", ["-c", "exit 7"])
assert code == 7
end
test "run/2 returns {:error, {:enoent, _}} when binary is missing" do
assert {:error, {:enoent, _}} = Shell.run("/does/not/exist/nope", [])
end
test "run/2 logs to Diagnostics when dump_dir is set" do
dir = Path.join(System.tmp_dir!(), "shell-dump-#{System.unique_integer([:positive])}")
File.mkdir_p!(dir)
:ok = ProxmoxAgent.Diagnostics.configure(dir)
{:ok, writer} = ProxmoxAgent.Diagnostics.Writer.start_link(dir: dir)
on_exit(fn ->
if Process.alive?(writer), do: GenServer.stop(writer)
Application.delete_env(:agent, :dump_dir)
File.rm_rf(dir)
end)
assert {:ok, _} = Shell.run("/bin/echo", ["diagnostics-test"])
:ok = GenServer.call(ProxmoxAgent.Diagnostics.Writer, :flush)
body = File.read!(Path.join(dir, "commands.log"))
assert body =~ "$ /bin/echo diagnostics-test"
assert body =~ "exit=0"
assert body =~ "diagnostics-test"
end
end