Welcome to mirror list, hosted at ThFree Co, Russian Federation.

duration_spec.rb « pipeline « ci « gitlab « lib « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 89c0ce4623779718f6259562006e2e71a8d383f0 (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
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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::Ci::Pipeline::Duration, feature_category: :continuous_integration do
  describe '.from_periods' do
    let(:calculated_duration) { calculate(data) }

    shared_examples 'calculating duration' do
      it do
        expect(calculated_duration).to eq(duration)
      end
    end

    context 'test sample A' do
      let(:data) do
        [[0, 1],
         [1, 2],
         [3, 4],
         [5, 6]]
      end

      let(:duration) { 4 }

      it_behaves_like 'calculating duration'
    end

    context 'test sample B' do
      let(:data) do
        [[0, 1],
         [1, 2],
         [2, 3],
         [3, 4],
         [0, 4]]
      end

      let(:duration) { 4 }

      it_behaves_like 'calculating duration'
    end

    context 'test sample C' do
      let(:data) do
        [[0, 4],
         [2, 6],
         [5, 7],
         [8, 9]]
      end

      let(:duration) { 8 }

      it_behaves_like 'calculating duration'
    end

    context 'test sample D' do
      let(:data) do
        [[0, 1],
         [2, 3],
         [4, 5],
         [6, 7]]
      end

      let(:duration) { 4 }

      it_behaves_like 'calculating duration'
    end

    context 'test sample E' do
      let(:data) do
        [[0, 1],
         [3, 9],
         [3, 4],
         [3, 5],
         [3, 8],
         [4, 5],
         [4, 7],
         [5, 8]]
      end

      let(:duration) { 7 }

      it_behaves_like 'calculating duration'
    end

    context 'test sample F' do
      let(:data) do
        [[1, 3],
         [2, 4],
         [2, 4],
         [2, 4],
         [5, 8]]
      end

      let(:duration) { 6 }

      it_behaves_like 'calculating duration'
    end

    context 'test sample G' do
      let(:data) do
        [[1, 3],
         [2, 4],
         [6, 7]]
      end

      let(:duration) { 4 }

      it_behaves_like 'calculating duration'
    end

    def calculate(data)
      periods = data.shuffle.map do |(first, last)|
        described_class::Period.new(first, last)
      end

      described_class.send(:from_periods, periods.sort_by(&:first))
    end
  end

  describe '.from_pipeline' do
    let_it_be_with_reload(:pipeline) { create(:ci_pipeline) }

    let_it_be(:start_time) { Time.current.change(usec: 0) }
    let_it_be(:current) { start_time + 1000 }
    let_it_be(:success_build) { create_build(:success, started_at: start_time, finished_at: start_time + 50) }
    let_it_be(:failed_build) { create_build(:failed, started_at: start_time + 60, finished_at: start_time + 110) }
    let_it_be(:canceled_build) { create_build(:canceled, started_at: start_time + 120, finished_at: start_time + 180) }
    let_it_be(:skipped_build) { create_build(:skipped, started_at: start_time) }
    let_it_be(:pending_build) { create_build(:pending) }
    let_it_be(:created_build) { create_build(:created) }
    let_it_be(:preparing_build) { create_build(:preparing) }
    let_it_be(:scheduled_build) { create_build(:scheduled) }
    let_it_be(:expired_scheduled_build) { create_build(:expired_scheduled) }
    let_it_be(:manual_build) { create_build(:manual) }

    let!(:running_build) { create_build(:running, started_at: start_time) }

    it 'returns the duration of the running build' do
      travel_to(current) do
        expect(described_class.from_pipeline(pipeline)).to eq 1000.seconds
      end
    end

    context 'when there is no running build' do
      let!(:running_build) { nil }

      it 'returns the duration for all the builds' do
        travel_to(current) do
          # 160 =  success (50) + failed (50) + canceled (60)
          expect(described_class.from_pipeline(pipeline)).to eq 160.seconds
        end
      end
    end

    context 'when there are direct bridge jobs' do
      let_it_be(:success_bridge) do
        create_bridge(:success, started_at: start_time + 220, finished_at: start_time + 280)
      end

      let_it_be(:failed_bridge) { create_bridge(:failed, started_at: start_time + 180, finished_at: start_time + 240) }
      # NOTE: bridge won't be `canceled` as it will be marked as failed when downstream pipeline is canceled
      # @see Ci::Bridge#inherit_status_from_downstream
      let_it_be(:canceled_bridge) do
        create_bridge(:failed, started_at: start_time + 180, finished_at: start_time + 210)
      end

      let_it_be(:skipped_bridge) { create_bridge(:skipped, started_at: start_time) }
      let_it_be(:created_bridge) { create_bridge(:created) }
      let_it_be(:manual_bridge) { create_bridge(:manual) }

      let_it_be(:success_bridge_pipeline) do
        create(:ci_pipeline, :success, started_at: start_time + 230, finished_at: start_time + 280).tap do |p|
          create(:ci_sources_pipeline, source_job: success_bridge, pipeline: p)
          create_build(:success, pipeline: p, started_at: start_time + 235, finished_at: start_time + 280)
          create_bridge(:success, pipeline: p, started_at: start_time + 240, finished_at: start_time + 280)
        end
      end

      let_it_be(:failed_bridge_pipeline) do
        create(:ci_pipeline, :failed, started_at: start_time + 225, finished_at: start_time + 240).tap do |p|
          create(:ci_sources_pipeline, source_job: failed_bridge, pipeline: p)
          create_build(:failed, pipeline: p, started_at: start_time + 230, finished_at: start_time + 240)
          create_bridge(:success, pipeline: p, started_at: start_time + 235, finished_at: start_time + 240)
        end
      end

      let_it_be(:canceled_bridge_pipeline) do
        create(:ci_pipeline, :canceled, started_at: start_time + 190, finished_at: start_time + 210).tap do |p|
          create(:ci_sources_pipeline, source_job: canceled_bridge, pipeline: p)
          create_build(:canceled, pipeline: p, started_at: start_time + 200, finished_at: start_time + 210)
          create_bridge(:success, pipeline: p, started_at: start_time + 205, finished_at: start_time + 210)
        end
      end

      it 'returns the duration of the running build' do
        travel_to(current) do
          expect(described_class.from_pipeline(pipeline)).to eq 1000.seconds
        end
      end

      context 'when there is no running build' do
        let!(:running_build) { nil }

        it 'returns the duration for all the builds (including self and downstreams)' do
          travel_to(current) do
            # 220 = 160 (see above)
            #     + success build (45) + failed (10) + canceled (10) - overlapping (success & failed) (5)
            expect(described_class.from_pipeline(pipeline)).to eq 220.seconds
          end
        end
      end

      # rubocop:disable RSpec/MultipleMemoizedHelpers
      context 'when there are downstream bridge jobs' do
        let_it_be(:success_direct_bridge) do
          create_bridge(:success, started_at: start_time + 280, finished_at: start_time + 400)
        end

        let_it_be(:success_downstream_pipeline) do
          create(:ci_pipeline, :success, started_at: start_time + 285, finished_at: start_time + 300).tap do |p|
            create(:ci_sources_pipeline, source_job: success_direct_bridge, pipeline: p)
            create_build(:success, pipeline: p, started_at: start_time + 290, finished_at: start_time + 296)
            create_bridge(:success, pipeline: p, started_at: start_time + 285, finished_at: start_time + 288)
          end
        end

        let_it_be(:failed_downstream_pipeline) do
          create(:ci_pipeline, :failed, started_at: start_time + 305, finished_at: start_time + 350).tap do |p|
            create(:ci_sources_pipeline, source_job: success_direct_bridge, pipeline: p)
            create_build(:failed, pipeline: p, started_at: start_time + 320, finished_at: start_time + 327)
            create_bridge(:success, pipeline: p, started_at: start_time + 305, finished_at: start_time + 350)
          end
        end

        let_it_be(:canceled_downstream_pipeline) do
          create(:ci_pipeline, :canceled, started_at: start_time + 360, finished_at: start_time + 400).tap do |p|
            create(:ci_sources_pipeline, source_job: success_direct_bridge, pipeline: p)
            create_build(:canceled, pipeline: p, started_at: start_time + 390, finished_at: start_time + 398)
            create_bridge(:success, pipeline: p, started_at: start_time + 360, finished_at: start_time + 378)
          end
        end

        it 'returns the duration of the running build' do
          travel_to(current) do
            expect(described_class.from_pipeline(pipeline)).to eq 1000.seconds
          end
        end

        context 'when there is no running build' do
          let!(:running_build) { nil }

          it 'returns the duration for all the builds (including self and downstreams)' do
            travel_to(current) do
              # 241 = 220 (see above)
              #     + success downstream build (6) + failed (7) + canceled (8)
              expect(described_class.from_pipeline(pipeline)).to eq 241.seconds
            end
          end
        end
      end
      # rubocop:enable RSpec/MultipleMemoizedHelpers
    end

    it 'does not generate N+1 queries if more builds are added' do
      travel_to(current) do
        expect do
          described_class.from_pipeline(pipeline)
        end.not_to exceed_query_limit(1)

        create_list(:ci_build, 2, :success, pipeline: pipeline, started_at: start_time, finished_at: start_time + 50)

        expect do
          described_class.from_pipeline(pipeline)
        end.not_to exceed_query_limit(1)
      end
    end

    it 'does not generate N+1 queries if more bridges and their pipeline builds are added' do
      travel_to(current) do
        expect do
          described_class.from_pipeline(pipeline)
        end.not_to exceed_query_limit(1)

        create_list(
          :ci_bridge, 2, :success,
          pipeline: pipeline, started_at: start_time + 220, finished_at: start_time + 280).each do |bridge|
          create(:ci_pipeline, :success, started_at: start_time + 235, finished_at: start_time + 280).tap do |p|
            create(:ci_sources_pipeline, source_job: bridge, pipeline: p)
            create_builds(3, :success)
          end
        end

        expect do
          described_class.from_pipeline(pipeline)
        end.not_to exceed_query_limit(1)
      end
    end

    private

    def create_build(trait, **opts)
      create(:ci_build, trait, pipeline: pipeline, **opts)
    end

    def create_builds(counts, trait, **opts)
      create_list(:ci_build, counts, trait, pipeline: pipeline, **opts)
    end

    def create_bridge(trait, **opts)
      create(:ci_bridge, trait, pipeline: pipeline, **opts)
    end
  end
end