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

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

require 'spec_helper'

RSpec.describe Gitlab::Ci::Build::Rules, feature_category: :pipeline_composition do
  let_it_be(:pipeline) { create(:ci_pipeline) }
  let_it_be(:ci_build) { build(:ci_build, pipeline: pipeline) }

  let(:seed) do
    double('build seed',
      to_resource: ci_build,
      variables_hash: ci_build.scoped_variables.to_hash
    )
  end

  let(:rules) { described_class.new(rule_list, default_when: 'on_success') }

  describe '.new' do
    let(:rules_ivar)   { rules.instance_variable_get :@rule_list }
    let(:default_when) { rules.instance_variable_get :@default_when }

    context 'with no rules' do
      let(:rule_list) { [] }

      it 'sets @rule_list to an empty array' do
        expect(rules_ivar).to eq([])
      end

      it 'sets @default_when to "on_success"' do
        expect(default_when).to eq('on_success')
      end
    end

    context 'with one rule' do
      let(:rule_list) { [{ if: '$VAR == null', when: 'always' }] }

      it 'sets @rule_list to an array of a single rule' do
        expect(rules_ivar).to be_an(Array)
      end

      it 'sets @default_when to "on_success"' do
        expect(default_when).to eq('on_success')
      end
    end

    context 'with multiple rules' do
      let(:rule_list) do
        [
          { if: '$VAR == null', when: 'always' },
          { if: '$VAR == null', when: 'always' }
        ]
      end

      it 'sets @rule_list to an array of a single rule' do
        expect(rules_ivar).to be_an(Array)
      end

      it 'sets @default_when to "on_success"' do
        expect(default_when).to eq('on_success')
      end
    end

    context 'with a specified default when:' do
      let(:rule_list) { [{ if: '$VAR == null', when: 'always' }] }
      let(:rules)     { described_class.new(rule_list, default_when: 'manual') }

      it 'sets @rule_list to an array of a single rule' do
        expect(rules_ivar).to be_an(Array)
      end

      it 'sets @default_when to "manual"' do
        expect(default_when).to eq('manual')
      end
    end
  end

  describe '#evaluate' do
    subject { rules.evaluate(pipeline, seed) }

    context 'with nil rules' do
      let(:rule_list) { nil }

      it { is_expected.to eq(described_class::Result.new(when: 'on_success')) }

      context 'and when:manual set as the default' do
        let(:rules) { described_class.new(rule_list, default_when: 'manual') }

        it { is_expected.to eq(described_class::Result.new(when: 'manual')) }
      end
    end

    context 'with no rules' do
      let(:rule_list) { [] }

      it { is_expected.to eq(described_class::Result.new(when: 'never')) }

      context 'and when:manual set as the default' do
        let(:rules) { described_class.new(rule_list, default_when: 'manual') }

        it { is_expected.to eq(described_class::Result.new(when: 'never')) }
      end
    end

    context 'with one rule without any clauses' do
      let(:rule_list) { [{ when: 'manual', allow_failure: true }] }

      it { is_expected.to eq(described_class::Result.new(when: 'manual', allow_failure: true)) }
    end

    context 'with one matching rule' do
      let(:rule_list) { [{ if: '$VAR == null', when: 'always' }] }

      it { is_expected.to eq(described_class::Result.new(when: 'always')) }
    end

    context 'with two matching rules' do
      let(:rule_list) do
        [
          { if: '$VAR == null', when: 'delayed', start_in: '1 day' },
          { if: '$VAR == null', when: 'always' }
        ]
      end

      it 'returns the value of the first matched rule in the list' do
        expect(subject).to eq(described_class::Result.new(when: 'delayed', start_in: '1 day'))
      end
    end

    context 'with a non-matching and matching rule' do
      let(:rule_list) do
        [
          { if: '$VAR =! null', when: 'delayed', start_in: '1 day' },
          { if: '$VAR == null', when: 'always' }
        ]
      end

      it { is_expected.to eq(described_class::Result.new(when: 'always')) }
    end

    context 'with a matching and non-matching rule' do
      let(:rule_list) do
        [
          { if: '$VAR == null', when: 'delayed', start_in: '1 day' },
          { if: '$VAR != null', when: 'always' }
        ]
      end

      it { is_expected.to eq(described_class::Result.new(when: 'delayed', start_in: '1 day')) }
    end

    context 'with non-matching rules' do
      let(:rule_list) do
        [
          { if: '$VAR != null', when: 'delayed', start_in: '1 day' },
          { if: '$VAR != null', when: 'always' }
        ]
      end

      it { is_expected.to eq(described_class::Result.new(when: 'never')) }

      context 'and when:manual set as the default' do
        let(:rules) { described_class.new(rule_list, default_when: 'manual') }

        it 'does not return the default when:' do
          expect(subject).to eq(described_class::Result.new(when: 'never'))
        end
      end
    end

    context 'with only allow_failure' do
      context 'with matching rule' do
        let(:rule_list) { [{ if: '$VAR == null', allow_failure: true }] }

        it { is_expected.to eq(described_class::Result.new(when: 'on_success', allow_failure: true)) }
      end

      context 'with non-matching rule' do
        let(:rule_list) { [{ if: '$VAR != null', allow_failure: true }] }

        it { is_expected.to eq(described_class::Result.new(when: 'never')) }
      end
    end

    context 'with needs' do
      context 'when single need is specified' do
        let(:rule_list) do
          [{ if: '$VAR == null', needs: [{ name: 'test', artifacts: true, optional: false }] }]
        end

        it {
          is_expected.to eq(described_class::Result.new(
            when: 'on_success',
            needs: [{ name: 'test',
                      artifacts: true,
                      optional: false }]
          ))
        }
      end

      context 'when multiple needs are specified' do
        let(:rule_list) do
          [{ if: '$VAR == null',
             needs: [{ name: 'test', artifacts: true, optional: false },
               { name: 'rspec', artifacts: true, optional: false }] }]
        end

        it {
          is_expected.to eq(described_class::Result.new(
            when: 'on_success',
            needs: [{ name: 'test',
                      artifacts: true,
                      optional: false },
              { name: 'rspec',
                artifacts: true,
                optional: false }]))
        }
      end

      context 'when there are no needs specified' do
        let(:rule_list) { [{ if: '$VAR == null' }] }

        it {
          is_expected.to eq(described_class::Result.new(when: 'on_success'))
        }
      end

      context 'when need is specified with additional attibutes' do
        let(:rule_list) do
          [{ if: '$VAR == null', needs: [{
            artifacts: false,
            name: 'test',
            optional: true,
            when: 'never'
          }] }]
        end

        it {
          is_expected.to eq(
            described_class::Result.new(
              when: 'on_success',
              needs: [{ artifacts: false,
                        name: 'test',
                        optional: true,
                        when: 'never' }]))
        }
      end

      context 'when feature flag is disabled' do
        before do
          stub_feature_flags(introduce_rules_with_needs: false)
        end

        context 'with needs' do
          context 'when single need is specified' do
            let(:rule_list) do
              [{ if: '$VAR == null', needs: [{ name: 'test', artifacts: true, optional: false }] }]
            end

            it {
              is_expected.to eq(described_class::Result.new(when: 'on_success'))
            }
          end

          context 'when multiple needs are specified' do
            let(:rule_list) do
              [{ if: '$VAR == null',
                 needs: [{ name: 'test', artifacts: true, optional: false },
                   { name: 'rspec', artifacts: true, optional: false }] }]
            end

            it {
              is_expected.to eq(described_class::Result.new(when: 'on_success'))
            }
          end

          context 'when there are no needs specified' do
            let(:rule_list) { [{ if: '$VAR == null' }] }

            it {
              is_expected.to eq(described_class::Result.new(when: 'on_success'))
            }
          end

          context 'when need is specified with additional attibutes' do
            let(:rule_list) do
              [{ if: '$VAR == null', needs:  [{
                artifacts: false,
                name: 'test',
                optional: true,
                when: 'never'
              }] }]
            end

            it {
              is_expected.to eq(described_class::Result.new(when: 'on_success'))
            }
          end
        end
      end
    end

    context 'with variables' do
      context 'with matching rule' do
        let(:rule_list) { [{ if: '$VAR == null', variables: { MY_VAR: 'my var' } }] }

        it { is_expected.to eq(described_class::Result.new(when: 'on_success', variables: { MY_VAR: 'my var' })) }
      end
    end

    context 'with a regexp variable matching rule' do
      let(:rule_list) { [{ if: '"abcde" =~ $pattern' }] }

      before do
        allow(ci_build).to receive(:scoped_variables).and_return(
          Gitlab::Ci::Variables::Collection.new
            .append(key: 'pattern', value: '/^ab.*/', public: true)
        )
      end

      it { is_expected.to eq(described_class::Result.new(when: 'on_success')) }
    end
  end

  describe 'Gitlab::Ci::Build::Rules::Result' do
    let(:when_value) { 'on_success' }
    let(:start_in) { nil }
    let(:allow_failure) { nil }
    let(:variables) { nil }
    let(:needs) { nil }

    subject(:result) do
      Gitlab::Ci::Build::Rules::Result.new(
        when: when_value,
        start_in: start_in,
        allow_failure: allow_failure,
        variables: variables,
        needs: needs)
    end

    describe '#build_attributes' do
      subject(:build_attributes) do
        result.build_attributes
      end

      it 'compacts nil values' do
        is_expected.to eq(options: {}, when: 'on_success')
      end

      context 'scheduling_type' do
        context 'when rules have needs' do
          context 'single need' do
            let(:needs) do
              { job: [{ name: 'test' }] }
            end

            it 'saves needs' do
              expect(subject[:needs_attributes]).to eq([{ name: "test" }])
            end

            it 'adds schedule type to the build_attributes' do
              expect(subject[:scheduling_type]).to eq(:dag)
            end
          end

          context 'multiple needs' do
            let(:needs) do
              { job: [{ name: 'test' }, { name: 'test_2', artifacts: true, optional: false }] }
            end

            it 'saves needs' do
              expect(subject[:needs_attributes]).to match_array([{ name: "test" },
                { name: 'test_2', artifacts: true, optional: false }])
            end

            it 'adds schedule type to the build_attributes' do
              expect(subject[:scheduling_type]).to eq(:dag)
            end
          end
        end

        context 'when rules do not have needs' do
          it 'does not add schedule type to the build_attributes' do
            expect(subject.key?(:scheduling_type)).to be_falsy
          end
        end
      end
    end

    describe '#pass?' do
      context "'when' is 'never'" do
        let!(:when_value) { 'never' }

        it 'returns false' do
          expect(result.pass?).to eq(false)
        end
      end

      context "'when' is 'on_success'" do
        let!(:when_value) { 'on_success' }

        it 'returns true' do
          expect(result.pass?).to eq(true)
        end
      end
    end
  end
end