2020-04-22 19:07:51 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
RSpec.describe Projects::Prometheus::Alerts::NotifyService do
|
2020-06-23 00:09:42 +05:30
|
|
|
include PrometheusHelpers
|
2021-03-11 19:13:27 +05:30
|
|
|
using RSpec::Parameterized::TableSyntax
|
2020-06-23 00:09:42 +05:30
|
|
|
|
2020-04-22 19:07:51 +05:30
|
|
|
let_it_be(:project, reload: true) { create(:project) }
|
|
|
|
|
2021-02-22 17:27:13 +05:30
|
|
|
let(:service) { described_class.new(project, payload) }
|
2020-04-22 19:07:51 +05:30
|
|
|
let(:token_input) { 'token' }
|
|
|
|
|
|
|
|
let!(:setting) do
|
|
|
|
create(:project_incident_management_setting, project: project, send_email: true, create_issue: true)
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:subject) { service.execute(token_input) }
|
|
|
|
|
|
|
|
context 'with valid payload' do
|
2020-06-23 00:09:42 +05:30
|
|
|
let_it_be(:alert_firing) { create(:prometheus_alert, project: project) }
|
|
|
|
let_it_be(:alert_resolved) { create(:prometheus_alert, project: project) }
|
|
|
|
let_it_be(:cluster) { create(:cluster, :provided_by_user, projects: [project]) }
|
|
|
|
let(:payload_raw) { prometheus_alert_payload(firing: [alert_firing], resolved: [alert_resolved]) }
|
2020-04-22 19:07:51 +05:30
|
|
|
let(:payload) { ActionController::Parameters.new(payload_raw).permit! }
|
|
|
|
let(:payload_alert_firing) { payload_raw['alerts'].first }
|
|
|
|
let(:token) { 'token' }
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
context 'with environment specific clusters' do
|
|
|
|
let(:prd_cluster) do
|
|
|
|
cluster
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:stg_cluster) do
|
|
|
|
create(:cluster, :provided_by_user, projects: [project], enabled: true, environment_scope: 'stg/*')
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:stg_environment) do
|
|
|
|
create(:environment, project: project, name: 'stg/1')
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:alert_firing) do
|
|
|
|
create(:prometheus_alert, project: project, environment: stg_environment)
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
create(:clusters_applications_prometheus, :installed,
|
|
|
|
cluster: prd_cluster, alert_manager_token: token)
|
|
|
|
create(:clusters_applications_prometheus, :installed,
|
|
|
|
cluster: stg_cluster, alert_manager_token: nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'without token' do
|
|
|
|
let(:token_input) { nil }
|
|
|
|
|
2020-10-24 23:57:45 +05:30
|
|
|
it_behaves_like 'Alert Notification Service sends notification email'
|
2020-07-28 23:09:34 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
context 'with token' do
|
2020-10-24 23:57:45 +05:30
|
|
|
it_behaves_like 'Alert Notification Service sends no notifications', http_status: :unauthorized
|
2020-07-28 23:09:34 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-04-22 19:07:51 +05:30
|
|
|
context 'with project specific cluster' do
|
|
|
|
where(:cluster_enabled, :status, :configured_token, :token_input, :result) do
|
|
|
|
true | :installed | token | token | :success
|
|
|
|
true | :installed | nil | nil | :success
|
|
|
|
true | :updated | token | token | :success
|
|
|
|
true | :updating | token | token | :failure
|
|
|
|
true | :installed | token | 'x' | :failure
|
|
|
|
true | :installed | nil | token | :failure
|
|
|
|
true | :installed | token | nil | :failure
|
|
|
|
true | nil | token | token | :failure
|
|
|
|
false | :installed | token | token | :failure
|
|
|
|
end
|
|
|
|
|
|
|
|
with_them do
|
|
|
|
before do
|
2020-06-23 00:09:42 +05:30
|
|
|
cluster.update!(enabled: cluster_enabled)
|
2020-04-22 19:07:51 +05:30
|
|
|
|
|
|
|
if status
|
|
|
|
create(:clusters_applications_prometheus, status,
|
|
|
|
cluster: cluster,
|
|
|
|
alert_manager_token: configured_token)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
case result = params[:result]
|
|
|
|
when :success
|
2020-10-24 23:57:45 +05:30
|
|
|
it_behaves_like 'Alert Notification Service sends notification email'
|
2020-04-22 19:07:51 +05:30
|
|
|
when :failure
|
2020-10-24 23:57:45 +05:30
|
|
|
it_behaves_like 'Alert Notification Service sends no notifications', http_status: :unauthorized
|
2020-04-22 19:07:51 +05:30
|
|
|
else
|
|
|
|
raise "invalid result: #{result.inspect}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'without project specific cluster' do
|
|
|
|
let!(:cluster) { create(:cluster, enabled: true) }
|
|
|
|
|
2020-10-24 23:57:45 +05:30
|
|
|
it_behaves_like 'Alert Notification Service sends no notifications', http_status: :unauthorized
|
2020-04-22 19:07:51 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
context 'with manual prometheus installation' do
|
|
|
|
where(:alerting_setting, :configured_token, :token_input, :result) do
|
|
|
|
true | token | token | :success
|
|
|
|
true | token | 'x' | :failure
|
|
|
|
true | token | nil | :failure
|
|
|
|
false | nil | nil | :success
|
|
|
|
false | nil | token | :failure
|
|
|
|
end
|
|
|
|
|
|
|
|
with_them do
|
|
|
|
let(:alert_manager_token) { token_input }
|
|
|
|
|
|
|
|
before do
|
|
|
|
create(:prometheus_service, project: project)
|
|
|
|
|
|
|
|
if alerting_setting
|
|
|
|
create(:project_alerting_setting,
|
|
|
|
project: project,
|
|
|
|
token: configured_token)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
case result = params[:result]
|
|
|
|
when :success
|
2020-10-24 23:57:45 +05:30
|
|
|
it_behaves_like 'Alert Notification Service sends notification email'
|
2020-04-22 19:07:51 +05:30
|
|
|
when :failure
|
2020-10-24 23:57:45 +05:30
|
|
|
it_behaves_like 'Alert Notification Service sends no notifications', http_status: :unauthorized
|
2020-04-22 19:07:51 +05:30
|
|
|
else
|
|
|
|
raise "invalid result: #{result.inspect}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-02-22 17:27:13 +05:30
|
|
|
context 'with HTTP integration' do
|
|
|
|
where(:active, :token, :result) do
|
2020-06-23 00:09:42 +05:30
|
|
|
:active | :valid | :success
|
|
|
|
:active | :invalid | :failure
|
|
|
|
:active | nil | :failure
|
|
|
|
:inactive | :valid | :failure
|
|
|
|
nil | nil | :failure
|
|
|
|
end
|
|
|
|
|
|
|
|
with_them do
|
2021-02-22 17:27:13 +05:30
|
|
|
let(:valid) { integration.token }
|
2020-06-23 00:09:42 +05:30
|
|
|
let(:invalid) { 'invalid token' }
|
|
|
|
let(:token_input) { public_send(token) if token }
|
2021-02-22 17:27:13 +05:30
|
|
|
let(:integration) { create(:alert_management_http_integration, active, project: project) if active }
|
2020-06-23 00:09:42 +05:30
|
|
|
|
2021-02-22 17:27:13 +05:30
|
|
|
let(:subject) { service.execute(token_input, integration) }
|
2020-06-23 00:09:42 +05:30
|
|
|
|
|
|
|
case result = params[:result]
|
|
|
|
when :success
|
2020-10-24 23:57:45 +05:30
|
|
|
it_behaves_like 'Alert Notification Service sends notification email'
|
2020-06-23 00:09:42 +05:30
|
|
|
when :failure
|
2020-10-24 23:57:45 +05:30
|
|
|
it_behaves_like 'Alert Notification Service sends no notifications', http_status: :unauthorized
|
2020-06-23 00:09:42 +05:30
|
|
|
else
|
|
|
|
raise "invalid result: #{result.inspect}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-04-22 19:07:51 +05:30
|
|
|
context 'alert emails' do
|
|
|
|
before do
|
|
|
|
create(:prometheus_service, project: project)
|
|
|
|
create(:project_alerting_setting, project: project, token: token)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when incident_management_setting does not exist' do
|
|
|
|
let!(:setting) { nil }
|
|
|
|
|
|
|
|
it 'does not send notification email', :sidekiq_might_not_need_inline do
|
|
|
|
expect_any_instance_of(NotificationService)
|
|
|
|
.not_to receive(:async)
|
|
|
|
|
|
|
|
expect(subject).to be_success
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when incident_management_setting.send_email is true' do
|
2020-10-24 23:57:45 +05:30
|
|
|
it_behaves_like 'Alert Notification Service sends notification email'
|
2020-04-22 19:07:51 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
context 'incident_management_setting.send_email is false' do
|
|
|
|
let!(:setting) do
|
|
|
|
create(:project_incident_management_setting, send_email: false, project: project)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not send notification' do
|
|
|
|
expect(NotificationService).not_to receive(:new)
|
|
|
|
|
|
|
|
expect(subject).to be_success
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
context 'process Alert Management alerts' do
|
|
|
|
let(:process_service) { instance_double(AlertManagement::ProcessPrometheusAlertService) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
create(:prometheus_service, project: project)
|
|
|
|
create(:project_alerting_setting, project: project, token: token)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with multiple firing alerts and resolving alerts' do
|
|
|
|
let(:payload_raw) do
|
2020-06-23 00:09:42 +05:30
|
|
|
prometheus_alert_payload(firing: [alert_firing, alert_firing], resolved: [alert_resolved])
|
2020-05-24 23:13:21 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'processes Prometheus alerts' do
|
|
|
|
expect(AlertManagement::ProcessPrometheusAlertService)
|
|
|
|
.to receive(:new)
|
2021-02-22 17:27:13 +05:30
|
|
|
.with(project, kind_of(Hash))
|
2020-05-24 23:13:21 +05:30
|
|
|
.exactly(3).times
|
|
|
|
.and_return(process_service)
|
|
|
|
expect(process_service).to receive(:execute).exactly(3).times
|
|
|
|
|
|
|
|
subject
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-04-22 19:07:51 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
context 'with invalid payload' do
|
2020-06-23 00:09:42 +05:30
|
|
|
context 'when payload is not processable' do
|
2020-04-22 19:07:51 +05:30
|
|
|
let(:payload) { {} }
|
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
before do
|
|
|
|
allow(described_class).to receive(:processable?).with(payload)
|
|
|
|
.and_return(false)
|
|
|
|
end
|
2020-04-22 19:07:51 +05:30
|
|
|
|
2020-10-24 23:57:45 +05:30
|
|
|
it_behaves_like 'Alert Notification Service sends no notifications', http_status: :unprocessable_entity
|
2020-04-22 19:07:51 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the payload is too big' do
|
|
|
|
let(:payload) { { 'the-payload-is-too-big' => true } }
|
|
|
|
let(:deep_size_object) { instance_double(Gitlab::Utils::DeepSize, valid?: false) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
allow(Gitlab::Utils::DeepSize).to receive(:new).and_return(deep_size_object)
|
|
|
|
end
|
|
|
|
|
2020-10-24 23:57:45 +05:30
|
|
|
it_behaves_like 'Alert Notification Service sends no notifications', http_status: :bad_request
|
2020-04-22 19:07:51 +05:30
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
it 'does not process Prometheus alerts' do
|
|
|
|
expect(AlertManagement::ProcessPrometheusAlertService)
|
|
|
|
.not_to receive(:new)
|
|
|
|
|
|
|
|
subject
|
|
|
|
end
|
2020-04-22 19:07:51 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
describe '.processable?' do
|
|
|
|
let(:valid_payload) { prometheus_alert_payload }
|
|
|
|
|
|
|
|
subject { described_class.processable?(payload) }
|
|
|
|
|
|
|
|
context 'with valid payload' do
|
|
|
|
let(:payload) { valid_payload }
|
|
|
|
|
|
|
|
it { is_expected.to eq(true) }
|
|
|
|
|
|
|
|
context 'containing unrelated keys' do
|
|
|
|
let(:payload) { valid_payload.merge('unrelated' => 'key') }
|
2020-04-22 19:07:51 +05:30
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
it { is_expected.to eq(true) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with invalid payload' do
|
|
|
|
where(:missing_key) do
|
|
|
|
described_class::REQUIRED_PAYLOAD_KEYS.to_a
|
|
|
|
end
|
|
|
|
|
|
|
|
with_them do
|
|
|
|
let(:payload) { valid_payload.except(missing_key) }
|
|
|
|
|
|
|
|
it { is_expected.to eq(false) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with unsupported version' do
|
|
|
|
let(:payload) { valid_payload.merge('version' => '5') }
|
|
|
|
|
|
|
|
it { is_expected.to eq(false) }
|
|
|
|
end
|
2020-04-22 19:07:51 +05:30
|
|
|
end
|
|
|
|
end
|