2020-03-13 15:44:24 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
2023-05-27 22:25:52 +05:30
|
|
|
RSpec.describe Projects::Alerting::NotifyService, feature_category: :projects do
|
2021-06-08 01:23:25 +05:30
|
|
|
let_it_be_with_reload(:project) { create(:project) }
|
|
|
|
|
|
|
|
let(:payload) { ActionController::Parameters.new(payload_raw).permit! }
|
|
|
|
let(:payload_raw) { {} }
|
|
|
|
|
|
|
|
let(:service) { described_class.new(project, payload) }
|
2020-03-13 15:44:24 +05:30
|
|
|
|
|
|
|
before do
|
2021-06-08 01:23:25 +05:30
|
|
|
stub_licensed_features(oncall_schedules: false, generic_alert_fingerprinting: false)
|
2020-03-13 15:44:24 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
describe '#execute' do
|
2021-06-08 01:23:25 +05:30
|
|
|
include_context 'incident management settings enabled'
|
2020-10-24 23:57:45 +05:30
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
subject { service.execute(token, integration) }
|
2020-03-13 15:44:24 +05:30
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
context 'with HTTP integration' do
|
|
|
|
let_it_be_with_reload(:integration) { create(:alert_management_http_integration, project: project) }
|
2020-03-13 15:44:24 +05:30
|
|
|
|
|
|
|
context 'with valid token' do
|
2021-01-29 00:20:46 +05:30
|
|
|
let(:token) { integration.token }
|
2020-04-08 14:13:33 +05:30
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
context 'with valid payload' do
|
2021-06-08 01:23:25 +05:30
|
|
|
let_it_be(:environment) { create(:environment, project: project) }
|
|
|
|
let_it_be(:fingerprint) { 'testing' }
|
|
|
|
let_it_be(:source) { 'GitLab RSpec' }
|
|
|
|
let_it_be(:starts_at) { Time.current.change(usec: 0) }
|
|
|
|
|
|
|
|
let(:ended_at) { nil }
|
|
|
|
let(:domain) { 'operations' }
|
|
|
|
let(:payload_raw) do
|
|
|
|
{
|
|
|
|
title: 'alert title',
|
|
|
|
start_time: starts_at.rfc3339,
|
|
|
|
end_time: ended_at&.rfc3339,
|
|
|
|
severity: 'low',
|
|
|
|
monitoring_tool: source,
|
|
|
|
service: 'GitLab Test Suite',
|
|
|
|
description: 'Very detailed description',
|
|
|
|
hosts: ['1.1.1.1', '2.2.2.2'],
|
|
|
|
fingerprint: fingerprint,
|
|
|
|
gitlab_environment_name: environment.name
|
|
|
|
}.with_indifferent_access
|
2020-07-28 23:09:34 +05:30
|
|
|
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
|
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
it_behaves_like 'processes new firing alert'
|
|
|
|
it_behaves_like 'properly assigns the alert properties'
|
2022-08-27 11:52:29 +05:30
|
|
|
include_examples 'handles race condition in alert creation'
|
2020-06-23 00:09:42 +05:30
|
|
|
|
2021-03-11 19:13:27 +05:30
|
|
|
it 'passes the integration to alert processing' do
|
|
|
|
expect(Gitlab::AlertManagement::Payload)
|
|
|
|
.to receive(:parse)
|
|
|
|
.with(project, payload.to_h, integration: integration)
|
|
|
|
.and_call_original
|
|
|
|
|
|
|
|
subject
|
|
|
|
end
|
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
context 'with partial payload' do
|
|
|
|
let_it_be(:source) { integration.name }
|
|
|
|
let_it_be(:payload_raw) do
|
2020-05-24 23:13:21 +05:30
|
|
|
{
|
|
|
|
title: 'alert title',
|
|
|
|
start_time: starts_at.rfc3339
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
include_examples 'processes never-before-seen alert'
|
2020-05-24 23:13:21 +05:30
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
it 'assigns the alert properties' do
|
2020-05-24 23:13:21 +05:30
|
|
|
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',
|
2021-01-03 14:25:43 +05:30
|
|
|
status: AlertManagement::Alert.status_value(:triggered),
|
2020-05-24 23:13:21 +05:30
|
|
|
events: 1,
|
|
|
|
hosts: [],
|
2021-02-22 17:27:13 +05:30
|
|
|
domain: 'operations',
|
2020-05-24 23:13:21 +05:30
|
|
|
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
|
2021-01-03 14:25:43 +05:30
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
context 'with existing alert with matching payload' do
|
|
|
|
let_it_be(:fingerprint) { payload_raw.except(:start_time).stringify_keys }
|
|
|
|
let_it_be(:gitlab_fingerprint) { Gitlab::AlertManagement::Fingerprint.generate(fingerprint) }
|
|
|
|
let_it_be(:alert) { create(:alert_management_alert, project: project, fingerprint: gitlab_fingerprint) }
|
|
|
|
|
|
|
|
include_examples 'processes never-before-seen alert'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with resolving payload' do
|
|
|
|
let(:ended_at) { Time.current.change(usec: 0) }
|
|
|
|
|
|
|
|
it_behaves_like 'processes recovery alert'
|
2020-05-24 23:13:21 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
context 'with overlong payload' do
|
2022-08-27 11:52:29 +05:30
|
|
|
let(:payload_raw) { { 'the-payload-is-too-big' => true } }
|
2021-01-03 14:25:43 +05:30
|
|
|
|
|
|
|
before do
|
2022-08-27 11:52:29 +05:30
|
|
|
stub_const('::Gitlab::Utils::DeepSize::DEFAULT_MAX_DEPTH', 0)
|
2020-11-24 15:15:51 +05:30
|
|
|
end
|
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
it_behaves_like 'alerts service responds with an error and takes no actions', :bad_request
|
2020-11-24 15:15:51 +05:30
|
|
|
end
|
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
context 'with inactive integration' do
|
|
|
|
before do
|
|
|
|
integration.update!(active: false)
|
2020-06-23 00:09:42 +05:30
|
|
|
end
|
2020-04-08 14:13:33 +05:30
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
it_behaves_like 'alerts service responds with an error and takes no actions', :forbidden
|
2020-03-13 15:44:24 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with invalid token' do
|
2021-06-08 01:23:25 +05:30
|
|
|
let(:token) { 'invalid-token' }
|
2021-01-29 00:20:46 +05:30
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
it_behaves_like 'alerts service responds with an error and takes no actions', :unauthorized
|
2021-01-29 00:20:46 +05:30
|
|
|
end
|
2021-06-08 01:23:25 +05:30
|
|
|
end
|
2020-03-13 15:44:24 +05:30
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
context 'without HTTP integration' do
|
|
|
|
let(:integration) { nil }
|
|
|
|
let(:token) { nil }
|
2020-03-13 15:44:24 +05:30
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
it_behaves_like 'alerts service responds with an error and takes no actions', :forbidden
|
2020-03-13 15:44:24 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|