test the init system in a reasonable way, bring back excoveralls

pull/363/head
Connor Rigby 2017-08-10 23:37:42 -07:00
parent 8469a573a4
commit 2ddfb5868f
10 changed files with 135 additions and 15 deletions

6
coveralls.json 100644
View File

@ -0,0 +1,6 @@
{
"skip_files": [
"test/support",
"nerves/"
]
}

View File

@ -11,10 +11,11 @@ defmodule Farmbot do
@doc """
Entry Point to Farmbot
"""
def start(type, args)
def start(_, args) do
def start(type, start_opts)
def start(_, start_opts) do
Logger.info ">> Booting Farmbot OS version: #{@version} - #{@commit}"
case Supervisor.start_link(__MODULE__, args, [name: Farmbot]) do
name = Keyword.get(start_opts, :name, __MODULE__)
case Supervisor.start_link(__MODULE__, [], [name: name]) do
{:ok, pid} -> {:ok, pid}
{:error, reason} -> Farmbot.System.factory_reset(reason)
end

View File

@ -40,7 +40,6 @@ defmodule Farmbot.System do
raise "Doing factory reset: #{inspect reason}"
rescue
e ->
debug_log e.message <> " #{inspect System.stacktrace()}"
@system_tasks.factory_reset(reason)
end
end

View File

@ -2,16 +2,8 @@ defmodule Farmbot.System.Supervisor do
@moduledoc """
Supervises Platform specific stuff for Farmbot to operate
"""
use Supervisor
alias Farmbot.System.Init
import Init
error_msg = """
Please configure your environment's init system!
"""
@children Application.get_env(:farmbot, :init) || Mix.raise(error_msg)
use Supervisor
import Farmbot.System.Init
@doc "Start the System Services. This is more or less `init`."
def start_link(args, opts \\ []) do
@ -19,7 +11,8 @@ defmodule Farmbot.System.Supervisor do
end
def init(args) do
@children
:farmbot
|> Application.get_env(:init)
|> Enum.map(fn(child) -> fb_init(child, [args, [name: child]]) end)
|> supervise([strategy: :one_for_all])
end

View File

@ -0,0 +1,9 @@
defmodule Farmbot.System.InitTest do
@moduledoc "Tests init behaviour"
use ExUnit.Case
test "returns a supervisor spec" do
ret = Farmbot.System.Init.fb_init(SomeMod, [])
assert ret == {SomeMod, {SomeMod, :start_link, []}, :permanent, :infinity, :supervisor, [SomeMod]}
end
end

View File

@ -0,0 +1,26 @@
defmodule Farmbot.System.SupervisorTest do
@moduledoc "Tests Init System"
use ExUnit.Case
test "initializes all the things" do
Application.put_env(:farmbot, :init, [
Farmbot.Test.TestInit
])
{:ok, pid} = Farmbot.System.Supervisor.start_link([], [])
assert is_pid(pid)
assert Process.alive?(pid)
mod_proc = Process.whereis(Farmbot.Test.TestInit)
assert is_pid(mod_proc)
assert Process.alive?(mod_proc)
fun = fn() ->
assert true == true
end
Farmbot.Test.TestInit.Worker.test_fun(fun)
Farmbot.Test.TestInit.Worker.exec()
end
end

View File

@ -0,0 +1,22 @@
defmodule Farmbot.SystemTest do
@moduledoc "Tests system functionaity."
use ExUnit.Case
test "does factory reset" do
Farmbot.System.factory_reset("hey something bad happened!")
last = Farmbot.Test.SystemTasks.fetch_last()
assert last == {:factory_reset, "hey something bad happened!"}
end
test "does reboot" do
Farmbot.System.reboot("goodbye cruel world!")
last = Farmbot.Test.SystemTasks.fetch_last()
assert last == {:reboot, "goodbye cruel world!"}
end
test "does shutdown" do
Farmbot.System.shutdown "see you soon!"
last = Farmbot.Test.SystemTasks.fetch_last()
assert last == {:shutdown, "see you soon!"}
end
end

View File

@ -0,0 +1,7 @@
defmodule FarmbotTest do
@moduledoc "Tests Farmbot"
use ExUnit.Case
end

View File

@ -48,6 +48,10 @@ defmodule Farmbot.Test.SystemTasks do
{:reply, :ok, [thing | state]}
end
def handle_call(:fetch_last, _, [] = state) do
{:reply, nil, state}
end
def handle_call(:fetch_last, _, [last | _rest] = state) do
{:reply, last, state}
end

View File

@ -0,0 +1,53 @@
defmodule Farmbot.Test.TestInit do
@moduledoc "Test init module."
defmodule Worker do
@moduledoc "Worker process for this init module"
use GenServer
@doc "Start a test worker"
def start_link(args, opts \\ []) do
GenServer.start_link(__MODULE__, args, opts)
end
@doc "Add a test function"
def test_fun(server \\ __MODULE__, fun) do
GenServer.call(server, {:test_fun, fun})
end
@doc "execute a test function"
def exec(server \\ __MODULE__) do
GenServer.call(server, :exec)
end
def init(_) do
{:ok, nil}
end
def handle_call({:test_fun, fun}, _, _) do
{:reply, :ok, fun}
end
def handle_call(:exec, _, fun) do
# IO.puts "executing fun: #{inspect fun}"
ret = fun.()
{:reply, ret, nil}
end
end
use Supervisor
@behaviour Farmbot.System.Init
@doc "initializes a fake module"
def start_link(args, opts) do
Supervisor.start_link(__MODULE__, args, opts)
end
def init(args) do
children = [
worker(Worker, [args, [name: Worker]])
]
opts = [strategy: :one_for_one]
supervise(children, opts)
end
end