Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2021-04-29 15:09:58 +0300
committerGitLab Bot <gitlab-bot@gitlab.com>2021-04-29 15:09:58 +0300
commitdb36dea03b0e56ed242eb290c51be88ca4c61a65 (patch)
tree8352090b6b45cbd012be01a406a8f3d5b8d2b227 /spec/services
parent38e4bfea582e8c755dd21613bf21658b1771449b (diff)
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec/services')
-rw-r--r--spec/services/alert_management/process_prometheus_alert_service_spec.rb236
-rw-r--r--spec/services/projects/alerting/notify_service_spec.rb258
-rw-r--r--spec/services/projects/prometheus/alerts/notify_service_spec.rb83
3 files changed, 133 insertions, 444 deletions
diff --git a/spec/services/alert_management/process_prometheus_alert_service_spec.rb b/spec/services/alert_management/process_prometheus_alert_service_spec.rb
index 9bd71ea6f64..84c64508a8d 100644
--- a/spec/services/alert_management/process_prometheus_alert_service_spec.rb
+++ b/spec/services/alert_management/process_prometheus_alert_service_spec.rb
@@ -5,38 +5,27 @@ require 'spec_helper'
RSpec.describe AlertManagement::ProcessPrometheusAlertService do
let_it_be(:project, reload: true) { create(:project, :repository) }
- before do
- allow(ProjectServiceWorker).to receive(:perform_async)
- end
+ let(:service) { described_class.new(project, payload) }
describe '#execute' do
- let(:service) { described_class.new(project, payload) }
- let(:source) { 'Prometheus' }
- let(:auto_close_incident) { true }
- let(:create_issue) { true }
- let(:send_email) { true }
- let(:incident_management_setting) do
- double(
- auto_close_incident?: auto_close_incident,
- create_issue?: create_issue,
- send_email?: send_email
- )
- end
+ include_context 'incident management settings enabled'
+
+ subject(:execute) { service.execute }
before do
- allow(service)
- .to receive(:incident_management_setting)
- .and_return(incident_management_setting)
+ stub_licensed_features(oncall_schedules: false, generic_alert_fingerprinting: false)
end
- subject(:execute) { service.execute }
-
context 'when alert payload is valid' do
- let(:parsed_payload) { Gitlab::AlertManagement::Payload.parse(project, payload, monitoring_tool: source) }
- let(:fingerprint) { parsed_payload.gitlab_fingerprint }
+ let_it_be(:starts_at) { '2020-04-27T10:10:22.265949279Z' }
+ let_it_be(:title) { 'Alert title' }
+ let_it_be(:fingerprint) { [starts_at, title, 'vector(1)'].join('/') }
+ let_it_be(:source) { 'Prometheus' }
+
+ let(:prometheus_status) { 'firing' }
let(:payload) do
{
- 'status' => status,
+ 'status' => prometheus_status,
'labels' => {
'alertname' => 'GitalyFileServerDown',
'channel' => 'gitaly',
@@ -46,196 +35,32 @@ RSpec.describe AlertManagement::ProcessPrometheusAlertService do
'annotations' => {
'description' => 'Alert description',
'runbook' => 'troubleshooting/gitaly-down.md',
- 'title' => 'Alert title'
+ 'title' => title
},
- 'startsAt' => '2020-04-27T10:10:22.265949279Z',
+ 'startsAt' => starts_at,
'endsAt' => '2020-04-27T10:20:22.265949279Z',
- 'generatorURL' => 'http://8d467bd4607a:9090/graph?g0.expr=vector%281%29&g0.tab=1',
- 'fingerprint' => 'b6ac4d42057c43c1'
+ 'generatorURL' => 'http://8d467bd4607a:9090/graph?g0.expr=vector%281%29&g0.tab=1'
}
end
- let(:status) { 'firing' }
-
- context 'when Prometheus alert status is firing' do
- context 'when alert with the same fingerprint already exists' do
- let!(:alert) { create(:alert_management_alert, project: project, fingerprint: fingerprint) }
-
- it_behaves_like 'adds an alert management alert event'
- it_behaves_like 'processes incident issues'
- it_behaves_like 'Alert Notification Service sends notification email'
-
- context 'existing alert is resolved' do
- let!(:alert) { create(:alert_management_alert, :resolved, project: project, fingerprint: fingerprint) }
-
- it_behaves_like 'creates an alert management alert'
- it_behaves_like 'Alert Notification Service sends notification email'
- end
-
- context 'existing alert is ignored' do
- let!(:alert) { create(:alert_management_alert, :ignored, project: project, fingerprint: fingerprint) }
-
- it_behaves_like 'adds an alert management alert event'
- it_behaves_like 'Alert Notification Service sends no notifications'
- end
-
- context 'existing alert is acknowledged' do
- let!(:alert) { create(:alert_management_alert, :acknowledged, project: project, fingerprint: fingerprint) }
-
- it_behaves_like 'adds an alert management alert event'
- it_behaves_like 'Alert Notification Service sends no notifications'
- end
-
- context 'two existing alerts, one resolved one open' do
- let!(:resolved_alert) { create(:alert_management_alert, :resolved, project: project, fingerprint: fingerprint) }
- let!(:alert) { create(:alert_management_alert, project: project, fingerprint: fingerprint) }
-
- it_behaves_like 'adds an alert management alert event'
- it_behaves_like 'Alert Notification Service sends notification email'
- end
-
- context 'when auto-creation of issues is disabled' do
- let(:create_issue) { false }
-
- it_behaves_like 'does not process incident issues'
- end
-
- context 'when emails are disabled' do
- let(:send_email) { false }
-
- it_behaves_like 'Alert Notification Service sends no notifications'
- end
- end
-
- context 'when alert does not exist' do
- context 'when alert can be created' do
- it_behaves_like 'creates an alert management alert'
- it_behaves_like 'Alert Notification Service sends notification email'
- it_behaves_like 'processes incident issues'
-
- it_behaves_like 'creates single system note based on the source of the alert'
-
- context 'when auto-alert creation is disabled' do
- let(:create_issue) { false }
-
- it_behaves_like 'does not process incident issues'
- end
-
- context 'when emails are disabled' do
- let(:send_email) { false }
-
- it_behaves_like 'Alert Notification Service sends no notifications'
- end
- end
-
- context 'when alert cannot be created' do
- let(:errors) { double(messages: { hosts: ['hosts array is over 255 chars'] })}
-
- before do
- allow(service).to receive(:alert).and_call_original
- allow(service).to receive_message_chain(:alert, :save).and_return(false)
- allow(service).to receive_message_chain(:alert, :errors).and_return(errors)
- end
-
- it_behaves_like 'Alert Notification Service sends no notifications', http_status: :bad_request
- it_behaves_like 'does not process incident issues due to error', http_status: :bad_request
-
- it 'writes a warning to the log' do
- expect(Gitlab::AppLogger).to receive(:warn).with(
- message: 'Unable to create AlertManagement::Alert from Prometheus',
- project_id: project.id,
- alert_errors: { hosts: ['hosts array is over 255 chars'] }
- )
-
- execute
- end
- end
-
- it { is_expected.to be_success }
- end
- end
-
- context 'when Prometheus alert status is resolved' do
- let(:status) { 'resolved' }
- let!(:alert) { create(:alert_management_alert, project: project, fingerprint: fingerprint, monitoring_tool: source) }
-
- context 'when auto_resolve_incident set to true' do
- context 'when status can be changed' do
- it_behaves_like 'Alert Notification Service sends notification email'
- it_behaves_like 'does not process incident issues'
-
- it 'resolves an existing alert without error' do
- expect(Gitlab::AppLogger).not_to receive(:warn)
- expect { execute }.to change { alert.reload.resolved? }.to(true)
- end
-
- it_behaves_like 'creates status-change system note for an auto-resolved alert'
-
- context 'existing issue' do
- let!(:alert) { create(:alert_management_alert, :with_issue, project: project, fingerprint: fingerprint) }
-
- it 'closes the issue' do
- issue = alert.issue
-
- expect { execute }
- .to change { issue.reload.state }
- .from('opened')
- .to('closed')
- end
-
- it 'creates a resource state event' do
- expect { execute }.to change(ResourceStateEvent, :count).by(1)
- end
- end
- end
-
- context 'when status change did not succeed' do
- before do
- allow(AlertManagement::Alert).to receive(:for_fingerprint).and_return([alert])
- allow(alert).to receive(:resolve).and_return(false)
- end
-
- it 'writes a warning to the log' do
- expect(Gitlab::AppLogger).to receive(:warn).with(
- message: 'Unable to update AlertManagement::Alert status to resolved',
- project_id: project.id,
- alert_id: alert.id
- )
-
- execute
- end
-
- it_behaves_like 'Alert Notification Service sends notification email'
- end
-
- it { is_expected.to be_success }
- end
+ it_behaves_like 'processes new firing alert'
- context 'when auto_resolve_incident set to false' do
- let(:auto_close_incident) { false }
+ context 'with resolving payload' do
+ let(:prometheus_status) { 'resolved' }
- it 'does not resolve an existing alert' do
- expect { execute }.not_to change { alert.reload.resolved? }
- end
-
- it_behaves_like 'creates single system note based on the source of the alert'
- end
-
- context 'when emails are disabled' do
- let(:send_email) { false }
-
- it_behaves_like 'Alert Notification Service sends no notifications'
- end
+ it_behaves_like 'processes prometheus recovery alert'
end
context 'environment given' do
let(:environment) { create(:environment, project: project) }
+ let(:alert) { project.alert_management_alerts.last }
- it 'sets the environment' do
+ before do
payload['labels']['gitlab_environment_name'] = environment.name
- execute
+ end
- alert = project.alert_management_alerts.last
+ it 'sets the environment' do
+ execute
expect(alert.environment).to eq(environment)
end
@@ -243,12 +68,14 @@ RSpec.describe AlertManagement::ProcessPrometheusAlertService do
context 'prometheus alert given' do
let(:prometheus_alert) { create(:prometheus_alert, project: project) }
+ let(:alert) { project.alert_management_alerts.last }
- it 'sets the prometheus alert and environment' do
+ before do
payload['labels']['gitlab_alert_id'] = prometheus_alert.prometheus_metric_id
- execute
+ end
- alert = project.alert_management_alerts.last
+ it 'sets the prometheus alert and environment' do
+ execute
expect(alert.prometheus_alert).to eq(prometheus_alert)
expect(alert.environment).to eq(prometheus_alert.environment)
@@ -259,10 +86,7 @@ RSpec.describe AlertManagement::ProcessPrometheusAlertService do
context 'when alert payload is invalid' do
let(:payload) { {} }
- it 'responds with bad_request' do
- expect(execute).to be_error
- expect(execute.http_status).to eq(:bad_request)
- end
+ it_behaves_like 'alerts service responds with an error and takes no actions', :bad_request
end
end
end
diff --git a/spec/services/projects/alerting/notify_service_spec.rb b/spec/services/projects/alerting/notify_service_spec.rb
index c272ce13132..feae8f3967c 100644
--- a/spec/services/projects/alerting/notify_service_spec.rb
+++ b/spec/services/projects/alerting/notify_service_spec.rb
@@ -3,77 +3,49 @@
require 'spec_helper'
RSpec.describe Projects::Alerting::NotifyService do
- let_it_be_with_reload(:project) { create(:project, :repository) }
+ 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) }
before do
- allow(ProjectServiceWorker).to receive(:perform_async)
+ stub_licensed_features(oncall_schedules: false, generic_alert_fingerprinting: false)
end
describe '#execute' do
- let(:token) { 'invalid-token' }
- let(:starts_at) { Time.current.change(usec: 0) }
- let(:fingerprint) { 'testing' }
- let(:service) { described_class.new(project, payload) }
- let_it_be(:environment) { create(:environment, project: project) }
- let(:environment) { create(:environment, project: project) }
- let(:ended_at) { nil }
- let(:payload_raw) do
- {
- title: 'alert title',
- start_time: starts_at.rfc3339,
- end_time: ended_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'],
- fingerprint: fingerprint,
- gitlab_environment_name: environment.name
- }.with_indifferent_access
- end
+ include_context 'incident management settings enabled'
- let(:payload) { ActionController::Parameters.new(payload_raw).permit! }
+ subject { service.execute(token, integration) }
- subject { service.execute(token, nil) }
+ context 'with HTTP integration' do
+ let_it_be_with_reload(:integration) { create(:alert_management_http_integration, project: project) }
- shared_examples 'notifications are handled correctly' do
context 'with valid token' do
let(:token) { integration.token }
- let(:incident_management_setting) { double(send_email?: email_enabled, create_issue?: issue_enabled, auto_close_incident?: auto_close_enabled) }
- let(:email_enabled) { false }
- let(:issue_enabled) { false }
- let(:auto_close_enabled) { false }
-
- before do
- allow(service)
- .to receive(:incident_management_setting)
- .and_return(incident_management_setting)
- end
context 'with valid payload' do
- 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.status_value(:triggered),
- events: 1,
- domain: 'operations',
- 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),
- environment_id: environment.id,
- ended_at: nil,
- prometheus_alert_id: nil
- )
- end
+ 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
end
let(:last_alert_attributes) do
@@ -82,8 +54,8 @@ RSpec.describe Projects::Alerting::NotifyService do
.with_indifferent_access
end
- it_behaves_like 'creates an alert management alert'
- it_behaves_like 'assigns the alert properties'
+ it_behaves_like 'processes new firing alert'
+ it_behaves_like 'properly assigns the alert properties'
it 'passes the integration to alert processing' do
expect(Gitlab::AlertManagement::Payload)
@@ -94,101 +66,18 @@ RSpec.describe Projects::Alerting::NotifyService do
subject
end
- it 'creates a system note corresponding to alert creation' do
- expect { subject }.to change(Note, :count).by(1)
- expect(Note.last.note).to include(payload_raw.fetch(:monitoring_tool))
- end
-
- context 'existing alert with same fingerprint' do
- let(:fingerprint_sha) { Digest::SHA1.hexdigest(fingerprint) }
- let!(:alert) { create(:alert_management_alert, project: project, fingerprint: fingerprint_sha) }
-
- it_behaves_like 'adds an alert management alert event'
-
- 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'
- it_behaves_like 'creates single system note based on the source of the 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
-
- it_behaves_like 'creates status-change system note for an auto-resolved alert'
-
- context 'related issue exists' do
- let(:alert) { create(:alert_management_alert, :with_issue, project: project, fingerprint: fingerprint_sha) }
- let(:issue) { alert.issue }
-
- it { expect { subject }.to change { issue.reload.state }.from('opened').to('closed') }
- it { expect { subject }.to change(ResourceStateEvent, :count).by(1) }
- end
-
- context 'with issue enabled' do
- let(:issue_enabled) { true }
-
- it_behaves_like 'does not process incident issues'
- end
- end
- end
-
- context 'existing alert is resolved' do
- let!(:alert) { create(:alert_management_alert, :resolved, project: project, fingerprint: fingerprint_sha) }
-
- it_behaves_like 'creates an alert management alert'
- it_behaves_like 'assigns the alert properties'
- end
-
- 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'
- end
-
- 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) }
-
- it_behaves_like 'adds an alert management alert event'
- end
- end
-
- 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
-
- context 'with a minimal payload' do
- let(:payload_raw) do
+ context 'with partial payload' do
+ let_it_be(:source) { integration.name }
+ let_it_be(:payload_raw) do
{
title: 'alert title',
start_time: starts_at.rfc3339
}
end
- it_behaves_like 'creates an alert management alert'
+ include_examples 'processes never-before-seen alert'
- it 'created alert has all data properly assigned' do
+ it 'assigns the alert properties' do
subject
expect(last_alert_attributes).to match(
@@ -212,7 +101,19 @@ RSpec.describe Projects::Alerting::NotifyService do
)
end
- it_behaves_like 'creates single system note based on the source of the alert'
+ 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'
end
end
@@ -223,63 +124,30 @@ RSpec.describe Projects::Alerting::NotifyService do
allow(Gitlab::Utils::DeepSize).to receive(:new).and_return(deep_size_object)
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'
+ it_behaves_like 'alerts service responds with an error and takes no actions', :bad_request
end
- it_behaves_like 'does not process incident issues'
-
- context 'issue enabled' do
- let(:issue_enabled) { true }
-
- it_behaves_like 'processes incident issues'
-
- context 'when alert already exists' do
- let(:fingerprint_sha) { Digest::SHA1.hexdigest(fingerprint) }
- let!(:alert) { create(:alert_management_alert, project: project, fingerprint: fingerprint_sha) }
-
- 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
- let!(:alert) { create(:alert_management_alert, :with_issue, project: project, fingerprint: fingerprint_sha) }
-
- it_behaves_like 'does not process incident issues'
- end
+ context 'with inactive integration' do
+ before do
+ integration.update!(active: false)
end
- end
- context 'with emails turned on' do
- let(:email_enabled) { true }
-
- it_behaves_like 'Alert Notification Service sends notification email'
+ it_behaves_like 'alerts service responds with an error and takes no actions', :forbidden
end
end
context 'with invalid token' do
- it_behaves_like 'does not process incident issues due to error', http_status: :unauthorized
- it_behaves_like 'does not an create alert management alert'
- end
- end
-
- context 'with an HTTP Integration' do
- let_it_be_with_reload(:integration) { create(:alert_management_http_integration, project: project) }
+ let(:token) { 'invalid-token' }
- subject { service.execute(token, integration) }
-
- it_behaves_like 'notifications are handled correctly' do
- let(:source) { integration.name }
+ it_behaves_like 'alerts service responds with an error and takes no actions', :unauthorized
end
+ end
- context 'with deactivated HTTP Integration' do
- before do
- integration.update!(active: false)
- end
+ context 'without HTTP integration' do
+ let(:integration) { nil }
+ let(:token) { nil }
- it_behaves_like 'does not process incident issues due to error', http_status: :forbidden
- it_behaves_like 'does not an create alert management alert'
- end
+ it_behaves_like 'alerts service responds with an error and takes no actions', :forbidden
end
end
end
diff --git a/spec/services/projects/prometheus/alerts/notify_service_spec.rb b/spec/services/projects/prometheus/alerts/notify_service_spec.rb
index e196220eabe..e70a23de2a0 100644
--- a/spec/services/projects/prometheus/alerts/notify_service_spec.rb
+++ b/spec/services/projects/prometheus/alerts/notify_service_spec.rb
@@ -6,25 +6,26 @@ RSpec.describe Projects::Prometheus::Alerts::NotifyService do
include PrometheusHelpers
using RSpec::Parameterized::TableSyntax
- let_it_be(:project, reload: true) { create(:project) }
+ let_it_be_with_reload(:project) { create(:project) }
+ let_it_be_with_reload(:setting) do
+ create(:project_incident_management_setting, project: project, send_email: true, create_issue: true)
+ end
let(:service) { described_class.new(project, payload) }
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) }
+ subject { service.execute(token_input) }
context 'with valid payload' do
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_it_be(:cluster, reload: true) { create(:cluster, :provided_by_user, projects: [project]) }
+
let(:payload_raw) { prometheus_alert_payload(firing: [alert_firing], resolved: [alert_resolved]) }
let(:payload) { ActionController::Parameters.new(payload_raw).permit! }
let(:payload_alert_firing) { payload_raw['alerts'].first }
let(:token) { 'token' }
+ let(:source) { 'Prometheus' }
context 'with environment specific clusters' do
let(:prd_cluster) do
@@ -53,11 +54,11 @@ RSpec.describe Projects::Prometheus::Alerts::NotifyService do
context 'without token' do
let(:token_input) { nil }
- it_behaves_like 'Alert Notification Service sends notification email'
+ include_examples 'processes one firing and one resolved prometheus alerts'
end
context 'with token' do
- it_behaves_like 'Alert Notification Service sends no notifications', http_status: :unauthorized
+ it_behaves_like 'alerts service responds with an error and takes no actions', :unauthorized
end
end
@@ -87,9 +88,9 @@ RSpec.describe Projects::Prometheus::Alerts::NotifyService do
case result = params[:result]
when :success
- it_behaves_like 'Alert Notification Service sends notification email'
+ include_examples 'processes one firing and one resolved prometheus alerts'
when :failure
- it_behaves_like 'Alert Notification Service sends no notifications', http_status: :unauthorized
+ it_behaves_like 'alerts service responds with an error and takes no actions', :unauthorized
else
raise "invalid result: #{result.inspect}"
end
@@ -97,9 +98,9 @@ RSpec.describe Projects::Prometheus::Alerts::NotifyService do
end
context 'without project specific cluster' do
- let!(:cluster) { create(:cluster, enabled: true) }
+ let_it_be(:cluster) { create(:cluster, enabled: true) }
- it_behaves_like 'Alert Notification Service sends no notifications', http_status: :unauthorized
+ it_behaves_like 'alerts service responds with an error and takes no actions', :unauthorized
end
context 'with manual prometheus installation' do
@@ -126,9 +127,9 @@ RSpec.describe Projects::Prometheus::Alerts::NotifyService do
case result = params[:result]
when :success
- it_behaves_like 'Alert Notification Service sends notification email'
+ it_behaves_like 'processes one firing and one resolved prometheus alerts'
when :failure
- it_behaves_like 'Alert Notification Service sends no notifications', http_status: :unauthorized
+ it_behaves_like 'alerts service responds with an error and takes no actions', :unauthorized
else
raise "invalid result: #{result.inspect}"
end
@@ -150,50 +151,53 @@ RSpec.describe Projects::Prometheus::Alerts::NotifyService do
let(:token_input) { public_send(token) if token }
let(:integration) { create(:alert_management_http_integration, active, project: project) if active }
- let(:subject) { service.execute(token_input, integration) }
+ subject { service.execute(token_input, integration) }
case result = params[:result]
when :success
- it_behaves_like 'Alert Notification Service sends notification email'
+ it_behaves_like 'processes one firing and one resolved prometheus alerts'
when :failure
- it_behaves_like 'Alert Notification Service sends no notifications', http_status: :unauthorized
+ it_behaves_like 'alerts service responds with an error and takes no actions', :unauthorized
else
raise "invalid result: #{result.inspect}"
end
end
end
- context 'alert emails' do
+ context 'incident settings' 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)
+ it_behaves_like 'processes one firing and one resolved prometheus alerts'
- expect(subject).to be_success
+ context 'when incident_management_setting does not exist' do
+ before do
+ setting.destroy!
end
- end
- context 'when incident_management_setting.send_email is true' do
- it_behaves_like 'Alert Notification Service sends notification email'
+ it { is_expected.to be_success }
+ include_examples 'does not send alert notification emails'
+ include_examples 'does not process incident issues'
end
context 'incident_management_setting.send_email is false' do
- let!(:setting) do
- create(:project_incident_management_setting, send_email: false, project: project)
+ before do
+ setting.update!(send_email: false)
end
- it 'does not send notification' do
- expect(NotificationService).not_to receive(:new)
+ it { is_expected.to be_success }
+ include_examples 'does not send alert notification emails'
+ end
- expect(subject).to be_success
+ context 'incident_management_setting.create_issue is false' do
+ before do
+ setting.update!(create_issue: false)
end
+
+ it { is_expected.to be_success }
+ include_examples 'does not process incident issues'
end
end
@@ -233,7 +237,7 @@ RSpec.describe Projects::Prometheus::Alerts::NotifyService do
.and_return(false)
end
- it_behaves_like 'Alert Notification Service sends no notifications', http_status: :unprocessable_entity
+ it_behaves_like 'alerts service responds with an error and takes no actions', :unprocessable_entity
end
context 'when the payload is too big' do
@@ -244,14 +248,7 @@ RSpec.describe Projects::Prometheus::Alerts::NotifyService do
allow(Gitlab::Utils::DeepSize).to receive(:new).and_return(deep_size_object)
end
- it_behaves_like 'Alert Notification Service sends no notifications', http_status: :bad_request
-
- it 'does not process Prometheus alerts' do
- expect(AlertManagement::ProcessPrometheusAlertService)
- .not_to receive(:new)
-
- subject
- end
+ it_behaves_like 'alerts service responds with an error and takes no actions', :bad_request
end
end