Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue software updates discoveries on save/change settings #2519

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions lib/trento/hosts/host.ex
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,12 @@ defmodule Trento.Hosts.Host do

# Software Updates Discovery

def execute(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need this now, right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well does not harm and keeps away any possibility of weird things happening 😄

%Host{fully_qualified_domain_name: nil},
%DiscoverSoftwareUpdates{}
),
do: []

def execute(
%Host{
host_id: host_id,
Expand Down
37 changes: 15 additions & 22 deletions lib/trento/software_updates.ex
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ defmodule Trento.SoftwareUpdates do
| {:error, any()}
def save_settings(settings_submission, date_service \\ DateService) do
with {:ok, :settings_not_configured, settings} <- ensure_no_settings_configured() do
save_new_settings(settings, settings_submission, date_service)
settings
|> save_or_update_settings(settings_submission, date_service)
|> log_error("Error while saving software updates settings")
end
end

Expand All @@ -54,7 +56,9 @@ defmodule Trento.SoftwareUpdates do
| {:error, any()}
def change_settings(settings_submission, date_service \\ DateService) do
with {:ok, settings} <- get_settings() do
update_settings(settings, settings_submission, date_service)
settings
|> save_or_update_settings(settings_submission, date_service)
|> log_error("Error while updating software updates settings")
end
end

Expand Down Expand Up @@ -148,37 +152,26 @@ defmodule Trento.SoftwareUpdates do
end
end

defp save_new_settings(%Settings{} = settings, settings_submission, date_service) do
saving_result =
defp save_or_update_settings(%Settings{} = settings, settings_submission, date_service) do
result =
settings
|> Settings.changeset(settings_submission, date_service)
|> Repo.update()

case saving_result do
case result do
{:ok, _} = success ->
Discovery.discover_software_updates()
success

{:error, _} = error ->
Logger.error("Error while saving software updates settings: #{inspect(error)}")

error
end
end

defp update_settings(%Settings{} = settings, settings_submission, date_service) do
update_result =
settings
|> Settings.changeset(settings_submission, date_service)
|> Repo.update()

case update_result do
{:ok, _} = success ->
success

{:error, _} = error ->
Logger.error("Error while updating software updates settings: #{inspect(error)}")

error
end
defp log_error({:error, _} = error, message) do
Logger.error("#{message}: #{inspect(error)}")
error
end

defp log_error(result, _), do: result
end
27 changes: 27 additions & 0 deletions test/trento/hosts/host_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -1607,6 +1607,33 @@ defmodule Trento.Hosts.HostTest do
end
end

test "should not trigger the software updates discovery process without a valid FQDN" do
host_id = Faker.UUID.v4()

initial_events = [
build(:host_registered_event,
host_id: host_id,
fully_qualified_domain_name: nil
),
build(:heartbeat_succeded, host_id: host_id)
]

assert_events_and_state(
initial_events,
DiscoverSoftwareUpdates.new!(%{
host_id: host_id
}),
[],
fn host ->
assert %Host{
host_id: ^host_id,
fully_qualified_domain_name: nil,
heartbeat: Health.passing()
} = host
end
)
end

test "should trigger the software updates discovery process" do
host_id = Faker.UUID.v4()
fully_qualified_domain_name = Faker.Internet.domain_name()
Expand Down
29 changes: 29 additions & 0 deletions test/trento/software_updates_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ defmodule Trento.SoftwareUpdates.SettingsTest do

import Trento.Factory

alias Trento.Hosts.Commands.CompleteSoftwareUpdatesDiscovery
alias Trento.SoftwareUpdates
alias Trento.SoftwareUpdates.Settings

Expand Down Expand Up @@ -195,6 +196,34 @@ defmodule Trento.SoftwareUpdates.SettingsTest do

assert {:error, :settings_already_configured} = SoftwareUpdates.save_settings(settings)
end

test "should issue software updates discovery process when saving or updating settings" do
insert_list(5, :host)
insert(:host, deregistered_at: DateTime.to_iso8601(Faker.DateTime.backward(2)))

operations = [
&SoftwareUpdates.save_settings/1,
&SoftwareUpdates.change_settings/1
]

for operation <- operations do
expect(
Trento.Commanded.Mock,
:dispatch,
5,
fn %CompleteSoftwareUpdatesDiscovery{} -> :ok end
)

settings = %{
url: "https://valid.com",
username: Faker.Internet.user_name(),
password: Faker.Lorem.word(),
ca_cert: Faker.Lorem.sentence()
}

assert {:ok, _} = operation.(settings)
end
end
end

describe "changing software updates settings" do
Expand Down
Loading