-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4203217
commit cf25d5f
Showing
9 changed files
with
349 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
defmodule Wanda.Execution.ExpectationsTest do | ||
use ExUnit.Case | ||
|
||
alias Wanda.Execution.{ | ||
CheckResult, | ||
ExpectationResult, | ||
Expectations, | ||
Result | ||
} | ||
|
||
test "should eval the expectations and return a list of results" do | ||
gathered_facts = %{ | ||
"agent_1" => %{ | ||
"happy" => %{ | ||
"corosync_token_timeout" => 30_000, | ||
"http_port_open" => true | ||
} | ||
}, | ||
"agent_2" => %{ | ||
"happy" => %{ | ||
"corosync_token_timeout" => 30_001, | ||
"http_port_open" => false | ||
} | ||
} | ||
} | ||
|
||
assert [ | ||
%Result{ | ||
agent_id: "agent_1", | ||
checks_results: [ | ||
%CheckResult{ | ||
check_id: "happy", | ||
expectations_results: [ | ||
%ExpectationResult{name: "timeout", result: true}, | ||
%ExpectationResult{ | ||
name: "http_port_open", | ||
result: true | ||
} | ||
], | ||
facts: %{"corosync_token_timeout" => 30_000, "http_port_open" => true}, | ||
result: :passing | ||
} | ||
] | ||
}, | ||
%Result{ | ||
agent_id: "agent_2", | ||
checks_results: [ | ||
%CheckResult{ | ||
check_id: "happy", | ||
expectations_results: [ | ||
%ExpectationResult{name: "timeout", result: false}, | ||
%ExpectationResult{ | ||
name: "http_port_open", | ||
result: false | ||
} | ||
], | ||
facts: %{"corosync_token_timeout" => 30_001, "http_port_open" => false}, | ||
result: :critical | ||
} | ||
] | ||
} | ||
] == Expectations.eval(gathered_facts) | ||
end | ||
|
||
test "should return errors when the expressions are illegal" do | ||
gathered_facts = %{ | ||
"agent_1" => %{ | ||
"with_illegal_expression" => %{ | ||
"corosync_token_timeout" => 30_000 | ||
} | ||
} | ||
} | ||
|
||
assert [ | ||
%Result{ | ||
checks_results: [ | ||
%CheckResult{ | ||
result: :critical, | ||
check_id: "with_illegal_expression", | ||
expectations_results: [ | ||
%ExpectationResult{ | ||
name: "illegal_expression", | ||
result: :illegal_expression_error | ||
} | ||
] | ||
} | ||
] | ||
} | ||
] = Expectations.eval(gathered_facts) | ||
end | ||
|
||
test "should return errors when the facts are missing" do | ||
gathered_facts = %{ | ||
"agent_1" => %{ | ||
"with_fact_missing" => %{} | ||
} | ||
} | ||
|
||
assert [ | ||
%Result{ | ||
checks_results: [ | ||
%CheckResult{ | ||
result: :critical, | ||
check_id: "with_fact_missing", | ||
expectations_results: [ | ||
%ExpectationResult{ | ||
name: "fact_missing", | ||
result: :fact_missing_error | ||
} | ||
] | ||
} | ||
] | ||
} | ||
] = Expectations.eval(gathered_facts) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
defmodule Wanda.Execution.GatheringTest do | ||
use ExUnit.Case | ||
|
||
import Wanda.Factory | ||
|
||
alias Wanda.Execution.{ | ||
Fact, | ||
Gathering, | ||
Target | ||
} | ||
|
||
describe "put gathered facts" do | ||
test "should put the gathered facts for the proper agent id" do | ||
agent_id_1 = UUID.uuid4() | ||
[%Fact{check_id: check_id_1, name: name_1, value: value_1}] = facts = build_list(1, :fact) | ||
|
||
assert %{ | ||
^agent_id_1 => %{ | ||
^check_id_1 => %{ | ||
^name_1 => ^value_1 | ||
} | ||
} | ||
} = gathered_facts = Gathering.put_gathered_facts(%{}, agent_id_1, facts) | ||
|
||
agent_id_2 = UUID.uuid4() | ||
|
||
[ | ||
%Fact{check_id: check_id_2, name: name_2, value: value_2}, | ||
%Fact{check_id: check_id_3, name: name_3, value: value_3} | ||
] = facts = build_list(2, :fact) | ||
|
||
assert %{ | ||
^agent_id_1 => %{ | ||
^check_id_1 => %{ | ||
^name_1 => ^value_1 | ||
} | ||
}, | ||
^agent_id_2 => %{ | ||
^check_id_2 => %{ | ||
^name_2 => ^value_2 | ||
}, | ||
^check_id_3 => %{ | ||
^name_3 => ^value_3 | ||
} | ||
} | ||
} = Gathering.put_gathered_facts(gathered_facts, agent_id_2, facts) | ||
end | ||
end | ||
|
||
describe "target" do | ||
test "should return true if the agent_id is present inside targets" do | ||
[%Target{agent_id: agent_id} | _] = targets = build_list(Enum.random(1..100), :target) | ||
|
||
assert Gathering.target?(targets, agent_id) | ||
end | ||
|
||
test "should return false if the agent_id is not present inside targets" do | ||
targets = build_list(Enum.random(1..100), :target) | ||
|
||
refute Gathering.target?(targets, UUID.uuid4()) | ||
end | ||
end | ||
|
||
describe "all agents sent facts" do | ||
test "should return true if all the agents have sent facts" do | ||
[%Target{agent_id: agent_id} | _] = targets = build_list(1, :target) | ||
|
||
gathered_facts = %{ | ||
agent_id => %{ | ||
"check_1" => %{ | ||
"fact_name" => "fact_value" | ||
} | ||
} | ||
} | ||
|
||
assert Gathering.all_agents_sent_facts?(gathered_facts, targets) | ||
end | ||
|
||
test "should return false if all the agents have not sent facts" do | ||
[%Target{agent_id: agent_id} | _] = targets = build_list(2, :target) | ||
|
||
gathered_facts = %{ | ||
agent_id => %{ | ||
"check_1" => %{ | ||
"fact_name" => "fact_value" | ||
} | ||
} | ||
} | ||
|
||
refute Gathering.all_agents_sent_facts?(gathered_facts, targets) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
defmodule Wanda.ExecutionTest do | ||
use Wanda.Support.MessagingCase, async: false | ||
|
||
import Mox | ||
import Wanda.Factory | ||
|
||
alias Wanda.Execution | ||
|
||
setup [:set_mox_from_context, :verify_on_exit!] | ||
|
||
describe "start_link/3" do | ||
test "should accept an execution_id, a group_id and targets on start" do | ||
execution_id = UUID.uuid4() | ||
group_id = UUID.uuid4() | ||
|
||
assert {:ok, pid} = | ||
start_supervised(%{ | ||
id: Execution, | ||
start: | ||
{Execution, :start_link, [execution_id, group_id, build_list(10, :target)]} | ||
}) | ||
|
||
assert pid == :global.whereis_name({Execution, execution_id}) | ||
end | ||
end | ||
|
||
describe "execution orchestration" do | ||
test "should start an execution" do | ||
pid = self() | ||
|
||
expect(Wanda.Messaging.Adapters.Mock, :publish, fn _ -> | ||
send(pid, :wandalorian) | ||
|
||
:ok | ||
end) | ||
|
||
execution_id = UUID.uuid4() | ||
group_id = UUID.uuid4() | ||
|
||
start_supervised!(%{ | ||
id: {Execution, execution_id}, | ||
start: {Execution, :start_link, [execution_id, group_id, build_list(10, :target)]} | ||
}) | ||
|
||
assert_receive :wandalorian | ||
end | ||
|
||
test "should exit when all facts are sent by all agents" do | ||
pid = self() | ||
execution_id = UUID.uuid4() | ||
group_id = UUID.uuid4() | ||
|
||
targets = build_list(3, :target, %{checks: ["happy"]}) | ||
|
||
expect(Wanda.Messaging.Adapters.Mock, :publish, 2, fn | ||
"stuff2" -> | ||
send(pid, :executed) | ||
|
||
:ok | ||
|
||
_ -> | ||
:ok | ||
end) | ||
|
||
{:ok, pid} = | ||
start_supervised(%{ | ||
id: {Execution, execution_id}, | ||
start: {Execution, :start_link, [execution_id, group_id, targets]} | ||
}) | ||
|
||
ref = Process.monitor(pid) | ||
|
||
Enum.each(targets, fn target -> | ||
Execution.receive_facts(execution_id, target.agent_id, [ | ||
%Wanda.Execution.Fact{ | ||
check_id: "happy", | ||
name: "corosync_token_timeout", | ||
value: 30_000 | ||
}, | ||
%Wanda.Execution.Fact{ | ||
check_id: "happy", | ||
name: "http_port_open", | ||
value: true | ||
} | ||
]) | ||
end) | ||
|
||
assert_receive :executed | ||
assert_receive {:DOWN, ^ref, _, ^pid, :normal} | ||
end | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
defmodule Wanda.Factory do | ||
use ExMachina | ||
|
||
alias Wanda.Execution.{ | ||
Fact, | ||
Target | ||
} | ||
|
||
def target_factory(attrs) do | ||
%Target{ | ||
agent_id: Map.get(attrs, :agent_id, UUID.uuid4()), | ||
checks: Map.get(attrs, :checks, Enum.map(1..10, fn _ -> UUID.uuid4() end)) | ||
} | ||
end | ||
|
||
def fact_factory(attrs) do | ||
%Fact{ | ||
check_id: Map.get(attrs, :name, UUID.uuid4()), | ||
name: Map.get(attrs, :name, Faker.StarWars.character()), | ||
value: Map.get(attrs, :value, Faker.StarWars.planet()) | ||
} | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
defmodule Wanda.Support.MessagingCase do | ||
@moduledoc nil | ||
|
||
use ExUnit.CaseTemplate | ||
|
||
setup _ do | ||
Mox.stub_with(Wanda.Messaging.Adapters.Mock, Wanda.Support.PublisherStub) | ||
|
||
:ok | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
defmodule Wanda.Support.PublisherStub do | ||
@moduledoc nil | ||
|
||
@behaviour Wanda.Messaging.Adapters.Behaviour | ||
|
||
@impl true | ||
def publish(_), do: :ok | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,6 @@ | ||
ExUnit.start() | ||
Mox.defmock(Wanda.Messaging.Adapters.Mock, for: Wanda.Messaging.Adapters.Behaviour) | ||
|
||
Application.put_env(:wanda, Wanda.Messaging.Publisher, adapter: Wanda.Messaging.Adapters.Mock) | ||
|
||
ExUnit.start(capture_log: true) | ||
Faker.start() |