diff options
Diffstat (limited to 'spec/lib/gitlab')
-rw-r--r-- | spec/lib/gitlab/alerting/alert_spec.rb | 42 |
1 files changed, 40 insertions, 2 deletions
diff --git a/spec/lib/gitlab/alerting/alert_spec.rb b/spec/lib/gitlab/alerting/alert_spec.rb index 8b67cf949ce..6d97f08af91 100644 --- a/spec/lib/gitlab/alerting/alert_spec.rb +++ b/spec/lib/gitlab/alerting/alert_spec.rb @@ -9,11 +9,14 @@ describe Gitlab::Alerting::Alert do let(:payload) { {} } shared_context 'gitlab alert' do - let(:gitlab_alert_id) { gitlab_alert.prometheus_metric_id.to_s } let!(:gitlab_alert) { create(:prometheus_alert, project: project) } + let(:gitlab_alert_id) { gitlab_alert.id } before do - payload['labels'] = { 'gitlab_alert_id' => gitlab_alert_id } + payload['labels'] = { + 'gitlab_alert_id' => gitlab_alert.prometheus_metric_id.to_s, + 'gitlab_prometheus_alert_id' => gitlab_alert_id + } end end @@ -68,6 +71,41 @@ describe Gitlab::Alerting::Alert do it { is_expected.to be_nil } end + + context 'when two alerts with the same metric exist' do + include_context 'gitlab alert' + + let!(:second_gitlab_alert) do + create(:prometheus_alert, + project: project, + prometheus_metric_id: gitlab_alert.prometheus_metric_id + ) + end + + context 'alert id given in params' do + before do + payload['labels'] = { + 'gitlab_alert_id' => gitlab_alert.prometheus_metric_id.to_s, + 'gitlab_prometheus_alert_id' => second_gitlab_alert.id + } + end + + it { is_expected.to eq(second_gitlab_alert) } + end + + context 'metric id given in params' do + # This tests the case when two alerts are found, as metric id + # is not unique. + + # Note the metric id was incorrectly named as 'gitlab_alert_id' + # in PrometheusAlert#to_param. + before do + payload['labels'] = { 'gitlab_alert_id' => gitlab_alert.prometheus_metric_id } + end + + it { is_expected.to be_nil } + end + end end describe '#title' do |