2020-03-13 15:44:24 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
RSpec.describe Projects::Alerting::NotifyService do
|
2020-11-24 15:15:51 +05:30
|
|
|
let_it_be_with_reload(:project) { create(:project, :repository) }
|
2020-03-13 15:44:24 +05:30
|
|
|
|
|
|
|
before do
|
2020-06-23 00:09:42 +05:30
|
|
|
allow(ProjectServiceWorker).to receive(:perform_async)
|
2020-03-13 15:44:24 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
describe '#execute' do
|
|
|
|
let(:token) { 'invalid-token' }
|
2020-05-24 23:13:21 +05:30
|
|
|
let(:starts_at) { Time.current.change(usec: 0) }
|
2020-06-23 00:09:42 +05:30
|
|
|
let(:fingerprint) { 'testing' }
|
2020-03-13 15:44:24 +05:30
|
|
|
let(:service) { described_class.new(project, nil, payload) }
|
2020-11-24 15:15:51 +05:30
|
|
|
let_it_be(:environment) { create(:environment, project: project) }
|
|
|
|
let(:environment) { create(:environment, project: project) }
|
|
|
|
let(:ended_at) { nil }
|
2020-03-13 15:44:24 +05:30
|
|
|
let(:payload_raw) do
|
|
|
|
{
|
2020-05-24 23:13:21 +05:30
|
|
|
title: 'alert title',
|
|
|
|
start_time: starts_at.rfc3339,
|
2020-11-24 15:15:51 +05:30
|
|
|
end_time: ended_at&.rfc3339,
|
2020-05-24 23:13:21 +05:30
|
|
|
severity: 'low',
|
|
|
|
monitoring_tool: 'GitLab RSpec',
|
|
|
|
service: 'GitLab Test Suite',
|
|
|
|
description: 'Very detailed description',
|
2020-06-23 00:09:42 +05:30
|
|
|
hosts: ['1.1.1.1', '2.2.2.2'],
|
2020-11-24 15:15:51 +05:30
|
|
|
fingerprint: fingerprint,
|
|
|
|
gitlab_environment_name: environment.name
|
2020-05-24 23:13:21 +05:30
|
|
|
}.with_indifferent_access
|
2020-03-13 15:44:24 +05:30
|
|
|
end
|
2020-10-24 23:57:45 +05:30
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
let(:payload) { ActionController::Parameters.new(payload_raw).permit! }
|
|
|
|
|
|
|
|
subject { service.execute(token) }
|
|
|
|
|
|
|
|
context 'with activated Alerts Service' do
|
2020-11-24 15:15:51 +05:30
|
|
|
let_it_be_with_reload(:alerts_service) { create(:alerts_service, project: project) }
|
2020-03-13 15:44:24 +05:30
|
|
|
|
|
|
|
context 'with valid token' do
|
|
|
|
let(:token) { alerts_service.token }
|
2020-11-24 15:15:51 +05:30
|
|
|
let(:incident_management_setting) { double(send_email?: email_enabled, create_issue?: issue_enabled, auto_close_incident?: auto_close_enabled) }
|
2020-04-08 14:13:33 +05:30
|
|
|
let(:email_enabled) { false }
|
|
|
|
let(:issue_enabled) { false }
|
2020-11-24 15:15:51 +05:30
|
|
|
let(:auto_close_enabled) { false }
|
2020-04-08 14:13:33 +05:30
|
|
|
|
|
|
|
before do
|
|
|
|
allow(service)
|
|
|
|
.to receive(:incident_management_setting)
|
|
|
|
.and_return(incident_management_setting)
|
|
|
|
end
|
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
context 'with valid payload' do
|
2020-07-28 23:09:34 +05:30
|
|
|
shared_examples 'assigns the alert properties' do
|
|
|
|
it 'ensure that created alert has all data properly assigned' do
|
|
|
|
subject
|
|
|
|
|
|
|
|
expect(last_alert_attributes).to match(
|
|
|
|
project_id: project.id,
|
|
|
|
title: payload_raw.fetch(:title),
|
|
|
|
started_at: Time.zone.parse(payload_raw.fetch(:start_time)),
|
|
|
|
severity: payload_raw.fetch(:severity),
|
|
|
|
status: AlertManagement::Alert::STATUSES[:triggered],
|
|
|
|
events: 1,
|
|
|
|
hosts: payload_raw.fetch(:hosts),
|
|
|
|
payload: payload_raw.with_indifferent_access,
|
|
|
|
issue_id: nil,
|
|
|
|
description: payload_raw.fetch(:description),
|
|
|
|
monitoring_tool: payload_raw.fetch(:monitoring_tool),
|
|
|
|
service: payload_raw.fetch(:service),
|
|
|
|
fingerprint: Digest::SHA1.hexdigest(fingerprint),
|
2020-11-24 15:15:51 +05:30
|
|
|
environment_id: environment.id,
|
2020-07-28 23:09:34 +05:30
|
|
|
ended_at: nil,
|
2020-11-24 15:15:51 +05:30
|
|
|
prometheus_alert_id: nil
|
2020-07-28 23:09:34 +05:30
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
let(:last_alert_attributes) do
|
|
|
|
AlertManagement::Alert.last.attributes
|
|
|
|
.except('id', 'iid', 'created_at', 'updated_at')
|
|
|
|
.with_indifferent_access
|
|
|
|
end
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
it_behaves_like 'creates an alert management alert'
|
|
|
|
it_behaves_like 'assigns the alert properties'
|
2020-06-23 00:09:42 +05:30
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
it 'creates a system note corresponding to alert creation' do
|
|
|
|
expect { subject }.to change(Note, :count).by(1)
|
|
|
|
end
|
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
context 'existing alert with same fingerprint' do
|
|
|
|
let(:fingerprint_sha) { Digest::SHA1.hexdigest(fingerprint) }
|
2020-07-28 23:09:34 +05:30
|
|
|
let!(:alert) { create(:alert_management_alert, project: project, fingerprint: fingerprint_sha) }
|
|
|
|
|
|
|
|
it_behaves_like 'adds an alert management alert event'
|
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
context 'end time given' do
|
|
|
|
let(:ended_at) { Time.current.change(nsec: 0) }
|
|
|
|
|
|
|
|
it 'does not resolve the alert' do
|
|
|
|
expect { subject }.not_to change { alert.reload.status }
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not set the ended at' do
|
|
|
|
subject
|
|
|
|
|
|
|
|
expect(alert.reload.ended_at).to be_nil
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'does not an create alert management alert'
|
|
|
|
|
|
|
|
context 'auto_close_enabled setting enabled' do
|
|
|
|
let(:auto_close_enabled) { true }
|
|
|
|
|
|
|
|
it 'resolves the alert and sets the end time', :aggregate_failures do
|
|
|
|
subject
|
|
|
|
alert.reload
|
|
|
|
|
|
|
|
expect(alert.resolved?).to eq(true)
|
|
|
|
expect(alert.ended_at).to eql(ended_at)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'related issue exists' do
|
|
|
|
let(:alert) { create(:alert_management_alert, :with_issue, project: project, fingerprint: fingerprint_sha) }
|
|
|
|
let(:issue) { alert.issue }
|
|
|
|
|
|
|
|
context 'state_tracking is enabled' do
|
|
|
|
before do
|
|
|
|
stub_feature_flags(track_resource_state_change_events: true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it { expect { subject }.to change { issue.reload.state }.from('opened').to('closed') }
|
|
|
|
it { expect { subject }.to change(ResourceStateEvent, :count).by(1) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'state_tracking is disabled' do
|
|
|
|
before do
|
|
|
|
stub_feature_flags(track_resource_state_change_events: false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it { expect { subject }.to change { issue.reload.state }.from('opened').to('closed') }
|
|
|
|
it { expect { subject }.to change(Note, :count).by(1) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
context 'existing alert is resolved' do
|
|
|
|
let!(:alert) { create(:alert_management_alert, :resolved, project: project, fingerprint: fingerprint_sha) }
|
2020-06-23 00:09:42 +05:30
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
it_behaves_like 'creates an alert management alert'
|
|
|
|
it_behaves_like 'assigns the alert properties'
|
2020-06-23 00:09:42 +05:30
|
|
|
end
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
context 'existing alert is ignored' do
|
|
|
|
let!(:alert) { create(:alert_management_alert, :ignored, project: project, fingerprint: fingerprint_sha) }
|
|
|
|
|
|
|
|
it_behaves_like 'adds an alert management alert event'
|
2020-06-23 00:09:42 +05:30
|
|
|
end
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
context 'two existing alerts, one resolved one open' do
|
|
|
|
let!(:resolved_existing_alert) { create(:alert_management_alert, :resolved, project: project, fingerprint: fingerprint_sha) }
|
|
|
|
let!(:alert) { create(:alert_management_alert, project: project, fingerprint: fingerprint_sha) }
|
2020-06-23 00:09:42 +05:30
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
it_behaves_like 'adds an alert management alert event'
|
2020-06-23 00:09:42 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
context 'end time given' do
|
|
|
|
let(:ended_at) { Time.current }
|
|
|
|
|
|
|
|
it_behaves_like 'creates an alert management alert'
|
|
|
|
it_behaves_like 'assigns the alert properties'
|
|
|
|
end
|
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
context 'with a minimal payload' do
|
|
|
|
let(:payload_raw) do
|
|
|
|
{
|
|
|
|
title: 'alert title',
|
|
|
|
start_time: starts_at.rfc3339
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
it_behaves_like 'creates an alert management alert'
|
2020-05-24 23:13:21 +05:30
|
|
|
|
|
|
|
it 'created alert has all data properly assigned' do
|
|
|
|
subject
|
|
|
|
|
|
|
|
expect(last_alert_attributes).to match(
|
|
|
|
project_id: project.id,
|
|
|
|
title: payload_raw.fetch(:title),
|
|
|
|
started_at: Time.zone.parse(payload_raw.fetch(:start_time)),
|
|
|
|
severity: 'critical',
|
|
|
|
status: AlertManagement::Alert::STATUSES[:triggered],
|
|
|
|
events: 1,
|
|
|
|
hosts: [],
|
|
|
|
payload: payload_raw.with_indifferent_access,
|
|
|
|
issue_id: nil,
|
|
|
|
description: nil,
|
|
|
|
monitoring_tool: nil,
|
|
|
|
service: nil,
|
|
|
|
fingerprint: nil,
|
2020-07-28 23:09:34 +05:30
|
|
|
ended_at: nil,
|
|
|
|
prometheus_alert_id: nil,
|
|
|
|
environment_id: nil
|
2020-05-24 23:13:21 +05:30
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
context 'with overlong payload' do
|
|
|
|
let(:payload_raw) do
|
|
|
|
{
|
|
|
|
title: 'a' * Gitlab::Utils::DeepSize::DEFAULT_MAX_SIZE,
|
|
|
|
start_time: starts_at.rfc3339
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'does not process incident issues due to error', http_status: :bad_request
|
|
|
|
it_behaves_like 'does not an create alert management alert'
|
|
|
|
end
|
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
it_behaves_like 'does not process incident issues'
|
|
|
|
|
|
|
|
context 'issue enabled' do
|
|
|
|
let(:issue_enabled) { true }
|
2020-03-13 15:44:24 +05:30
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
it_behaves_like 'processes incident issues'
|
2020-03-13 15:44:24 +05:30
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
context 'with an invalid payload' do
|
|
|
|
before do
|
|
|
|
allow(Gitlab::Alerting::NotificationPayloadParser)
|
|
|
|
.to receive(:call)
|
|
|
|
.and_raise(Gitlab::Alerting::NotificationPayloadParser::BadPayloadError)
|
|
|
|
end
|
|
|
|
|
2020-04-22 19:07:51 +05:30
|
|
|
it_behaves_like 'does not process incident issues due to error', http_status: :bad_request
|
2020-07-28 23:09:34 +05:30
|
|
|
it_behaves_like 'does not an create alert management alert'
|
2020-03-13 15:44:24 +05:30
|
|
|
end
|
2020-06-23 00:09:42 +05:30
|
|
|
|
|
|
|
context 'when alert already exists' do
|
|
|
|
let(:fingerprint_sha) { Digest::SHA1.hexdigest(fingerprint) }
|
2020-07-28 23:09:34 +05:30
|
|
|
let!(:alert) { create(:alert_management_alert, project: project, fingerprint: fingerprint_sha) }
|
2020-06-23 00:09:42 +05:30
|
|
|
|
|
|
|
context 'when existing alert does not have an associated issue' do
|
|
|
|
it_behaves_like 'processes incident issues'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when existing alert has an associated issue' do
|
2020-07-28 23:09:34 +05:30
|
|
|
let!(:alert) { create(:alert_management_alert, :with_issue, project: project, fingerprint: fingerprint_sha) }
|
2020-06-23 00:09:42 +05:30
|
|
|
|
|
|
|
it_behaves_like 'does not process incident issues'
|
|
|
|
end
|
|
|
|
end
|
2020-04-08 14:13:33 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
context 'with emails turned on' do
|
|
|
|
let(:email_enabled) { true }
|
2020-03-13 15:44:24 +05:30
|
|
|
|
2020-10-24 23:57:45 +05:30
|
|
|
it_behaves_like 'Alert Notification Service sends notification email'
|
2020-03-13 15:44:24 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with invalid token' do
|
2020-04-22 19:07:51 +05:30
|
|
|
it_behaves_like 'does not process incident issues due to error', http_status: :unauthorized
|
2020-07-28 23:09:34 +05:30
|
|
|
it_behaves_like 'does not an create alert management alert'
|
2020-03-13 15:44:24 +05:30
|
|
|
end
|
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
context 'with deactivated Alerts Service' do
|
2020-11-24 15:15:51 +05:30
|
|
|
before do
|
|
|
|
alerts_service.update!(active: false)
|
|
|
|
end
|
2020-03-13 15:44:24 +05:30
|
|
|
|
2020-04-22 19:07:51 +05:30
|
|
|
it_behaves_like 'does not process incident issues due to error', http_status: :forbidden
|
2020-07-28 23:09:34 +05:30
|
|
|
it_behaves_like 'does not an create alert management alert'
|
2020-04-08 14:13:33 +05:30
|
|
|
end
|
2020-03-13 15:44:24 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|