From a10eac64d3e6ca64bb90815d4e471d916064d5bb Mon Sep 17 00:00:00 2001 From: arbulu89 Date: Wed, 24 Apr 2024 08:41:11 +0200 Subject: [PATCH] Avoid SUMA health complete on settings not configured error --- lib/trento/software_updates/discovery.ex | 6 +++ .../software_updates/discovery_test.exs | 44 +++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/lib/trento/software_updates/discovery.ex b/lib/trento/software_updates/discovery.ex index ce409c0cb7..d1148653e6 100644 --- a/lib/trento/software_updates/discovery.ex +++ b/lib/trento/software_updates/discovery.ex @@ -90,6 +90,9 @@ defmodule Trento.SoftwareUpdates.Discovery do |> commanded().dispatch() do {:ok, host_id, system_id, relevant_patches} else + {:error, :settings_not_configured} -> + {:error, :settings_not_configured} + {:error, discovery_error} = error -> Logger.error( "An error occurred during software updates discovery for host #{host_id}: #{inspect(error)}" @@ -106,6 +109,9 @@ defmodule Trento.SoftwareUpdates.Discovery do end end + defp discover_host_software_updates(_, _, {:error, :settings_not_configured} = error), + do: error + defp discover_host_software_updates(host_id, _, {:error, error}) do commanded().dispatch( CompleteSoftwareUpdatesDiscovery.new!(%{ diff --git a/test/trento/software_updates/discovery_test.exs b/test/trento/software_updates/discovery_test.exs index 94f7ee5b06..8cd4ceccde 100644 --- a/test/trento/software_updates/discovery_test.exs +++ b/test/trento/software_updates/discovery_test.exs @@ -73,6 +73,29 @@ defmodule Trento.SoftwareUpdates.DiscoveryTest do Discovery.discover_host_software_updates(host_id, fully_qualified_domain_name) end + test "should handle failure when SUMA settings are not configured" do + host_id = Faker.UUID.v4() + fully_qualified_domain_name = Faker.Internet.domain_name() + + expect( + SoftwareUpdatesDiscoveryMock, + :get_system_id, + fn ^fully_qualified_domain_name -> {:error, :settings_not_configured} end + ) + + expect( + Trento.Commanded.Mock, + :dispatch, + 0, + fn _ -> + :ok + end + ) + + {:error, :settings_not_configured} = + Discovery.discover_host_software_updates(host_id, fully_qualified_domain_name) + end + test "should complete discovery" do scenarios = [ %{ @@ -184,6 +207,27 @@ defmodule Trento.SoftwareUpdates.DiscoveryTest do end) end + test "should handle SUMA settings not configured error" do + expect(SoftwareUpdatesDiscoveryMock, :setup, fn -> {:error, :settings_not_configured} end) + + [%{id: host_id1}, %{id: host_id2}] = insert_list(2, :host) + + expect( + Trento.Commanded.Mock, + :dispatch, + 0, + fn _ -> + :ok + end + ) + + {:ok, {[], errored_discoveries}} = Discovery.discover_software_updates() + + Enum.each([host_id1, host_id2], fn host_id -> + assert {:error, host_id, :settings_not_configured} in errored_discoveries + end) + end + test "should handle hosts without fqdn" do expect(SoftwareUpdatesDiscoveryMock, :setup, fn -> :ok end)