summaryrefslogtreecommitdiff
path: root/spec/services/ci/process_pipeline_service_spec.rb
blob: ad8c2485888d0cf6955662737bbb710ca5c544d9 (plain)
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
require 'spec_helper'

describe Ci::ProcessPipelineService, services: true do
  let(:pipeline) { create(:ci_pipeline, ref: 'master') }
  let(:user) { create(:user) }
  let(:all_builds) { pipeline.builds }
  let(:builds) { all_builds.where.not(status: [:created, :skipped]) }
  let(:config) { nil }

  before do
    allow(pipeline).to receive(:ci_yaml_file).and_return(config)
  end

  describe '#execute' do
    def create_builds
      described_class.new(pipeline.project, user).execute(pipeline)
    end

    def succeed_pending
      builds.pending.update_all(status: 'success')
    end

    context 'start queuing next builds' do
      before do
        create(:ci_build, :created, pipeline: pipeline, name: 'linux', stage_idx: 0)
        create(:ci_build, :created, pipeline: pipeline, name: 'mac', stage_idx: 0)
        create(:ci_build, :created, pipeline: pipeline, name: 'rspec', stage_idx: 1)
        create(:ci_build, :created, pipeline: pipeline, name: 'rubocop', stage_idx: 1)
        create(:ci_build, :created, pipeline: pipeline, name: 'deploy', stage_idx: 2)
      end

      it 'processes a pipeline' do
        expect(create_builds).to be_truthy
        succeed_pending
        expect(builds.success.count).to eq(2)

        expect(create_builds).to be_truthy
        succeed_pending
        expect(builds.success.count).to eq(4)

        expect(create_builds).to be_truthy
        succeed_pending
        expect(builds.success.count).to eq(5)

        expect(create_builds).to be_falsey
      end

      it 'does not process pipeline if existing stage is running' do
        expect(create_builds).to be_truthy
        expect(builds.pending.count).to eq(2)
        
        expect(create_builds).to be_falsey
        expect(builds.pending.count).to eq(2)
      end
    end

    context 'custom stage with first job allowed to fail' do
      before do
        create(:ci_build, :created, pipeline: pipeline, name: 'clean_job', stage_idx: 0, allow_failure: true)
        create(:ci_build, :created, pipeline: pipeline, name: 'test_job', stage_idx: 1, allow_failure: true)
      end

      it 'automatically triggers a next stage when build finishes' do
        expect(create_builds).to be_truthy
        expect(builds.pluck(:status)).to contain_exactly('pending')

        pipeline.builds.running_or_pending.each(&:drop)
        expect(builds.pluck(:status)).to contain_exactly('failed', 'pending')
      end
    end

    context 'properly creates builds when "when" is defined' do
      before do
        create(:ci_build, :created, pipeline: pipeline, name: 'build', stage_idx: 0)
        create(:ci_build, :created, pipeline: pipeline, name: 'test', stage_idx: 1)
        create(:ci_build, :created, pipeline: pipeline, name: 'test_failure', stage_idx: 2, when: 'on_failure')
        create(:ci_build, :created, pipeline: pipeline, name: 'deploy', stage_idx: 3)
        create(:ci_build, :created, pipeline: pipeline, name: 'production', stage_idx: 3, when: 'manual')
        create(:ci_build, :created, pipeline: pipeline, name: 'cleanup', stage_idx: 4, when: 'always')
        create(:ci_build, :created, pipeline: pipeline, name: 'clear cache', stage_idx: 4, when: 'manual')
      end

      context 'when builds are successful' do
        it 'properly creates builds' do
          expect(create_builds).to be_truthy
          expect(builds.pluck(:name)).to contain_exactly('build')
          expect(builds.pluck(:status)).to contain_exactly('pending')
          pipeline.builds.running_or_pending.each(&:success)

          expect(builds.pluck(:name)).to contain_exactly('build', 'test')
          expect(builds.pluck(:status)).to contain_exactly('success', 'pending')
          pipeline.builds.running_or_pending.each(&:success)

          expect(builds.pluck(:name)).to contain_exactly('build', 'test', 'deploy')
          expect(builds.pluck(:status)).to contain_exactly('success', 'success', 'pending')
          pipeline.builds.running_or_pending.each(&:success)

          expect(builds.pluck(:name)).to contain_exactly('build', 'test', 'deploy', 'cleanup')
          expect(builds.pluck(:status)).to contain_exactly('success', 'success', 'success', 'pending')
          pipeline.builds.running_or_pending.each(&:success)

          expect(builds.pluck(:status)).to contain_exactly('success', 'success', 'success', 'success')
          pipeline.reload
          expect(pipeline.status).to eq('success')
        end
      end

      context 'when test job fails' do
        it 'properly creates builds' do
          expect(create_builds).to be_truthy
          expect(builds.pluck(:name)).to contain_exactly('build')
          expect(builds.pluck(:status)).to contain_exactly('pending')
          pipeline.builds.running_or_pending.each(&:success)

          expect(builds.pluck(:name)).to contain_exactly('build', 'test')
          expect(builds.pluck(:status)).to contain_exactly('success', 'pending')
          pipeline.builds.running_or_pending.each(&:drop)

          expect(builds.pluck(:name)).to contain_exactly('build', 'test', 'test_failure')
          expect(builds.pluck(:status)).to contain_exactly('success', 'failed', 'pending')
          pipeline.builds.running_or_pending.each(&:success)

          expect(builds.pluck(:name)).to contain_exactly('build', 'test', 'test_failure', 'cleanup')
          expect(builds.pluck(:status)).to contain_exactly('success', 'failed', 'success', 'pending')
          pipeline.builds.running_or_pending.each(&:success)

          expect(builds.pluck(:status)).to contain_exactly('success', 'failed', 'success', 'success')
          pipeline.reload
          expect(pipeline.status).to eq('failed')
        end
      end

      context 'when test and test_failure jobs fail' do
        it 'properly creates builds' do
          expect(create_builds).to be_truthy
          expect(builds.pluck(:name)).to contain_exactly('build')
          expect(builds.pluck(:status)).to contain_exactly('pending')
          pipeline.builds.running_or_pending.each(&:success)

          expect(builds.pluck(:name)).to contain_exactly('build', 'test')
          expect(builds.pluck(:status)).to contain_exactly('success', 'pending')
          pipeline.builds.running_or_pending.each(&:drop)

          expect(builds.pluck(:name)).to contain_exactly('build', 'test', 'test_failure')
          expect(builds.pluck(:status)).to contain_exactly('success', 'failed', 'pending')
          pipeline.builds.running_or_pending.each(&:drop)

          expect(builds.pluck(:name)).to contain_exactly('build', 'test', 'test_failure', 'cleanup')
          expect(builds.pluck(:status)).to contain_exactly('success', 'failed', 'failed', 'pending')
          pipeline.builds.running_or_pending.each(&:success)

          expect(builds.pluck(:name)).to contain_exactly('build', 'test', 'test_failure', 'cleanup')
          expect(builds.pluck(:status)).to contain_exactly('success', 'failed', 'failed', 'success')
          pipeline.reload
          expect(pipeline.status).to eq('failed')
        end
      end

      context 'when deploy job fails' do
        it 'properly creates builds' do
          expect(create_builds).to be_truthy
          expect(builds.pluck(:name)).to contain_exactly('build')
          expect(builds.pluck(:status)).to contain_exactly('pending')
          pipeline.builds.running_or_pending.each(&:success)

          expect(builds.pluck(:name)).to contain_exactly('build', 'test')
          expect(builds.pluck(:status)).to contain_exactly('success', 'pending')
          pipeline.builds.running_or_pending.each(&:success)

          expect(builds.pluck(:name)).to contain_exactly('build', 'test', 'deploy')
          expect(builds.pluck(:status)).to contain_exactly('success', 'success', 'pending')
          pipeline.builds.running_or_pending.each(&:drop)

          expect(builds.pluck(:name)).to contain_exactly('build', 'test', 'deploy', 'cleanup')
          expect(builds.pluck(:status)).to contain_exactly('success', 'success', 'failed', 'pending')
          pipeline.builds.running_or_pending.each(&:success)

          expect(builds.pluck(:status)).to contain_exactly('success', 'success', 'failed', 'success')
          pipeline.reload
          expect(pipeline.status).to eq('failed')
        end
      end

      context 'when build is canceled in the second stage' do
        it 'does not schedule builds after build has been canceled' do
          expect(create_builds).to be_truthy
          expect(builds.pluck(:name)).to contain_exactly('build')
          expect(builds.pluck(:status)).to contain_exactly('pending')
          pipeline.builds.running_or_pending.each(&:success)

          expect(builds.running_or_pending).not_to be_empty

          expect(builds.pluck(:name)).to contain_exactly('build', 'test')
          expect(builds.pluck(:status)).to contain_exactly('success', 'pending')
          pipeline.builds.running_or_pending.each(&:cancel)

          expect(builds.running_or_pending).to be_empty
          expect(pipeline.reload.status).to eq('canceled')
        end
      end

      context 'when listing manual actions' do
        it 'returns only for skipped builds' do
          # currently all builds are created
          expect(create_builds).to be_truthy
          expect(manual_actions).to be_empty

          # succeed stage build
          pipeline.builds.running_or_pending.each(&:success)
          expect(manual_actions).to be_empty

          # succeed stage test
          pipeline.builds.running_or_pending.each(&:success)
          expect(manual_actions).to be_one # production

          # succeed stage deploy
          pipeline.builds.running_or_pending.each(&:success)
          expect(manual_actions).to be_many # production and clear cache
        end

        def manual_actions
          pipeline.manual_actions
        end
      end
    end

    context 'creates a builds from .gitlab-ci.yml' do
      let(:config) do
        YAML.dump({
          rspec: {
            stage: 'test',
            script: 'rspec'
          },
          rubocop: {
            stage: 'test',
            script: 'rubocop'
          },
          deploy: {
            stage: 'deploy',
            script: 'deploy'
          }
        })
      end

      # Using stubbed .gitlab-ci.yml created in commit factory
      #

      before do
        stub_ci_pipeline_yaml_file(config)
        create(:ci_build, :created, pipeline: pipeline, name: 'linux', stage: 'build', stage_idx: 0)
        create(:ci_build, :created, pipeline: pipeline, name: 'mac', stage: 'build', stage_idx: 0)
      end

      it 'when processing a pipeline' do
        # Currently we have two builds with state created
        expect(builds.count).to eq(0)
        expect(all_builds.count).to eq(2)

        # Create builds will mark the created as pending
        expect(create_builds).to be_truthy
        expect(builds.count).to eq(2)
        expect(all_builds.count).to eq(2)

        # When we builds succeed we will create a rest of pipeline from .gitlab-ci.yml
        # We will have 2 succeeded, 2 pending (from stage test), total 5 (one more build from deploy)
        succeed_pending
        expect(create_builds).to be_truthy
        expect(builds.success.count).to eq(2)
        expect(builds.pending.count).to eq(2)
        expect(all_builds.count).to eq(5)

        # When we succeed the 2 pending from stage test,
        # We will queue a deploy stage, no new builds will be created
        succeed_pending
        expect(create_builds).to be_truthy
        expect(builds.pending.count).to eq(1)
        expect(builds.success.count).to eq(4)
        expect(all_builds.count).to eq(5)

        # When we succeed last pending build, we will have a total of 5 succeeded builds, no new builds will be created
        succeed_pending
        expect(create_builds).to be_falsey
        expect(builds.success.count).to eq(5)
        expect(all_builds.count).to eq(5)
      end
    end
  end
end