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
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe 'Jobs/Deploy.gitlab-ci.yml' do
include Ci::TemplateHelpers
subject(:template) do
<<~YAML
stages:
- test
- review
- staging
- canary
- production
- incremental rollout 10%
- incremental rollout 25%
- incremental rollout 50%
- incremental rollout 100%
- cleanup
include:
- template: Jobs/Deploy.gitlab-ci.yml
placeholder:
script:
- echo "Ensure at least one job to keep pipeline validator happy"
YAML
end
describe 'AUTO_DEPLOY_IMAGE_VERSION' do
it 'corresponds to a published image in the registry' do
template = Gitlab::Template::GitlabCiYmlTemplate.find('Jobs/Deploy')
registry = "https://#{template_registry_host}"
repository = "gitlab-org/cluster-integration/auto-deploy-image"
reference = YAML.safe_load(template.content, aliases: true).dig('variables', 'AUTO_DEPLOY_IMAGE_VERSION')
expect(public_image_exist?(registry, repository, reference)).to be true
end
end
describe 'the created pipeline' do
let_it_be(:project, refind: true) { create(:project, :repository) }
let(:user) { project.first_owner }
let(:default_branch) { 'master' }
let(:pipeline_ref) { default_branch }
let(:service) { Ci::CreatePipelineService.new(project, user, ref: pipeline_ref) }
let(:pipeline) { service.execute(:push).payload }
let(:build_names) { pipeline.builds.pluck(:name) }
before do
stub_ci_pipeline_yaml_file(template)
allow_any_instance_of(Ci::BuildScheduleWorker).to receive(:perform).and_return(true)
allow(project).to receive(:default_branch).and_return(default_branch)
end
context 'with no cluster or agent' do
it 'does not create any kubernetes deployment jobs' do
expect(build_names).to eq %w[placeholder]
end
end
context 'with only a disabled cluster' do
before do
create(:cluster, :project, :provided_by_gcp, enabled: false, projects: [project])
end
it 'does not create any kubernetes deployment jobs' do
expect(build_names).to eq %w[placeholder]
end
end
shared_examples_for 'pipeline with deployment jobs' do
context 'on master' do
it 'by default' do
expect(build_names).to include('production')
expect(build_names).not_to include('review')
end
it 'when CI_DEPLOY_FREEZE is present' do
create(:ci_variable, project: project, key: 'CI_DEPLOY_FREEZE', value: 'true')
expect(build_names).to eq %w[placeholder]
end
it 'when CANARY_ENABLED' do
create(:ci_variable, project: project, key: 'CANARY_ENABLED', value: 'true')
expect(build_names).to include('production_manual')
expect(build_names).to include('canary')
expect(build_names).not_to include('production')
end
it 'when STAGING_ENABLED' do
create(:ci_variable, project: project, key: 'STAGING_ENABLED', value: 'true')
expect(build_names).to include('production_manual')
expect(build_names).to include('staging')
expect(build_names).not_to include('production')
end
it 'when INCREMENTAL_ROLLOUT_MODE == timed' do
create(:ci_variable, project: project, key: 'INCREMENTAL_ROLLOUT_ENABLED', value: 'true')
create(:ci_variable, project: project, key: 'INCREMENTAL_ROLLOUT_MODE', value: 'timed')
expect(build_names).not_to include('production_manual')
expect(build_names).not_to include('production')
expect(build_names).not_to include(
'rollout 10%',
'rollout 25%',
'rollout 50%',
'rollout 100%'
)
expect(build_names).to include(
'timed rollout 10%',
'timed rollout 25%',
'timed rollout 50%',
'timed rollout 100%'
)
end
it 'when INCREMENTAL_ROLLOUT_ENABLED' do
create(:ci_variable, project: project, key: 'INCREMENTAL_ROLLOUT_ENABLED', value: 'true')
expect(build_names).not_to include('production_manual')
expect(build_names).not_to include('production')
expect(build_names).not_to include(
'timed rollout 10%',
'timed rollout 25%',
'timed rollout 50%',
'timed rollout 100%'
)
expect(build_names).to include(
'rollout 10%',
'rollout 25%',
'rollout 50%',
'rollout 100%'
)
end
it 'when INCREMENTAL_ROLLOUT_MODE == manual' do
create(:ci_variable, project: project, key: 'INCREMENTAL_ROLLOUT_MODE', value: 'manual')
expect(build_names).not_to include('production_manual')
expect(build_names).not_to include('production')
expect(build_names).not_to include(
'timed rollout 10%',
'timed rollout 25%',
'timed rollout 50%',
'timed rollout 100%'
)
expect(build_names).to include(
'rollout 10%',
'rollout 25%',
'rollout 50%',
'rollout 100%'
)
end
end
shared_examples_for 'review app deployment' do
it 'creates the review and stop_review jobs but no production jobs' do
expect(build_names).to include('review')
expect(build_names).to include('stop_review')
expect(build_names).not_to include('production')
expect(build_names).not_to include('production_manual')
expect(build_names).not_to include('staging')
expect(build_names).not_to include('canary')
expect(build_names).not_to include('timed rollout 10%')
expect(build_names).not_to include('timed rollout 25%')
expect(build_names).not_to include('timed rollout 50%')
expect(build_names).not_to include('timed rollout 100%')
expect(build_names).not_to include('rollout 10%')
expect(build_names).not_to include('rollout 25%')
expect(build_names).not_to include('rollout 50%')
expect(build_names).not_to include('rollout 100%')
end
it 'does not include review when REVIEW_DISABLED' do
create(:ci_variable, project: project, key: 'REVIEW_DISABLED', value: 'true')
expect(build_names).not_to include('review')
expect(build_names).not_to include('stop_review')
end
end
context 'on branch' do
let(:pipeline_ref) { 'feature' }
before do
allow_any_instance_of(Gitlab::Ci::Pipeline::Chain::Validate::Repository).to receive(:perform!).and_return(true)
end
it_behaves_like 'review app deployment'
context 'when INCREMENTAL_ROLLOUT_ENABLED' do
before do
create(:ci_variable, project: project, key: 'INCREMENTAL_ROLLOUT_ENABLED', value: 'true')
end
it_behaves_like 'review app deployment'
end
context 'when INCREMENTAL_ROLLOUT_MODE == "timed"' do
before do
create(:ci_variable, project: project, key: 'INCREMENTAL_ROLLOUT_MODE', value: 'timed')
end
it_behaves_like 'review app deployment'
end
context 'when INCREMENTAL_ROLLOUT_MODE == "manual"' do
before do
create(:ci_variable, project: project, key: 'INCREMENTAL_ROLLOUT_MODE', value: 'manual')
end
it_behaves_like 'review app deployment'
end
end
context 'on tag' do
let(:pipeline_ref) { 'v1.0.0' }
it_behaves_like 'review app deployment'
end
context 'on merge request' do
let(:service) { MergeRequests::CreatePipelineService.new(project: project, current_user: user) }
let(:merge_request) { create(:merge_request, :simple, source_project: project) }
let(:pipeline) { service.execute(merge_request).payload }
it 'has no jobs' do
expect(pipeline).to be_merge_request_event
expect(build_names).to be_empty
end
end
end
context 'with an agent' do
before do
create(:cluster_agent, project: project)
end
it_behaves_like 'pipeline with deployment jobs'
end
context 'with a cluster' do
before do
create(:cluster, :project, :provided_by_gcp, projects: [project])
end
it_behaves_like 'pipeline with deployment jobs'
end
end
end
|