Not mocking correctly?

pull/309/head
Rick Carlino 2017-05-18 14:57:11 -05:00
parent c79263147a
commit 32d7d441b5
11 changed files with 58 additions and 36 deletions

View File

@ -1,9 +1,8 @@
use Mix.Config use Mix.Config
import_config "dev.exs" import_config "dev.exs"
# # We hopefully don't need logger ¯\_(ツ)_/¯ # We hopefully don't need logger ¯\_(ツ)_/¯
# config :logger, :console, config :logger, :console, format: ""
# format: ""
config :farmbot, config :farmbot,
path: "/tmp/farmbot_test", path: "/tmp/farmbot_test",

View File

@ -33,7 +33,7 @@ defmodule Farmbot do
# The worker for diffing db entries. # The worker for diffing db entries.
worker(Farmbot.Database.Supervisor, [], restart: :permanent), worker(Farmbot.Database.Supervisor, [], restart: :permanent),
# Handles tracking of various parts of the bots state. # Handles tracking of various parts of the bots state.
supervisor(Farmbot.BotState.Supervisor, [name: Farmbot.BotState.Supervisor], restart: :permanent), supervisor(Farmbot.BotState.Supervisor, [context, [name: Farmbot.BotState.Supervisor]], restart: :permanent),
# Handles FarmEvents. # Handles FarmEvents.
supervisor(Farmbot.FarmEvent.Supervisor, [], restart: :permanent), supervisor(Farmbot.FarmEvent.Supervisor, [], restart: :permanent),
# Handles the passing of messages from one part of the system to another. # Handles the passing of messages from one part of the system to another.

View File

@ -10,12 +10,15 @@ defmodule Farmbot.BotState.ProcessSupervisor do
@doc """ @doc """
Starts the Farm Procss Supervisor Starts the Farm Procss Supervisor
""" """
def start_link(%Context{} = ctx, opts), do: Supervisor.start_link(__MODULE__, ctx, opts) def start_link(%Context{} = ctx, opts),
do: Supervisor.start_link(__MODULE__, ctx, opts)
def init(ctx) do def init(ctx) do
Logger.info ">> Starting FarmProcess Supervisor" Logger.info ">> Starting FarmProcess Supervisor"
children = [ children = [
worker(Farmbot.BotState.ProcessTracker, [ctx, name: Farmbot.BotState.ProcessTracker], [restart: :permanent]) worker(Farmbot.BotState.ProcessTracker,
[ctx, [name: Farmbot.BotState.ProcessTracker]],
[restart: :permanent])
] ]
opts = [strategy: :one_for_one] opts = [strategy: :one_for_one]
supervise(children, opts) supervise(children, opts)

View File

@ -11,6 +11,7 @@ defmodule Farmbot.BotState.ProcessTracker do
require Logger require Logger
alias Nerves.Lib.UUID alias Nerves.Lib.UUID
alias Farmbot.RegimenRunner alias Farmbot.RegimenRunner
alias Farmbot.CeleryScript.Ast.Context
defmodule Info do defmodule Info do
@moduledoc false @moduledoc false
@ -42,8 +43,8 @@ defmodule Farmbot.BotState.ProcessTracker do
@doc """ @doc """
Starts the Process Tracker Starts the Process Tracker
""" """
@spec start_link :: {:ok, pid} def start_link(%Context{} = context, opts),
def start_link, do: GenServer.start_link(__MODULE__, [], name: __MODULE__) do: GenServer.start_link(__MODULE__, [], opts)
@doc """ @doc """
Registers a kind, name with a database entry to be tracked Registers a kind, name with a database entry to be tracked

View File

@ -13,10 +13,10 @@ defmodule Farmbot.BotState.Supervisor do
alias Farmbot.EasterEggs alias Farmbot.EasterEggs
def init(ctx) do def init(ctx) do
children = [ children = [
worker(Farmbot.BotState.Monitor, [ctx, name: Farmbot.BotState.Monitor ], [restart: :permanent]), worker(Farmbot.BotState.Monitor, [ctx, [name: Farmbot.BotState.Monitor] ], [restart: :permanent]),
worker(Farmbot.BotState.Configuration, [ctx, name: Farmbot.BotState.Configuration ], [restart: :permanent]), worker(Farmbot.BotState.Configuration, [ctx, [name: Farmbot.BotState.Configuration] ], [restart: :permanent]),
worker(Farmbot.BotState.Hardware, [ctx, name: Farmbot.BotState.Hardware ], [restart: :permanent]), worker(Farmbot.BotState.Hardware, [ctx, [name: Farmbot.BotState.Hardware] ], [restart: :permanent]),
worker(Farmbot.BotState.ProcessSupervisor, [ctx, name: Farmbot.BotState.ProcessSupervisor ], [restart: :permanent]), worker(Farmbot.BotState.ProcessSupervisor, [ctx, [name: Farmbot.BotState.ProcessSupervisor] ], [restart: :permanent]),
worker(EasterEggs, [name: EasterEggs], [restart: :permanent]) worker(EasterEggs, [name: EasterEggs], [restart: :permanent])
] ]
opts = [strategy: :one_for_one] opts = [strategy: :one_for_one]

View File

@ -5,8 +5,10 @@ defmodule Farmbot.CeleryScript.Ast.Context do
alias Farmbot.CeleryScript.Ast alias Farmbot.CeleryScript.Ast
@enforce_keys [:auth, :database, :network, :serial, :hardware, :configuration] @enforce_keys [:auth, :database, :network, :serial, :hardware,
defstruct [:auth, :database, :network, :serial, :hardware, :configuration, data_stack: []] :monitor, :configuration]
defstruct [:auth, :database, :network, :serial, :hardware,
:monitor,:configuration, data_stack: []]
@typedoc false @typedoc false
@type database :: Farmbot.Database.database @type database :: Farmbot.Database.database
@ -23,6 +25,9 @@ defmodule Farmbot.CeleryScript.Ast.Context do
@typedoc false @typedoc false
@type hardware :: Farmbot.BotState.Hardware.hardware @type hardware :: Farmbot.BotState.Hardware.hardware
@typedoc false
@type monitor :: Farmbot.BotState.Monitor.monitor
@typedoc false @typedoc false
@type configuration :: Farmbot.BotState.Configuration.configuration @type configuration :: Farmbot.BotState.Configuration.configuration
@ -35,8 +40,9 @@ defmodule Farmbot.CeleryScript.Ast.Context do
network: network, network: network,
serial: serial, serial: serial,
configuration: configuration, configuration: configuration,
monitor: monitor,
hardware: hardware, hardware: hardware,
data_stack: [Ast.t] data_stack: [Ast.t]
} }
@ -61,6 +67,7 @@ defmodule Farmbot.CeleryScript.Ast.Context do
%__MODULE__{ data_stack: [], %__MODULE__{ data_stack: [],
configuration: Farmbot.BotState.Configuration, configuration: Farmbot.BotState.Configuration,
hardware: Farmbot.BotState.Hardware, hardware: Farmbot.BotState.Hardware,
monitor: Farmbot.BotState.Monitor,
database: Farmbot.Database, database: Farmbot.Database,
network: Farmbot.System.Network, network: Farmbot.System.Network,
serial: Farmbot.Serial.Handler, serial: Farmbot.Serial.Handler,

View File

@ -7,7 +7,7 @@ defmodule Farmware.Worker do
use GenStage use GenStage
@tracker Farmware.Tracker @tracker Farmware.Tracker
alias Farmware.FarmScript alias Farmware.FarmScript
alias Farmbot.CeleryScript.Ast.Context
@type env :: map @type env :: map
@doc """ @doc """
@ -30,10 +30,11 @@ defmodule Farmware.Worker do
@spec initial_env :: env @spec initial_env :: env
defp initial_env do defp initial_env do
context = Context.new()
%{"WRITE_PATH" => "/tmp", # common write path (non persistant) %{"WRITE_PATH" => "/tmp", # common write path (non persistant)
"BEGIN_CS" => "<<< ", # some in band signaling for executing celeryscript. "BEGIN_CS" => "<<< ", # some in band signaling for executing celeryscript.
"IMAGES" => "/tmp/images"} # Dump images here to upload them to the api "IMAGES" => "/tmp/images"} # Dump images here to upload them to the api
|> Map.merge(Farmbot.BotState.get_config(:user_env)) |> Map.merge(Farmbot.BotState.get_config(context, :user_env))
end end
# when a queue of scripts comes in execute them in order # when a queue of scripts comes in execute them in order

View File

@ -103,9 +103,11 @@ defmodule Farmbot.HTTP do
# We only want to upload if we get a 2XX response. # We only want to upload if we get a 2XX response.
defp finish_upload({:ok, %HTTPoison.Response{status_code: s}}, attachment_url) defp finish_upload({:ok, %HTTPoison.Response{status_code: s}}, attachment_url)
when s < 300 do when s < 300 do
[x, y, z] = Farmbot.BotState.get_current_pos context = Context.new()
meta = %{x: x, y: y, z: z} [x, y, z] = Farmbot.BotState.get_current_pos(context)
json = Poison.encode! %{"attachment_url" => attachment_url, "meta" => meta} meta = %{x: x, y: y, z: z}
json = Poison.encode! %{"attachment_url" => attachment_url,
"meta" => meta}
Farmbot.HTTP.post "/api/images", json Farmbot.HTTP.post "/api/images", json
end end
end end

View File

@ -9,6 +9,7 @@ defmodule Farmbot.System.Updates do
@path "/tmp/update.fw" @path "/tmp/update.fw"
require Logger require Logger
alias Farmbot.System.FS alias Farmbot.System.FS
alias Farmbot.CeleryScript.Ast.Context
# TODO(connor): THIS IS A MINOR IMPROVEMENT FROM THE LAST VERSION OF THIS FILE # TODO(connor): THIS IS A MINOR IMPROVEMENT FROM THE LAST VERSION OF THIS FILE
# BUT IT DEFINATELY NEEDS FURTHER REFACTORING. # BUT IT DEFINATELY NEEDS FURTHER REFACTORING.
@ -26,7 +27,8 @@ defmodule Farmbot.System.Updates do
Checks for updates if the bot says so Checks for updates if the bot says so
""" """
def do_update_check do def do_update_check do
if Farmbot.BotState.get_config(:os_auto_update) do context = Farmbot.CeleryScript.Ast.Context.new()
if Farmbot.BotState.get_config(context, :os_auto_update) do
check_and_download_updates() check_and_download_updates()
else else
Logger.info ">> Will not do update check!" Logger.info ">> Will not do update check!"
@ -58,7 +60,8 @@ defmodule Farmbot.System.Updates do
""" """
@spec check_updates :: {:update, binary} | :no_updates | {:error, term} @spec check_updates :: {:update, binary} | :no_updates | {:error, term}
def check_updates do def check_updates do
current = Farmbot.BotState.get_os_version context = Context.new()
current = Farmbot.BotState.get_os_version(context)
if String.contains?(current, "rc") do if String.contains?(current, "rc") do
msg = "Release Candidate Releases don't currently support updates!" msg = "Release Candidate Releases don't currently support updates!"
Logger.info msg, type: :warn Logger.info msg, type: :warn

View File

@ -23,7 +23,7 @@
"exquisite": {:hex, :exquisite, "0.1.7", "4106503e976f409246731b168cd76eb54262bd04f4facc5cba82c2f53982aaf0", [:mix], []}, "exquisite": {:hex, :exquisite, "0.1.7", "4106503e976f409246731b168cd76eb54262bd04f4facc5cba82c2f53982aaf0", [:mix], []},
"exvcr": {:hex, :exvcr, "0.8.7", "e76f33b10dfefbcf32afa6d6867140566d0d54797e352b47485eed0241dd7edf", [:mix], [{:exactor, "~> 2.2", [hex: :exactor, optional: false]}, {:exjsx, "~> 3.2", [hex: :exjsx, optional: false]}, {:httpoison, "~> 0.8", [hex: :httpoison, optional: true]}, {:httpotion, "~> 3.0", [hex: :httpotion, optional: true]}, {:ibrowse, "~> 4.2.2", [hex: :ibrowse, optional: true]}, {:meck, "~> 0.8.3", [hex: :meck, optional: false]}]}, "exvcr": {:hex, :exvcr, "0.8.7", "e76f33b10dfefbcf32afa6d6867140566d0d54797e352b47485eed0241dd7edf", [:mix], [{:exactor, "~> 2.2", [hex: :exactor, optional: false]}, {:exjsx, "~> 3.2", [hex: :exjsx, optional: false]}, {:httpoison, "~> 0.8", [hex: :httpoison, optional: true]}, {:httpotion, "~> 3.0", [hex: :httpotion, optional: true]}, {:ibrowse, "~> 4.2.2", [hex: :ibrowse, optional: true]}, {:meck, "~> 0.8.3", [hex: :meck, optional: false]}]},
"faker": {:hex, :faker, "0.7.0", "2c42deeac7be717173c78c77fb3edc749fb5d5e460e33d01fe592ae99acc2f0d", [:mix], []}, "faker": {:hex, :faker, "0.7.0", "2c42deeac7be717173c78c77fb3edc749fb5d5e460e33d01fe592ae99acc2f0d", [:mix], []},
"farmbot_simulator": {:hex, :farmbot_simulator, "0.1.3", "6b1d1d6028f7fe12ea465434becee536e98a2c9eb4da8fac8deda6bf8b22cf87", [:make, :mix], [{:elixir_make, "~> 0.4", [hex: :elixir_make, optional: false]}, {:nerves_uart, "~> 0.1.2", [hex: :nerves_uart, optional: false]}]}, "farmbot_simulator": {:git, "https://github.com/farmbot-labs/farmbot_simulator.git", "f5b878dc0d4fc845db0b678e7df9e361b3e80834", []},
"fs": {:hex, :fs, "0.9.2", "ed17036c26c3f70ac49781ed9220a50c36775c6ca2cf8182d123b6566e49ec59", [:rebar], []}, "fs": {:hex, :fs, "0.9.2", "ed17036c26c3f70ac49781ed9220a50c36775c6ca2cf8182d123b6566e49ec59", [:rebar], []},
"gen_mqtt": {:hex, :gen_mqtt, "0.3.1", "6ce6af7c2bcb125d5b4125c67c5ab1f29bcec2638236509bcc6abf510a6661ed", [:mix], [{:vmq_commons, "1.0.0", [hex: :vmq_commons, optional: false]}]}, "gen_mqtt": {:hex, :gen_mqtt, "0.3.1", "6ce6af7c2bcb125d5b4125c67c5ab1f29bcec2638236509bcc6abf510a6661ed", [:mix], [{:vmq_commons, "1.0.0", [hex: :vmq_commons, optional: false]}]},
"gen_stage": {:hex, :gen_stage, "0.11.0", "943bdfa85c75fa624e0a36a9d135baad20a523be040178f5a215444b45c66ea4", [:mix], []}, "gen_stage": {:hex, :gen_stage, "0.11.0", "943bdfa85c75fa624e0a36a9d135baad20a523be040178f5a215444b45c66ea4", [:mix], []},

View File

@ -1,29 +1,35 @@
defmodule Farmbot.BotState.ConfigurationTest do defmodule Farmbot.BotState.ConfigurationTest do
use ExUnit.Case, async: false use ExUnit.Case, async: false
alias Farmbot.CeleryScript.Ast.Context
test "makes sure we dont mess state up with bad calls or casts" do setup_all do
before_call = get_state() cs_context = Context.new()
resp = GenServer.call(Farmbot.BotState.Configuration, :do_a_barrel_roll) [cs_context: cs_context]
after_call = get_state() end
test "makes sure we dont mess state up with bad calls or casts", context do
before_call = get_state(context.cs_context)
resp = GenServer.call(context.cs_context.configuration, :do_a_barrel_roll)
after_call = get_state(context.cs_context)
assert(resp == :unhandled) assert(resp == :unhandled)
assert(after_call == before_call) assert(after_call == before_call)
GenServer.cast(Farmbot.BotState.Configuration, :bot_net_start) GenServer.cast(context.cs_context.configuration, :bot_net_start)
after_cast = get_state() after_cast = get_state(context.cs_context)
assert(before_call == after_cast) assert(before_call == after_cast)
end end
test "updates a setting inside informational settings" do test "updates a setting inside informational settings", context do
old = get_state() conf = context.cs_context.configuration
GenServer.cast(Farmbot.BotState.Configuration, old = get_state(context.cs_context)
{:update_info, :i_know_this, :its_unix}) GenServer.cast(conf, {:update_info, :i_know_this, :its_unix})
# maybe bug? change this cast to a call? # maybe bug? change this cast to a call?
new = get_state() new = get_state(context.cs_context)
assert(old != new) assert(old != new)
end end
defp get_state() do defp get_state(ctx) do
Process.sleep(10) Process.sleep(10)
Farmbot.BotState.Monitor.get_state() |> Map.get(:configuration) :sys.get_state(ctx.monitor).state |> Map.fetch!(:configuration)
end end
end end