Skip to content

Commit

Permalink
Discarded discovery events to be recorded (#3040)
Browse files Browse the repository at this point in the history
  • Loading branch information
balanza authored Oct 3, 2024
1 parent 66524ae commit 79ab215
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/trento/discovery.ex
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ defmodule Trento.Discovery do
@spec handle(map) :: :ok | {:error, any}
def handle(event) do
with {:ok, commands} <- do_handle(event),
{:ok, _} <- store_discovery_event(event) do
dispatch(commands)
{:ok, _} <- store_discovery_event(event),
:ok <- dispatch(commands) do
:ok
else
{:error, reason} = error ->
Logger.error("Failed to handle discovery event: #{inspect(reason)}")
Expand Down
14 changes: 14 additions & 0 deletions test/support/factory.ex
Original file line number Diff line number Diff line change
Expand Up @@ -902,6 +902,20 @@ defmodule Trento.Factory do
}
end

def host_discovery_event_factory do
%{
"hostname" => Faker.StarWars.character(),
"ip_addresses" => [Faker.Internet.ip_v4_address()],
"agent_version" => Faker.App.semver(),
"cpu_count" => Enum.random(1..16),
"total_memory_mb" => Enum.random(1..128),
"socket_count" => Enum.random(1..16),
"os_version" => Faker.App.semver(),
"installation_source" => Enum.random(["community", "suse", "unknown"]),
"fully_qualified_domain_name" => Faker.Internet.domain_name()
}
end

def software_updates_discovery_health_changed_event_factory do
SoftwareUpdatesHealthChanged.new!(%{
host_id: Faker.UUID.v4(),
Expand Down
23 changes: 23 additions & 0 deletions test/trento/discovery_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ defmodule Trento.DiscoveryTest do
use ExUnit.Case
use Trento.DataCase

import Mox

import Trento.Factory

alias Trento.Discovery
Expand Down Expand Up @@ -121,4 +123,25 @@ defmodule Trento.DiscoveryTest do
%DiscardedDiscoveryEvent{payload: ^event}
] = discarded_events
end

test "should discard discovery events when the dispatch action fails" do
error = :any_error

event = %{
"agent_id" => Faker.UUID.v4(),
"discovery_type" => "host_discovery",
"payload" => build(:host_discovery_event)
}

expect(Trento.Commanded.Mock, :dispatch, fn _ ->
{:error, error}
end)

{:error, ^error} = Discovery.handle(event)

[discarded_event] = Trento.Repo.all(DiscardedDiscoveryEvent)

assert %DiscardedDiscoveryEvent{payload: ^event} =
discarded_event
end
end
30 changes: 30 additions & 0 deletions test/trento_web/controllers/v1/discovery_controller_test.exs
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
defmodule TrentoWeb.V1.DiscoveryControllerTest do
use TrentoWeb.ConnCase, async: true

import Trento.DiscoveryFixturesHelper

import Mox

alias Trento.Discovery

alias Trento.Discovery.DiscardedDiscoveryEvent

describe "discovery" do
test "collect action should return an unprocessable entity error when the event is unknown",
%{conn: conn} do
Expand Down Expand Up @@ -32,5 +40,27 @@ defmodule TrentoWeb.V1.DiscoveryControllerTest do
})
|> json_response(202)
end

test "collect action discards application instance registrations when the associated database does not exists",
%{conn: conn} do
body =
load_discovery_event_fixture("sap_system_discovery_application")

expect(Trento.Commanded.Mock, :dispatch, fn _ ->
{:error, :any_error}
end)

%{status: status} =
conn
|> put_req_header("content-type", "application/json")
|> post("/api/v1/collect", body)

assert status != 202

[discarded_event] = Discovery.get_discarded_discovery_events(1)

assert %DiscardedDiscoveryEvent{payload: ^body, reason: "[:any_error]"} =
discarded_event
end
end
end

0 comments on commit 79ab215

Please sign in to comment.