2020-04-22 20:09:41 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 14:09:03 -04:00
|
|
|
RSpec.describe AlertManagement::Alert do
|
2020-07-28 08:09:49 -04:00
|
|
|
let_it_be(:project) { create(:project) }
|
|
|
|
let_it_be(:project2) { create(:project) }
|
|
|
|
let_it_be(:triggered_alert, reload: true) { create(:alert_management_alert, :triggered, project: project) }
|
|
|
|
let_it_be(:acknowledged_alert, reload: true) { create(:alert_management_alert, :acknowledged, project: project) }
|
|
|
|
let_it_be(:resolved_alert, reload: true) { create(:alert_management_alert, :resolved, project: project2) }
|
|
|
|
let_it_be(:ignored_alert, reload: true) { create(:alert_management_alert, :ignored, project: project2) }
|
|
|
|
|
2020-04-22 20:09:41 -04:00
|
|
|
describe 'associations' do
|
|
|
|
it { is_expected.to belong_to(:project) }
|
2020-06-26 14:09:03 -04:00
|
|
|
it { is_expected.to belong_to(:issue).optional }
|
|
|
|
it { is_expected.to belong_to(:prometheus_alert).optional }
|
|
|
|
it { is_expected.to belong_to(:environment).optional }
|
2020-05-29 17:08:35 -04:00
|
|
|
it { is_expected.to have_many(:assignees).through(:alert_assignees) }
|
2020-06-12 08:08:56 -04:00
|
|
|
it { is_expected.to have_many(:notes) }
|
2020-06-15 17:08:35 -04:00
|
|
|
it { is_expected.to have_many(:ordered_notes) }
|
2020-06-12 08:08:56 -04:00
|
|
|
it { is_expected.to have_many(:user_mentions) }
|
2020-04-22 20:09:41 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
describe 'validations' do
|
|
|
|
it { is_expected.to validate_presence_of(:title) }
|
|
|
|
it { is_expected.to validate_presence_of(:events) }
|
|
|
|
it { is_expected.to validate_presence_of(:severity) }
|
|
|
|
it { is_expected.to validate_presence_of(:status) }
|
|
|
|
it { is_expected.to validate_presence_of(:started_at) }
|
|
|
|
|
|
|
|
it { is_expected.to validate_length_of(:title).is_at_most(200) }
|
|
|
|
it { is_expected.to validate_length_of(:description).is_at_most(1000) }
|
|
|
|
it { is_expected.to validate_length_of(:service).is_at_most(100) }
|
|
|
|
it { is_expected.to validate_length_of(:monitoring_tool).is_at_most(100) }
|
|
|
|
|
2020-05-07 11:09:29 -04:00
|
|
|
context 'when status is triggered' do
|
2020-07-28 08:09:49 -04:00
|
|
|
subject { triggered_alert }
|
2020-05-07 11:09:29 -04:00
|
|
|
|
2020-07-28 08:09:49 -04:00
|
|
|
context 'when ended_at is blank' do
|
2020-05-07 11:09:29 -04:00
|
|
|
it { is_expected.to be_valid }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when ended_at is present' do
|
2020-07-28 08:09:49 -04:00
|
|
|
before do
|
|
|
|
triggered_alert.ended_at = Time.current
|
|
|
|
end
|
2020-05-07 11:09:29 -04:00
|
|
|
|
|
|
|
it { is_expected.to be_invalid }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when status is acknowledged' do
|
2020-07-28 08:09:49 -04:00
|
|
|
subject { acknowledged_alert }
|
2020-05-07 11:09:29 -04:00
|
|
|
|
2020-07-28 08:09:49 -04:00
|
|
|
context 'when ended_at is blank' do
|
2020-05-07 11:09:29 -04:00
|
|
|
it { is_expected.to be_valid }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when ended_at is present' do
|
2020-07-28 08:09:49 -04:00
|
|
|
before do
|
|
|
|
acknowledged_alert.ended_at = Time.current
|
|
|
|
end
|
2020-05-07 11:09:29 -04:00
|
|
|
|
|
|
|
it { is_expected.to be_invalid }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when status is resolved' do
|
2020-07-28 08:09:49 -04:00
|
|
|
subject { resolved_alert }
|
|
|
|
|
2020-05-07 11:09:29 -04:00
|
|
|
context 'when ended_at is blank' do
|
2020-07-28 08:09:49 -04:00
|
|
|
before do
|
|
|
|
resolved_alert.ended_at = nil
|
|
|
|
end
|
2020-05-07 11:09:29 -04:00
|
|
|
|
|
|
|
it { is_expected.to be_invalid }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when ended_at is present' do
|
|
|
|
it { is_expected.to be_valid }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when status is ignored' do
|
2020-07-28 08:09:49 -04:00
|
|
|
subject { ignored_alert }
|
2020-05-07 11:09:29 -04:00
|
|
|
|
2020-07-28 08:09:49 -04:00
|
|
|
context 'when ended_at is blank' do
|
2020-05-07 11:09:29 -04:00
|
|
|
it { is_expected.to be_valid }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when ended_at is present' do
|
2020-07-28 08:09:49 -04:00
|
|
|
before do
|
|
|
|
ignored_alert.ended_at = Time.current
|
|
|
|
end
|
2020-05-07 11:09:29 -04:00
|
|
|
|
|
|
|
it { is_expected.to be_invalid }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-04-22 20:09:41 -04:00
|
|
|
describe 'fingerprint' do
|
|
|
|
let_it_be(:fingerprint) { 'fingerprint' }
|
|
|
|
let(:new_alert) { build(:alert_management_alert, fingerprint: fingerprint, project: project) }
|
|
|
|
|
|
|
|
subject { new_alert }
|
|
|
|
|
|
|
|
context 'adding an alert with the same fingerprint' do
|
2020-07-13 08:09:18 -04:00
|
|
|
context 'same project, various states' do
|
|
|
|
using RSpec::Parameterized::TableSyntax
|
|
|
|
|
2020-07-28 08:09:49 -04:00
|
|
|
let_it_be(:existing_alert) { create(:alert_management_alert, fingerprint: fingerprint, project: project) }
|
|
|
|
|
2020-07-13 08:09:18 -04:00
|
|
|
# We are only validating uniqueness for non-resolved alerts
|
|
|
|
where(:existing_status, :new_status, :valid) do
|
|
|
|
:resolved | :triggered | true
|
|
|
|
:resolved | :acknowledged | true
|
|
|
|
:resolved | :ignored | true
|
|
|
|
:resolved | :resolved | true
|
|
|
|
:triggered | :triggered | false
|
|
|
|
:triggered | :acknowledged | false
|
|
|
|
:triggered | :ignored | false
|
|
|
|
:triggered | :resolved | true
|
|
|
|
:acknowledged | :triggered | false
|
|
|
|
:acknowledged | :acknowledged | false
|
|
|
|
:acknowledged | :ignored | false
|
|
|
|
:acknowledged | :resolved | true
|
|
|
|
:ignored | :triggered | false
|
|
|
|
:ignored | :acknowledged | false
|
|
|
|
:ignored | :ignored | false
|
|
|
|
:ignored | :resolved | true
|
|
|
|
end
|
|
|
|
|
|
|
|
with_them do
|
|
|
|
let(:new_alert) { build(:alert_management_alert, new_status, fingerprint: fingerprint, project: project) }
|
|
|
|
|
2020-07-28 08:09:49 -04:00
|
|
|
before do
|
2020-10-06 08:08:38 -04:00
|
|
|
existing_alert.change_status_to(existing_status)
|
2020-07-28 08:09:49 -04:00
|
|
|
end
|
|
|
|
|
2020-07-13 08:09:18 -04:00
|
|
|
if params[:valid]
|
|
|
|
it { is_expected.to be_valid }
|
|
|
|
else
|
|
|
|
it { is_expected.to be_invalid }
|
|
|
|
end
|
|
|
|
end
|
2020-04-22 20:09:41 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'different project' do
|
2020-07-28 08:09:49 -04:00
|
|
|
let_it_be(:existing_alert) { create(:alert_management_alert, fingerprint: fingerprint, project: project2) }
|
2020-04-22 20:09:41 -04:00
|
|
|
|
|
|
|
it { is_expected.to be_valid }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'hosts' do
|
2020-07-28 08:09:49 -04:00
|
|
|
subject(:alert) { triggered_alert }
|
|
|
|
|
|
|
|
before do
|
|
|
|
triggered_alert.hosts = hosts
|
|
|
|
end
|
2020-04-22 20:09:41 -04:00
|
|
|
|
|
|
|
context 'over 255 total chars' do
|
|
|
|
let(:hosts) { ['111.111.111.111'] * 18 }
|
|
|
|
|
|
|
|
it { is_expected.not_to be_valid }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'under 255 chars' do
|
|
|
|
let(:hosts) { ['111.111.111.111'] * 17 }
|
|
|
|
|
|
|
|
it { is_expected.to be_valid }
|
|
|
|
end
|
2020-10-20 08:08:54 -04:00
|
|
|
|
|
|
|
context 'nested array' do
|
|
|
|
let(:hosts) { ['111.111.111.111', ['111.111.111.111']] }
|
|
|
|
|
|
|
|
it { is_expected.not_to be_valid }
|
|
|
|
end
|
2020-04-22 20:09:41 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'enums' do
|
|
|
|
let(:severity_values) do
|
|
|
|
{ critical: 0, high: 1, medium: 2, low: 3, info: 4, unknown: 5 }
|
|
|
|
end
|
|
|
|
|
|
|
|
it { is_expected.to define_enum_for(:severity).with_values(severity_values) }
|
|
|
|
end
|
2020-04-28 11:09:29 -04:00
|
|
|
|
2020-05-11 11:09:37 -04:00
|
|
|
describe 'scopes' do
|
|
|
|
describe '.for_iid' do
|
2020-07-28 08:09:49 -04:00
|
|
|
subject { project.alert_management_alerts.for_iid(triggered_alert.iid) }
|
2020-04-28 11:09:29 -04:00
|
|
|
|
2020-05-19 20:08:20 -04:00
|
|
|
it { is_expected.to match_array(triggered_alert) }
|
2020-05-11 11:09:37 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
describe '.for_status' do
|
2020-10-08 11:08:17 -04:00
|
|
|
let(:status) { :resolved }
|
2020-05-11 11:09:37 -04:00
|
|
|
|
|
|
|
subject { AlertManagement::Alert.for_status(status) }
|
|
|
|
|
2020-05-19 20:08:20 -04:00
|
|
|
it { is_expected.to match_array(resolved_alert) }
|
2020-05-11 11:09:37 -04:00
|
|
|
|
|
|
|
context 'with multiple statuses' do
|
2020-10-08 11:08:17 -04:00
|
|
|
let(:status) { [:resolved, :ignored] }
|
2020-05-11 11:09:37 -04:00
|
|
|
|
2020-05-19 20:08:20 -04:00
|
|
|
it { is_expected.to match_array([resolved_alert, ignored_alert]) }
|
2020-05-11 11:09:37 -04:00
|
|
|
end
|
|
|
|
end
|
2020-05-06 08:09:36 -04:00
|
|
|
|
2020-05-19 20:08:20 -04:00
|
|
|
describe '.for_fingerprint' do
|
2020-07-28 08:09:49 -04:00
|
|
|
let(:fingerprint) { SecureRandom.hex }
|
|
|
|
let(:alert_with_fingerprint) { triggered_alert }
|
|
|
|
let(:unrelated_alert_with_finger_print) { resolved_alert }
|
2020-05-07 11:09:29 -04:00
|
|
|
|
2020-05-19 20:08:20 -04:00
|
|
|
subject { described_class.for_fingerprint(project, fingerprint) }
|
|
|
|
|
2020-07-28 08:09:49 -04:00
|
|
|
before do
|
|
|
|
alert_with_fingerprint.update!(fingerprint: fingerprint)
|
|
|
|
unrelated_alert_with_finger_print.update!(fingerprint: fingerprint)
|
|
|
|
end
|
|
|
|
|
2020-05-19 20:08:20 -04:00
|
|
|
it { is_expected.to contain_exactly(alert_with_fingerprint) }
|
|
|
|
end
|
2020-05-07 11:09:29 -04:00
|
|
|
|
2020-07-02 17:09:14 -04:00
|
|
|
describe '.for_environment' do
|
|
|
|
let(:environment) { create(:environment, project: project) }
|
2020-07-28 08:09:49 -04:00
|
|
|
let(:env_alert) { triggered_alert }
|
2020-07-02 17:09:14 -04:00
|
|
|
|
|
|
|
subject { described_class.for_environment(environment) }
|
|
|
|
|
2020-07-28 08:09:49 -04:00
|
|
|
before do
|
|
|
|
triggered_alert.update!(environment: environment)
|
|
|
|
end
|
|
|
|
|
2020-07-02 17:09:14 -04:00
|
|
|
it { is_expected.to match_array(env_alert) }
|
|
|
|
end
|
|
|
|
|
2020-10-12 14:08:31 -04:00
|
|
|
describe '.for_assignee_username' do
|
|
|
|
let_it_be(:alert) { triggered_alert }
|
|
|
|
let_it_be(:assignee) { create(:user) }
|
|
|
|
|
|
|
|
subject { AlertManagement::Alert.for_assignee_username(assignee_username) }
|
|
|
|
|
|
|
|
before_all do
|
|
|
|
alert.update!(assignees: [assignee])
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when matching assignee_username' do
|
|
|
|
let(:assignee_username) { assignee.username }
|
|
|
|
|
|
|
|
it { is_expected.to contain_exactly(alert) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when unknown assignee_username' do
|
|
|
|
let(:assignee_username) { 'unknown username' }
|
|
|
|
|
|
|
|
it { is_expected.to be_empty }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with empty assignee_username' do
|
|
|
|
let(:assignee_username) { ' ' }
|
|
|
|
|
|
|
|
it { is_expected.to be_empty }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-08-13 14:10:36 -04:00
|
|
|
describe '.order_severity_with_open_prometheus_alert' do
|
|
|
|
subject { described_class.where(project: alert_project).order_severity_with_open_prometheus_alert }
|
|
|
|
|
|
|
|
let_it_be(:alert_project) { create(:project) }
|
|
|
|
let_it_be(:resolved_critical_alert) { create(:alert_management_alert, :resolved, :critical, project: alert_project) }
|
|
|
|
let_it_be(:triggered_critical_alert) { create(:alert_management_alert, :triggered, :critical, project: alert_project) }
|
|
|
|
let_it_be(:triggered_high_alert) { create(:alert_management_alert, :triggered, :high, project: alert_project) }
|
|
|
|
|
|
|
|
it { is_expected.to eq([triggered_critical_alert, triggered_high_alert]) }
|
|
|
|
end
|
|
|
|
|
2020-07-02 17:09:14 -04:00
|
|
|
describe '.counts_by_project_id' do
|
|
|
|
subject { described_class.counts_by_project_id }
|
|
|
|
|
|
|
|
it do
|
|
|
|
is_expected.to eq(
|
2020-07-28 08:09:49 -04:00
|
|
|
project.id => 2,
|
|
|
|
project2.id => 2
|
2020-07-02 17:09:14 -04:00
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.open' do
|
|
|
|
subject { described_class.open }
|
|
|
|
|
|
|
|
it { is_expected.to contain_exactly(acknowledged_alert, triggered_alert) }
|
|
|
|
end
|
2020-07-13 17:09:24 -04:00
|
|
|
|
|
|
|
describe '.not_resolved' do
|
|
|
|
subject { described_class.not_resolved }
|
|
|
|
|
|
|
|
it { is_expected.to contain_exactly(acknowledged_alert, triggered_alert, ignored_alert) }
|
|
|
|
end
|
2020-07-02 17:09:14 -04:00
|
|
|
end
|
|
|
|
|
2020-10-08 11:08:17 -04:00
|
|
|
describe '.status_value' do
|
|
|
|
using RSpec::Parameterized::TableSyntax
|
|
|
|
|
|
|
|
where(:status, :status_value) do
|
|
|
|
:triggered | 0
|
|
|
|
:acknowledged | 1
|
|
|
|
:resolved | 2
|
|
|
|
:ignored | 3
|
|
|
|
:unknown | nil
|
|
|
|
end
|
|
|
|
|
|
|
|
with_them do
|
|
|
|
it 'returns status value by its name' do
|
|
|
|
expect(described_class.status_value(status)).to eq(status_value)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.status_name' do
|
|
|
|
using RSpec::Parameterized::TableSyntax
|
|
|
|
|
|
|
|
where(:raw_status, :status) do
|
|
|
|
0 | :triggered
|
|
|
|
1 | :acknowledged
|
|
|
|
2 | :resolved
|
|
|
|
3 | :ignored
|
|
|
|
-1 | nil
|
|
|
|
end
|
|
|
|
|
|
|
|
with_them do
|
|
|
|
it 'returns status name by its values' do
|
|
|
|
expect(described_class.status_name(raw_status)).to eq(status)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.counts_by_status' do
|
|
|
|
subject { described_class.counts_by_status }
|
|
|
|
|
|
|
|
it do
|
|
|
|
is_expected.to eq(
|
|
|
|
triggered: 1,
|
|
|
|
acknowledged: 1,
|
|
|
|
resolved: 1,
|
|
|
|
ignored: 1
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-07-02 17:09:14 -04:00
|
|
|
describe '.last_prometheus_alert_by_project_id' do
|
|
|
|
subject { described_class.last_prometheus_alert_by_project_id }
|
|
|
|
|
2020-07-28 08:09:49 -04:00
|
|
|
let!(:p1_alert_1) { triggered_alert }
|
|
|
|
let!(:p1_alert_2) { acknowledged_alert }
|
2020-07-02 17:09:14 -04:00
|
|
|
|
2020-07-28 08:09:49 -04:00
|
|
|
let!(:p2_alert_1) { resolved_alert }
|
|
|
|
let!(:p2_alert_2) { ignored_alert }
|
2020-07-02 17:09:14 -04:00
|
|
|
|
|
|
|
it 'returns the latest alert for each project' do
|
2020-07-21 08:09:30 -04:00
|
|
|
expect(subject).to contain_exactly(p1_alert_2, p2_alert_2)
|
2020-07-02 17:09:14 -04:00
|
|
|
end
|
2020-05-07 11:09:29 -04:00
|
|
|
end
|
|
|
|
|
2020-05-18 05:08:12 -04:00
|
|
|
describe '.search' do
|
2020-07-28 08:09:49 -04:00
|
|
|
let(:alert) { triggered_alert }
|
|
|
|
|
|
|
|
before do
|
|
|
|
alert.update!(title: 'Title', description: 'Desc', service: 'Service', monitoring_tool: 'Monitor')
|
2020-05-18 05:08:12 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
subject { AlertManagement::Alert.search(query) }
|
|
|
|
|
|
|
|
context 'does not contain search string' do
|
|
|
|
let(:query) { 'something else' }
|
|
|
|
|
|
|
|
it { is_expected.to be_empty }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'title includes query' do
|
|
|
|
let(:query) { alert.title.upcase }
|
|
|
|
|
|
|
|
it { is_expected.to contain_exactly(alert) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'description includes query' do
|
|
|
|
let(:query) { alert.description.upcase }
|
|
|
|
|
|
|
|
it { is_expected.to contain_exactly(alert) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'service includes query' do
|
|
|
|
let(:query) { alert.service.upcase }
|
|
|
|
|
|
|
|
it { is_expected.to contain_exactly(alert) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'monitoring tool includes query' do
|
|
|
|
let(:query) { alert.monitoring_tool.upcase }
|
|
|
|
|
|
|
|
it { is_expected.to contain_exactly(alert) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-09-14 23:09:24 -04:00
|
|
|
describe '.reference_pattern' do
|
|
|
|
subject { described_class.reference_pattern }
|
|
|
|
|
|
|
|
it { is_expected.to match('gitlab-org/gitlab^alert#123') }
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.link_reference_pattern' do
|
|
|
|
subject { described_class.link_reference_pattern }
|
|
|
|
|
|
|
|
it { is_expected.to match(triggered_alert.details_url) }
|
|
|
|
it { is_expected.not_to match("#{Gitlab.config.gitlab.url}/gitlab-org/gitlab/alert_management/123") }
|
|
|
|
it { is_expected.not_to match("#{Gitlab.config.gitlab.url}/gitlab-org/gitlab/issues/123") }
|
|
|
|
it { is_expected.not_to match("gitlab-org/gitlab/-/alert_management/123") }
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.reference_valid?' do
|
|
|
|
using RSpec::Parameterized::TableSyntax
|
|
|
|
|
|
|
|
where(:ref, :result) do
|
|
|
|
'123456' | true
|
|
|
|
'1' | true
|
|
|
|
'-1' | false
|
|
|
|
nil | false
|
|
|
|
'123456891012345678901234567890' | false
|
|
|
|
end
|
|
|
|
|
|
|
|
with_them do
|
|
|
|
it { expect(described_class.reference_valid?(ref)).to eq(result) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-10-07 05:09:13 -04:00
|
|
|
describe '.open_status?' do
|
|
|
|
using RSpec::Parameterized::TableSyntax
|
|
|
|
|
|
|
|
where(:status, :is_open_status) do
|
|
|
|
:triggered | true
|
|
|
|
:acknowledged | true
|
|
|
|
:resolved | false
|
|
|
|
:ignored | false
|
|
|
|
nil | false
|
|
|
|
end
|
|
|
|
|
|
|
|
with_them do
|
|
|
|
it 'returns true when the status is open status' do
|
|
|
|
expect(described_class.open_status?(status)).to eq(is_open_status)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-06-05 14:08:19 -04:00
|
|
|
describe '#to_reference' do
|
2020-09-14 23:09:24 -04:00
|
|
|
it { expect(triggered_alert.to_reference).to eq("^alert##{triggered_alert.iid}") }
|
2020-06-05 14:08:19 -04:00
|
|
|
end
|
|
|
|
|
2020-05-07 11:09:29 -04:00
|
|
|
describe '#trigger' do
|
|
|
|
subject { alert.trigger }
|
|
|
|
|
|
|
|
context 'when alert is in triggered state' do
|
2020-07-28 08:09:49 -04:00
|
|
|
let(:alert) { triggered_alert }
|
2020-05-07 11:09:29 -04:00
|
|
|
|
|
|
|
it 'does not change the alert status' do
|
|
|
|
expect { subject }.not_to change { alert.reload.status }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when alert not in triggered state' do
|
2020-07-28 08:09:49 -04:00
|
|
|
let(:alert) { resolved_alert }
|
2020-05-07 11:09:29 -04:00
|
|
|
|
|
|
|
it 'changes the alert status to triggered' do
|
|
|
|
expect { subject }.to change { alert.triggered? }.to(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'resets ended at' do
|
|
|
|
expect { subject }.to change { alert.reload.ended_at }.to nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#acknowledge' do
|
|
|
|
subject { alert.acknowledge }
|
|
|
|
|
2020-07-28 08:09:49 -04:00
|
|
|
let(:alert) { resolved_alert }
|
2020-05-07 11:09:29 -04:00
|
|
|
|
|
|
|
it 'changes the alert status to acknowledged' do
|
|
|
|
expect { subject }.to change { alert.acknowledged? }.to(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'resets ended at' do
|
|
|
|
expect { subject }.to change { alert.reload.ended_at }.to nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#resolve' do
|
|
|
|
let!(:ended_at) { Time.current }
|
|
|
|
|
|
|
|
subject do
|
|
|
|
alert.ended_at = ended_at
|
|
|
|
alert.resolve
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when alert already resolved' do
|
2020-07-28 08:09:49 -04:00
|
|
|
let(:alert) { resolved_alert }
|
2020-05-07 11:09:29 -04:00
|
|
|
|
|
|
|
it 'does not change the alert status' do
|
2020-07-28 08:09:49 -04:00
|
|
|
expect { subject }.not_to change { resolved_alert.reload.status }
|
2020-05-07 11:09:29 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when alert is not resolved' do
|
2020-07-28 08:09:49 -04:00
|
|
|
let(:alert) { triggered_alert }
|
2020-05-07 11:09:29 -04:00
|
|
|
|
|
|
|
it 'changes alert status to "resolved"' do
|
|
|
|
expect { subject }.to change { alert.resolved? }.to(true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#ignore' do
|
|
|
|
subject { alert.ignore }
|
|
|
|
|
2020-07-28 08:09:49 -04:00
|
|
|
let(:alert) { resolved_alert }
|
2020-05-07 11:09:29 -04:00
|
|
|
|
|
|
|
it 'changes the alert status to ignored' do
|
|
|
|
expect { subject }.to change { alert.ignored? }.to(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'resets ended at' do
|
|
|
|
expect { subject }.to change { alert.reload.ended_at }.to nil
|
|
|
|
end
|
|
|
|
end
|
2020-05-25 05:08:30 -04:00
|
|
|
|
|
|
|
describe '#register_new_event!' do
|
|
|
|
subject { alert.register_new_event! }
|
|
|
|
|
2020-07-28 08:09:49 -04:00
|
|
|
let(:alert) { triggered_alert }
|
2020-05-25 05:08:30 -04:00
|
|
|
|
|
|
|
it 'increments the events count by 1' do
|
2020-06-04 14:08:32 -04:00
|
|
|
expect { subject }.to change { alert.events }.by(1)
|
2020-05-25 05:08:30 -04:00
|
|
|
end
|
|
|
|
end
|
2020-10-06 08:08:38 -04:00
|
|
|
|
|
|
|
describe '#status_event_for' do
|
|
|
|
using RSpec::Parameterized::TableSyntax
|
|
|
|
|
|
|
|
where(:for_status, :event) do
|
|
|
|
:triggered | :trigger
|
|
|
|
'triggered' | :trigger
|
|
|
|
:acknowledged | :acknowledge
|
|
|
|
'acknowledged' | :acknowledge
|
|
|
|
:resolved | :resolve
|
|
|
|
'resolved' | :resolve
|
|
|
|
:ignored | :ignore
|
|
|
|
'ignored' | :ignore
|
|
|
|
:unknown | nil
|
|
|
|
nil | nil
|
|
|
|
'' | nil
|
|
|
|
1 | nil
|
|
|
|
end
|
|
|
|
|
|
|
|
with_them do
|
|
|
|
let(:alert) { build(:alert_management_alert, project: project) }
|
|
|
|
|
|
|
|
it 'returns event by status name' do
|
|
|
|
expect(alert.status_event_for(for_status)).to eq(event)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#change_status_to' do
|
|
|
|
let_it_be_with_reload(:alert) { create(:alert_management_alert, project: project) }
|
|
|
|
|
|
|
|
context 'with valid statuses' do
|
|
|
|
it 'changes the status to triggered' do
|
|
|
|
alert.acknowledge! # change to non-triggered status
|
|
|
|
expect { alert.change_status_to(:triggered) }.to change { alert.triggered? }.to(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
%i(acknowledged resolved ignored).each do |status|
|
|
|
|
it "changes the status to #{status}" do
|
|
|
|
expect { alert.change_status_to(status) }.to change { alert.public_send(:"#{status}?") }.to(true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with invalid status' do
|
|
|
|
it 'does not change the current status' do
|
|
|
|
expect { alert.change_status_to(nil) }.not_to change { alert.status }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-04-22 20:09:41 -04:00
|
|
|
end
|