[#6] adding tests for Plugins
This commit is contained in:
parent
64d1d89a2f
commit
edc2e949cf
|
@ -1,23 +1,23 @@
|
||||||
use Mix.Config
|
use Mix.Config
|
||||||
|
|
||||||
|
|
||||||
# configuration for collectors. The format for each collector is {collector_process_id, module, [target_transfers], label, args}
|
# configuration for collectors. The format for each collector is {collector_process_id, module, label, args}
|
||||||
config :poa_agent,
|
config :poa_agent,
|
||||||
:collectors,
|
:collectors,
|
||||||
[
|
[
|
||||||
{:my_collector, POAAgent.Plugins.Collectors.MyCollector, :my_metrics, [host: "localhost", port: 1234]}
|
# {:my_collector, POAAgent.Plugins.Collectors.MyCollector, :my_metrics, [host: "localhost", port: 1234]}
|
||||||
]
|
]
|
||||||
|
|
||||||
# configuration for transfers. The format for each collector is {collector_process_id, module, args}
|
# configuration for transfers. The format for each collector is {collector_process_id, module, args}
|
||||||
config :poa_agent,
|
config :poa_agent,
|
||||||
:transfers,
|
:transfers,
|
||||||
[
|
[
|
||||||
{:my_transfer, POAAgent.Plugins.Transfers.MyTransfer, [ws_key: "mykey", other_stuff: "hello"]}
|
# {:my_transfer, POAAgent.Plugins.Transfers.MyTransfer, [ws_key: "mykey", other_stuff: "hello"]}
|
||||||
]
|
]
|
||||||
|
|
||||||
# configuration for mappings. This relates one collector with a list of transfers which the data will be sent
|
# configuration for mappings. This relates one collector with a list of transfers which the data will be sent
|
||||||
config :poa_agent,
|
config :poa_agent,
|
||||||
:mappings,
|
:mappings,
|
||||||
[
|
[
|
||||||
{:my_collector, [:my_transfer]}
|
# {:my_collector, [:my_transfer]}
|
||||||
]
|
]
|
|
@ -55,11 +55,12 @@ defmodule POAAgent.Plugins.Collector do
|
||||||
end
|
end
|
||||||
|
|
||||||
@doc false
|
@doc false
|
||||||
def transfer(data, label, transfers) do
|
defp transfer(data, label, transfers) do
|
||||||
Enum.each(transfers, &GenServer.cast(&1, %{label: label, data: data}))
|
Enum.each(transfers, &GenServer.cast(&1, %{label: label, data: data}))
|
||||||
:ok
|
:ok
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@doc false
|
||||||
defp set_collector_timer() do
|
defp set_collector_timer() do
|
||||||
Process.send_after(self(), :collect, 5000) # TODO timeout must be configurable
|
Process.send_after(self(), :collect, 5000) # TODO timeout must be configurable
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,18 +0,0 @@
|
||||||
defmodule POAAgent.Plugins.Collectors.MyCollector do
|
|
||||||
use POAAgent.Plugins.Collector
|
|
||||||
|
|
||||||
def init_collector(args) do
|
|
||||||
IO.puts "init_collector args = #{inspect args}"
|
|
||||||
{:ok, :no_state}
|
|
||||||
end
|
|
||||||
|
|
||||||
def collect(:no_state) do
|
|
||||||
IO.puts "I am collecting data!"
|
|
||||||
{:ok, "data retrieved", :no_state}
|
|
||||||
end
|
|
||||||
|
|
||||||
def terminate(_state) do
|
|
||||||
:ok
|
|
||||||
end
|
|
||||||
|
|
||||||
end
|
|
|
@ -1,18 +0,0 @@
|
||||||
defmodule POAAgent.Plugins.Transfers.MyTransfer do
|
|
||||||
use POAAgent.Plugins.Transfer
|
|
||||||
|
|
||||||
def init_transfer(args) do
|
|
||||||
IO.puts "init_transfer args = #{inspect args}"
|
|
||||||
{:ok, :no_state}
|
|
||||||
end
|
|
||||||
|
|
||||||
def data_received(label, data, state) do
|
|
||||||
IO.puts "Received data with label #{inspect label}, data #{inspect data} and internal_state #{inspect state}"
|
|
||||||
{:ok, :no_state}
|
|
||||||
end
|
|
||||||
|
|
||||||
def terminate(_state) do
|
|
||||||
:ok
|
|
||||||
end
|
|
||||||
|
|
||||||
end
|
|
|
@ -0,0 +1,101 @@
|
||||||
|
defmodule POAAgent.PluginsTest do
|
||||||
|
use ExUnit.Case
|
||||||
|
|
||||||
|
test "__using__ Collector" do
|
||||||
|
defmodule Collector1 do
|
||||||
|
use POAAgent.Plugins.Collector
|
||||||
|
|
||||||
|
def init_collector(_args) do
|
||||||
|
{:ok, :no_state}
|
||||||
|
end
|
||||||
|
|
||||||
|
def collect(:no_state) do
|
||||||
|
{:ok, "data retrieved", :no_state}
|
||||||
|
end
|
||||||
|
|
||||||
|
def terminate(_state) do
|
||||||
|
:ok
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
assert Collector1.init(%{}) == {:ok, %{internal_state: :no_state}}
|
||||||
|
assert Collector1.handle_call(:msg, :from, :state) == {:noreply, :state}
|
||||||
|
assert Collector1.handle_info(:msg, :state) == {:noreply, :state}
|
||||||
|
assert Collector1.handle_cast(:msg, :state) == {:noreply, :state}
|
||||||
|
assert Collector1.code_change(:old, :state, :extra) == {:ok, :state}
|
||||||
|
assert Collector1.terminate(:reason, :state) == :ok
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
test "__using__ Transfer" do
|
||||||
|
defmodule Transfer1 do
|
||||||
|
use POAAgent.Plugins.Transfer
|
||||||
|
|
||||||
|
def init_transfer(_args) do
|
||||||
|
{:ok, :no_state}
|
||||||
|
end
|
||||||
|
|
||||||
|
def data_received(_label, _data, _state) do
|
||||||
|
{:ok, :no_state}
|
||||||
|
end
|
||||||
|
|
||||||
|
def terminate(_state) do
|
||||||
|
:ok
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
assert Transfer1.init(%{args: :args}) == {:ok, %{internal_state: :no_state, args: :args}}
|
||||||
|
assert Transfer1.handle_call(:msg, :from, :state) == {:noreply, :state}
|
||||||
|
assert Transfer1.handle_info(:msg, :state) == {:noreply, :state}
|
||||||
|
assert Transfer1.handle_cast(:msg, :state) == {:noreply, :state}
|
||||||
|
assert Transfer1.code_change(:old, :state, :extra) == {:ok, :state}
|
||||||
|
assert Transfer1.terminate(:reason, :state) == :ok
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
test "Collector - Transfer integration" do
|
||||||
|
defmodule Collector2 do
|
||||||
|
use POAAgent.Plugins.Collector
|
||||||
|
|
||||||
|
def init_collector(test_pid) do
|
||||||
|
{:ok, test_pid}
|
||||||
|
end
|
||||||
|
|
||||||
|
def collect(test_pid) do
|
||||||
|
data = "data retrieved"
|
||||||
|
send test_pid, {:sent, self(), data}
|
||||||
|
{:ok, data, test_pid}
|
||||||
|
end
|
||||||
|
|
||||||
|
def terminate(_state) do
|
||||||
|
:ok
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
defmodule Transfer2 do
|
||||||
|
use POAAgent.Plugins.Transfer
|
||||||
|
|
||||||
|
def init_transfer(test_pid) do
|
||||||
|
{:ok, test_pid}
|
||||||
|
end
|
||||||
|
|
||||||
|
def data_received(label, data, test_pid) do
|
||||||
|
send test_pid, {:received, self(), label, data}
|
||||||
|
{:ok, test_pid}
|
||||||
|
end
|
||||||
|
|
||||||
|
def terminate(_state) do
|
||||||
|
:ok
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
transfer1 = :transfer2
|
||||||
|
|
||||||
|
{:ok, tpid} = Transfer2.start_link(%{name: transfer1, args: self()})
|
||||||
|
{:ok, cpid} = Collector2.start_link(%{name: :collector2, transfers: [transfer1], label: :label, args: self()})
|
||||||
|
|
||||||
|
assert_receive {:sent, ^cpid, "data retrieved"}, 20_000
|
||||||
|
assert_receive {:received, ^tpid, :label, "data retrieved"}, 20_000
|
||||||
|
|
||||||
|
end
|
||||||
|
end
|
|
@ -1,8 +0,0 @@
|
||||||
defmodule POAAgentTest do
|
|
||||||
use ExUnit.Case
|
|
||||||
doctest POAAgent
|
|
||||||
|
|
||||||
test "greets the world" do
|
|
||||||
# assert POAAgent.hello() == :world
|
|
||||||
end
|
|
||||||
end
|
|
Loading…
Reference in New Issue