From 81fbb58ac61980ee26a992e9794aad9cb5f03ed5 Mon Sep 17 00:00:00 2001 From: Jamie Rodriguez Date: Tue, 29 Nov 2022 15:59:49 +0100 Subject: [PATCH] Add tests --- lib/wanda/catalog.ex | 7 ++----- lib/wanda/executions/server.ex | 10 ++++++++++ test/executions/server_test.exs | 26 +++++++++++++++++++++++--- 3 files changed, 35 insertions(+), 8 deletions(-) diff --git a/lib/wanda/catalog.ex b/lib/wanda/catalog.ex index 285b26752..0db2ed245 100644 --- a/lib/wanda/catalog.ex +++ b/lib/wanda/catalog.ex @@ -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 -> @@ -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 diff --git a/lib/wanda/executions/server.ex b/lib/wanda/executions/server.ex index 331a3915f..c2a33732d 100644 --- a/lib/wanda/executions/server.ex +++ b/lib/wanda/executions/server.ex @@ -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 @@ -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, diff --git a/test/executions/server_test.exs b/test/executions/server_test.exs index ee873380d..6e289a36e 100644 --- a/test/executions/server_test.exs +++ b/test/executions/server_test.exs @@ -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, @@ -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