From c9d22ae05afcd491b27a6a51596916fb19c4e3f4 Mon Sep 17 00:00:00 2001 From: arbulu89 Date: Mon, 11 Apr 2022 18:12:48 +0200 Subject: [PATCH] Update unit tests --- test/support/factory.ex | 10 ++ .../integration/checks/checks_test.exs | 123 ++++++++++++++++++ .../check_result_projector_test.exs | 70 +++++++++- 3 files changed, 201 insertions(+), 2 deletions(-) diff --git a/test/support/factory.ex b/test/support/factory.ex index 0225122afb..de5842dde7 100644 --- a/test/support/factory.ex +++ b/test/support/factory.ex @@ -37,6 +37,7 @@ defmodule Trento.Factory do ClusterReadModel, DatabaseInstanceReadModel, DatabaseReadModel, + HostChecksExecutionsReadModel, HostConnectionSettings, HostReadModel, HostTelemetryReadModel, @@ -387,6 +388,15 @@ defmodule Trento.Factory do }) end + def host_checks_result_projection(attrs \\ []) do + Repo.insert!(%HostChecksExecutionsReadModel{ + cluster_id: Keyword.get(attrs, :cluster_id, Faker.UUID.v4()), + host_id: Keyword.get(attrs, :host_id, Faker.UUID.v4()), + reachable: Keyword.get(attrs, :reachable, true), + msg: Keyword.get(attrs, :msg, Faker.StarWars.planet()) + }) + end + def sap_system_with_cluster_and_hosts do %ClusterReadModel{id: cluster_id} = cluster_projection(type: :hana_scale_up, health: :passing) diff --git a/test/trento/application/integration/checks/checks_test.exs b/test/trento/application/integration/checks/checks_test.exs index 40bff99969..66767234cf 100644 --- a/test/trento/application/integration/checks/checks_test.exs +++ b/test/trento/application/integration/checks/checks_test.exs @@ -3,6 +3,7 @@ defmodule Trento.Integration.ChecksTest do use Trento.DataCase import Mox + import Mock alias Trento.Integration.Checks @@ -15,6 +16,16 @@ defmodule Trento.Integration.ChecksTest do ProviderDto } + alias Trento.Domain.Commands.{ + CompleteChecksExecution, + StartChecksExecution + } + + alias Trento.Domain.{ + CheckResult, + HostExecution + } + @runner_fixtures_path File.cwd!() <> "/test/fixtures/runner" def load_runner_fixture(name) do @@ -24,6 +35,8 @@ defmodule Trento.Integration.ChecksTest do |> Jason.decode!() end + @moduletag :integration + test "should return an error if the runner is not reachable" do expect(Trento.Integration.Checks.Mock, :get_catalog, fn -> {:error, "some error"} @@ -287,4 +300,114 @@ defmodule Trento.Integration.ChecksTest do assert {:ok, catalog_by_provider} == Checks.get_catalog_grouped_by_provider() end + + test "should handle execution started event properly" do + with_mock Trento.Commanded, dispatch: fn _, _ -> :ok end do + execution_id = Faker.UUID.v4() + cluster_id = Faker.UUID.v4() + + Checks.handle_callback(%{ + "event" => "execution_started", + "execution_id" => execution_id, + "payload" => %{ + "cluster_id" => cluster_id + } + }) + + assert_called Trento.Commanded.dispatch( + %StartChecksExecution{ + cluster_id: cluster_id + }, + correlation_id: execution_id + ) + end + end + + test "should handle execution completed event properly" do + with_mock Trento.Commanded, dispatch: fn _, _ -> :ok end do + execution_id = Faker.UUID.v4() + cluster_id = Faker.UUID.v4() + host_id_1 = Faker.UUID.v4() + host_id_2 = Faker.UUID.v4() + + Checks.handle_callback(%{ + "event" => "execution_completed", + "execution_id" => execution_id, + "payload" => %{ + "cluster_id" => cluster_id, + "hosts" => [ + %{ + "host_id" => host_id_1, + "reachable" => true, + "msg" => "", + "results" => [ + %{ + "check_id" => "check1", + "result" => "passing" + }, + %{ + "check_id" => "check2", + "result" => "warning" + } + ] + }, + %{ + "host_id" => host_id_2, + "reachable" => true, + "msg" => "", + "results" => [ + %{ + "check_id" => "check1", + "result" => "critical" + }, + %{ + "check_id" => "check2", + "result" => "warning" + } + ] + } + ] + } + }) + + assert_called Trento.Commanded.dispatch( + %CompleteChecksExecution{ + cluster_id: cluster_id, + hosts_executions: [ + %HostExecution{ + checks_results: [ + %CheckResult{ + check_id: "check1", + result: :passing + }, + %CheckResult{ + check_id: "check2", + result: :warning + } + ], + host_id: host_id_1, + msg: nil, + reachable: true + }, + %HostExecution{ + checks_results: [ + %CheckResult{ + check_id: "check1", + result: :critical + }, + %CheckResult{ + check_id: "check2", + result: :warning + } + ], + host_id: host_id_2, + msg: nil, + reachable: true + } + ] + }, + correlation_id: execution_id + ) + end + end end diff --git a/test/trento/application/projectors/check_result_projector_test.exs b/test/trento/application/projectors/check_result_projector_test.exs index 1483084587..c100bc5346 100644 --- a/test/trento/application/projectors/check_result_projector_test.exs +++ b/test/trento/application/projectors/check_result_projector_test.exs @@ -8,10 +8,14 @@ defmodule Trento.CheckResultProjectorTest do alias Trento.{ CheckResultProjector, - CheckResultReadModel + CheckResultReadModel, + HostChecksExecutionsReadModel } - alias Trento.Domain.Events.HostChecksExecutionCompleted + alias Trento.Domain.Events.{ + ChecksExecutionRequested, + HostChecksExecutionCompleted + } test "should project checks results with result unknown when a ChecksExecutionRequested event is received" do event = checks_execution_requested_event() @@ -32,6 +36,36 @@ defmodule Trento.CheckResultProjectorTest do end) end + test "should project hosts executions with emtpy data when a ChecksExecutionRequested event is received" do + host_checks_result_projection( + cluster_id: cluster_id = Faker.UUID.v4(), + host_id: host_id = Faker.UUID.v4(), + reachable: true, + msg: "" + ) + + event = + ChecksExecutionRequested.new!(%{ + cluster_id: cluster_id, + hosts: [host_id], + checks: ["check1"] + }) + + ProjectorTestHelper.project( + CheckResultProjector, + event, + "check_result_projector" + ) + + hosts_executions = Repo.all(HostChecksExecutionsReadModel) + + assert Enum.all?(hosts_executions, fn %HostChecksExecutionsReadModel{ + reachable: reachable + } -> + reachable == nil + end) + end + test "should update a check result when HostChecksExecutionCompleted event is received" do check_result_projection( cluster_id: cluster_id = Faker.UUID.v4(), @@ -61,4 +95,36 @@ defmodule Trento.CheckResultProjectorTest do assert :critical == check_result_projections.result end + + test "should update a host execution when HostChecksExecutionCompleted event is received" do + host_checks_result_projection( + cluster_id: cluster_id = Faker.UUID.v4(), + host_id: host_id = Faker.UUID.v4(), + reachable: true, + msg: "" + ) + + event = + HostChecksExecutionCompleted.new!(%{ + cluster_id: cluster_id, + host_id: host_id, + reachable: true, + msg: "", + checks_results: [] + }) + + ProjectorTestHelper.project( + CheckResultProjector, + event, + "check_result_projector" + ) + + hosts_executions = Repo.all(HostChecksExecutionsReadModel) + + assert Enum.all?(hosts_executions, fn %HostChecksExecutionsReadModel{ + reachable: reachable + } -> + reachable == true + end) + end end