debian-mirror-gitlab/spec/services/projects/alerting/notify_service_spec.rb

214 lines
6.8 KiB
Ruby
Raw Normal View History

2020-03-13 15:44:24 +05:30
# frozen_string_literal: true
require 'spec_helper'
describe Projects::Alerting::NotifyService do
let_it_be(:project, reload: true) { create(:project) }
before do
# We use `let_it_be(:project)` so we make sure to clear caches
project.clear_memoization(:licensed_feature_available)
end
shared_examples 'processes incident issues' do |amount|
let(:create_incident_service) { spy }
2020-05-24 23:13:21 +05:30
let(:new_alert) { instance_double(AlertManagement::Alert, id: 503, persisted?: true) }
2020-03-13 15:44:24 +05:30
it 'processes issues' do
2020-05-24 23:13:21 +05:30
expect(AlertManagement::Alert)
.to receive(:create)
.and_return(new_alert)
2020-03-13 15:44:24 +05:30
expect(IncidentManagement::ProcessAlertWorker)
.to receive(:perform_async)
2020-05-24 23:13:21 +05:30
.with(project.id, kind_of(Hash), new_alert.id)
2020-03-13 15:44:24 +05:30
.exactly(amount).times
Sidekiq::Testing.inline! do
2020-04-22 19:07:51 +05:30
expect(subject).to be_success
2020-03-13 15:44:24 +05:30
end
end
end
2020-04-08 14:13:33 +05:30
shared_examples 'sends notification email' do
let(:notification_service) { spy }
it 'sends a notification for firing alerts only' do
expect(NotificationService)
.to receive(:new)
.and_return(notification_service)
expect(notification_service)
.to receive_message_chain(:async, :prometheus_alerts_fired)
2020-04-22 19:07:51 +05:30
expect(subject).to be_success
2020-04-08 14:13:33 +05:30
end
end
shared_examples 'does not process incident issues' do
it 'does not process issues' do
expect(IncidentManagement::ProcessAlertWorker)
.not_to receive(:perform_async)
2020-04-22 19:07:51 +05:30
expect(subject).to be_success
2020-04-08 14:13:33 +05:30
end
end
shared_examples 'does not process incident issues due to error' do |http_status:|
2020-03-13 15:44:24 +05:30
it 'does not process issues' do
expect(IncidentManagement::ProcessAlertWorker)
.not_to receive(:perform_async)
2020-04-22 19:07:51 +05:30
expect(subject).to be_error
2020-03-13 15:44:24 +05:30
expect(subject.http_status).to eq(http_status)
end
end
2020-05-24 23:13:21 +05:30
shared_examples 'NotifyService does not create alert' do
it 'does not create alert' do
expect { subject }.not_to change(AlertManagement::Alert, :count)
end
end
2020-03-13 15:44:24 +05:30
describe '#execute' do
let(:token) { 'invalid-token' }
2020-05-24 23:13:21 +05:30
let(:starts_at) { Time.current.change(usec: 0) }
2020-03-13 15:44:24 +05:30
let(:service) { described_class.new(project, nil, payload) }
let(:payload_raw) do
{
2020-05-24 23:13:21 +05:30
title: 'alert title',
start_time: starts_at.rfc3339,
severity: 'low',
monitoring_tool: 'GitLab RSpec',
service: 'GitLab Test Suite',
description: 'Very detailed description',
hosts: ['1.1.1.1', '2.2.2.2']
}.with_indifferent_access
2020-03-13 15:44:24 +05:30
end
let(:payload) { ActionController::Parameters.new(payload_raw).permit! }
subject { service.execute(token) }
context 'with activated Alerts Service' do
let!(:alerts_service) { create(:alerts_service, project: project) }
context 'with valid token' do
let(:token) { alerts_service.token }
2020-04-08 14:13:33 +05:30
let(:incident_management_setting) { double(send_email?: email_enabled, create_issue?: issue_enabled) }
let(:email_enabled) { false }
let(:issue_enabled) { false }
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
let(:last_alert_attributes) do
AlertManagement::Alert.last.attributes
.except('id', 'iid', 'created_at', 'updated_at')
.with_indifferent_access
end
it 'creates AlertManagement::Alert' do
expect { subject }.to change(AlertManagement::Alert, :count).by(1)
end
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: 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: nil,
ended_at: nil
)
end
context 'with a minimal payload' do
let(:payload_raw) do
{
title: 'alert title',
start_time: starts_at.rfc3339
}
end
it 'creates AlertManagement::Alert' do
expect { subject }.to change(AlertManagement::Alert, :count).by(1)
end
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,
ended_at: nil
)
end
end
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
it_behaves_like 'processes incident issues', 1
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-05-24 23:13:21 +05:30
it_behaves_like 'NotifyService does not create alert'
2020-03-13 15:44:24 +05:30
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-04-08 14:13:33 +05:30
it_behaves_like '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-05-24 23:13:21 +05:30
it_behaves_like 'NotifyService does not create alert'
2020-03-13 15:44:24 +05:30
end
2020-04-08 14:13:33 +05:30
context 'with deactivated Alerts Service' do
let!(:alerts_service) { create(:alerts_service, :inactive, project: project) }
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-05-24 23:13:21 +05:30
it_behaves_like 'NotifyService does not create alert'
2020-04-08 14:13:33 +05:30
end
2020-03-13 15:44:24 +05:30
end
end
end