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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Ci::HasStatus, feature_category: :continuous_integration do
describe '.composite_status' do
using RSpec::Parameterized::TableSyntax
subject { CommitStatus.composite_status }
shared_examples 'build status summary' do
context 'all successful' do
let!(:statuses) { Array.new(2) { create(type, status: :success) } }
it { is_expected.to eq 'success' }
end
context 'at least one failed' do
let!(:statuses) do
[create(type, status: :success), create(type, status: :failed)]
end
it { is_expected.to eq 'failed' }
end
context 'at least one running' do
let!(:statuses) do
[create(type, status: :success), create(type, status: :running)]
end
it { is_expected.to eq 'running' }
end
context 'at least one pending' do
let!(:statuses) do
[create(type, status: :success), create(type, status: :pending)]
end
it { is_expected.to eq 'running' }
end
context 'all waiting for resource' do
let!(:statuses) do
[create(type, status: :waiting_for_resource), create(type, status: :waiting_for_resource)]
end
it { is_expected.to eq 'waiting_for_resource' }
end
context 'at least one waiting for resource' do
let!(:statuses) do
[create(type, status: :success), create(type, status: :waiting_for_resource)]
end
it { is_expected.to eq 'waiting_for_resource' }
end
context 'all preparing' do
let!(:statuses) do
[create(type, status: :preparing), create(type, status: :preparing)]
end
it { is_expected.to eq 'preparing' }
end
context 'at least one preparing' do
let!(:statuses) do
[create(type, status: :success), create(type, status: :preparing)]
end
it { is_expected.to eq 'preparing' }
end
context 'success and failed but allowed to fail' do
let!(:statuses) do
[create(type, status: :success),
create(type, status: :failed, allow_failure: true)]
end
it { is_expected.to eq 'success' }
end
context 'one failed but allowed to fail' do
let!(:statuses) do
[create(type, status: :failed, allow_failure: true)]
end
it { is_expected.to eq 'success' }
end
context 'success and canceled' do
let!(:statuses) do
[create(type, status: :success), create(type, status: :canceled)]
end
it { is_expected.to eq 'canceled' }
end
context 'one failed and one canceled' do
let!(:statuses) do
[create(type, status: :failed), create(type, status: :canceled)]
end
it { is_expected.to eq 'failed' }
end
context 'one failed but allowed to fail and one canceled' do
let!(:statuses) do
[create(type, status: :failed, allow_failure: true),
create(type, status: :canceled)]
end
it { is_expected.to eq 'canceled' }
end
context 'one running one canceled' do
let!(:statuses) do
[create(type, status: :running), create(type, status: :canceled)]
end
it { is_expected.to eq 'running' }
end
context 'all canceled' do
let!(:statuses) do
[create(type, status: :canceled), create(type, status: :canceled)]
end
it { is_expected.to eq 'canceled' }
end
context 'success and canceled but allowed to fail' do
let!(:statuses) do
[create(type, status: :success),
create(type, status: :canceled, allow_failure: true)]
end
it { is_expected.to eq 'success' }
end
context 'one finished and second running but allowed to fail' do
let!(:statuses) do
[create(type, status: :success),
create(type, status: :running, allow_failure: true)]
end
it { is_expected.to eq 'running' }
end
context 'when one status finished and second is still created' do
let!(:statuses) do
[create(type, status: :success), create(type, status: :created)]
end
it { is_expected.to eq 'running' }
end
context 'when there is a manual status before created status' do
let!(:statuses) do
[create(type, status: :success),
create(type, status: :manual, allow_failure: false),
create(type, status: :created)]
end
it { is_expected.to eq 'manual' }
end
context 'when one status is a blocking manual action' do
let!(:statuses) do
[create(type, status: :failed),
create(type, status: :manual, allow_failure: false)]
end
it { is_expected.to eq 'manual' }
end
context 'when one status is a non-blocking manual action' do
let!(:statuses) do
[create(type, status: :failed),
create(type, status: :manual, allow_failure: true)]
end
it { is_expected.to eq 'failed' }
end
end
context 'ci build statuses' do
let(:type) { :ci_build }
it_behaves_like 'build status summary'
end
context 'generic commit statuses' do
let(:type) { :generic_commit_status }
it_behaves_like 'build status summary'
end
end
context 'for scope with one status' do
shared_examples 'having a job' do |status|
%i[ci_build generic_commit_status].each do |type|
context "when it's #{status} #{type} job" do
let!(:job) { create(type, status) }
describe ".#{status}" do
it 'contains the job' do
expect(CommitStatus.public_send(status).all)
.to contain_exactly(job)
end
end
describe '.relevant' do
if status == :created
it 'contains nothing' do
expect(CommitStatus.relevant.all).to be_empty
end
else
it 'contains the job' do
expect(CommitStatus.relevant.all).to contain_exactly(job)
end
end
end
end
end
end
%i[created waiting_for_resource preparing running pending success
failed canceled skipped].each do |status|
it_behaves_like 'having a job', status
end
end
context 'for scope with more statuses' do
shared_examples 'containing the job' do |status|
%i[ci_build generic_commit_status].each do |type|
context "when it's #{status} #{type} job" do
let!(:job) { create(type, status) }
it 'contains the job' do
is_expected.to contain_exactly(job)
end
end
end
end
shared_examples 'not containing the job' do |status|
%i[ci_build generic_commit_status].each do |type|
context "when it's #{status} #{type} job" do
let!(:job) { create(type, status) }
it 'contains nothing' do
is_expected.to be_empty
end
end
end
end
describe '.running_or_pending' do
subject { CommitStatus.running_or_pending }
%i[running pending].each do |status|
it_behaves_like 'containing the job', status
end
%i[created failed success].each do |status|
it_behaves_like 'not containing the job', status
end
end
describe '.alive' do
subject { CommitStatus.alive }
%i[running pending waiting_for_resource preparing created].each do |status|
it_behaves_like 'containing the job', status
end
%i[failed success].each do |status|
it_behaves_like 'not containing the job', status
end
end
describe '.alive_or_scheduled' do
subject { CommitStatus.alive_or_scheduled }
%i[running pending waiting_for_resource preparing created scheduled].each do |status|
it_behaves_like 'containing the job', status
end
%i[failed success canceled skipped].each do |status|
it_behaves_like 'not containing the job', status
end
end
describe '.created_or_pending' do
subject { CommitStatus.created_or_pending }
%i[created pending].each do |status|
it_behaves_like 'containing the job', status
end
%i[running failed success].each do |status|
it_behaves_like 'not containing the job', status
end
end
describe '.finished' do
subject { CommitStatus.finished }
%i[success failed canceled].each do |status|
it_behaves_like 'containing the job', status
end
%i[created running pending].each do |status|
it_behaves_like 'not containing the job', status
end
end
describe '.cancelable' do
subject { CommitStatus.cancelable }
%i[running pending waiting_for_resource preparing created scheduled].each do |status|
it_behaves_like 'containing the job', status
end
%i[failed success skipped canceled manual].each do |status|
it_behaves_like 'not containing the job', status
end
end
describe '.manual' do
subject { CommitStatus.manual }
%i[manual].each do |status|
it_behaves_like 'containing the job', status
end
%i[failed success skipped canceled].each do |status|
it_behaves_like 'not containing the job', status
end
end
describe '.scheduled' do
subject { CommitStatus.scheduled }
%i[scheduled].each do |status|
it_behaves_like 'containing the job', status
end
%i[failed success skipped canceled].each do |status|
it_behaves_like 'not containing the job', status
end
end
describe '.complete' do
subject { CommitStatus.complete }
described_class::COMPLETED_STATUSES.each do |status|
it_behaves_like 'containing the job', status
end
described_class::ACTIVE_STATUSES.each do |status|
it_behaves_like 'not containing the job', status
end
end
describe '.waiting_for_resource_or_upcoming' do
subject { CommitStatus.waiting_for_resource_or_upcoming }
%i[created scheduled waiting_for_resource].each do |status|
it_behaves_like 'containing the job', status
end
%i[running failed success canceled].each do |status|
it_behaves_like 'not containing the job', status
end
end
end
describe '::DEFAULT_STATUS' do
it 'is a status created' do
expect(described_class::DEFAULT_STATUS).to eq 'created'
end
end
describe '::BLOCKED_STATUS' do
it 'is a status manual' do
expect(described_class::BLOCKED_STATUS).to eq %w[manual scheduled]
end
end
describe 'blocked?' do
subject { object.blocked? }
%w[ci_pipeline ci_stage ci_build generic_commit_status].each do |type|
context "when #{type}" do
let(:object) { build(type, status: status) }
context 'when status is scheduled' do
let(:status) { :scheduled }
it { is_expected.to be_truthy }
end
context 'when status is manual' do
let(:status) { :manual }
it { is_expected.to be_truthy }
end
context 'when status is created' do
let(:status) { :created }
it { is_expected.to be_falsy }
end
end
end
end
end
|