Skip to content

Commit

Permalink
Add Host Check Selection Domain logic
Browse files Browse the repository at this point in the history
Co-authored-by: Eugen Maksymenko <eugen.maksymenko@suse.com>
  • Loading branch information
nelsonkopliku and EMaksy committed Jun 19, 2023
1 parent 08fd7a8 commit 37d424b
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 1 deletion.
36 changes: 36 additions & 0 deletions lib/trento/domain/host/host.ex
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ defmodule Trento.Domain.Host do
alias Trento.Domain.Commands.{
RegisterHost,
RollUpHost,
SelectHostChecks,
UpdateHeartbeat,
UpdateProvider,
UpdateSlesSubscriptions
Expand All @@ -39,6 +40,7 @@ defmodule Trento.Domain.Host do
alias Trento.Domain.Events.{
HeartbeatFailed,
HeartbeatSucceded,
HostChecksSelected,
HostDetailsUpdated,
HostRegistered,
HostRolledUp,
Expand Down Expand Up @@ -66,6 +68,7 @@ defmodule Trento.Domain.Host do
field :installation_source, Ecto.Enum, values: [:community, :suse, :unknown]
field :heartbeat, Ecto.Enum, values: [:passing, :critical, :unknown]
field :rolling_up, :boolean, default: false
field :selected_checks, {:array, :string}, default: []

embeds_many :subscriptions, SlesSubscription

Expand Down Expand Up @@ -267,6 +270,27 @@ defmodule Trento.Domain.Host do
}
end

def execute(
%Host{host_id: nil},
%SelectHostChecks{}
) do
{:error, :host_not_registered}
end

def execute(
%Host{
host_id: host_id
},
%SelectHostChecks{
checks: selected_checks
}
) do
%HostChecksSelected{
host_id: host_id,
checks: selected_checks
}
end

def apply(
%Host{} = host,
%HostRegistered{
Expand Down Expand Up @@ -373,4 +397,16 @@ defmodule Trento.Domain.Host do
}) do
snapshot
end

def apply(
%Host{} = host,
%HostChecksSelected{
checks: selected_checks
}
) do
%Host{
host
| selected_checks: selected_checks
}
end
end
4 changes: 3 additions & 1 deletion lib/trento/infrastructure/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ defmodule Trento.Router do
RollUpHost,
RollUpSapSystem,
SelectChecks,
SelectHostChecks,
UpdateHeartbeat,
UpdateProvider,
UpdateSlesSubscriptions
Expand All @@ -33,7 +34,8 @@ defmodule Trento.Router do
UpdateHeartbeat,
UpdateProvider,
UpdateSlesSubscriptions,
RollUpHost
RollUpHost,
SelectHostChecks
],
to: Host,
lifespan: Host.Lifespan
Expand Down
42 changes: 42 additions & 0 deletions test/trento/domain/host/host_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ defmodule Trento.HostTest do
alias Trento.Domain.Commands.{
RegisterHost,
RollUpHost,
SelectHostChecks,
UpdateHeartbeat,
UpdateProvider,
UpdateSlesSubscriptions
Expand All @@ -14,6 +15,7 @@ defmodule Trento.HostTest do
alias Trento.Domain.Events.{
HeartbeatFailed,
HeartbeatSucceded,
HostChecksSelected,
HostDetailsUpdated,
HostRegistered,
HostRolledUp,
Expand Down Expand Up @@ -154,6 +156,46 @@ defmodule Trento.HostTest do
end
end

describe "host checks selection" do
test "should select desired checks for host" do
host_id = Faker.UUID.v4()
selected_host_checks = Enum.map(0..4, fn _ -> Faker.Cat.name() end)
host_registered_event = build(:host_registered_event, host_id: host_id)

assert_events_and_state(
host_registered_event,
SelectHostChecks.new!(%{
host_id: host_id,
checks: selected_host_checks
}),
[
%HostChecksSelected{
host_id: host_id,
checks: selected_host_checks
}
],
fn host ->
assert %Host{
selected_checks: ^selected_host_checks
} = host
end
)
end

test "should not accept checks selection if a host is not registered yet" do
host_id = Faker.UUID.v4()
selected_host_checks = Enum.map(0..4, fn _ -> Faker.Cat.name() end)

assert_error(
SelectHostChecks.new!(%{
host_id: host_id,
checks: selected_host_checks
}),
{:error, :host_not_registered}
)
end
end

describe "heartbeat" do
test "should emit an HeartbeatSucceded event if the Host never received an heartbeat already" do
host_id = Faker.UUID.v4()
Expand Down

0 comments on commit 37d424b

Please sign in to comment.