summaryrefslogtreecommitdiff
path: root/spec/workers/incident_management
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2020-07-20 12:26:25 +0000
committerGitLab Bot <gitlab-bot@gitlab.com>2020-07-20 12:26:25 +0000
commita09983ae35713f5a2bbb100981116d31ce99826e (patch)
tree2ee2af7bd104d57086db360a7e6d8c9d5d43667a /spec/workers/incident_management
parent18c5ab32b738c0b6ecb4d0df3994000482f34bd8 (diff)
downloadgitlab-ce-a09983ae35713f5a2bbb100981116d31ce99826e.tar.gz
Add latest changes from gitlab-org/gitlab@13-2-stable-ee
Diffstat (limited to 'spec/workers/incident_management')
-rw-r--r--spec/workers/incident_management/pager_duty/process_incident_worker_spec.rb57
-rw-r--r--spec/workers/incident_management/process_alert_worker_spec.rb66
-rw-r--r--spec/workers/incident_management/process_prometheus_alert_worker_spec.rb134
3 files changed, 87 insertions, 170 deletions
diff --git a/spec/workers/incident_management/pager_duty/process_incident_worker_spec.rb b/spec/workers/incident_management/pager_duty/process_incident_worker_spec.rb
new file mode 100644
index 00000000000..e2be91516b9
--- /dev/null
+++ b/spec/workers/incident_management/pager_duty/process_incident_worker_spec.rb
@@ -0,0 +1,57 @@
+# frozen_string_literal: true
+
+require 'spec_helper'
+
+RSpec.describe IncidentManagement::PagerDuty::ProcessIncidentWorker do
+ let_it_be(:project) { create(:project) }
+ let_it_be(:incident_management_setting) { create(:project_incident_management_setting, project: project, pagerduty_active: true) }
+
+ describe '#perform' do
+ subject(:perform) { described_class.new.perform(project.id, incident_payload) }
+
+ context 'with valid incident payload' do
+ let(:incident_payload) do
+ {
+ 'url' => 'https://webdemo.pagerduty.com/incidents/PRORDTY',
+ 'incident_number' => 33,
+ 'title' => 'My new incident',
+ 'status' => 'triggered',
+ 'created_at' => '2017-09-26T15:14:36Z',
+ 'urgency' => 'high',
+ 'incident_key' => nil,
+ 'assignees' => [{
+ 'summary' => 'Laura Haley', 'url' => 'https://webdemo.pagerduty.com/users/P553OPV'
+ }],
+ 'impacted_services' => [{
+ 'summary' => 'Production XDB Cluster', 'url' => 'https://webdemo.pagerduty.com/services/PN49J75'
+ }]
+ }
+ end
+
+ it 'creates a GitLab issue' do
+ expect { perform }.to change(Issue, :count).by(1)
+ end
+ end
+
+ context 'with invalid incident payload' do
+ let(:incident_payload) { {} }
+
+ before do
+ allow(Gitlab::AppLogger).to receive(:warn).and_call_original
+ end
+
+ it 'does not create a GitLab issue' do
+ expect { perform }.not_to change(Issue, :count)
+ end
+
+ it 'logs a warning' do
+ perform
+
+ expect(Gitlab::AppLogger).to have_received(:warn).with(
+ message: 'Cannot create issue for PagerDuty incident',
+ issue_errors: "Title can't be blank"
+ )
+ end
+ end
+ end
+end
diff --git a/spec/workers/incident_management/process_alert_worker_spec.rb b/spec/workers/incident_management/process_alert_worker_spec.rb
index 0470552d933..75696d15ab8 100644
--- a/spec/workers/incident_management/process_alert_worker_spec.rb
+++ b/spec/workers/incident_management/process_alert_worker_spec.rb
@@ -2,40 +2,36 @@
require 'spec_helper'
-describe IncidentManagement::ProcessAlertWorker do
+RSpec.describe IncidentManagement::ProcessAlertWorker do
let_it_be(:project) { create(:project) }
let_it_be(:settings) { create(:project_incident_management_setting, project: project, create_issue: true) }
describe '#perform' do
- let(:alert_management_alert_id) { nil }
- let(:alert_payload) do
- {
- 'annotations' => { 'title' => 'title' },
- 'startsAt' => Time.now.rfc3339
- }
- end
-
- let(:created_issue) { Issue.last }
+ let_it_be(:started_at) { Time.now.rfc3339 }
+ let_it_be(:payload) { { 'title' => 'title', 'start_time' => started_at } }
+ let_it_be(:parsed_payload) { Gitlab::Alerting::NotificationPayloadParser.call(payload, project) }
+ let_it_be(:alert) { create(:alert_management_alert, project: project, payload: payload, started_at: started_at) }
+ let(:created_issue) { Issue.last! }
- subject { described_class.new.perform(project.id, alert_payload, alert_management_alert_id) }
+ subject { described_class.new.perform(nil, nil, alert.id) }
before do
allow(IncidentManagement::CreateIssueService)
- .to receive(:new).with(project, alert_payload)
+ .to receive(:new).with(alert.project, parsed_payload)
.and_call_original
end
it 'creates an issue' do
expect(IncidentManagement::CreateIssueService)
- .to receive(:new).with(project, alert_payload)
+ .to receive(:new).with(alert.project, parsed_payload)
expect { subject }.to change { Issue.count }.by(1)
end
- context 'with invalid project' do
- let(:invalid_project_id) { non_existing_record_id }
+ context 'with invalid alert' do
+ let(:invalid_alert_id) { non_existing_record_id }
- subject { described_class.new.perform(invalid_project_id, alert_payload) }
+ subject { described_class.new.perform(nil, nil, invalid_alert_id) }
it 'does not create issues' do
expect(IncidentManagement::CreateIssueService).not_to receive(:new)
@@ -44,16 +40,8 @@ describe IncidentManagement::ProcessAlertWorker do
end
end
- context 'when alert_management_alert_id is present' do
- let!(:alert) { create(:alert_management_alert, project: project) }
- let(:alert_management_alert_id) { alert.id }
-
+ context 'with valid alert' do
before do
- allow(AlertManagement::Alert)
- .to receive(:find_by_id)
- .with(alert_management_alert_id)
- .and_return(alert)
-
allow(Gitlab::AppLogger).to receive(:warn).and_call_original
end
@@ -69,24 +57,24 @@ describe IncidentManagement::ProcessAlertWorker do
expect(Gitlab::AppLogger).not_to have_received(:warn)
end
- end
- context 'when alert cannot be updated' do
- let(:alert) { create(:alert_management_alert, :with_validation_errors, project: project) }
+ context 'when alert cannot be updated' do
+ let_it_be(:alert) { create(:alert_management_alert, :with_validation_errors, project: project, payload: payload) }
- it 'updates AlertManagement::Alert#issue_id' do
- expect { subject }.not_to change { alert.reload.issue_id }
- end
+ it 'updates AlertManagement::Alert#issue_id' do
+ expect { subject }.not_to change { alert.reload.issue_id }
+ end
- it 'logs a warning' do
- subject
+ it 'logs a warning' do
+ subject
- expect(Gitlab::AppLogger).to have_received(:warn).with(
- message: 'Cannot link an Issue with Alert',
- issue_id: created_issue.id,
- alert_id: alert_management_alert_id,
- alert_errors: { hosts: ['hosts array is over 255 chars'] }
- )
+ expect(Gitlab::AppLogger).to have_received(:warn).with(
+ message: 'Cannot link an Issue with Alert',
+ issue_id: created_issue.id,
+ alert_id: alert.id,
+ alert_errors: { hosts: ['hosts array is over 255 chars'] }
+ )
+ end
end
end
end
diff --git a/spec/workers/incident_management/process_prometheus_alert_worker_spec.rb b/spec/workers/incident_management/process_prometheus_alert_worker_spec.rb
index c9ea96df5c2..c294892a66f 100644
--- a/spec/workers/incident_management/process_prometheus_alert_worker_spec.rb
+++ b/spec/workers/incident_management/process_prometheus_alert_worker_spec.rb
@@ -2,7 +2,7 @@
require 'spec_helper'
-describe IncidentManagement::ProcessPrometheusAlertWorker do
+RSpec.describe IncidentManagement::ProcessPrometheusAlertWorker do
describe '#perform' do
let_it_be(:project) { create(:project) }
let_it_be(:prometheus_alert) { create(:prometheus_alert, project: project) }
@@ -19,137 +19,9 @@ describe IncidentManagement::ProcessPrometheusAlertWorker do
}.with_indifferent_access
end
- it 'creates an issue' do
+ it 'does nothing' do
expect { subject.perform(project.id, alert_params) }
- .to change(Issue, :count)
- .by(1)
- end
-
- it 'relates issue to an event' do
- expect { subject.perform(project.id, alert_params) }
- .to change(prometheus_alert.related_issues, :count)
- .from(0)
- .to(1)
- end
-
- context 'resolved event' do
- let(:issue) { create(:issue, project: project) }
-
- before do
- prometheus_alert_event.related_issues << issue
- prometheus_alert_event.resolve
- end
-
- it 'does not create an issue' do
- expect { subject.perform(project.id, alert_params) }
- .not_to change(Issue, :count)
- end
-
- it 'closes the existing issue' do
- expect { subject.perform(project.id, alert_params) }
- .to change { issue.reload.state }
- .from('opened')
- .to('closed')
- end
-
- it 'leaves a system note on the issue' do
- expect(SystemNoteService)
- .to receive(:auto_resolve_prometheus_alert)
-
- subject.perform(project.id, alert_params)
- end
- end
-
- context 'when project could not be found' do
- let(:non_existing_project_id) { non_existing_record_id }
-
- it 'does not create an issue' do
- expect { subject.perform(non_existing_project_id, alert_params) }
- .not_to change(Issue, :count)
- end
-
- it 'does not relate issue to an event' do
- expect { subject.perform(non_existing_project_id, alert_params) }
- .not_to change(prometheus_alert.related_issues, :count)
- end
- end
-
- context 'when event could not be found' do
- before do
- alert_params[:labels][:gitlab_alert_id] = non_existing_record_id
- end
-
- it 'does not create an issue' do
- expect { subject.perform(project.id, alert_params) }
- .not_to change(Issue, :count)
- end
-
- it 'does not relate issue to an event' do
- expect { subject.perform(project.id, alert_params) }
- .not_to change(prometheus_alert.related_issues, :count)
- end
- end
-
- context 'when issue could not be created' do
- before do
- allow_next_instance_of(IncidentManagement::CreateIssueService) do |instance|
- allow(instance).to receive(:execute).and_return( { error: true } )
- end
- end
-
- it 'does not relate issue to an event' do
- expect { subject.perform(project.id, alert_params) }
- .not_to change(prometheus_alert.related_issues, :count)
- end
- end
-
- context 'self-managed alert' do
- let(:alert_name) { 'alert' }
- let(:starts_at) { Time.now.rfc3339 }
-
- let!(:prometheus_alert_event) do
- create(:self_managed_prometheus_alert_event, project: project, payload_key: payload_key)
- end
-
- let(:alert_params) do
- {
- startsAt: starts_at,
- generatorURL: 'http://localhost:9090/graph?g0.expr=vector%281%29&g0.tab=1',
- labels: {
- alertname: alert_name
- }
- }.with_indifferent_access
- end
-
- it 'creates an issue' do
- expect { subject.perform(project.id, alert_params) }
- .to change(Issue, :count)
- .by(1)
- end
-
- it 'relates issue to an event' do
- expect { subject.perform(project.id, alert_params) }
- .to change(prometheus_alert_event.related_issues, :count)
- .from(0)
- .to(1)
- end
-
- context 'when event could not be found' do
- before do
- alert_params[:generatorURL] = 'http://somethingelse.com'
- end
-
- it 'creates an issue' do
- expect { subject.perform(project.id, alert_params) }
- .to change(Issue, :count)
- .by(1)
- end
-
- it 'does not relate issue to an event' do
- expect { subject.perform(project.id, alert_params) }
- .not_to change(prometheus_alert.related_issues, :count)
- end
- end
+ .not_to change(Issue, :count)
end
end
end