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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
|
# frozen_string_literal: true
# rubocop:disable Layout/LineLength
# rubocop:disable RSpec/ContextWording
RSpec.shared_examples 'a deployable job' do
it { is_expected.to have_one(:deployment) }
shared_examples 'calling proper BuildFinishedWorker' do
it 'calls Ci::BuildFinishedWorker' do
expect(Ci::BuildFinishedWorker).to receive(:perform_async)
subject
end
end
describe '#outdated_deployment?' do
subject { build.outdated_deployment? }
let(:build) { create(:ci_build, :created, :with_deployment, pipeline: pipeline, environment: 'production') }
context 'when build has no environment' do
let(:build) { create(:ci_build, :created, pipeline: pipeline, environment: nil) }
it { expect(subject).to be_falsey }
end
context 'when project has forward deployment disabled' do
before do
project.ci_cd_settings.update!(forward_deployment_enabled: false)
end
it { expect(subject).to be_falsey }
end
context 'when build is not an outdated deployment' do
before do
allow(build.deployment).to receive(:older_than_last_successful_deployment?).and_return(false)
end
it { expect(subject).to be_falsey }
end
context 'when build is older than the latest deployment and still pending status' do
before do
allow(build.deployment).to receive(:older_than_last_successful_deployment?).and_return(true)
end
it { expect(subject).to be_truthy }
end
context 'when build is older than the latest deployment but succeeded once' do
let(:build) { create(:ci_build, :success, :with_deployment, pipeline: pipeline, environment: 'production') }
before do
allow(build.deployment).to receive(:older_than_last_successful_deployment?).and_return(true)
end
it 'returns false for allowing rollback' do
expect(subject).to be_falsey
end
context 'when forward_deployment_rollback_allowed option is disabled' do
before do
project.ci_cd_settings.update!(forward_deployment_rollback_allowed: false)
end
it 'returns true for disallowing rollback' do
expect(subject).to eq(true)
end
end
end
end
describe 'state transition as a deployable' do
subject { build.send(event) }
let!(:build) { create(:ci_build, :with_deployment, :start_review_app, pipeline: pipeline) }
let(:deployment) { build.deployment }
let(:environment) { deployment.environment }
before do
allow(Deployments::LinkMergeRequestWorker).to receive(:perform_async)
allow(Deployments::HooksWorker).to receive(:perform_async)
end
it 'has deployments record with created status' do
expect(deployment).to be_created
expect(environment.name).to eq('review/master')
end
shared_examples_for 'avoid deadlock' do
it 'executes UPDATE in the right order' do
recorded = with_cross_database_modification_prevented do
ActiveRecord::QueryRecorder.new { subject }
end
index_for_build = recorded.log.index { |l| l.include?("UPDATE #{Ci::Build.quoted_table_name}") }
index_for_deployment = recorded.log.index { |l| l.include?("UPDATE \"deployments\"") }
expect(index_for_build).to be < index_for_deployment
end
end
context 'when transits to running' do
let(:event) { :run! }
it_behaves_like 'avoid deadlock'
it 'transits deployment status to running' do
with_cross_database_modification_prevented do
subject
end
expect(deployment).to be_running
end
context 'when deployment is already running state' do
before do
build.deployment.success!
end
it 'does not change deployment status and tracks an error' do
expect(Gitlab::ErrorTracking)
.to receive(:track_exception).with(
instance_of(Deployment::StatusSyncError), deployment_id: deployment.id, build_id: build.id)
with_cross_database_modification_prevented do
expect { subject }.not_to change { deployment.reload.status }
end
end
end
end
context 'when transits to success' do
let(:event) { :success! }
before do
allow(Deployments::UpdateEnvironmentWorker).to receive(:perform_async)
allow(Deployments::HooksWorker).to receive(:perform_async)
end
it_behaves_like 'avoid deadlock'
it_behaves_like 'calling proper BuildFinishedWorker'
it 'transits deployment status to success' do
with_cross_database_modification_prevented do
subject
end
expect(deployment).to be_success
end
end
context 'when transits to failed' do
let(:event) { :drop! }
it_behaves_like 'avoid deadlock'
it_behaves_like 'calling proper BuildFinishedWorker'
it 'transits deployment status to failed' do
with_cross_database_modification_prevented do
subject
end
expect(deployment).to be_failed
end
end
context 'when transits to skipped' do
let(:event) { :skip! }
it_behaves_like 'avoid deadlock'
it 'transits deployment status to skipped' do
with_cross_database_modification_prevented do
subject
end
expect(deployment).to be_skipped
end
end
context 'when transits to canceled' do
let(:event) { :cancel! }
it_behaves_like 'avoid deadlock'
it_behaves_like 'calling proper BuildFinishedWorker'
it 'transits deployment status to canceled' do
with_cross_database_modification_prevented do
subject
end
expect(deployment).to be_canceled
end
end
# Mimic playing a manual job that needs another job.
# `needs + when:manual` scenario, see: https://gitlab.com/gitlab-org/gitlab/-/issues/347502
context 'when transits from skipped to created to running' do
before do
build.skip!
end
context 'during skipped to created' do
let(:event) { :process! }
it 'transitions to created' do
subject
expect(deployment).to be_created
end
end
context 'during created to running' do
let(:event) { :run! }
before do
build.process!
build.enqueue!
end
it 'transitions to running and calls webhook' do
freeze_time do
expect(Deployments::HooksWorker)
.to receive(:perform_async).with(hash_including({ 'deployment_id' => deployment.id, 'status' => 'running', 'status_changed_at' => Time.current.to_s }))
subject
end
expect(deployment).to be_running
end
end
end
end
describe '#on_stop' do
subject { build.on_stop }
context 'when a job has a specification that it can be stopped from the other job' do
let(:build) { create(:ci_build, :start_review_app, pipeline: pipeline) }
it 'returns the other job name' do
is_expected.to eq('stop_review_app')
end
end
context 'when a job does not have environment information' do
let(:build) { create(:ci_build, pipeline: pipeline) }
it 'returns nil' do
is_expected.to be_nil
end
end
end
describe '#environment_tier_from_options' do
subject { build.environment_tier_from_options }
let(:build) { Ci::Build.new(options: options) }
let(:options) { { environment: { deployment_tier: 'production' } } }
it { is_expected.to eq('production') }
context 'when options does not include deployment_tier' do
let(:options) { { environment: { name: 'production' } } }
it { is_expected.to be_nil }
end
end
describe '#environment_tier' do
subject { build.environment_tier }
let(:options) { { environment: { deployment_tier: 'production' } } }
let!(:environment) { create(:environment, name: 'production', tier: 'development', project: project) }
let(:build) { Ci::Build.new(options: options, environment: 'production', project: project) }
it { is_expected.to eq('production') }
context 'when options does not include deployment_tier' do
let(:options) { { environment: { name: 'production' } } }
it 'uses tier from environment' do
is_expected.to eq('development')
end
context 'when persisted environment is absent' do
let(:environment) { nil }
it { is_expected.to be_nil }
end
end
end
describe 'environment' do
describe '#has_environment_keyword?' do
subject { build.has_environment_keyword? }
context 'when environment is defined' do
before do
build.update!(environment: 'review')
end
it { is_expected.to be_truthy }
end
context 'when environment is not defined' do
before do
build.update!(environment: nil)
end
it { is_expected.to be_falsey }
end
end
describe '#expanded_environment_name' do
subject { build.expanded_environment_name }
context 'when environment uses $CI_COMMIT_REF_NAME' do
let(:build) do
create(
:ci_build,
ref: 'master',
environment: 'review/$CI_COMMIT_REF_NAME',
pipeline: pipeline
)
end
it { is_expected.to eq('review/master') }
end
context 'when environment uses yaml_variables containing symbol keys' do
let(:build) do
create(
:ci_build,
yaml_variables: [{ key: :APP_HOST, value: 'host' }],
environment: 'review/$APP_HOST',
pipeline: pipeline
)
end
it 'returns an expanded environment name with a list of variables' do
is_expected.to eq('review/host')
end
context 'when build metadata has already persisted the expanded environment name' do
before do
build.metadata.expanded_environment_name = 'review/foo'
end
it 'returns a persisted expanded environment name without a list of variables' do
expect(build).not_to receive(:simple_variables)
is_expected.to eq('review/foo')
end
end
end
context 'when using persisted variables' do
let(:build) do
create(:ci_build, environment: 'review/x$CI_JOB_ID', pipeline: pipeline)
end
it { is_expected.to eq('review/x') }
end
context 'when environment name uses a nested variable' do
let(:yaml_variables) do
[
{ key: 'ENVIRONMENT_NAME', value: '${CI_COMMIT_REF_NAME}' }
]
end
let(:build) do
create(
:ci_build,
ref: 'master',
yaml_variables: yaml_variables,
environment: 'review/$ENVIRONMENT_NAME',
pipeline: pipeline
)
end
it { is_expected.to eq('review/master') }
end
end
describe '#expanded_kubernetes_namespace' do
let(:build) { create(:ci_build, environment: environment, options: options, pipeline: pipeline) }
subject { build.expanded_kubernetes_namespace }
context 'environment and namespace are not set' do
let(:environment) { nil }
let(:options) { nil }
it { is_expected.to be_nil }
end
context 'environment is specified' do
let(:environment) { 'production' }
context 'namespace is not set' do
let(:options) { nil }
it { is_expected.to be_nil }
end
context 'namespace is provided' do
let(:options) do
{
environment: {
name: environment,
kubernetes: {
namespace: namespace
}
}
}
end
context 'with a static value' do
let(:namespace) { 'production' }
it { is_expected.to eq namespace }
end
context 'with a dynamic value' do
let(:namespace) { 'deploy-$CI_COMMIT_REF_NAME' }
it { is_expected.to eq 'deploy-master' }
end
end
end
end
describe '#deployment_job?' do
subject { build.deployment_job? }
context 'when environment is defined' do
before do
build.update!(environment: 'review')
end
context 'no action is defined' do
it { is_expected.to be_truthy }
end
context 'and start action is defined' do
before do
build.update!(options: { environment: { action: 'start' } })
end
it { is_expected.to be_truthy }
end
end
context 'when environment is not defined' do
before do
build.update!(environment: nil)
end
it { is_expected.to be_falsey }
end
end
describe '#stops_environment?' do
subject { build.stops_environment? }
context 'when environment is defined' do
before do
build.update!(environment: 'review')
end
context 'no action is defined' do
it { is_expected.to be_falsey }
end
context 'and stop action is defined' do
before do
build.update!(options: { environment: { action: 'stop' } })
end
it { is_expected.to be_truthy }
end
end
context 'when environment is not defined' do
before do
build.update!(environment: nil)
end
it { is_expected.to be_falsey }
end
end
end
describe '#persisted_environment' do
let!(:environment) do
create(:environment, project: project, name: "foo-#{project.default_branch}")
end
subject { build.persisted_environment }
context 'when referenced literally' do
let(:build) do
create(:ci_build, pipeline: pipeline, environment: "foo-#{project.default_branch}")
end
it { is_expected.to eq(environment) }
end
context 'when referenced with a variable' do
let(:build) do
create(:ci_build, pipeline: pipeline, environment: "foo-$CI_COMMIT_REF_NAME")
end
it { is_expected.to eq(environment) }
end
context 'when there is no environment' do
it { is_expected.to be_nil }
end
context 'when build has a stop environment' do
let(:build) { create(:ci_build, :stop_review_app, pipeline: pipeline, environment: "foo-#{project.default_branch}") }
it 'expands environment name' do
expect(build).to receive(:expanded_environment_name).and_call_original
is_expected.to eq(environment)
end
end
end
describe '#deployment_status' do
before do
allow_any_instance_of(Ci::Build).to receive(:create_deployment) # rubocop:disable RSpec/AnyInstanceOf
end
context 'when build is a last deployment' do
let(:build) { create(:ci_build, :success, environment: 'production', pipeline: pipeline) }
let(:environment) { create(:environment, name: 'production', project: build.project) }
let!(:deployment) { create(:deployment, :success, environment: environment, project: environment.project, deployable: build) }
it { expect(build.deployment_status).to eq(:last) }
end
context 'when there is a newer build with deployment' do
let(:build) { create(:ci_build, :success, environment: 'production', pipeline: pipeline) }
let(:environment) { create(:environment, name: 'production', project: build.project) }
let!(:deployment) { create(:deployment, :success, environment: environment, project: environment.project, deployable: build) }
let!(:last_deployment) { create(:deployment, :success, environment: environment, project: environment.project) }
it { expect(build.deployment_status).to eq(:out_of_date) }
end
context 'when build with deployment has failed' do
let(:build) { create(:ci_build, :failed, environment: 'production', pipeline: pipeline) }
let(:environment) { create(:environment, name: 'production', project: build.project) }
let!(:deployment) { create(:deployment, :success, environment: environment, project: environment.project, deployable: build) }
it { expect(build.deployment_status).to eq(:failed) }
end
context 'when build with deployment is running' do
let(:build) { create(:ci_build, environment: 'production', pipeline: pipeline) }
let(:environment) { create(:environment, name: 'production', project: build.project) }
let!(:deployment) { create(:deployment, :success, environment: environment, project: environment.project, deployable: build) }
it { expect(build.deployment_status).to eq(:creating) }
end
end
end
# rubocop:enable Layout/LineLength
# rubocop:enable RSpec/ContextWording
|