Skip to content

Commit

Permalink
Update unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
arbulu89 committed Apr 11, 2022
1 parent 0c8a0c9 commit c9d22ae
Show file tree
Hide file tree
Showing 3 changed files with 201 additions and 2 deletions.
10 changes: 10 additions & 0 deletions test/support/factory.ex
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ defmodule Trento.Factory do
ClusterReadModel,
DatabaseInstanceReadModel,
DatabaseReadModel,
HostChecksExecutionsReadModel,
HostConnectionSettings,
HostReadModel,
HostTelemetryReadModel,
Expand Down Expand Up @@ -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)

Expand Down
123 changes: 123 additions & 0 deletions test/trento/application/integration/checks/checks_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ defmodule Trento.Integration.ChecksTest do
use Trento.DataCase

import Mox
import Mock

alias Trento.Integration.Checks

Expand All @@ -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
Expand All @@ -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"}
Expand Down Expand Up @@ -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
70 changes: 68 additions & 2 deletions test/trento/application/projectors/check_result_projector_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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(),
Expand Down Expand Up @@ -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

0 comments on commit c9d22ae

Please sign in to comment.