From 523c7febc9ef434d21b508a6b56f59a679041bca Mon Sep 17 00:00:00 2001 From: Frank Hunleth Date: Tue, 6 Jul 2021 17:07:03 -0400 Subject: [PATCH] Add unit test for ifup_deamon.ex --- .../interface/ifup_daemon_test.exs | 75 +++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 test/vintage_net/interface/ifup_daemon_test.exs diff --git a/test/vintage_net/interface/ifup_daemon_test.exs b/test/vintage_net/interface/ifup_daemon_test.exs new file mode 100644 index 00000000..41873465 --- /dev/null +++ b/test/vintage_net/interface/ifup_daemon_test.exs @@ -0,0 +1,75 @@ +defmodule VintageNet.Interface.IfupDaemonTest do + use ExUnit.Case + import ExUnit.CaptureLog + + alias VintageNet.Interface.IfupDaemon + + doctest IfupDaemon + + test "reactions to the interface coming up and going down" do + # capture_log to suppress log messages + capture_log(fn -> + pid = + start_supervised!( + {IfupDaemon, ifname: "test0", command: "sleep", args: ["10000"], opts: []} + ) + + refute IfupDaemon.running?(pid) + + # Simulate interface starting + send(pid, {VintageNet, ["interface", "test0", "lower_up"], false, true, %{}}) + Process.sleep(10) + + assert IfupDaemon.running?(pid) + + # Simulate interface stopping + send(pid, {VintageNet, ["interface", "test0", "lower_up"], true, false, %{}}) + Process.sleep(10) + + refute IfupDaemon.running?(pid) + end) + end + + test "short running programs stop ok" do + # capture_log to suppress log messages + capture_log(fn -> + pid = + start_supervised!( + {IfupDaemon, ifname: "test0", command: "echo", args: ["hello"], opts: []} + ) + + refute IfupDaemon.running?(pid) + + # Simulate interface starting + send(pid, {VintageNet, ["interface", "test0", "lower_up"], false, true, %{}}) + Process.sleep(50) + + # Check that a successful daemon exit doesn't take down the IfupDaemon + assert Process.alive?(pid) + refute IfupDaemon.running?(pid) + + # Simulate interface stopping - mostly to check that it doesn't crash + send(pid, {VintageNet, ["interface", "test0", "lower_up"], true, false, %{}}) + Process.sleep(10) + refute IfupDaemon.running?(pid) + end) + end + + test "invalid commands crash" do + capture_log(fn -> + pid = + start_supervised!( + {IfupDaemon, ifname: "test0", command: "missing_program", args: [], opts: []} + ) + + refute IfupDaemon.running?(pid) + + # Simulate interface starting + send(pid, {VintageNet, ["interface", "test0", "lower_up"], false, true, %{}}) + Process.sleep(50) + + # Should have crashed by now + refute Process.alive?(pid) + end) + end +end