Skip to content

Commit

Permalink
Filter out non-registered hosts from affected systems in SUMA patch
Browse files Browse the repository at this point in the history
detail
  • Loading branch information
dottorblaster committed Sep 13, 2024
1 parent 37aa303 commit 1302774
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,9 @@ defmodule Trento.Infrastructure.SoftwareUpdates.MockSuma do
{:ok,
[
%{
name: "test"
name: "vmdrbddev01"
},
%{name: "test2"}
%{name: "vmdrbddev02"}
]}

@impl true
Expand Down
18 changes: 17 additions & 1 deletion lib/trento_web/controllers/v1/suse_manager_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ defmodule TrentoWeb.V1.SUSEManagerController do
use TrentoWeb, :controller
use OpenApiSpex.ControllerSpecs

alias Trento.Hosts
alias Trento.Hosts.Projections.HostReadModel

alias Trento.SoftwareUpdates
alias Trento.SoftwareUpdates.Discovery

Expand Down Expand Up @@ -94,6 +97,16 @@ defmodule TrentoWeb.V1.SUSEManagerController do

@spec errata_details(Plug.Conn.t(), any) :: Plug.Conn.t()
def errata_details(conn, %{advisory_name: advisory_name}) do
hosts = Hosts.get_all_hosts()

registered_hosts =
Enum.flat_map(hosts, fn %HostReadModel{
fully_qualified_domain_name: fqdn,
hostname: hostname
} ->
[fqdn, hostname]
end)

with {:ok, errata_details} <- Discovery.get_errata_details(advisory_name),
{:ok, cves} <- Discovery.get_cves(advisory_name),
{:ok, fixes} <- Discovery.get_bugzilla_fixes(advisory_name),
Expand All @@ -104,7 +117,10 @@ defmodule TrentoWeb.V1.SUSEManagerController do
cves: cves,
fixes: fixes,
affected_packages: affected_packages,
affected_systems: affected_systems
affected_systems:
Enum.filter(affected_systems, fn %{name: system} ->
Enum.member?(registered_hosts, system)
end)
})
end
end
Expand Down
4 changes: 2 additions & 2 deletions test/e2e/cypress/e2e/suse_manager_overviews.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ context('SUSE Manager overviews', () => {
cy.contains('Affected Packages').next().should('contain', 'kernel');
cy.contains('Affected Systems')
.next()
.should('contain', 'test')
.should('contain', 'test2');
.should('contain', 'vmdrbddev01')
.should('contain', 'vmdrbddev02');

cy.clearSUMASettings();
});
Expand Down
56 changes: 56 additions & 0 deletions test/trento_web/controllers/v1/suse_manager_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,10 @@ defmodule TrentoWeb.V1.SUSEManagerControllerTest do

affected_systems = build_list(10, :affected_system)

Enum.each(affected_systems, fn %{name: system} ->
insert(:host, hostname: system)
end)

expect(Trento.SoftwareUpdates.Discovery.Mock, :get_affected_systems, 1, fn _ ->
{:ok, affected_systems}
end)
Expand Down Expand Up @@ -226,6 +230,58 @@ defmodule TrentoWeb.V1.SUSEManagerControllerTest do
} = result
end

test "should filter out non-registered hosts from affected systems", %{
conn: conn,
api_spec: api_spec
} do
insert_software_updates_settings()

advisory_name = Faker.Pokemon.name()

errata_details = build(:errata_details)

expect(Trento.SoftwareUpdates.Discovery.Mock, :get_errata_details, 1, fn _ ->
{:ok, errata_details}
end)

cves = build_list(10, :cve)

expect(Trento.SoftwareUpdates.Discovery.Mock, :get_cves, 1, fn _ ->
{:ok, cves}
end)

fixes = build(:bugzilla_fix)

expect(Trento.SoftwareUpdates.Discovery.Mock, :get_bugzilla_fixes, 1, fn _ ->
{:ok, fixes}
end)

affected_packages = build_list(10, :affected_package)

expect(Trento.SoftwareUpdates.Discovery.Mock, :get_affected_packages, 1, fn _ ->
{:ok, affected_packages}
end)

[%{name: first_affected_system} | _] = affected_systems = build_list(10, :affected_system)

insert(:host, fully_qualified_domain_name: first_affected_system)

expect(Trento.SoftwareUpdates.Discovery.Mock, :get_affected_systems, 1, fn _ ->
{:ok, affected_systems}
end)

json =
conn
|> get("/api/v1/software_updates/errata_details/#{advisory_name}")
|> json_response(:ok)

result = assert_schema(json, "ErrataDetailsResponse", api_spec)

%{
affected_systems: [%{name: ^first_affected_system}]
} = result
end

error_scenarios = [
%{
name: "advisory details not found",
Expand Down

0 comments on commit 1302774

Please sign in to comment.