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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Ci::TestFailureHistoryService, :aggregate_failures do
describe '#execute' do
let(:project) { create(:project) }
let(:pipeline) { create(:ci_empty_pipeline, status: :created, project: project) }
subject(:execute_service) { described_class.new(pipeline).execute }
context 'when pipeline has failed builds with test reports' do
before do
# The test report has 2 test case failures
create(:ci_build, :failed, :test_reports, pipeline: pipeline, project: project)
end
it 'creates test case failures records' do
execute_service
expect(Ci::TestCase.count).to eq(2)
expect(Ci::TestCaseFailure.count).to eq(2)
end
context 'when feature flag for test failure history is disabled' do
before do
stub_feature_flags(test_failure_history: false)
end
it 'does not persist data' do
execute_service
expect(Ci::TestCase.count).to eq(0)
expect(Ci::TestCaseFailure.count).to eq(0)
end
end
context 'when pipeline is not for the default branch' do
before do
pipeline.update_column(:ref, 'new-feature')
end
it 'does not persist data' do
execute_service
expect(Ci::TestCase.count).to eq(0)
expect(Ci::TestCaseFailure.count).to eq(0)
end
end
context 'when test failure data have already been persisted with the same exact attributes' do
before do
execute_service
end
it 'does not fail but does not persist new data' do
expect { described_class.new(pipeline).execute }.not_to raise_error
expect(Ci::TestCase.count).to eq(2)
expect(Ci::TestCaseFailure.count).to eq(2)
end
end
context 'when number of failed test cases exceed the limit' do
before do
stub_const("#{described_class.name}::MAX_TRACKABLE_FAILURES", 1)
end
it 'does not persist data' do
execute_service
expect(Ci::TestCase.count).to eq(0)
expect(Ci::TestCaseFailure.count).to eq(0)
end
end
context 'when number of failed test cases across multiple builds exceed the limit' do
before do
stub_const("#{described_class.name}::MAX_TRACKABLE_FAILURES", 2)
# This other test report has 1 unique test case failure which brings us to 3 total failures across all builds
# thus exceeding the limit of 2 for MAX_TRACKABLE_FAILURES
create(:ci_build, :failed, :test_reports_with_duplicate_failed_test_names, pipeline: pipeline, project: project)
end
it 'does not persist data' do
execute_service
expect(Ci::TestCase.count).to eq(0)
expect(Ci::TestCaseFailure.count).to eq(0)
end
end
end
context 'when test failure data have duplicates within the same payload (happens when the JUnit report has duplicate test case names but have different failures)' do
before do
# The test report has 2 test case failures but with the same test case keys
create(:ci_build, :failed, :test_reports_with_duplicate_failed_test_names, pipeline: pipeline, project: project)
end
it 'does not fail but does not persist duplicate data' do
expect { execute_service }.not_to raise_error
expect(Ci::TestCase.count).to eq(1)
expect(Ci::TestCaseFailure.count).to eq(1)
end
end
context 'when pipeline has no failed builds with test reports' do
before do
create(:ci_build, :test_reports, pipeline: pipeline, project: project)
create(:ci_build, :failed, pipeline: pipeline, project: project)
end
it 'does not persist data' do
execute_service
expect(Ci::TestCase.count).to eq(0)
expect(Ci::TestCaseFailure.count).to eq(0)
end
end
end
describe '#should_track_failures?' do
let(:project) { create(:project, :repository) }
let(:pipeline) { create(:ci_empty_pipeline, status: :created, project: project, ref: project.default_branch) }
subject { described_class.new(pipeline).should_track_failures? }
before do
create(:ci_build, :test_reports, :failed, pipeline: pipeline, project: project)
create(:ci_build, :test_reports, :failed, pipeline: pipeline, project: project)
end
context 'when feature flag is enabled and pipeline ref is the default branch' do
it { is_expected.to eq(true) }
end
context 'when feature flag is disabled' do
before do
stub_feature_flags(test_failure_history: false)
end
it { is_expected.to eq(false) }
end
context 'when pipeline is not equal to the project default branch' do
before do
pipeline.update_column(:ref, 'some-other-branch')
end
it { is_expected.to eq(false) }
end
context 'when total number of builds with failed tests exceeds the max number of trackable failures' do
before do
stub_const("#{described_class.name}::MAX_TRACKABLE_FAILURES", 1)
end
it { is_expected.to eq(false) }
end
end
describe '#async' do
let(:pipeline) { double(id: 1) }
let(:service) { described_class.new(pipeline) }
context 'when service should track failures' do
before do
allow(service).to receive(:should_track_failures?).and_return(true)
end
it 'enqueues the worker when #perform_if_needed is called' do
expect(Ci::TestFailureHistoryWorker).to receive(:perform_async).with(pipeline.id)
service.async.perform_if_needed
end
end
context 'when service should not track failures' do
before do
allow(service).to receive(:should_track_failures?).and_return(false)
end
it 'does not enqueue the worker when #perform_if_needed is called' do
expect(Ci::TestFailureHistoryWorker).not_to receive(:perform_async)
service.async.perform_if_needed
end
end
end
end
|