Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jamie-suse committed Nov 30, 2022
1 parent 95d1e68 commit 81fbb58
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 8 deletions.
7 changes: 2 additions & 5 deletions lib/wanda/catalog.ex
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ defmodule Wanda.Catalog do
def get_check(check_id) do
with path <- Path.join(get_catalog_path(), "#{check_id}.yaml"),
{:ok, file_content} <- YamlElixir.read_from_file(path),
{:ok, check} <-
map_check(file_content) do
{:ok, check} <- map_check(file_content) do
{:ok, check}
else
{:error, :malformed_check} = error ->
Expand Down Expand Up @@ -93,9 +92,7 @@ defmodule Wanda.Catalog do
}}
end

defp map_check(_) do
{:error, :malformed_check}
end
defp map_check(_), do: {:error, :malformed_check}

defp map_severity(%{"severity" => "critical"}), do: :critical
defp map_severity(%{"severity" => "warning"}), do: :warning
Expand Down
10 changes: 10 additions & 0 deletions lib/wanda/executions/server.ex
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ defmodule Wanda.Executions.Server do
|> Target.get_checks_from_targets()
|> Catalog.get_checks()

checks_ids = Enum.map(checks, & &1.id)

# Remove non-existent checks from targets
targets =
Enum.map(targets, fn %{checks: target_checks} = target ->
%Target{target | checks: intersection(target_checks, checks_ids)}
end)

maybe_start_execution(execution_id, group_id, targets, checks, env, config)
end

Expand Down Expand Up @@ -145,6 +153,8 @@ defmodule Wanda.Executions.Server do
{:stop, :normal, state}
end

defp intersection(list_a, list_b), do: list_a -- list_a -- list_b

defp continue_or_complete_execution(
%State{
execution_id: execution_id,
Expand Down
26 changes: 23 additions & 3 deletions test/executions/server_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,7 @@ defmodule Wanda.Executions.ServerTest do
group_id = UUID.uuid4()
targets = build_list(2, :target, checks: ["expect_check"])

expect(Wanda.Messaging.Adapters.Mock, :publish, 0, fn _, _ ->
:ok
end)
expect(Wanda.Messaging.Adapters.Mock, :publish, 0, fn _, _ -> :ok end)

start_supervised!(
{Server,
Expand Down Expand Up @@ -262,5 +260,27 @@ defmodule Wanda.Executions.ServerTest do
assert {:error, :no_checks_selected} =
Server.start_execution(UUID.uuid4(), UUID.uuid4(), targets, %{})
end

test "should execute existing checks if non-existent checks are selected" do
group_id = UUID.uuid4()

[%{agent_id: agent_1}, %{agent_id: agent_2}] =
targets = [
build(:target, checks: ["expect_check", "non_existing"]),
build(:target, checks: ["expect_same_check", "non_existing"])
]

assert :ok = Server.start_execution(UUID.uuid4(), group_id, targets, %{})

pid = :global.whereis_name({Server, group_id})
%{targets: actual_targets} = :sys.get_state(pid)

expected_targets = [
build(:target, agent_id: agent_1, checks: ["expect_check"]),
build(:target, agent_id: agent_2, checks: ["expect_same_check"])
]

assert actual_targets == expected_targets
end
end
end

0 comments on commit 81fbb58

Please sign in to comment.