Commit 5f51186d authored by Robert Speicher's avatar Robert Speicher

Merge branch '212412-extract-duplicated-shared-examples' into 'master'

Extract shared examples from Alert Notify Services

Closes #212412

See merge request gitlab-org/gitlab!37948
parents 92b3cee9 f9da8419
...@@ -21,38 +21,6 @@ RSpec.describe Projects::Prometheus::Alerts::NotifyService do ...@@ -21,38 +21,6 @@ RSpec.describe Projects::Prometheus::Alerts::NotifyService do
project.clear_memoization(:licensed_feature_available) project.clear_memoization(:licensed_feature_available)
end end
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)
expect(subject).to be_success
end
end
shared_examples 'notifies alerts' do
it_behaves_like 'sends notification email'
end
shared_examples 'no notifications' do |http_status:|
let(:notification_service) { spy }
let(:create_events_service) { spy }
it 'does not notify' do
expect(notification_service).not_to receive(:async)
expect(create_events_service).not_to receive(:execute)
expect(subject).to be_error
expect(subject.http_status).to eq(http_status)
end
end
context 'with valid payload' do context 'with valid payload' do
let(:alert_firing) { create(:prometheus_alert, project: project) } let(:alert_firing) { create(:prometheus_alert, project: project) }
let(:alert_resolved) { create(:prometheus_alert, project: project) } let(:alert_resolved) { create(:prometheus_alert, project: project) }
...@@ -87,11 +55,11 @@ RSpec.describe Projects::Prometheus::Alerts::NotifyService do ...@@ -87,11 +55,11 @@ RSpec.describe Projects::Prometheus::Alerts::NotifyService do
context 'without token' do context 'without token' do
let(:token_input) { nil } let(:token_input) { nil }
it_behaves_like 'notifies alerts' it_behaves_like 'Alert Notification Service sends notification email'
end end
context 'with token' do context 'with token' do
it_behaves_like 'no notifications', http_status: :unauthorized it_behaves_like 'Alert Notification Service sends no notifications', http_status: :unauthorized
end end
end end
end end
......
...@@ -30,21 +30,6 @@ RSpec.describe Projects::Alerting::NotifyService do ...@@ -30,21 +30,6 @@ RSpec.describe Projects::Alerting::NotifyService do
end end
end end
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)
expect(subject).to be_success
end
end
shared_examples 'does not process incident issues' do shared_examples 'does not process incident issues' do
it 'does not process issues' do it 'does not process issues' do
expect(IncidentManagement::ProcessAlertWorker) expect(IncidentManagement::ProcessAlertWorker)
...@@ -234,7 +219,7 @@ RSpec.describe Projects::Alerting::NotifyService do ...@@ -234,7 +219,7 @@ RSpec.describe Projects::Alerting::NotifyService do
context 'with emails turned on' do context 'with emails turned on' do
let(:email_enabled) { true } let(:email_enabled) { true }
it_behaves_like 'sends notification email' it_behaves_like 'Alert Notification Service sends notification email'
end end
end end
......
...@@ -21,38 +21,6 @@ RSpec.describe Projects::Prometheus::Alerts::NotifyService do ...@@ -21,38 +21,6 @@ RSpec.describe Projects::Prometheus::Alerts::NotifyService do
project.clear_memoization(:licensed_feature_available) project.clear_memoization(:licensed_feature_available)
end end
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)
expect(subject).to be_success
end
end
shared_examples 'notifies alerts' do
it_behaves_like 'sends notification email'
end
shared_examples 'no notifications' do |http_status:|
let(:notification_service) { spy }
let(:create_events_service) { spy }
it 'does not notify' do
expect(notification_service).not_to receive(:async)
expect(create_events_service).not_to receive(:execute)
expect(subject).to be_error
expect(subject.http_status).to eq(http_status)
end
end
context 'with valid payload' do context 'with valid payload' do
let_it_be(:alert_firing) { create(:prometheus_alert, project: project) } let_it_be(:alert_firing) { create(:prometheus_alert, project: project) }
let_it_be(:alert_resolved) { create(:prometheus_alert, project: project) } let_it_be(:alert_resolved) { create(:prometheus_alert, project: project) }
...@@ -89,11 +57,11 @@ RSpec.describe Projects::Prometheus::Alerts::NotifyService do ...@@ -89,11 +57,11 @@ RSpec.describe Projects::Prometheus::Alerts::NotifyService do
context 'without token' do context 'without token' do
let(:token_input) { nil } let(:token_input) { nil }
it_behaves_like 'notifies alerts' it_behaves_like 'Alert Notification Service sends notification email'
end end
context 'with token' do context 'with token' do
it_behaves_like 'no notifications', http_status: :unauthorized it_behaves_like 'Alert Notification Service sends no notifications', http_status: :unauthorized
end end
end end
...@@ -125,9 +93,9 @@ RSpec.describe Projects::Prometheus::Alerts::NotifyService do ...@@ -125,9 +93,9 @@ RSpec.describe Projects::Prometheus::Alerts::NotifyService do
case result = params[:result] case result = params[:result]
when :success when :success
it_behaves_like 'notifies alerts' it_behaves_like 'Alert Notification Service sends notification email'
when :failure when :failure
it_behaves_like 'no notifications', http_status: :unauthorized it_behaves_like 'Alert Notification Service sends no notifications', http_status: :unauthorized
else else
raise "invalid result: #{result.inspect}" raise "invalid result: #{result.inspect}"
end end
...@@ -137,7 +105,7 @@ RSpec.describe Projects::Prometheus::Alerts::NotifyService do ...@@ -137,7 +105,7 @@ RSpec.describe Projects::Prometheus::Alerts::NotifyService do
context 'without project specific cluster' do context 'without project specific cluster' do
let!(:cluster) { create(:cluster, enabled: true) } let!(:cluster) { create(:cluster, enabled: true) }
it_behaves_like 'no notifications', http_status: :unauthorized it_behaves_like 'Alert Notification Service sends no notifications', http_status: :unauthorized
end end
context 'with manual prometheus installation' do context 'with manual prometheus installation' do
...@@ -166,9 +134,9 @@ RSpec.describe Projects::Prometheus::Alerts::NotifyService do ...@@ -166,9 +134,9 @@ RSpec.describe Projects::Prometheus::Alerts::NotifyService do
case result = params[:result] case result = params[:result]
when :success when :success
it_behaves_like 'notifies alerts' it_behaves_like 'Alert Notification Service sends notification email'
when :failure when :failure
it_behaves_like 'no notifications', http_status: :unauthorized it_behaves_like 'Alert Notification Service sends no notifications', http_status: :unauthorized
else else
raise "invalid result: #{result.inspect}" raise "invalid result: #{result.inspect}"
end end
...@@ -199,9 +167,9 @@ RSpec.describe Projects::Prometheus::Alerts::NotifyService do ...@@ -199,9 +167,9 @@ RSpec.describe Projects::Prometheus::Alerts::NotifyService do
case result = params[:result] case result = params[:result]
when :success when :success
it_behaves_like 'notifies alerts' it_behaves_like 'Alert Notification Service sends notification email'
when :failure when :failure
it_behaves_like 'no notifications', http_status: :unauthorized it_behaves_like 'Alert Notification Service sends no notifications', http_status: :unauthorized
else else
raise "invalid result: #{result.inspect}" raise "invalid result: #{result.inspect}"
end end
...@@ -226,7 +194,7 @@ RSpec.describe Projects::Prometheus::Alerts::NotifyService do ...@@ -226,7 +194,7 @@ RSpec.describe Projects::Prometheus::Alerts::NotifyService do
end end
context 'when incident_management_setting.send_email is true' do context 'when incident_management_setting.send_email is true' do
it_behaves_like 'notifies alerts' it_behaves_like 'Alert Notification Service sends notification email'
end end
context 'incident_management_setting.send_email is false' do context 'incident_management_setting.send_email is false' do
...@@ -278,7 +246,7 @@ RSpec.describe Projects::Prometheus::Alerts::NotifyService do ...@@ -278,7 +246,7 @@ RSpec.describe Projects::Prometheus::Alerts::NotifyService do
.and_return(false) .and_return(false)
end end
it_behaves_like 'no notifications', http_status: :unprocessable_entity it_behaves_like 'Alert Notification Service sends no notifications', http_status: :unprocessable_entity
end end
context 'when the payload is too big' do context 'when the payload is too big' do
...@@ -289,7 +257,7 @@ RSpec.describe Projects::Prometheus::Alerts::NotifyService do ...@@ -289,7 +257,7 @@ RSpec.describe Projects::Prometheus::Alerts::NotifyService do
allow(Gitlab::Utils::DeepSize).to receive(:new).and_return(deep_size_object) allow(Gitlab::Utils::DeepSize).to receive(:new).and_return(deep_size_object)
end end
it_behaves_like 'no notifications', http_status: :bad_request it_behaves_like 'Alert Notification Service sends no notifications', http_status: :bad_request
it 'does not process Prometheus alerts' do it 'does not process Prometheus alerts' do
expect(AlertManagement::ProcessPrometheusAlertService) expect(AlertManagement::ProcessPrometheusAlertService)
......
# frozen_string_literal: true
RSpec.shared_examples 'Alert Notification Service 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)
expect(subject).to be_success
end
end
RSpec.shared_examples 'Alert Notification Service sends no notifications' do |http_status:|
let(:notification_service) { spy }
let(:create_events_service) { spy }
it 'does not notify' do
expect(notification_service).not_to receive(:async)
expect(create_events_service).not_to receive(:execute)
expect(subject).to be_error
expect(subject.http_status).to eq(http_status)
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment