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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
|
# frozen_string_literal: true
require 'spec_helper'
describe Ci::RetryPipelineService, '#execute' do
include ProjectForksHelper
let(:user) { create(:user) }
let(:project) { create(:project) }
let(:pipeline) { create(:ci_pipeline, project: project) }
let(:service) { described_class.new(project, user) }
context 'when user has full ability to modify pipeline' do
before do
project.add_developer(user)
create(:protected_branch, :developers_can_merge,
name: pipeline.ref, project: project)
end
context 'when there are already retried jobs present' do
before do
create_build('rspec', :canceled, 0, retried: true)
create_build('rspec', :failed, 0)
end
it 'does not retry jobs that has already been retried' do
expect(statuses.first).to be_retried
expect { service.execute(pipeline) }
.to change { CommitStatus.count }.by(1)
end
end
context 'when there are failed builds in the last stage' do
before do
create_build('rspec 1', :success, 0)
create_build('rspec 2', :failed, 1)
create_build('rspec 3', :canceled, 1)
end
it 'enqueues all builds in the last stage' do
service.execute(pipeline)
expect(build('rspec 2')).to be_pending
expect(build('rspec 3')).to be_pending
expect(pipeline.reload).to be_running
end
end
context 'when there are failed or canceled builds in the first stage' do
before do
create_build('rspec 1', :failed, 0)
create_build('rspec 2', :canceled, 0)
create_build('rspec 3', :canceled, 1)
create_build('spinach 1', :canceled, 2)
end
it 'retries builds failed builds and marks subsequent for processing' do
service.execute(pipeline)
expect(build('rspec 1')).to be_pending
expect(build('rspec 2')).to be_pending
expect(build('rspec 3')).to be_created
expect(build('spinach 1')).to be_created
expect(pipeline.reload).to be_running
end
end
context 'when there is failed build present which was run on failure' do
before do
create_build('rspec 1', :failed, 0)
create_build('rspec 2', :canceled, 0)
create_build('rspec 3', :canceled, 1)
create_build('report 1', :failed, 2)
end
it 'retries builds only in the first stage' do
service.execute(pipeline)
expect(build('rspec 1')).to be_pending
expect(build('rspec 2')).to be_pending
expect(build('rspec 3')).to be_created
expect(build('report 1')).to be_created
expect(pipeline.reload).to be_running
end
it 'creates a new job for report job in this case' do
service.execute(pipeline)
expect(statuses.where(name: 'report 1').first).to be_retried
end
end
context 'when there is a failed test in a DAG' do
before do
create_build('build', :success, 0)
create_build('build2', :success, 0)
test_build = create_build('test', :failed, 1)
create(:ci_build_need, build: test_build, name: 'build')
create(:ci_build_need, build: test_build, name: 'build2')
end
it 'retries the test' do
service.execute(pipeline)
expect(build('build')).to be_success
expect(build('build2')).to be_success
expect(build('test')).to be_pending
expect(build('test').needs.map(&:name)).to match_array(%w(build build2))
end
end
context 'when the last stage was skipepd' do
before do
create_build('build 1', :success, 0)
create_build('test 2', :failed, 1)
create_build('report 3', :skipped, 2)
create_build('report 4', :skipped, 2)
end
it 'retries builds only in the first stage' do
service.execute(pipeline)
expect(build('build 1')).to be_success
expect(build('test 2')).to be_pending
expect(build('report 3')).to be_created
expect(build('report 4')).to be_created
expect(pipeline.reload).to be_running
end
end
context 'when pipeline contains manual actions' do
context 'when there are optional manual actions only' do
context 'when there is a canceled manual action in first stage' do
before do
create_build('rspec 1', :failed, 0)
create_build('staging', :canceled, 0, when: :manual, allow_failure: true)
create_build('rspec 2', :canceled, 1)
end
it 'retries failed builds and marks subsequent for processing' do
service.execute(pipeline)
expect(build('rspec 1')).to be_pending
expect(build('staging')).to be_manual
expect(build('rspec 2')).to be_created
expect(pipeline.reload).to be_running
end
end
end
context 'when pipeline has blocking manual actions defined' do
context 'when pipeline retry should enqueue builds' do
before do
create_build('test', :failed, 0)
create_build('deploy', :canceled, 0, when: :manual, allow_failure: false)
create_build('verify', :canceled, 1)
end
it 'retries failed builds' do
service.execute(pipeline)
expect(build('test')).to be_pending
expect(build('deploy')).to be_manual
expect(build('verify')).to be_created
expect(pipeline.reload).to be_running
end
end
context 'when pipeline retry should block pipeline immediately' do
before do
create_build('test', :success, 0)
create_build('deploy:1', :success, 1, when: :manual, allow_failure: false)
create_build('deploy:2', :failed, 1, when: :manual, allow_failure: false)
create_build('verify', :canceled, 2)
end
it 'reprocesses blocking manual action and blocks pipeline' do
service.execute(pipeline)
expect(build('deploy:1')).to be_success
expect(build('deploy:2')).to be_manual
expect(build('verify')).to be_created
expect(pipeline.reload).to be_blocked
end
end
end
context 'when there is a skipped manual action in last stage' do
before do
create_build('rspec 1', :canceled, 0)
create_build('rspec 2', :skipped, 0, when: :manual, allow_failure: true)
create_build('staging', :skipped, 1, when: :manual, allow_failure: true)
end
it 'retries canceled job and reprocesses manual actions' do
service.execute(pipeline)
expect(build('rspec 1')).to be_pending
expect(build('rspec 2')).to be_manual
expect(build('staging')).to be_created
expect(pipeline.reload).to be_running
end
end
context 'when there is a created manual action in the last stage' do
before do
create_build('rspec 1', :canceled, 0)
create_build('staging', :created, 1, when: :manual, allow_failure: true)
end
it 'retries canceled job and does not update the manual action' do
service.execute(pipeline)
expect(build('rspec 1')).to be_pending
expect(build('staging')).to be_created
expect(pipeline.reload).to be_running
end
end
context 'when there is a created manual action in the first stage' do
before do
create_build('rspec 1', :canceled, 0)
create_build('staging', :created, 0, when: :manual, allow_failure: true)
end
it 'retries canceled job and processes the manual action' do
service.execute(pipeline)
expect(build('rspec 1')).to be_pending
expect(build('staging')).to be_manual
expect(pipeline.reload).to be_running
end
end
end
it 'closes all todos about failed jobs for pipeline' do
expect(MergeRequests::AddTodoWhenBuildFailsService)
.to receive_message_chain(:new, :close_all)
service.execute(pipeline)
end
it 'reprocesses the pipeline' do
expect_any_instance_of(Ci::ProcessPipelineService).to receive(:execute)
service.execute(pipeline)
end
end
context 'when user is not allowed to retry pipeline' do
it 'raises an error' do
expect { service.execute(pipeline) }
.to raise_error Gitlab::Access::AccessDeniedError
end
end
context 'when user is not allowed to trigger manual action' do
before do
project.add_developer(user)
create(:protected_branch, :maintainers_can_push,
name: pipeline.ref, project: project)
end
context 'when there is a failed manual action present' do
before do
create_build('test', :failed, 0)
create_build('deploy', :failed, 0, when: :manual)
create_build('verify', :canceled, 1)
end
it 'raises an error' do
expect { service.execute(pipeline) }
.to raise_error Gitlab::Access::AccessDeniedError
end
end
context 'when there is a failed manual action in later stage' do
before do
create_build('test', :failed, 0)
create_build('deploy', :failed, 1, when: :manual)
create_build('verify', :canceled, 2)
end
it 'raises an error' do
expect { service.execute(pipeline) }
.to raise_error Gitlab::Access::AccessDeniedError
end
end
end
context 'when maintainer is allowed to push to forked project' do
let(:user) { create(:user) }
let(:project) { create(:project, :public) }
let(:forked_project) { fork_project(project) }
let(:pipeline) { create(:ci_pipeline, project: forked_project, ref: 'fixes') }
before do
project.add_maintainer(user)
create(:merge_request,
source_project: forked_project,
target_project: project,
source_branch: 'fixes',
allow_collaboration: true)
create_build('rspec 1', :failed, 1)
end
it 'allows to retry failed pipeline' do
allow_any_instance_of(Project).to receive(:branch_allows_collaboration?).and_return(true)
allow_any_instance_of(Project).to receive(:empty_repo?).and_return(false)
service.execute(pipeline)
expect(build('rspec 1')).to be_pending
expect(pipeline.reload).to be_running
end
end
def statuses
pipeline.reload.statuses
end
def build(name)
statuses.latest.find_by(name: name)
end
def create_build(name, status, stage_num, **opts)
create(:ci_build, name: name,
status: status,
stage: "stage_#{stage_num}",
stage_idx: stage_num,
pipeline: pipeline, **opts) do |build|
pipeline.update_legacy_status
end
end
end
|