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
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Ci::BuildDependencies do
let_it_be(:user) { create(:user) }
let_it_be(:project, reload: true) { create(:project, :repository) }
let_it_be(:pipeline, reload: true) do
create(:ci_pipeline, project: project,
sha: project.commit.id,
ref: project.default_branch,
status: 'success')
end
let!(:build) { create(:ci_build, pipeline: pipeline, name: 'build', stage_idx: 0, stage: 'build') }
let!(:rspec_test) { create(:ci_build, pipeline: pipeline, name: 'rspec', stage_idx: 1, stage: 'test') }
let!(:rubocop_test) { create(:ci_build, pipeline: pipeline, name: 'rubocop', stage_idx: 1, stage: 'test') }
let!(:staging) { create(:ci_build, pipeline: pipeline, name: 'staging', stage_idx: 2, stage: 'deploy') }
describe '#local' do
subject { described_class.new(job).local }
describe 'jobs from previous stages' do
context 'when job is in the first stage' do
let(:job) { build }
it { is_expected.to be_empty }
end
context 'when job is in the second stage' do
let(:job) { rspec_test }
it 'contains all jobs from the first stage' do
is_expected.to contain_exactly(build)
end
end
context 'when job is in the last stage' do
let(:job) { staging }
it 'contains all jobs from all previous stages' do
is_expected.to contain_exactly(build, rspec_test, rubocop_test)
end
context 'when a job is retried' do
before do
project.add_developer(user)
end
let(:retried_job) { Ci::Build.retry(rspec_test, user) }
it 'contains the retried job instead of the original one' do
is_expected.to contain_exactly(build, retried_job, rubocop_test)
end
end
end
end
describe 'jobs from specified dependencies' do
let(:dependencies) { }
let(:needs) { }
let!(:job) do
scheduling_type = needs.present? ? :dag : :stage
create(:ci_build,
pipeline: pipeline,
name: 'final',
scheduling_type: scheduling_type,
stage_idx: 3,
stage: 'deploy',
options: { dependencies: dependencies }
)
end
before do
needs.to_a.each do |need|
create(:ci_build_need, build: job, **need)
end
end
context 'when dependencies are defined' do
let(:dependencies) { %w(rspec staging) }
it { is_expected.to contain_exactly(rspec_test, staging) }
end
context 'when needs are defined' do
let(:needs) do
[
{ name: 'build', artifacts: true },
{ name: 'rspec', artifacts: true },
{ name: 'staging', artifacts: true }
]
end
it { is_expected.to contain_exactly(build, rspec_test, staging) }
end
context 'when need artifacts are defined' do
let(:needs) do
[
{ name: 'build', artifacts: true },
{ name: 'rspec', artifacts: false },
{ name: 'staging', artifacts: true }
]
end
it { is_expected.to contain_exactly(build, staging) }
end
context 'when needs and dependencies are defined' do
let(:dependencies) { %w(rspec staging) }
let(:needs) do
[
{ name: 'build', artifacts: true },
{ name: 'rspec', artifacts: true },
{ name: 'staging', artifacts: true }
]
end
it { is_expected.to contain_exactly(rspec_test, staging) }
end
context 'when needs and dependencies contradict' do
let(:dependencies) { %w(rspec staging) }
let(:needs) do
[
{ name: 'build', artifacts: true },
{ name: 'rspec', artifacts: false },
{ name: 'staging', artifacts: true }
]
end
it 'returns only the intersection' do
is_expected.to contain_exactly(staging)
end
end
context 'when nor dependencies or needs are defined' do
it 'returns the jobs from previous stages' do
is_expected.to contain_exactly(build, rspec_test, rubocop_test, staging)
end
end
end
end
describe '#cross_pipeline' do
let!(:job) do
create(:ci_build,
pipeline: pipeline,
name: 'build_with_pipeline_dependency',
options: { cross_dependencies: dependencies })
end
subject { described_class.new(job) }
let(:cross_pipeline_deps) { subject.cross_pipeline }
context 'when dependency specifications are valid' do
context 'when pipeline exists in the hierarchy' do
let!(:pipeline) { create(:ci_pipeline, child_of: parent_pipeline) }
let!(:parent_pipeline) { create(:ci_pipeline, project: project) }
context 'when job exists' do
let(:dependencies) do
[{ pipeline: parent_pipeline.id.to_s, job: upstream_job.name, artifacts: true }]
end
let!(:upstream_job) { create(:ci_build, :success, pipeline: parent_pipeline) }
it { expect(cross_pipeline_deps).to contain_exactly(upstream_job) }
it { is_expected.to be_valid }
context 'when pipeline and job are specified via variables' do
let(:dependencies) do
[{ pipeline: '$parent_pipeline_ID', job: '$UPSTREAM_JOB', artifacts: true }]
end
before do
job.yaml_variables.push(key: 'parent_pipeline_ID', value: parent_pipeline.id.to_s, public: true)
job.yaml_variables.push(key: 'UPSTREAM_JOB', value: upstream_job.name, public: true)
job.save!
end
it { expect(cross_pipeline_deps).to contain_exactly(upstream_job) }
it { is_expected.to be_valid }
end
context 'when feature flag `ci_cross_pipeline_artifacts_download` is disabled' do
before do
stub_feature_flags(ci_cross_pipeline_artifacts_download: false)
end
it { expect(cross_pipeline_deps).to be_empty }
it { is_expected.to be_valid }
end
end
context 'when same job names exist in other pipelines in the hierarchy' do
let(:cross_pipeline_limit) do
::Gitlab::Ci::Config::Entry::Needs::NEEDS_CROSS_PIPELINE_DEPENDENCIES_LIMIT
end
let(:sibling_pipeline) { create(:ci_pipeline, child_of: parent_pipeline) }
before do
cross_pipeline_limit.times do |index|
create(:ci_build, :success,
pipeline: parent_pipeline, name: "dependency-#{index}",
stage_idx: 1, stage: 'build', user: user
)
create(:ci_build, :success,
pipeline: sibling_pipeline, name: "dependency-#{index}",
stage_idx: 1, stage: 'build', user: user
)
end
end
let(:dependencies) do
[
{ pipeline: parent_pipeline.id.to_s, job: 'dependency-0', artifacts: true },
{ pipeline: parent_pipeline.id.to_s, job: 'dependency-1', artifacts: true },
{ pipeline: parent_pipeline.id.to_s, job: 'dependency-2', artifacts: true },
{ pipeline: sibling_pipeline.id.to_s, job: 'dependency-3', artifacts: true },
{ pipeline: sibling_pipeline.id.to_s, job: 'dependency-4', artifacts: true },
{ pipeline: sibling_pipeline.id.to_s, job: 'dependency-5', artifacts: true }
]
end
it 'returns a limited number of dependencies with the right match' do
expect(job.options[:cross_dependencies].size).to eq(cross_pipeline_limit.next)
expect(cross_pipeline_deps.size).to eq(cross_pipeline_limit)
expect(cross_pipeline_deps.map { |dep| [dep.pipeline_id, dep.name] }).to contain_exactly(
[parent_pipeline.id, 'dependency-0'],
[parent_pipeline.id, 'dependency-1'],
[parent_pipeline.id, 'dependency-2'],
[sibling_pipeline.id, 'dependency-3'],
[sibling_pipeline.id, 'dependency-4'])
end
end
context 'when job does not exist' do
let(:dependencies) do
[{ pipeline: parent_pipeline.id.to_s, job: 'non-existent', artifacts: true }]
end
it { expect(cross_pipeline_deps).to be_empty }
it { is_expected.not_to be_valid }
end
end
context 'when pipeline does not exist' do
let(:dependencies) do
[{ pipeline: '123', job: 'non-existent', artifacts: true }]
end
it { expect(cross_pipeline_deps).to be_empty }
it { is_expected.not_to be_valid }
end
context 'when jobs exist in different pipelines in the hierarchy' do
let!(:pipeline) { create(:ci_pipeline, child_of: parent_pipeline) }
let!(:parent_pipeline) { create(:ci_pipeline, project: project) }
let!(:parent_job) { create(:ci_build, :success, name: 'parent_job', pipeline: parent_pipeline) }
let!(:sibling_pipeline) { create(:ci_pipeline, child_of: parent_pipeline) }
let!(:sibling_job) { create(:ci_build, :success, name: 'sibling_job', pipeline: sibling_pipeline) }
context 'when pipeline and jobs dependencies are mismatched' do
let(:dependencies) do
[
{ pipeline: parent_pipeline.id.to_s, job: sibling_job.name, artifacts: true },
{ pipeline: sibling_pipeline.id.to_s, job: parent_job.name, artifacts: true }
]
end
it { expect(cross_pipeline_deps).to be_empty }
it { is_expected.not_to be_valid }
context 'when dependencies contain a valid pair' do
let(:dependencies) do
[
{ pipeline: parent_pipeline.id.to_s, job: sibling_job.name, artifacts: true },
{ pipeline: sibling_pipeline.id.to_s, job: parent_job.name, artifacts: true },
{ pipeline: sibling_pipeline.id.to_s, job: sibling_job.name, artifacts: true }
]
end
it 'filters out the invalid ones' do
expect(cross_pipeline_deps).to contain_exactly(sibling_job)
end
it { is_expected.not_to be_valid }
end
end
end
context 'when job and pipeline exist outside the hierarchy' do
let!(:pipeline) { create(:ci_pipeline, project: project) }
let!(:another_pipeline) { create(:ci_pipeline, project: project) }
let!(:dependency) { create(:ci_build, :success, pipeline: another_pipeline) }
let(:dependencies) do
[{ pipeline: another_pipeline.id.to_s, job: dependency.name, artifacts: true }]
end
it 'ignores jobs outside the pipeline hierarchy' do
expect(cross_pipeline_deps).to be_empty
end
it { is_expected.not_to be_valid }
end
context 'when current pipeline is specified' do
let!(:pipeline) { create(:ci_pipeline, project: project) }
let!(:dependency) { create(:ci_build, :success, pipeline: pipeline) }
let(:dependencies) do
[{ pipeline: pipeline.id.to_s, job: dependency.name, artifacts: true }]
end
it 'ignores jobs from the current pipeline as simple needs should be used instead' do
expect(cross_pipeline_deps).to be_empty
end
it { is_expected.not_to be_valid }
end
end
context 'when artifacts:false' do
let!(:pipeline) { create(:ci_pipeline, child_of: parent_pipeline) }
let!(:parent_pipeline) { create(:ci_pipeline, project: project) }
let!(:parent_job) { create(:ci_build, :success, name: 'parent_job', pipeline: parent_pipeline) }
let(:dependencies) do
[{ pipeline: parent_pipeline.id.to_s, job: parent_job.name, artifacts: false }]
end
it { expect(cross_pipeline_deps).to be_empty }
it { is_expected.to be_valid } # we simply ignore it
end
end
describe '#all' do
let!(:job) do
create(:ci_build, pipeline: pipeline, name: 'deploy', stage_idx: 3, stage: 'deploy')
end
let(:dependencies) { described_class.new(job) }
subject { dependencies.all }
it 'returns the union of all local dependencies and any cross project dependencies' do
expect(dependencies).to receive(:local).and_return([1, 2, 3])
expect(dependencies).to receive(:cross_project).and_return([3, 4])
expect(subject).to contain_exactly(1, 2, 3, 4)
end
end
end
|