1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
# frozen_string_literal: true
require 'spec_helper'
require_migration!
RSpec.describe UnsetEscalationPoliciesForAlertIncidents do
let(:namespaces) { table(:namespaces) }
let(:projects) { table(:projects) }
let(:issues) { table(:issues) }
let(:alerts) { table(:alert_management_alerts) }
let(:escalation_policies) { table(:incident_management_escalation_policies) }
let(:escalation_statuses) { table(:incident_management_issuable_escalation_statuses) }
let(:current_time) { Time.current.change(usec: 0) }
let!(:namespace) { namespaces.create!(name: 'namespace', path: 'namespace') }
let!(:project_namespace) { namespaces.create!(name: 'project', path: 'project', type: 'project') }
let!(:project) { projects.create!(namespace_id: namespace.id, project_namespace_id: project_namespace.id) }
let!(:policy) { escalation_policies.create!(project_id: project.id, name: 'escalation policy') }
# Escalation status with policy from alert; Policy & escalation start time should be nullified
let!(:issue_1) { create_issue }
let!(:escalation_status_1) { create_status(issue_1, policy, current_time) }
let!(:alert_1) { create_alert(1, issue_1) }
# Escalation status without policy, but with alert; Should be ignored
let!(:issue_2) { create_issue }
let!(:escalation_status_2) { create_status(issue_2, nil, current_time) }
let!(:alert_2) { create_alert(2, issue_2) }
# Escalation status without alert, but with policy; Should be ignored
let!(:issue_3) { create_issue }
let!(:escalation_status_3) { create_status(issue_3, policy, current_time) }
# Alert without issue; Should be ignored
let!(:alert_3) { create_alert(3) }
it 'removes the escalation policy if the incident corresponds to an alert' do
expect { migrate! }
.to change { escalation_status_1.reload.policy_id }.from(policy.id).to(nil)
.and change { escalation_status_1.escalations_started_at }.from(current_time).to(nil)
.and not_change { policy_attrs(escalation_status_2) }
.and not_change { policy_attrs(escalation_status_3) }
end
private
def create_issue
issues.create!(project_id: project.id)
end
def create_status(issue, policy = nil, escalations_started_at = nil)
escalation_statuses.create!(
issue_id: issue.id,
policy_id: policy&.id,
escalations_started_at: escalations_started_at
)
end
def create_alert(iid, issue = nil)
alerts.create!(
project_id: project.id,
started_at: current_time,
title: "alert #{iid}",
iid: iid.to_s,
issue_id: issue&.id
)
end
def policy_attrs(escalation_status)
escalation_status.reload.slice(:policy_id, :escalations_started_at)
end
end
|