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

Cluster health when no checks are selected #555

Merged
merged 5 commits into from
May 20, 2022
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
35 changes: 27 additions & 8 deletions lib/trento/domain/cluster/cluster.ex
Original file line number Diff line number Diff line change
Expand Up @@ -136,20 +136,34 @@ defmodule Trento.Domain.Cluster do
def execute(
%Cluster{
cluster_id: cluster_id
},
} = cluster,
%SelectChecks{
checks: selected_checks
}
) do
[
%ChecksSelected{
cluster_id: cluster_id,
checks: selected_checks
}
]
cluster
|> Multi.new()
|> Multi.execute(fn _ ->
[
%ChecksSelected{
cluster_id: cluster_id,
checks: selected_checks
}
]
end)
|> Multi.execute(fn cluster -> maybe_emit_cluster_health_changed_event(cluster) end)
end

# Request checks execution
def execute(
%Cluster{
cluster_id: cluster_id,
selected_checks: []
},
%RequestChecksExecution{cluster_id: cluster_id}
),
do: nil

def execute(
%Cluster{
cluster_id: cluster_id,
Expand Down Expand Up @@ -478,14 +492,19 @@ defmodule Trento.Domain.Cluster do
}
end

defp maybe_add_checks_health(healths, _, []), do: healths
defp maybe_add_checks_health(healths, checks_health, _), do: [checks_health | healths]

defp maybe_emit_cluster_health_changed_event(%Cluster{
cluster_id: cluster_id,
discovered_health: discovered_health,
checks_health: checks_health,
selected_checks: selected_checks,
health: health
}) do
new_health =
[discovered_health, checks_health]
[discovered_health]
|> maybe_add_checks_health(checks_health, selected_checks)
|> Enum.filter(& &1)
|> HealthService.compute_aggregated_health()

Expand Down
5 changes: 5 additions & 0 deletions test/e2e/cypress/integration/dashboard.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { agents } from '../fixtures/hosts-overview/available_hosts';
import { allClusterIds } from '../fixtures/clusters-overview/available_clusters';

describe('Dashboard page', () => {
before(() => {
cy.task('startAgentHeartbeat', agents());
allClusterIds().forEach((clusterId) => {
cy.selectChecks(clusterId, []);
});
cy.navigateToItem('Dashboard');
cy.url().should('include', '/');
});
Expand Down
17 changes: 17 additions & 0 deletions test/e2e/cypress/support/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,20 @@ Cypress.Commands.add('navigateToItem', (item) => {
Cypress.Commands.add('clickOutside', () => {
return cy.get('body').click(0, 0); //0,0 here are the x and y coordinates
});

Cypress.Commands.add('selectChecks', (clusterId, checks) => {
const [webAPIHost, webAPIPort] = [
Cypress.env('web_api_host'),
Cypress.env('web_api_port'),
];
const checksBody = JSON.stringify({
checks: checks,
});

const headers = {
'Content-Type': 'application/json;charset=UTF-8',
};

const url = `http://${webAPIHost}:${webAPIPort}/api/clusters/${clusterId}/checks`;
cy.request({ method: 'POST', url: url, body: checksBody, headers: headers });
});
17 changes: 17 additions & 0 deletions test/support/factory.ex
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ defmodule Trento.Factory do

alias Trento.Domain.Commands.{
RegisterApplicationInstance,
RegisterClusterHost,
RegisterDatabaseInstance
}

Expand Down Expand Up @@ -98,6 +99,22 @@ defmodule Trento.Factory do
}
end

def register_cluster_host_factory do
%RegisterClusterHost{
cluster_id: Faker.UUID.v4(),
host_id: Faker.UUID.v4(),
name: Faker.StarWars.character(),
sid: Faker.StarWars.planet(),
provider: :azure,
resources_number: 8,
hosts_number: 2,
details: hana_cluster_details_value_object(),
type: :hana_scale_up,
discovered_health: :passing,
designated_controller: true
}
end

def cluster_registered_event_factory do
%ClusterRegistered{
cluster_id: Faker.UUID.v4(),
Expand Down
133 changes: 127 additions & 6 deletions test/trento/domain/cluster/cluster_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,50 @@ defmodule Trento.ClusterTest do
)
end

test "should use discovered cluster health when no checks are selected" do
cluster_id = Faker.UUID.v4()
name = Faker.StarWars.character()
sid = Faker.StarWars.planet()

assert_events_and_state(
[
build(
:cluster_registered_event,
cluster_id: cluster_id,
name: name,
sid: sid,
details: nil
)
],
[
build(
:register_cluster_host,
cluster_id: cluster_id,
name: name,
sid: sid,
details: nil,
discovered_health: :passing
),
SelectChecks.new!(%{
cluster_id: cluster_id,
checks: []
})
],
[
%ChecksSelected{
cluster_id: cluster_id,
checks: []
}
],
fn cluster ->
assert %Cluster{
selected_checks: [],
health: :passing
} = cluster
end
)
end

test "should request a checks execution with the selected checks" do
cluster_id = Faker.UUID.v4()
host_id = Faker.UUID.v4()
Expand Down Expand Up @@ -305,6 +349,24 @@ defmodule Trento.ClusterTest do
)
end

test "should not start a checks execution if no checks are selected" do
cluster_id = Faker.UUID.v4()

assert_events(
[
build(:cluster_registered_event, cluster_id: cluster_id),
%ChecksSelected{
cluster_id: cluster_id,
checks: []
}
],
RequestChecksExecution.new!(%{
cluster_id: cluster_id
}),
[]
)
end

test "should start a checks execution" do
cluster_id = Faker.UUID.v4()
host_id = Faker.UUID.v4()
Expand Down Expand Up @@ -475,9 +537,14 @@ defmodule Trento.ClusterTest do
assert_events_and_state(
[
cluster_registered_event,
%HostAddedToCluster{
host_added_to_cluster_event,
%ChecksExecutionCompleted{
cluster_id: cluster_registered_event.cluster_id,
host_id: host_added_to_cluster_event.host_id
health: :unknown
},
%ChecksSelected{
cluster_id: cluster_registered_event.cluster_id,
checks: []
}
],
RegisterClusterHost.new!(%{
Expand All @@ -491,21 +558,23 @@ defmodule Trento.ClusterTest do
hosts_number: cluster_registered_event.hosts_number,
details: StructHelper.to_map(cluster_registered_event.details),
designated_controller: true,
discovered_health: :critical
discovered_health: :warning
}),
[
%ClusterDiscoveredHealthChanged{
cluster_id: cluster_registered_event.cluster_id,
discovered_health: :critical
discovered_health: :warning
},
%ClusterHealthChanged{
cluster_id: cluster_registered_event.cluster_id,
health: :critical
health: :warning
}
],
fn cluster ->
%Cluster{
discovered_health: :critical
discovered_health: :warning,
checks_health: :unknown,
health: :warning
} = cluster
end
)
Expand Down Expand Up @@ -546,5 +615,57 @@ defmodule Trento.ClusterTest do
end
)
end

test "should not change the the cluster aggregated health if checks health is worst" do
cluster_registered_event = build(:cluster_registered_event, health: :passing)

host_added_to_cluster_event =
build(:host_added_to_cluster_event, cluster_id: cluster_registered_event.cluster_id)

assert_events_and_state(
[
cluster_registered_event,
host_added_to_cluster_event,
%ChecksSelected{
cluster_id: cluster_registered_event.cluster_id,
checks: [Faker.Cat.name()]
},
%ChecksExecutionCompleted{
cluster_id: cluster_registered_event.cluster_id,
health: :critical
},
%ClusterHealthChanged{
cluster_id: cluster_registered_event.cluster_id,
health: :critical
}
],
RegisterClusterHost.new!(%{
cluster_id: cluster_registered_event.cluster_id,
host_id: host_added_to_cluster_event.host_id,
name: cluster_registered_event.name,
sid: cluster_registered_event.sid,
provider: :azure,
type: cluster_registered_event.type,
resources_number: cluster_registered_event.resources_number,
hosts_number: cluster_registered_event.hosts_number,
details: StructHelper.to_map(cluster_registered_event.details),
designated_controller: true,
discovered_health: :warning
}),
[
%ClusterDiscoveredHealthChanged{
cluster_id: cluster_registered_event.cluster_id,
discovered_health: :warning
}
],
fn cluster ->
%Cluster{
discovered_health: :warning,
checks_health: :critical,
health: :critical
} = cluster
end
)
end
end
end