debian-mirror-gitlab/spec/workers/incident_management/process_alert_worker_spec.rb

88 lines
2.7 KiB
Ruby
Raw Normal View History

2020-03-13 15:44:24 +05:30
# frozen_string_literal: true
require 'spec_helper'
2020-07-28 23:09:34 +05:30
RSpec.describe IncidentManagement::ProcessAlertWorker do
2020-03-13 15:44:24 +05:30
let_it_be(:project) { create(:project) }
2020-06-23 00:09:42 +05:30
let_it_be(:settings) { create(:project_incident_management_setting, project: project, create_issue: true) }
2020-03-13 15:44:24 +05:30
describe '#perform' do
2020-07-28 23:09:34 +05:30
let_it_be(:started_at) { Time.now.rfc3339 }
let_it_be(:payload) { { 'title' => 'title', 'start_time' => started_at } }
let_it_be(:alert) { create(:alert_management_alert, project: project, payload: payload, started_at: started_at) }
let(:created_issue) { Issue.last! }
2020-03-13 15:44:24 +05:30
2020-07-28 23:09:34 +05:30
subject { described_class.new.perform(nil, nil, alert.id) }
2020-05-24 23:13:21 +05:30
before do
2020-08-18 19:51:02 +05:30
allow(Gitlab::AppLogger).to receive(:warn).and_call_original
2020-10-24 23:57:45 +05:30
allow(AlertManagement::CreateAlertIssueService)
.to receive(:new).with(alert, User.alert_bot)
2020-06-11 16:45:22 +05:30
.and_call_original
2020-05-24 23:13:21 +05:30
end
2020-03-13 15:44:24 +05:30
2020-08-18 19:51:02 +05:30
shared_examples 'creates issue successfully' do
it 'creates an issue' do
2020-10-24 23:57:45 +05:30
expect(AlertManagement::CreateAlertIssueService)
.to receive(:new).with(alert, User.alert_bot)
2020-03-13 15:44:24 +05:30
2020-08-18 19:51:02 +05:30
expect { subject }.to change { Issue.count }.by(1)
end
2020-03-13 15:44:24 +05:30
2020-08-18 19:51:02 +05:30
it 'updates AlertManagement::Alert#issue_id' do
subject
2020-03-13 15:44:24 +05:30
2020-08-18 19:51:02 +05:30
expect(alert.reload.issue_id).to eq(created_issue.id)
end
2020-03-13 15:44:24 +05:30
2020-08-18 19:51:02 +05:30
it 'does not write a warning to log' do
subject
2020-03-13 15:44:24 +05:30
2020-08-18 19:51:02 +05:30
expect(Gitlab::AppLogger).not_to have_received(:warn)
2020-03-13 15:44:24 +05:30
end
end
2020-05-24 23:13:21 +05:30
2020-07-28 23:09:34 +05:30
context 'with valid alert' do
2020-08-18 19:51:02 +05:30
it_behaves_like 'creates issue successfully'
2020-05-24 23:13:21 +05:30
2020-08-18 19:51:02 +05:30
context 'when alert cannot be updated' do
let_it_be(:alert) { create(:alert_management_alert, :with_validation_errors, project: project, payload: payload) }
2020-06-11 16:45:22 +05:30
2020-08-18 19:51:02 +05:30
it 'updates AlertManagement::Alert#issue_id' do
expect { subject }.not_to change { alert.reload.issue_id }
2020-05-24 23:13:21 +05:30
end
2020-08-18 19:51:02 +05:30
it 'logs a warning' do
2020-05-24 23:13:21 +05:30
subject
2020-08-18 19:51:02 +05:30
expect(Gitlab::AppLogger).to have_received(:warn).with(
2020-10-24 23:57:45 +05:30
message: 'Cannot process an Incident',
2020-08-18 19:51:02 +05:30
issue_id: created_issue.id,
alert_id: alert.id,
2020-10-24 23:57:45 +05:30
errors: 'Hosts hosts array is over 255 chars'
2020-08-18 19:51:02 +05:30
)
2020-05-24 23:13:21 +05:30
end
2020-08-18 19:51:02 +05:30
end
2020-05-24 23:13:21 +05:30
2020-08-18 19:51:02 +05:30
context 'prometheus alert' do
let_it_be(:alert) { create(:alert_management_alert, :prometheus, project: project, started_at: started_at) }
2020-05-24 23:13:21 +05:30
2020-08-18 19:51:02 +05:30
it_behaves_like 'creates issue successfully'
end
end
2020-05-24 23:13:21 +05:30
2020-08-18 19:51:02 +05:30
context 'with invalid alert' do
let(:invalid_alert_id) { non_existing_record_id }
2020-05-24 23:13:21 +05:30
2020-08-18 19:51:02 +05:30
subject { described_class.new.perform(nil, nil, invalid_alert_id) }
it 'does not create issues' do
2020-10-24 23:57:45 +05:30
expect(AlertManagement::CreateAlertIssueService).not_to receive(:new)
2020-08-18 19:51:02 +05:30
expect { subject }.not_to change { Issue.count }
2020-05-24 23:13:21 +05:30
end
end
2020-03-13 15:44:24 +05:30
end
end