job_spec.rb 17.6 KB
Newer Older
1
2
# frozen_string_literal: true

3
require 'spec_helper'
4

5
describe Gitlab::Ci::Config::Entry::Job do
6
  let(:entry) { described_class.new(config, name: :rspec) }
7

8
9
10
11
12
13
14
15
16
17
18
19
  it_behaves_like 'with inheritable CI config' do
    let(:inheritable_key) { 'default' }
    let(:inheritable_class) { Gitlab::Ci::Config::Entry::Default }

    # These are entries defined in Default
    # that we know that we don't want to inherit
    # as they do not have sense in context of Job
    let(:ignored_inheritable_columns) do
      %i[]
    end
  end

20
21
22
23
24
25
  describe '.nodes' do
    context 'when filtering all the entry/node names' do
      subject { described_class.nodes.keys }

      let(:result) do
        %i[before_script script stage type after_script cache
26
           image services only except rules needs variables artifacts
27
           environment coverage retry interruptible timeout tags]
28
29
30
31
32
33
      end

      it { is_expected.to match_array result }
    end
  end

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
  describe '.matching?' do
    subject { described_class.matching?(name, config) }

    context 'when config is not a hash' do
      let(:name) { :rspec }
      let(:config) { 'string' }

      it { is_expected.to be_falsey }
    end

    context 'when config is a regular job' do
      let(:name) { :rspec }
      let(:config) do
        { script: 'ls -al' }
      end

      it { is_expected.to be_truthy }
    end

    context 'when config is a bridge job' do
      let(:name) { :rspec }
      let(:config) do
        { trigger: 'other-project' }
      end

      it { is_expected.to be_falsey }
    end

    context 'when config is a hidden job' do
      let(:name) { '.rspec' }
      let(:config) do
        { script: 'ls -al' }
      end

      it { is_expected.to be_falsey }
    end
  end

72
  describe 'validations' do
73
74
75
    before do
      entry.compose!
    end
76

77
78
79
80
81
82
83
84
    context 'when entry config value is correct' do
      let(:config) { { script: 'rspec' } }

      describe '#valid?' do
        it 'is valid' do
          expect(entry).to be_valid
        end
      end
85
86

      context 'when job name is empty' do
87
        let(:entry) { described_class.new(config, name: ''.to_sym) }
88
89

        it 'reports error' do
90
          expect(entry.errors).to include "job name can't be blank"
91
92
        end
      end
93
94
95

      context 'when delayed job' do
        context 'when start_in is specified' do
96
          let(:config) { { script: 'echo', when: 'delayed', start_in: '1 week' } }
97

98
          it { expect(entry).to be_valid }
99
100
        end
      end
Kamil Trzciński's avatar
Kamil Trzciński committed
101
102
103

      context 'when has needs' do
        let(:config) do
104
105
106
107
108
          {
            stage: 'test',
            script: 'echo',
            needs: ['another-job']
          }
Kamil Trzciński's avatar
Kamil Trzciński committed
109
110
111
112
113
114
        end

        it { expect(entry).to be_valid }

        context 'when has dependencies' do
          let(:config) do
115
116
117
118
119
120
            {
              stage: 'test',
              script: 'echo',
              dependencies: ['another-job'],
              needs: ['another-job']
            }
Kamil Trzciński's avatar
Kamil Trzciński committed
121
122
123
124
125
          end

          it { expect(entry).to be_valid }
        end
      end
126
127
128
129
130
131
132
    end

    context 'when entry value is not correct' do
      context 'incorrect config value type' do
        let(:config) { ['incorrect'] }

        describe '#errors' do
133
          it 'reports error about a config type' do
134
            expect(entry.errors)
135
              .to include 'job config should be a hash'
136
137
138
          end
        end
      end
139
140
141
142
143
144
145
146
147
148

      context 'when config is empty' do
        let(:config) { {} }

        describe '#valid' do
          it 'is invalid' do
            expect(entry).not_to be_valid
          end
        end
      end
149
150
151
152
153
154
155
156
157
158

      context 'when unknown keys detected' do
        let(:config) { { unknown: true } }

        describe '#valid' do
          it 'is not valid' do
            expect(entry).not_to be_valid
          end
        end
      end
159
160
161
162
163
164
165
166
167

      context 'when script is not provided' do
        let(:config) { { stage: 'test' } }

        it 'returns error about missing script entry' do
          expect(entry).not_to be_valid
          expect(entry.errors).to include "job script can't be blank"
        end
      end
168

169
170
171
172
173
      context 'when extends key is not a string' do
        let(:config) { { extends: 123 } }

        it 'returns error about wrong value type' do
          expect(entry).not_to be_valid
Wolphin's avatar
Wolphin committed
174
          expect(entry.errors).to include "job extends should be an array of strings or a string"
175
176
177
        end
      end

178
179
180
181
182
183
184
185
186
187
      context 'when parallel value is not correct' do
        context 'when it is not a numeric value' do
          let(:config) { { parallel: true } }

          it 'returns error about invalid type' do
            expect(entry).not_to be_valid
            expect(entry.errors).to include 'job parallel is not a number'
          end
        end

188
189
        context 'when it is lower than two' do
          let(:config) { { parallel: 1 } }
190
191
192
193

          it 'returns error about value too low' do
            expect(entry).not_to be_valid
            expect(entry.errors)
194
              .to include 'job parallel must be greater than or equal to 2'
195
196
197
          end
        end

Kamil Trzciński's avatar
Kamil Trzciński committed
198
199
200
201
202
203
204
205
206
207
        context 'when it is bigger than 50' do
          let(:config) { { parallel: 51 } }

          it 'returns error about value too high' do
            expect(entry).not_to be_valid
            expect(entry.errors)
              .to include 'job parallel must be less than or equal to 50'
          end
        end

208
209
210
211
212
213
214
215
        context 'when it is not an integer' do
          let(:config) { { parallel: 1.5 } }

          it 'returns error about wrong value' do
            expect(entry).not_to be_valid
            expect(entry.errors).to include 'job parallel must be an integer'
          end
        end
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230

        context 'when it uses both "when:" and "rules:"' do
          let(:config) do
            {
              script: 'echo',
              when: 'on_failure',
              rules: [{ if: '$VARIABLE', when: 'on_success' }]
            }
          end

          it 'returns an error about when: being combined with rules' do
            expect(entry).not_to be_valid
            expect(entry.errors).to include 'job config key may not be used with `rules`: when'
          end
        end
231
232
      end

233
234
      context 'when delayed job' do
        context 'when start_in is specified' do
235
          let(:config) { { script: 'echo', when: 'delayed', start_in: '1 week' } }
236

237
          it { expect(entry).to be_valid }
238
239
240
241
242
243
244
245
246
247
248
        end

        context 'when start_in is empty' do
          let(:config) { { when: 'delayed', start_in: nil } }

          it 'returns error about invalid type' do
            expect(entry).not_to be_valid
            expect(entry.errors).to include 'job start in should be a duration'
          end
        end

249
        context 'when start_in is not formatted as a duration' do
250
251
252
253
254
255
256
          let(:config) { { when: 'delayed', start_in: 'test' } }

          it 'returns error about invalid type' do
            expect(entry).not_to be_valid
            expect(entry.errors).to include 'job start in should be a duration'
          end
        end
257

258
259
        context 'when start_in is longer than one week' do
          let(:config) { { when: 'delayed', start_in: '8 days' } }
260
261
262
263
264
265

          it 'returns error about exceeding the limit' do
            expect(entry).not_to be_valid
            expect(entry.errors).to include 'job start in should not exceed the limit'
          end
        end
266
267
      end

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
      context 'when only: is used with rules:' do
        let(:config) { { only: ['merge_requests'], rules: [{ if: '$THIS' }] } }

        it 'returns error about mixing only: with rules:' do
          expect(entry).not_to be_valid
          expect(entry.errors).to include /may not be used with `rules`/
        end

        context 'and only: is blank' do
          let(:config) { { only: nil, rules: [{ if: '$THIS' }] } }

          it 'returns error about mixing only: with rules:' do
            expect(entry).not_to be_valid
            expect(entry.errors).to include /may not be used with `rules`/
          end
        end

        context 'and rules: is blank' do
          let(:config) { { only: ['merge_requests'], rules: nil } }

          it 'returns error about mixing only: with rules:' do
            expect(entry).not_to be_valid
            expect(entry.errors).to include /may not be used with `rules`/
          end
        end
      end

      context 'when except: is used with rules:' do
        let(:config) { { except: { refs: %w[master] }, rules: [{ if: '$THIS' }] } }

        it 'returns error about mixing except: with rules:' do
          expect(entry).not_to be_valid
          expect(entry.errors).to include /may not be used with `rules`/
        end

        context 'and except: is blank' do
          let(:config) { { except: nil, rules: [{ if: '$THIS' }] } }

          it 'returns error about mixing except: with rules:' do
            expect(entry).not_to be_valid
            expect(entry.errors).to include /may not be used with `rules`/
          end
        end

        context 'and rules: is blank' do
          let(:config) { { except: { refs: %w[master] }, rules: nil } }

          it 'returns error about mixing except: with rules:' do
            expect(entry).not_to be_valid
            expect(entry.errors).to include /may not be used with `rules`/
          end
        end
      end

      context 'when only: and except: are both used with rules:' do
        let(:config) do
          {
            only: %w[merge_requests],
            except: { refs: %w[master] },
            rules: [{ if: '$THIS' }]
          }
        end

        it 'returns errors about mixing both only: and except: with rules:' do
          expect(entry).not_to be_valid
          expect(entry.errors).to include /may not be used with `rules`/
          expect(entry.errors).to include /may not be used with `rules`/
        end

        context 'when only: and except: as both blank' do
          let(:config) do
            { only: nil, except: nil, rules: [{ if: '$THIS' }] }
          end

          it 'returns errors about mixing both only: and except: with rules:' do
            expect(entry).not_to be_valid
            expect(entry.errors).to include /may not be used with `rules`/
            expect(entry.errors).to include /may not be used with `rules`/
          end
        end

        context 'when rules: is blank' do
          let(:config) do
            { only: %w[merge_requests], except: { refs: %w[master] }, rules: nil }
          end

          it 'returns errors about mixing both only: and except: with rules:' do
            expect(entry).not_to be_valid
            expect(entry.errors).to include /may not be used with `rules`/
            expect(entry.errors).to include /may not be used with `rules`/
          end
        end
      end

362
363
364
365
366
367
368
369
      context 'when start_in specified without delayed specification' do
        let(:config) { { start_in: '1 day' } }

        it 'returns error about invalid type' do
          expect(entry).not_to be_valid
          expect(entry.errors).to include 'job start in must be blank'
        end
      end
Kamil Trzciński's avatar
Kamil Trzciński committed
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386

      context 'when has dependencies' do
        context 'that are not a array of strings' do
          let(:config) do
            { script: 'echo', dependencies: 'build-job' }
          end

          it 'returns error about invalid type' do
            expect(entry).not_to be_valid
            expect(entry.errors).to include 'job dependencies should be an array of strings'
          end
        end
      end

      context 'when has needs' do
        context 'when have dependencies that are not subset of needs' do
          let(:config) do
387
388
389
390
391
392
            {
              stage: 'test',
              script: 'echo',
              dependencies: ['another-job'],
              needs: ['build-job']
            }
Kamil Trzciński's avatar
Kamil Trzciński committed
393
394
395
396
397
398
399
          end

          it 'returns error about invalid data' do
            expect(entry).not_to be_valid
            expect(entry.errors).to include 'job dependencies the another-job should be part of needs'
          end
        end
400
401
402
403
404
405
406
407
408
409
410
411
412
413

        context 'when stage: is missing' do
          let(:config) do
            {
              script: 'echo',
              needs: ['build-job']
            }
          end

          it 'returns error about invalid data' do
            expect(entry).not_to be_valid
            expect(entry.errors).to include 'job config missing required keys: stage'
          end
        end
Kamil Trzciński's avatar
Kamil Trzciński committed
414
      end
415
416
417

      context 'when timeout value is not correct' do
        context 'when it is higher than instance wide timeout' do
418
          let(:config) { { timeout: '3 months', script: 'test' } }
419
420
421
422

          it 'returns error about value too high' do
            expect(entry).not_to be_valid
            expect(entry.errors)
423
              .to include "timeout config should not exceed the limit"
424
425
426
427
          end
        end

        context 'when it is not a duration' do
428
          let(:config) { { timeout: 100, script: 'test' } }
429
430
431

          it 'returns error about wrong value' do
            expect(entry).not_to be_valid
432
            expect(entry.errors).to include 'timeout config should be a duration'
433
434
435
436
437
438
439
440
441
442
443
444
445
          end
        end
      end

      context 'when timeout value is correct' do
        let(:config) { { script: 'echo', timeout: '1m 1s' } }

        it 'returns correct timeout' do
          expect(entry).to be_valid
          expect(entry.errors).to be_empty
          expect(entry.timeout).to eq('1m 1s')
        end
      end
446
447
    end
  end
448
449
450

  describe '#relevant?' do
    it 'is a relevant entry' do
451
452
      entry = described_class.new({ script: 'rspec' }, name: :rspec)

453
454
455
      expect(entry).to be_relevant
    end
  end
456
457
458
459
460
461

  describe '#compose!' do
    let(:specified) do
      double('specified', 'specified?' => true, value: 'specified')
    end

462
463
464
    let(:unspecified) { double('unspecified', 'specified?' => false) }
    let(:default) { double('default', '[]' => unspecified) }
    let(:deps) { double('deps', 'default' => default, '[]' => unspecified) }
465

466
    context 'when job config overrides default config' do
467
468
469
      before do
        entry.compose!(deps)
      end
470
471

      let(:config) do
472
        { script: 'rspec', image: 'some_image', cache: { key: 'test' } }
473
474
      end

475
      it 'overrides default config' do
476
        expect(entry[:image].value).to eq(name: 'some_image')
477
        expect(entry[:cache].value).to eq(key: 'test', policy: 'pull-push')
478
479
480
      end
    end

481
    context 'when job config does not override default config' do
482
      before do
483
484
        allow(default).to receive('[]').with(:image).and_return(specified)

485
486
487
488
489
        entry.compose!(deps)
      end

      let(:config) { { script: 'ls', cache: { key: 'test' } } }

490
      it 'uses config from default entry' do
491
        expect(entry[:image].value).to eq 'specified'
492
        expect(entry[:cache].value).to eq(key: 'test', policy: 'pull-push')
493
494
495
      end
    end
  end
496
497

  context 'when composed' do
498
499
500
    before do
      entry.compose!
    end
501
502

    describe '#value' do
503
504
505
      before do
        entry.compose!
      end
506
507
508
509
510
511
512
513
514
515
516
517
518
519

      context 'when entry is correct' do
        let(:config) do
          { before_script: %w[ls pwd],
            script: 'rspec',
            after_script: %w[cleanup] }
        end

        it 'returns correct value' do
          expect(entry.value)
            .to eq(name: :rspec,
                   before_script: %w[ls pwd],
                   script: %w[rspec],
                   stage: 'test',
520
                   ignore: false,
521
                   after_script: %w[cleanup],
522
523
                   only: { refs: %w[branches tags] },
                   variables: {})
524
525
526
527
        end
      end
    end
  end
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546

  describe '#manual_action?' do
    context 'when job is a manual action' do
      let(:config) { { script: 'deploy', when: 'manual' } }

      it 'is a manual action' do
        expect(entry).to be_manual_action
      end
    end

    context 'when job is not a manual action' do
      let(:config) { { script: 'deploy' } }

      it 'is not a manual action' do
        expect(entry).not_to be_manual_action
      end
    end
  end

547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
  describe '#delayed?' do
    context 'when job is a delayed' do
      let(:config) { { script: 'deploy', when: 'delayed' } }

      it 'is a delayed' do
        expect(entry).to be_delayed
      end
    end

    context 'when job is not a delayed' do
      let(:config) { { script: 'deploy' } }

      it 'is not a delayed' do
        expect(entry).not_to be_delayed
      end
    end
  end

565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
  describe '#ignored?' do
    context 'when job is a manual action' do
      context 'when it is not specified if job is allowed to fail' do
        let(:config) do
          { script: 'deploy', when: 'manual' }
        end

        it 'is an ignored job' do
          expect(entry).to be_ignored
        end
      end

      context 'when job is allowed to fail' do
        let(:config) do
          { script: 'deploy', when: 'manual', allow_failure: true }
        end

        it 'is an ignored job' do
          expect(entry).to be_ignored
        end
      end

      context 'when job is not allowed to fail' do
        let(:config) do
          { script: 'deploy', when: 'manual', allow_failure: false }
        end

        it 'is not an ignored job' do
          expect(entry).not_to be_ignored
        end
      end
    end

    context 'when job is not a manual action' do
      context 'when it is not specified if job is allowed to fail' do
        let(:config) { { script: 'deploy' } }

        it 'is not an ignored job' do
          expect(entry).not_to be_ignored
        end
      end

      context 'when job is allowed to fail' do
        let(:config) { { script: 'deploy', allow_failure: true } }

        it 'is an ignored job' do
          expect(entry).to be_ignored
        end
      end

      context 'when job is not allowed to fail' do
        let(:config) { { script: 'deploy', allow_failure: false } }

        it 'is not an ignored job' do
          expect(entry).not_to be_ignored
        end
      end
    end
  end
624
end