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>2020-04-08 18:09:29 +0300
committerGitLab Bot <gitlab-bot@gitlab.com>2020-04-08 18:09:29 +0300
commit5372e109c0660e4670aa987568a51082beca1b3c (patch)
tree76f8f1178d5f304f0aea8c0c610729f695c9e18e /spec/services
parent403678e00406edc8094f087ec70e00aa29e49bef (diff)
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec/services')
-rw-r--r--spec/services/prometheus/create_default_alerts_service_spec.rb74
1 files changed, 74 insertions, 0 deletions
diff --git a/spec/services/prometheus/create_default_alerts_service_spec.rb b/spec/services/prometheus/create_default_alerts_service_spec.rb
new file mode 100644
index 00000000000..3382844c99a
--- /dev/null
+++ b/spec/services/prometheus/create_default_alerts_service_spec.rb
@@ -0,0 +1,74 @@
+# frozen_string_literal: true
+
+require 'spec_helper'
+
+describe Prometheus::CreateDefaultAlertsService do
+ let_it_be(:project) { create(:project) }
+ let(:instance) { described_class.new(project: project) }
+ let(:expected_alerts) { described_class::DEFAULT_ALERTS }
+
+ describe '#execute' do
+ subject(:execute) { instance.execute }
+
+ shared_examples 'no alerts created' do
+ it 'does not create alerts' do
+ expect { execute }.not_to change { project.reload.prometheus_alerts.count }
+ end
+ end
+
+ context 'no environment' do
+ it_behaves_like 'no alerts created'
+ end
+
+ context 'environment exists' do
+ let_it_be(:environment) { create(:environment, project: project) }
+
+ context 'no found metric' do
+ it_behaves_like 'no alerts created'
+ end
+
+ context 'metric exists' do
+ before do
+ create_expected_metrics!
+ end
+
+ context 'alert exists already' do
+ before do
+ create_pre_existing_alerts!(environment)
+ end
+
+ it_behaves_like 'no alerts created'
+ end
+
+ it 'creates alerts' do
+ expect { execute }.to change { project.reload.prometheus_alerts.count }
+ .by(expected_alerts.size)
+ end
+
+ context 'multiple environments' do
+ let!(:production) { create(:environment, project: project, name: 'production') }
+
+ it 'uses the production environment' do
+ expect { execute }.to change { production.reload.prometheus_alerts.count }
+ .by(expected_alerts.size)
+ end
+ end
+ end
+ end
+ end
+
+ private
+
+ def create_expected_metrics!
+ expected_alerts.each do |alert_hash|
+ create(:prometheus_metric, :common, identifier: alert_hash.fetch(:identifier))
+ end
+ end
+
+ def create_pre_existing_alerts!(environment)
+ expected_alerts.each do |alert_hash|
+ metric = PrometheusMetric.for_identifier(alert_hash[:identifier]).first!
+ create(:prometheus_alert, prometheus_metric: metric, project: project, environment: environment)
+ end
+ end
+end