2020-02-11 15:08:44 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 15:08:50 +00:00
|
|
|
RSpec.describe IncidentManagement::ProcessAlertWorker do
|
2020-02-11 15:08:44 +00:00
|
|
|
let_it_be(:project) { create(:project) }
|
2020-06-15 12:08:44 +00:00
|
|
|
let_it_be(:settings) { create(:project_incident_management_setting, project: project, create_issue: true) }
|
2020-02-11 15:08:44 +00:00
|
|
|
|
|
|
|
describe '#perform' do
|
2020-06-29 00:08:58 +00:00
|
|
|
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-02-11 15:08:44 +00:00
|
|
|
|
2020-06-29 00:08:58 +00:00
|
|
|
subject { described_class.new.perform(nil, nil, alert.id) }
|
2020-04-29 12:10:00 +00:00
|
|
|
|
|
|
|
before do
|
2020-07-30 06:09:55 +00:00
|
|
|
allow(Gitlab::AppLogger).to receive(:warn).and_call_original
|
|
|
|
|
2020-08-12 21:09:54 +00:00
|
|
|
allow(AlertManagement::CreateAlertIssueService)
|
|
|
|
.to receive(:new).with(alert, User.alert_bot)
|
2020-06-03 15:08:05 +00:00
|
|
|
.and_call_original
|
2020-04-29 12:10:00 +00:00
|
|
|
end
|
2020-02-11 15:08:44 +00:00
|
|
|
|
2020-07-30 06:09:55 +00:00
|
|
|
shared_examples 'creates issue successfully' do
|
|
|
|
it 'creates an issue' do
|
2020-08-12 21:09:54 +00:00
|
|
|
expect(AlertManagement::CreateAlertIssueService)
|
|
|
|
.to receive(:new).with(alert, User.alert_bot)
|
2020-02-11 15:08:44 +00:00
|
|
|
|
2020-07-30 06:09:55 +00:00
|
|
|
expect { subject }.to change { Issue.count }.by(1)
|
|
|
|
end
|
2020-02-11 15:08:44 +00:00
|
|
|
|
2020-07-30 06:09:55 +00:00
|
|
|
it 'updates AlertManagement::Alert#issue_id' do
|
|
|
|
subject
|
2020-02-11 15:08:44 +00:00
|
|
|
|
2020-07-30 06:09:55 +00:00
|
|
|
expect(alert.reload.issue_id).to eq(created_issue.id)
|
|
|
|
end
|
2020-02-11 15:08:44 +00:00
|
|
|
|
2020-07-30 06:09:55 +00:00
|
|
|
it 'does not write a warning to log' do
|
|
|
|
subject
|
2020-02-11 15:08:44 +00:00
|
|
|
|
2020-07-30 06:09:55 +00:00
|
|
|
expect(Gitlab::AppLogger).not_to have_received(:warn)
|
2020-02-11 15:08:44 +00:00
|
|
|
end
|
|
|
|
end
|
2020-04-29 12:10:00 +00:00
|
|
|
|
2020-06-29 00:08:58 +00:00
|
|
|
context 'with valid alert' do
|
2020-07-30 06:09:55 +00:00
|
|
|
it_behaves_like 'creates issue successfully'
|
2020-04-29 12:10:00 +00:00
|
|
|
|
2020-07-30 06:09:55 +00:00
|
|
|
context 'when alert cannot be updated' do
|
|
|
|
let_it_be(:alert) { create(:alert_management_alert, :with_validation_errors, project: project, payload: payload) }
|
2020-06-03 15:08:05 +00:00
|
|
|
|
2020-07-30 06:09:55 +00:00
|
|
|
it 'updates AlertManagement::Alert#issue_id' do
|
|
|
|
expect { subject }.not_to change { alert.reload.issue_id }
|
2020-04-29 12:10:00 +00:00
|
|
|
end
|
|
|
|
|
2020-07-30 06:09:55 +00:00
|
|
|
it 'logs a warning' do
|
2020-04-29 12:10:00 +00:00
|
|
|
subject
|
|
|
|
|
2020-07-30 06:09:55 +00:00
|
|
|
expect(Gitlab::AppLogger).to have_received(:warn).with(
|
2020-08-12 21:09:54 +00:00
|
|
|
message: 'Cannot process an Incident',
|
2020-07-30 06:09:55 +00:00
|
|
|
issue_id: created_issue.id,
|
|
|
|
alert_id: alert.id,
|
2020-08-12 21:09:54 +00:00
|
|
|
errors: 'Hosts hosts array is over 255 chars'
|
2020-07-30 06:09:55 +00:00
|
|
|
)
|
2020-04-29 12:10:00 +00:00
|
|
|
end
|
2020-07-30 06:09:55 +00:00
|
|
|
end
|
2020-04-29 12:10:00 +00:00
|
|
|
|
2020-07-30 06:09:55 +00:00
|
|
|
context 'prometheus alert' do
|
|
|
|
let_it_be(:alert) { create(:alert_management_alert, :prometheus, project: project, started_at: started_at) }
|
2020-04-29 12:10:00 +00:00
|
|
|
|
2020-07-30 06:09:55 +00:00
|
|
|
it_behaves_like 'creates issue successfully'
|
|
|
|
end
|
|
|
|
end
|
2020-04-29 12:10:00 +00:00
|
|
|
|
2020-07-30 06:09:55 +00:00
|
|
|
context 'with invalid alert' do
|
|
|
|
let(:invalid_alert_id) { non_existing_record_id }
|
2020-04-29 12:10:00 +00:00
|
|
|
|
2020-07-30 06:09:55 +00:00
|
|
|
subject { described_class.new.perform(nil, nil, invalid_alert_id) }
|
|
|
|
|
|
|
|
it 'does not create issues' do
|
2020-08-12 21:09:54 +00:00
|
|
|
expect(AlertManagement::CreateAlertIssueService).not_to receive(:new)
|
2020-07-30 06:09:55 +00:00
|
|
|
|
|
|
|
expect { subject }.not_to change { Issue.count }
|
2020-04-29 12:10:00 +00:00
|
|
|
end
|
|
|
|
end
|
2020-02-11 15:08:44 +00:00
|
|
|
end
|
|
|
|
end
|