gitlab_ci_yaml_processor_spec.rb 46.2 KB
Newer Older
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
1
2
require 'spec_helper'

Valery Sizov's avatar
Valery Sizov committed
3
module Ci
Douwe Maan's avatar
Douwe Maan committed
4
  describe GitlabCiYamlProcessor, lib: true do
5
    let(:path) { 'path' }
6

Valery Sizov's avatar
Valery Sizov committed
7
8
    describe "#builds_for_ref" do
      let(:type) { 'test' }
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
9

Valery Sizov's avatar
Valery Sizov committed
10
11
12
13
14
15
      it "returns builds if no branch specified" do
        config = YAML.dump({
          before_script: ["pwd"],
          rspec: { script: "rspec" }
        })

16
        config_processor = GitlabCiYamlProcessor.new(config, path)
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
17

Valery Sizov's avatar
Valery Sizov committed
18
19
20
        expect(config_processor.builds_for_stage_and_ref(type, "master").size).to eq(1)
        expect(config_processor.builds_for_stage_and_ref(type, "master").first).to eq({
          stage: "test",
21
          stage_idx: 1,
Valery Sizov's avatar
Valery Sizov committed
22
          name: :rspec,
Kamil Trzcinski's avatar
Kamil Trzcinski committed
23
24
          commands: "pwd\nrspec",
          tag_list: [],
Valery Sizov's avatar
Valery Sizov committed
25
          options: {},
Kamil Trzcinski's avatar
Kamil Trzcinski committed
26
          allow_failure: false,
27
28
          when: "on_success",
          environment: nil,
29
          yaml_variables: []
Valery Sizov's avatar
Valery Sizov committed
30
31
        })
      end
32

33
      describe 'only' do
34
35
36
37
38
        it "does not return builds if only has another branch" do
          config = YAML.dump({
                               before_script: ["pwd"],
                               rspec: { script: "rspec", only: ["deploy"] }
                             })
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
39

40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
          config_processor = GitlabCiYamlProcessor.new(config, path)

          expect(config_processor.builds_for_stage_and_ref(type, "master").size).to eq(0)
        end

        it "does not return builds if only has regexp with another branch" do
          config = YAML.dump({
                               before_script: ["pwd"],
                               rspec: { script: "rspec", only: ["/^deploy$/"] }
                             })

          config_processor = GitlabCiYamlProcessor.new(config, path)

          expect(config_processor.builds_for_stage_and_ref(type, "master").size).to eq(0)
        end

        it "returns builds if only has specified this branch" do
          config = YAML.dump({
                               before_script: ["pwd"],
                               rspec: { script: "rspec", only: ["master"] }
                             })
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
61

62
63
64
65
66
67
68
69
70
71
          config_processor = GitlabCiYamlProcessor.new(config, path)

          expect(config_processor.builds_for_stage_and_ref(type, "master").size).to eq(1)
        end

        it "returns builds if only has a list of branches including specified" do
          config = YAML.dump({
                               before_script: ["pwd"],
                               rspec: { script: "rspec", type: type, only: ["master", "deploy"] }
                             })
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
72

73
74
75
76
77
78
79
80
81
82
83
84
          config_processor = GitlabCiYamlProcessor.new(config, path)

          expect(config_processor.builds_for_stage_and_ref(type, "deploy").size).to eq(1)
        end

        it "returns builds if only has a branches keyword specified" do
          config = YAML.dump({
                               before_script: ["pwd"],
                               rspec: { script: "rspec", type: type, only: ["branches"] }
                             })

          config_processor = GitlabCiYamlProcessor.new(config, path)
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
85

86
87
88
89
90
91
92
93
94
95
          expect(config_processor.builds_for_stage_and_ref(type, "deploy").size).to eq(1)
        end

        it "does not return builds if only has a tags keyword" do
          config = YAML.dump({
                               before_script: ["pwd"],
                               rspec: { script: "rspec", type: type, only: ["tags"] }
                             })

          config_processor = GitlabCiYamlProcessor.new(config, path)
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
96

97
98
99
          expect(config_processor.builds_for_stage_and_ref(type, "deploy").size).to eq(0)
        end

100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
        it "returns builds if only has a triggers keyword specified and a trigger is provided" do
          config = YAML.dump({
                               before_script: ["pwd"],
                               rspec: { script: "rspec", type: type, only: ["triggers"] }
                             })

          config_processor = GitlabCiYamlProcessor.new(config, path)

          expect(config_processor.builds_for_stage_and_ref(type, "deploy", false, true).size).to eq(1)
        end

        it "does not return builds if only has a triggers keyword specified and no trigger is provided" do
          config = YAML.dump({
                               before_script: ["pwd"],
                               rspec: { script: "rspec", type: type, only: ["triggers"] }
                             })

          config_processor = GitlabCiYamlProcessor.new(config, path)

          expect(config_processor.builds_for_stage_and_ref(type, "deploy").size).to eq(0)
        end

122
123
124
125
126
127
128
129
130
131
        it "returns builds if only has current repository path" do
          config = YAML.dump({
                               before_script: ["pwd"],
                               rspec: { script: "rspec", type: type, only: ["branches@path"] }
                             })

          config_processor = GitlabCiYamlProcessor.new(config, path)

          expect(config_processor.builds_for_stage_and_ref(type, "deploy").size).to eq(1)
        end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
132

133
134
135
136
137
138
139
140
141
142
143
144
145
146
        it "does not return builds if only has different repository path" do
          config = YAML.dump({
                               before_script: ["pwd"],
                               rspec: { script: "rspec", type: type, only: ["branches@fork"] }
                             })

          config_processor = GitlabCiYamlProcessor.new(config, path)

          expect(config_processor.builds_for_stage_and_ref(type, "deploy").size).to eq(0)
        end

        it "returns build only for specified type" do
          config = YAML.dump({
                               before_script: ["pwd"],
Kamil Trzcinski's avatar
Kamil Trzcinski committed
147
                               rspec: { script: "rspec", type: "test", only: ["master", "deploy"] },
148
149
150
                               staging: { script: "deploy", type: "deploy", only: ["master", "deploy"] },
                               production: { script: "deploy", type: "deploy", only: ["master@path", "deploy"] },
                             })
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
151

Kamil Trzcinski's avatar
Kamil Trzcinski committed
152
          config_processor = GitlabCiYamlProcessor.new(config, 'fork')
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
153

154
          expect(config_processor.builds_for_stage_and_ref("deploy", "deploy").size).to eq(2)
Kamil Trzcinski's avatar
Kamil Trzcinski committed
155
156
          expect(config_processor.builds_for_stage_and_ref("test", "deploy").size).to eq(1)
          expect(config_processor.builds_for_stage_and_ref("deploy", "master").size).to eq(1)
157
        end
158
159
160
161
162
163
164

        context 'for invalid value' do
          let(:config) { { rspec: { script: "rspec", type: "test", only: only } } }
          let(:processor) { GitlabCiYamlProcessor.new(YAML.dump(config)) }

          shared_examples 'raises an error' do
            it do
165
              expect { processor }.to raise_error(GitlabCiYamlProcessor::ValidationError, 'jobs:rspec:only config should be an array of strings or regexps')
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
            end
          end

          context 'when it is integer' do
            let(:only) { 1 }

            it_behaves_like 'raises an error'
          end

          context 'when it is an array of integers' do
            let(:only) { [1, 1] }

            it_behaves_like 'raises an error'
          end

          context 'when it is invalid regex' do
            let(:only) { ["/*invalid/"] }

            it_behaves_like 'raises an error'
          end
        end
Valery Sizov's avatar
Valery Sizov committed
187
      end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
188

189
      describe 'except' do
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
        it "returns builds if except has another branch" do
          config = YAML.dump({
                               before_script: ["pwd"],
                               rspec: { script: "rspec", except: ["deploy"] }
                             })

          config_processor = GitlabCiYamlProcessor.new(config, path)

          expect(config_processor.builds_for_stage_and_ref(type, "master").size).to eq(1)
        end

        it "returns builds if except has regexp with another branch" do
          config = YAML.dump({
                               before_script: ["pwd"],
                               rspec: { script: "rspec", except: ["/^deploy$/"] }
                             })

          config_processor = GitlabCiYamlProcessor.new(config, path)

          expect(config_processor.builds_for_stage_and_ref(type, "master").size).to eq(1)
        end

        it "does not return builds if except has specified this branch" do
          config = YAML.dump({
                               before_script: ["pwd"],
                               rspec: { script: "rspec", except: ["master"] }
                             })

          config_processor = GitlabCiYamlProcessor.new(config, path)

          expect(config_processor.builds_for_stage_and_ref(type, "master").size).to eq(0)
        end

        it "does not return builds if except has a list of branches including specified" do
          config = YAML.dump({
                               before_script: ["pwd"],
                               rspec: { script: "rspec", type: type, except: ["master", "deploy"] }
                             })

          config_processor = GitlabCiYamlProcessor.new(config, path)

          expect(config_processor.builds_for_stage_and_ref(type, "deploy").size).to eq(0)
        end

        it "does not return builds if except has a branches keyword specified" do
          config = YAML.dump({
                               before_script: ["pwd"],
                               rspec: { script: "rspec", type: type, except: ["branches"] }
                             })

          config_processor = GitlabCiYamlProcessor.new(config, path)
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
241

242
243
244
245
246
247
248
249
250
251
252
253
254
255
          expect(config_processor.builds_for_stage_and_ref(type, "deploy").size).to eq(0)
        end

        it "returns builds if except has a tags keyword" do
          config = YAML.dump({
                               before_script: ["pwd"],
                               rspec: { script: "rspec", type: type, except: ["tags"] }
                             })

          config_processor = GitlabCiYamlProcessor.new(config, path)

          expect(config_processor.builds_for_stage_and_ref(type, "deploy").size).to eq(1)
        end

256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
        it "does not return builds if except has a triggers keyword specified and a trigger is provided" do
          config = YAML.dump({
                               before_script: ["pwd"],
                               rspec: { script: "rspec", type: type, except: ["triggers"] }
                             })

          config_processor = GitlabCiYamlProcessor.new(config, path)

          expect(config_processor.builds_for_stage_and_ref(type, "deploy", false, true).size).to eq(0)
        end

        it "returns builds if except has a triggers keyword specified and no trigger is provided" do
          config = YAML.dump({
                               before_script: ["pwd"],
                               rspec: { script: "rspec", type: type, except: ["triggers"] }
                             })

          config_processor = GitlabCiYamlProcessor.new(config, path)

          expect(config_processor.builds_for_stage_and_ref(type, "deploy").size).to eq(1)
        end

278
279
280
281
282
283
284
285
286
287
        it "does not return builds if except has current repository path" do
          config = YAML.dump({
                               before_script: ["pwd"],
                               rspec: { script: "rspec", type: type, except: ["branches@path"] }
                             })

          config_processor = GitlabCiYamlProcessor.new(config, path)

          expect(config_processor.builds_for_stage_and_ref(type, "deploy").size).to eq(0)
        end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
288

289
290
291
292
293
294
295
296
297
298
299
300
301
302
        it "returns builds if except has different repository path" do
          config = YAML.dump({
                               before_script: ["pwd"],
                               rspec: { script: "rspec", type: type, except: ["branches@fork"] }
                             })

          config_processor = GitlabCiYamlProcessor.new(config, path)

          expect(config_processor.builds_for_stage_and_ref(type, "deploy").size).to eq(1)
        end

        it "returns build except specified type" do
          config = YAML.dump({
                               before_script: ["pwd"],
Kamil Trzcinski's avatar
Kamil Trzcinski committed
303
304
305
                               rspec: { script: "rspec", type: "test", except: ["master", "deploy", "test@fork"] },
                               staging: { script: "deploy", type: "deploy", except: ["master"] },
                               production: { script: "deploy", type: "deploy", except: ["master@fork"] },
306
307
                             })

Kamil Trzcinski's avatar
Kamil Trzcinski committed
308
          config_processor = GitlabCiYamlProcessor.new(config, 'fork')
309

Kamil Trzcinski's avatar
Kamil Trzcinski committed
310
311
312
          expect(config_processor.builds_for_stage_and_ref("deploy", "deploy").size).to eq(2)
          expect(config_processor.builds_for_stage_and_ref("test", "test").size).to eq(0)
          expect(config_processor.builds_for_stage_and_ref("deploy", "master").size).to eq(0)
313
        end
314
315
316
317
318
319
320

        context 'for invalid value' do
          let(:config) { { rspec: { script: "rspec", except: except } } }
          let(:processor) { GitlabCiYamlProcessor.new(YAML.dump(config)) }

          shared_examples 'raises an error' do
            it do
321
              expect { processor }.to raise_error(GitlabCiYamlProcessor::ValidationError, 'jobs:rspec:except config should be an array of strings or regexps')
322
323
324
325
326
            end
          end

          context 'when it is integer' do
            let(:except) { 1 }
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
327

328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
            it_behaves_like 'raises an error'
          end

          context 'when it is an array of integers' do
            let(:except) { [1, 1] }

            it_behaves_like 'raises an error'
          end

          context 'when it is invalid regex' do
            let(:except) { ["/*invalid/"] }

            it_behaves_like 'raises an error'
          end
        end
Valery Sizov's avatar
Valery Sizov committed
343
      end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
344
    end
345

346
347
348
    describe "Scripts handling" do
      let(:config_data) { YAML.dump(config) }
      let(:config_processor) { GitlabCiYamlProcessor.new(config_data, path) }
349

350
      subject { config_processor.builds_for_stage_and_ref("test", "master").first }
351

352
353
      describe "before_script" do
        context "in global context" do
Kamil Trzcinski's avatar
Kamil Trzcinski committed
354
          let(:config) do
355
356
357
358
            {
              before_script: ["global script"],
              test: { script: ["script"] }
            }
Kamil Trzcinski's avatar
Kamil Trzcinski committed
359
          end
360

361
362
363
364
          it "return commands with scripts concencaced" do
            expect(subject[:commands]).to eq("global script\nscript")
          end
        end
365

366
        context "overwritten in local context" do
Kamil Trzcinski's avatar
Kamil Trzcinski committed
367
          let(:config) do
368
369
370
371
            {
              before_script: ["global script"],
              test: { before_script: ["local script"], script: ["script"] }
            }
Kamil Trzcinski's avatar
Kamil Trzcinski committed
372
          end
373
374
375
376
377
378
379
380

          it "return commands with scripts concencaced" do
            expect(subject[:commands]).to eq("local script\nscript")
          end
        end
      end

      describe "script" do
Kamil Trzcinski's avatar
Kamil Trzcinski committed
381
        let(:config) do
382
383
384
          {
            test: { script: ["script"] }
          }
Kamil Trzcinski's avatar
Kamil Trzcinski committed
385
        end
386
387
388
389
390
391

        it "return commands with scripts concencaced" do
          expect(subject[:commands]).to eq("script")
        end
      end

392
      describe "after_script" do
393
        context "in global context" do
Kamil Trzcinski's avatar
Kamil Trzcinski committed
394
          let(:config) do
395
            {
396
              after_script: ["after_script"],
397
398
              test: { script: ["script"] }
            }
Kamil Trzcinski's avatar
Kamil Trzcinski committed
399
          end
400

401
402
          it "return after_script in options" do
            expect(subject[:options][:after_script]).to eq(["after_script"])
403
404
          end
        end
405
406

        context "overwritten in local context" do
Kamil Trzcinski's avatar
Kamil Trzcinski committed
407
          let(:config) do
408
            {
409
410
              after_script: ["local after_script"],
              test: { after_script: ["local after_script"], script: ["script"] }
411
            }
Kamil Trzcinski's avatar
Kamil Trzcinski committed
412
          end
413

414
415
          it "return after_script in options" do
            expect(subject[:options][:after_script]).to eq(["local after_script"])
416
417
          end
        end
418
419
      end
    end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
420

Valery Sizov's avatar
Valery Sizov committed
421
422
423
424
425
426
427
428
    describe "Image and service handling" do
      it "returns image and service when defined" do
        config = YAML.dump({
                             image: "ruby:2.1",
                             services: ["mysql"],
                             before_script: ["pwd"],
                             rspec: { script: "rspec" }
                           })
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
429

430
        config_processor = GitlabCiYamlProcessor.new(config, path)
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
431

Valery Sizov's avatar
Valery Sizov committed
432
433
434
        expect(config_processor.builds_for_stage_and_ref("test", "master").size).to eq(1)
        expect(config_processor.builds_for_stage_and_ref("test", "master").first).to eq({
          stage: "test",
435
          stage_idx: 1,
Valery Sizov's avatar
Valery Sizov committed
436
          name: :rspec,
Kamil Trzcinski's avatar
Kamil Trzcinski committed
437
438
          commands: "pwd\nrspec",
          tag_list: [],
Valery Sizov's avatar
Valery Sizov committed
439
440
441
442
          options: {
            image: "ruby:2.1",
            services: ["mysql"]
          },
443
          allow_failure: false,
444
445
          when: "on_success",
          environment: nil,
446
          yaml_variables: []
Valery Sizov's avatar
Valery Sizov committed
447
448
        })
      end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
449

Valery Sizov's avatar
Valery Sizov committed
450
451
452
453
454
455
456
457
      it "returns image and service when overridden for job" do
        config = YAML.dump({
                             image:         "ruby:2.1",
                             services:      ["mysql"],
                             before_script: ["pwd"],
                             rspec:         { image: "ruby:2.5", services: ["postgresql"], script: "rspec" }
                           })

458
        config_processor = GitlabCiYamlProcessor.new(config, path)
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
459

Valery Sizov's avatar
Valery Sizov committed
460
461
462
        expect(config_processor.builds_for_stage_and_ref("test", "master").size).to eq(1)
        expect(config_processor.builds_for_stage_and_ref("test", "master").first).to eq({
          stage: "test",
463
          stage_idx: 1,
Valery Sizov's avatar
Valery Sizov committed
464
          name: :rspec,
Kamil Trzcinski's avatar
Kamil Trzcinski committed
465
466
          commands: "pwd\nrspec",
          tag_list: [],
Valery Sizov's avatar
Valery Sizov committed
467
468
469
470
          options: {
            image: "ruby:2.5",
            services: ["postgresql"]
          },
471
          allow_failure: false,
472
473
          when: "on_success",
          environment: nil,
474
          yaml_variables: []
Valery Sizov's avatar
Valery Sizov committed
475
476
        })
      end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
477
478
    end

479
    describe 'Variables' do
480
481
482
483
      let(:config_processor) { GitlabCiYamlProcessor.new(YAML.dump(config), path) }

      subject { config_processor.builds.first[:yaml_variables] }

484
      context 'when global variables are defined' do
485
486
487
488
489
        let(:variables) do
          { VAR1: 'value1', VAR2: 'value2' }
        end
        let(:config) do
          {
490
491
492
            variables: variables,
            before_script: ['pwd'],
            rspec: { script: 'rspec' }
493
494
495
496
497
498
499
500
501
502
          }
        end

        it 'returns global variables' do
          expect(subject).to contain_exactly(
            { key: :VAR1, value: 'value1', public: true },
            { key: :VAR2, value: 'value2', public: true }
          )
        end
      end
503

504
505
506
507
508
509
510
511
512
513
514
515
516
517
      context 'when job and global variables are defined' do
        let(:global_variables) do
          { VAR1: 'global1', VAR3: 'global3' }
        end
        let(:job_variables) do
          { VAR1: 'value1', VAR2: 'value2' }
        end
        let(:config) do
          {
            before_script: ['pwd'],
            variables: global_variables,
            rspec: { script: 'rspec', variables: job_variables }
          }
        end
518

519
520
521
522
523
524
        it 'returns all unique variables' do
          expect(subject).to contain_exactly(
            { key: :VAR3, value: 'global3', public: true },
            { key: :VAR1, value: 'value1', public: true },
            { key: :VAR2, value: 'value2', public: true }
          )
525
526
        end
      end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
527

528
      context 'when job variables are defined' do
529
530
531
532
533
534
535
536
537
        let(:config) do
          {
            before_script: ['pwd'],
            rspec: { script: 'rspec', variables: variables }
          }
        end

        context 'when also global variables are defined' do
        end
538

539
540
541
542
        context 'when syntax is correct' do
          let(:variables) do
            { VAR1: 'value1', VAR2: 'value2' }
          end
543

544
545
546
547
548
          it 'returns job variables' do
            expect(subject).to contain_exactly(
              { key: :VAR1, value: 'value1', public: true },
              { key: :VAR2, value: 'value2', public: true }
            )
549
550
551
552
          end
        end

        context 'when syntax is incorrect' do
553
          context 'when variables defined but invalid' do
554
555
556
            let(:variables) do
              [ :VAR1, 'value1', :VAR2, 'value2' ]
            end
557

558
559
            it 'raises error' do
              expect { subject }
560
                .to raise_error(GitlabCiYamlProcessor::ValidationError,
561
                                 /jobs:rspec:variables config should be a hash of key value pairs/)
562
563
            end
          end
564

565
          context 'when variables key defined but value not specified' do
566
567
568
569
            let(:variables) do
              nil
            end

570
571
            it 'returns empty array' do
              ##
572
573
              # When variables config is empty, we assume this is a valid
              # configuration, see issue #18775
574
              #
575
576
              expect(subject).to be_an_instance_of(Array)
              expect(subject).to be_empty
577
            end
578
          end
579
        end
Valery Sizov's avatar
Valery Sizov committed
580
      end
581
582

      context 'when job variables are not defined' do
583
584
        let(:config) do
          {
585
586
            before_script: ['pwd'],
            rspec: { script: 'rspec' }
587
588
          }
        end
589

590
591
592
        it 'returns empty array' do
          expect(subject).to be_an_instance_of(Array)
          expect(subject).to be_empty
593
594
        end
      end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
595
596
    end

597
598
599
600
601
602
603
    describe "When" do
      %w(on_success on_failure always).each do |when_state|
        it "returns #{when_state} when defined" do
          config = YAML.dump({
                               rspec: { script: "rspec", when: when_state }
                             })

604
          config_processor = GitlabCiYamlProcessor.new(config, path)
Kamil Trzcinski's avatar
Kamil Trzcinski committed
605

606
607
608
609
610
611
612
          builds = config_processor.builds_for_stage_and_ref("test", "master")
          expect(builds.size).to eq(1)
          expect(builds.first[:when]).to eq(when_state)
        end
      end
    end

613
614
615
616
617
618
619
620
621
    describe 'cache' do
      context 'when cache definition has unknown keys' do
        it 'raises relevant validation error' do
          config = YAML.dump(
            { cache: { untracked: true, invalid: 'key' },
              rspec: { script: 'rspec' } })

          expect { GitlabCiYamlProcessor.new(config) }.to raise_error(
            GitlabCiYamlProcessor::ValidationError,
622
            'cache config contains unknown keys: invalid'
623
624
625
626
          )
        end
      end

627
628
      it "returns cache when defined globally" do
        config = YAML.dump({
629
                             cache: { paths: ["logs/", "binaries/"], untracked: true, key: 'key' },
630
631
632
633
634
635
636
637
638
639
640
                             rspec: {
                               script: "rspec"
                             }
                           })

        config_processor = GitlabCiYamlProcessor.new(config)

        expect(config_processor.builds_for_stage_and_ref("test", "master").size).to eq(1)
        expect(config_processor.builds_for_stage_and_ref("test", "master").first[:options][:cache]).to eq(
          paths: ["logs/", "binaries/"],
          untracked: true,
641
          key: 'key',
642
643
644
645
646
647
        )
      end

      it "returns cache when defined in a job" do
        config = YAML.dump({
                             rspec: {
648
                               cache: { paths: ["logs/", "binaries/"], untracked: true, key: 'key' },
649
650
651
652
653
654
655
656
657
658
                               script: "rspec"
                             }
                           })

        config_processor = GitlabCiYamlProcessor.new(config)

        expect(config_processor.builds_for_stage_and_ref("test", "master").size).to eq(1)
        expect(config_processor.builds_for_stage_and_ref("test", "master").first[:options][:cache]).to eq(
          paths: ["logs/", "binaries/"],
          untracked: true,
659
          key: 'key',
660
661
662
663
664
        )
      end

      it "overwrite cache when defined for a job and globally" do
        config = YAML.dump({
665
                             cache: { paths: ["logs/", "binaries/"], untracked: true, key: 'global' },
666
667
                             rspec: {
                               script: "rspec",
668
                               cache: { paths: ["test/"], untracked: false, key: 'local' },
669
670
671
672
673
674
675
676
677
                             }
                           })

        config_processor = GitlabCiYamlProcessor.new(config)

        expect(config_processor.builds_for_stage_and_ref("test", "master").size).to eq(1)
        expect(config_processor.builds_for_stage_and_ref("test", "master").first[:options][:cache]).to eq(
          paths: ["test/"],
          untracked: false,
678
          key: 'local',
679
680
681
682
        )
      end
    end

Kamil Trzcinski's avatar
Kamil Trzcinski committed
683
684
685
686
687
688
    describe "Artifacts" do
      it "returns artifacts when defined" do
        config = YAML.dump({
                             image:         "ruby:2.1",
                             services:      ["mysql"],
                             before_script: ["pwd"],
689
                             rspec:         {
690
691
692
693
694
695
                               artifacts: {
                                 paths: ["logs/", "binaries/"],
                                 untracked: true,
                                 name: "custom_name",
                                 expire_in: "7d"
                               },
696
697
                               script: "rspec"
                             }
Kamil Trzcinski's avatar
Kamil Trzcinski committed
698
699
700
701
702
703
704
705
706
707
708
709
710
711
                           })

        config_processor = GitlabCiYamlProcessor.new(config)

        expect(config_processor.builds_for_stage_and_ref("test", "master").size).to eq(1)
        expect(config_processor.builds_for_stage_and_ref("test", "master").first).to eq({
          stage: "test",
          stage_idx: 1,
          name: :rspec,
          commands: "pwd\nrspec",
          tag_list: [],
          options: {
            image: "ruby:2.1",
            services: ["mysql"],
712
            artifacts: {
713
              name: "custom_name",
714
              paths: ["logs/", "binaries/"],
715
716
              untracked: true,
              expire_in: "7d"
717
            }
Kamil Trzcinski's avatar
Kamil Trzcinski committed
718
719
          },
          when: "on_success",
720
721
          allow_failure: false,
          environment: nil,
722
          yaml_variables: []
Kamil Trzcinski's avatar
Kamil Trzcinski committed
723
724
        })
      end
Kamil Trzcinski's avatar
Kamil Trzcinski committed
725

Kamil Trzcinski's avatar
Kamil Trzcinski committed
726
      %w[on_success on_failure always].each do |when_state|
Kamil Trzcinski's avatar
Kamil Trzcinski committed
727
728
729
730
731
732
733
734
735
        it "returns artifacts for when #{when_state}  defined" do
          config = YAML.dump({
                               rspec: {
                                 script: "rspec",
                                 artifacts: { paths: ["logs/", "binaries/"], when: when_state }
                               }
                             })

          config_processor = GitlabCiYamlProcessor.new(config, path)
Kamil Trzcinski's avatar
Kamil Trzcinski committed
736

Kamil Trzcinski's avatar
Kamil Trzcinski committed
737
738
739
740
741
          builds = config_processor.builds_for_stage_and_ref("test", "master")
          expect(builds.size).to eq(1)
          expect(builds.first[:options][:artifacts][:when]).to eq(when_state)
        end
      end
Kamil Trzcinski's avatar
Kamil Trzcinski committed
742
743
    end

744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
    describe '#environment' do
      let(:config) do
        {
          deploy_to_production: { stage: 'deploy', script: 'test', environment: environment }
        }
      end

      let(:processor) { GitlabCiYamlProcessor.new(YAML.dump(config)) }
      let(:builds) { processor.builds_for_stage_and_ref('deploy', 'master') }

      context 'when a production environment is specified' do
        let(:environment) { 'production' }

        it 'does return production' do
          expect(builds.size).to eq(1)
          expect(builds.first[:environment]).to eq(environment)
        end
      end

      context 'when no environment is specified' do
        let(:environment) { nil }

        it 'does return nil environment' do
          expect(builds.size).to eq(1)
          expect(builds.first[:environment]).to be_nil
        end
      end

      context 'is not a string' do
        let(:environment) { 1 }

        it 'raises error' do
776
          expect { builds }.to raise_error("jobs:deploy_to_production environment #{Gitlab::Regex.environment_name_regex_message}")
777
778
779
780
781
782
783
        end
      end

      context 'is not a valid string' do
        let(:environment) { 'production staging' }

        it 'raises error' do
784
          expect { builds }.to raise_error("jobs:deploy_to_production environment #{Gitlab::Regex.environment_name_regex_message}")
785
786
787
788
        end
      end
    end

789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
    describe "Dependencies" do
      let(:config) do
        {
          build1: { stage: 'build', script: 'test' },
          build2: { stage: 'build', script: 'test' },
          test1: { stage: 'test', script: 'test', dependencies: dependencies },
          test2: { stage: 'test', script: 'test' },
          deploy: { stage: 'test', script: 'test' }
        }
      end

      subject { GitlabCiYamlProcessor.new(YAML.dump(config)) }

      context 'no dependencies' do
        let(:dependencies) { }

805
        it { expect { subject }.not_to raise_error }
806
807
808
      end

      context 'dependencies to builds' do
809
        let(:dependencies) { ['build1', 'build2'] }
810

811
        it { expect { subject }.not_to raise_error }
812
813
      end

814
815
816
      context 'dependencies to builds defined as symbols' do
        let(:dependencies) { [:build1, :build2] }

817
        it { expect { subject }.not_to raise_error }
818
819
      end

820
      context 'undefined dependency' do
821
        let(:dependencies) { ['undefined'] }
822
823
824
825
826

        it { expect { subject }.to raise_error(GitlabCiYamlProcessor::ValidationError, 'test1 job: undefined dependency: undefined') }
      end

      context 'dependencies to deploy' do
827
        let(:dependencies) { ['deploy'] }
828
829
830
831
832

        it { expect { subject }.to raise_error(GitlabCiYamlProcessor::ValidationError, 'test1 job: dependency deploy is not defined in prior stages') }
      end
    end

833
834
835
836
    describe "Hidden jobs" do
      let(:config_processor) { GitlabCiYamlProcessor.new(config) }
      subject { config_processor.builds_for_stage_and_ref("test", "master") }

837
      shared_examples 'hidden_job_handling' do
Tomasz Maczukin's avatar
Tomasz Maczukin committed
838
        it "doesn't create jobs that start with dot" do
839
840
841
842
843
844
845
846
847
          expect(subject.size).to eq(1)
          expect(subject.first).to eq({
            stage: "test",
            stage_idx: 1,
            name: :normal_job,
            commands: "test",
            tag_list: [],
            options: {},
            when: "on_success",
848
849
            allow_failure: false,
            environment: nil,
850
            yaml_variables: []
851
852
853
854
          })
        end
      end

Tomasz Maczukin's avatar
Tomasz Maczukin committed
855
      context 'when hidden job have a script definition' do
856
857
858
859
860
861
862
863
864
865
        let(:config) do
          YAML.dump({
                      '.hidden_job' => { image: 'ruby:2.1', script: 'test' },
                      'normal_job' => { script: 'test' }
                    })
        end

        it_behaves_like 'hidden_job_handling'
      end

Tomasz Maczukin's avatar
Tomasz Maczukin committed
866
      context "when hidden job doesn't have a script definition" do
867
868
869
870
871
872
873
874
        let(:config) do
          YAML.dump({
                      '.hidden_job' => { image: 'ruby:2.1' },
                      'normal_job' => { script: 'test' }
                    })
        end

        it_behaves_like 'hidden_job_handling'
875
876
877
      end
    end

878
    describe "YAML Alias/Anchor" do
879
880
881
882
883
884
885
886
887
888
889
890
891
892
      let(:config_processor) { GitlabCiYamlProcessor.new(config) }
      subject { config_processor.builds_for_stage_and_ref("build", "master") }

      shared_examples 'job_templates_handling' do
        it "is correctly supported for jobs" do
          expect(subject.size).to eq(2)
          expect(subject.first).to eq({
            stage: "build",
            stage_idx: 0,
            name: :job1,
            commands: "execute-script-for-job",
            tag_list: [],
            options: {},
            when: "on_success",
893
894
            allow_failure: false,
            environment: nil,
895
            yaml_variables: []
896
897
898
899
900
901
902
903
904
          })
          expect(subject.second).to eq({
            stage: "build",
            stage_idx: 0,
            name: :job2,
            commands: "execute-script-for-job",
            tag_list: [],
            options: {},
            when: "on_success",
905
906
            allow_failure: false,
            environment: nil,
907
            yaml_variables: []
908
909
910
911
          })
        end
      end

Tomasz Maczukin's avatar
Tomasz Maczukin committed
912
      context 'when template is a job' do
Tomasz Maczukin's avatar
Tomasz Maczukin committed
913
        let(:config) do
914
          <<EOT
915
job1: &JOBTMPL
916
  stage: build
917
918
919
920
  script: execute-script-for-job

job2: *JOBTMPL
EOT
921
        end
922

923
924
925
        it_behaves_like 'job_templates_handling'
      end

Tomasz Maczukin's avatar
Tomasz Maczukin committed
926
      context 'when template is a hidden job' do
Tomasz Maczukin's avatar
Tomasz Maczukin committed
927
        let(:config) do
928
929
930
931
932
933
934
935
936
937
          <<EOT
.template: &JOBTMPL
  stage: build
  script: execute-script-for-job

job1: *JOBTMPL

job2: *JOBTMPL
EOT
        end
938

939
940
941
        it_behaves_like 'job_templates_handling'
      end

Tomasz Maczukin's avatar
Tomasz Maczukin committed
942
      context 'when job adds its own keys to a template definition' do
Tomasz Maczukin's avatar
Tomasz Maczukin committed
943
        let(:config) do
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
          <<EOT
.template: &JOBTMPL
  stage: build

job1:
  <<: *JOBTMPL
  script: execute-script-for-job

job2:
  <<: *JOBTMPL
  script: execute-script-for-job
EOT
        end

        it_behaves_like 'job_templates_handling'
959
960
961
      end
    end

Valery Sizov's avatar
Valery Sizov committed
962
    describe "Error handling" do
963
964
965
966
      it "fails to parse YAML" do
        expect{GitlabCiYamlProcessor.new("invalid: yaml: test")}.to raise_error(Psych::SyntaxError)
      end

Valery Sizov's avatar
Valery Sizov committed
967
      it "indicates that object is invalid" do
968
        expect{GitlabCiYamlProcessor.new("invalid_yaml")}.to raise_error(GitlabCiYamlProcessor::ValidationError)
Valery Sizov's avatar
Valery Sizov committed
969
970
971
972
973
      end

      it "returns errors if tags parameter is invalid" do
        config = YAML.dump({ rspec: { script: "test", tags: "mysql" } })
        expect do
974
          GitlabCiYamlProcessor.new(config, path)
975
        end.to raise_error(GitlabCiYamlProcessor::ValidationError, "jobs:rspec tags should be an array of strings")
Valery Sizov's avatar
Valery Sizov committed
976
      end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
977

Valery Sizov's avatar
Valery Sizov committed
978
979
980
      it "returns errors if before_script parameter is invalid" do
        config = YAML.dump({ before_script: "bundle update", rspec: { script: "test" } })
        expect do
981
          GitlabCiYamlProcessor.new(config, path)
982
        end.to raise_error(GitlabCiYamlProcessor::ValidationError, "before_script config should be an array of strings")
Valery Sizov's avatar
Valery Sizov committed
983
      end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
984

985
986
987
988
      it "returns errors if job before_script parameter is not an array of strings" do
        config = YAML.dump({ rspec: { script: "test", before_script: [10, "test"] } })
        expect do
          GitlabCiYamlProcessor.new(config, path)
989
        end.to raise_error(GitlabCiYamlProcessor::ValidationError, "jobs:rspec:before_script config should be an array of strings")
990
991
      end

992
993
      it "returns errors if after_script parameter is invalid" do
        config = YAML.dump({ after_script: "bundle update", rspec: { script: "test" } })
994
995
        expect do
          GitlabCiYamlProcessor.new(config, path)
996
        end.to raise_error(GitlabCiYamlProcessor::ValidationError, "after_script config should be an array of strings")
997
998
      end

999
1000
      it "returns errors if job after_script parameter is not an array of strings" do
        config = YAML.dump({ rspec: { script: "test", after_script: [10, "test"] } })
1001
1002
        expect do
          GitlabCiYamlProcessor.new(config, path)
1003
        end.to raise_error(GitlabCiYamlProcessor::ValidationError, "jobs:rspec:after_script config should be an array of strings")
1004
1005
      end

Valery Sizov's avatar
Valery Sizov committed
1006
1007
1008
      it "returns errors if image parameter is invalid" do
        config = YAML.dump({ image: ["test"], rspec: { script: "test" } })
        expect do
1009
          GitlabCiYamlProcessor.new(config, path)
1010
        end.to raise_error(GitlabCiYamlProcessor::ValidationError, "image config should be a string")
Valery Sizov's avatar
Valery Sizov committed
1011
1012
      end

Kamil Trzcinski's avatar
Kamil Trzcinski committed
1013
1014
1015
      it "returns errors if job name is blank" do
        config = YAML.dump({ '' => { script: "test" } })
        expect do
1016
          GitlabCiYamlProcessor.new(config, path)
1017
        end.to raise_error(GitlabCiYamlProcessor::ValidationError, "jobs:job name can't be blank")
Kamil Trzcinski's avatar
Kamil Trzcinski committed
1018
1019
1020
1021
1022
      end

      it "returns errors if job name is non-string" do
        config = YAML.dump({ 10 => { script: "test" } })
        expect do
1023
          GitlabCiYamlProcessor.new(config, path)
1024
        end.to raise_error(GitlabCiYamlProcessor::ValidationError, "jobs:10 name should be a symbol")
Kamil Trzcinski's avatar
Kamil Trzcinski committed
1025
1026
      end

Valery Sizov's avatar
Valery Sizov committed
1027
1028
1029
      it "returns errors if job image parameter is invalid" do
        config = YAML.dump({ rspec: { script: "test", image: ["test"] } })
        expect do
1030
          GitlabCiYamlProcessor.new(config, path)
1031
        end.to raise_error(GitlabCiYamlProcessor::ValidationError, "jobs:rspec:image config should be a string")
Valery Sizov's avatar
Valery Sizov committed
1032
      end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
1033

Valery Sizov's avatar
Valery Sizov committed
1034
1035
1036
      it "returns errors if services parameter is not an array" do
        config = YAML.dump({ services: "test", rspec: { script: "test" } })
        expect do
1037
          GitlabCiYamlProcessor.new(config, path)
1038
        end.to raise_error(GitlabCiYamlProcessor::ValidationError, "services config should be an array of strings")
Valery Sizov's avatar
Valery Sizov committed
1039
1040
1041
1042
1043
      end

      it "returns errors if services parameter is not an array of strings" do
        config = YAML.dump({ services: [10, "test"], rspec: { script: "test" } })
        expect do
1044
          GitlabCiYamlProcessor.new(config, path)
1045
        end.to raise_error(GitlabCiYamlProcessor::ValidationError, "services config should be an array of strings")
Valery Sizov's avatar
Valery Sizov committed
1046
      end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
1047

Valery Sizov's avatar
Valery Sizov committed
1048
1049
1050
      it "returns errors if job services parameter is not an array" do
        config = YAML.dump({ rspec: { script: "test", services: "test" } })
        expect do
1051
          GitlabCiYamlProcessor.new(config, path)
1052
        end.to raise_error(GitlabCiYamlProcessor::ValidationError, "jobs:rspec:services config should be an array of strings")
Valery Sizov's avatar
Valery Sizov committed
1053
      end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
1054

Valery Sizov's avatar
Valery Sizov committed
1055
1056
1057
      it "returns errors if job services parameter is not an array of strings" do
        config = YAML.dump({ rspec: { script: "test", services: [10, "test"] } })
        expect do
1058
          GitlabCiYamlProcessor.new(config, path)
1059
        end.to raise_error(GitlabCiYamlProcessor::ValidationError, "jobs:rspec:services config should be an array of strings")
Valery Sizov's avatar
Valery Sizov committed
1060
1061
      end

1062
      it "returns error if job configuration is invalid" do
Valery Sizov's avatar
Valery Sizov committed
1063
1064
        config = YAML.dump({ extra: "bundle update" })
        expect do
1065
          GitlabCiYamlProcessor.new(config, path)
1066
        end.to raise_error(GitlabCiYamlProcessor::ValidationError, "jobs:extra config should be a hash")
Valery Sizov's avatar
Valery Sizov committed
1067
      end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
1068

Valery Sizov's avatar
Valery Sizov committed
1069
1070
1071
      it "returns errors if there are unknown parameters that are hashes, but doesn't have a script" do
        config = YAML.dump({ extra: { services: "test" } })
        expect do
1072
          GitlabCiYamlProcessor.new(config, path)
1073
        end.to raise_error(GitlabCiYamlProcessor::ValidationError, "jobs:extra:services config should be an array of strings")
Valery Sizov's avatar
Valery Sizov committed
1074
1075
      end

1076
      it "returns errors if there are no jobs defined" do
Valery Sizov's avatar
Valery Sizov committed
1077
1078
        config = YAML.dump({ before_script: ["bundle update"] })
        expect do
1079
          GitlabCiYamlProcessor.new(config, path)
1080
1081
1082
1083
        end.to raise_error(GitlabCiYamlProcessor::ValidationError, "jobs config should contain at least one visible job")
      end

      it "returns errors if there are no visible jobs defined" do
1084
        config = YAML.dump({ before_script: ["bundle update"], '.hidden'.to_sym => { script: 'ls' } })
1085
1086
1087
        expect do
          GitlabCiYamlProcessor.new(config, path)
        end.to raise_error(GitlabCiYamlProcessor::ValidationError, "jobs config should contain at least one visible job")
Valery Sizov's avatar
Valery Sizov committed
1088
      end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
1089

Valery Sizov's avatar
Valery Sizov committed
1090
1091
1092
      it "returns errors if job allow_failure parameter is not an boolean" do
        config = YAML.dump({ rspec: { script: "test", allow_failure: "string" } })
        expect do
1093
          GitlabCiYamlProcessor.new(config, path)
1094
        end.to raise_error(GitlabCiYamlProcessor::ValidationError, "jobs:rspec allow failure should be a boolean value")
Valery Sizov's avatar
Valery Sizov committed
1095
      end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
1096

Valery Sizov's avatar
Valery Sizov committed
1097
      it "returns errors if job stage is not a string" do
1098
        config = YAML.dump({ rspec: { script: "test", type: 1 } })
Valery Sizov's avatar
Valery Sizov committed
1099
        expect do
1100
          GitlabCiYamlProcessor.new(config, path)
1101
        end.to raise_error(GitlabCiYamlProcessor::ValidationError, "jobs:rspec:type config should be a string")
Valery Sizov's avatar
Valery Sizov committed
1102
1103
1104
      end

      it "returns errors if job stage is not a pre-defined stage" do
1105
        config = YAML.dump({ rspec: { script: "test", type: "acceptance" } })
Valery Sizov's avatar
Valery Sizov committed
1106
        expect do
1107
          GitlabCiYamlProcessor.new(config, path)
Valery Sizov's avatar
Valery Sizov committed
1108
1109
        end.to raise_error(GitlabCiYamlProcessor::ValidationError, "rspec job: stage parameter should be build, test, deploy")
      end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
1110

Valery Sizov's avatar
Valery Sizov committed
1111
      it "returns errors if job stage is not a defined stage" do
1112
        config = YAML.dump({ types: ["build", "test"], rspec: { script: "test", type: "acceptance" } })
Valery Sizov's avatar
Valery Sizov committed
1113
        expect do
1114
          GitlabCiYamlProcessor.new(config, path)
Valery Sizov's avatar
Valery Sizov committed
1115
1116
        end.to raise_error(GitlabCiYamlProcessor::ValidationError, "rspec job: stage parameter should be build, test")
      end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
1117

Valery Sizov's avatar
Valery Sizov committed
1118
      it "returns errors if stages is not an array" do
1119
        config = YAML.dump({ stages: "test", rspec: { script: "test" } })
Valery Sizov's avatar
Valery Sizov committed
1120
        expect do
1121
          GitlabCiYamlProcessor.new(config, path)
1122
        end.to raise_error(GitlabCiYamlProcessor::ValidationError, "stages config should be an array of strings")
Valery Sizov's avatar
Valery Sizov committed
1123
      end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
1124

Valery Sizov's avatar
Valery Sizov committed
1125
      it "returns errors if stages is not an array of strings" do
1126
        config = YAML.dump({ stages: [true, "test"], rspec: { script: "test" } })
Valery Sizov's avatar
Valery Sizov committed
1127
        expect do
1128
          GitlabCiYamlProcessor.new(config, path)
1129
        end.to raise_error(GitlabCiYamlProcessor::ValidationError, "stages config should be an array of strings")
Valery Sizov's avatar
Valery Sizov committed
1130
      end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
1131

Valery Sizov's avatar
Valery Sizov committed
1132
1133
1134
      it "returns errors if variables is not a map" do
        config = YAML.dump({ variables: "test", rspec: { script: "test" } })
        expect do
1135
          GitlabCiYamlProcessor.new(config, path)
1136
        end.to raise_error(GitlabCiYamlProcessor::ValidationError, "variables config should be a hash of key value pairs")
Valery Sizov's avatar
Valery Sizov committed
1137
      end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
1138

1139
      it "returns errors if variables is not a map of key-value strings" do
Valery Sizov's avatar
Valery Sizov committed
1140
1141
        config = YAML.dump({ variables: { test: false }, rspec: { script: "test" } })
        expect do
1142
          GitlabCiYamlProcessor.new(config, path)
1143
        end.to raise_error(GitlabCiYamlProcessor::ValidationError, "variables config should be a hash of key value pairs")
Valery Sizov's avatar
Valery Sizov committed
1144
      end
1145
1146

      it "returns errors if job when is not on_success, on_failure or always" do
Kamil Trzcinski's avatar
Kamil Trzcinski committed
1147
        config = YAML.dump({ rspec: { script: "test", when: 1 } })
1148
        expect do
1149
          GitlabCiYamlProcessor.new(config, path)
1150
        end.to raise_error(GitlabCiYamlProcessor::ValidationError, "jobs:rspec when should be on_success, on_failure, always or manual")
1151
      end
Kamil Trzcinski's avatar
Kamil Trzcinski committed
1152

1153
1154
1155
1156
      it "returns errors if job artifacts:name is not an a string" do
        config = YAML.dump({ types: ["build", "test"], rspec: { script: "test", artifacts: { name: 1 } } })
        expect do
          GitlabCiYamlProcessor.new(config)
1157
        end.to raise_error(GitlabCiYamlProcessor::ValidationError, "jobs:rspec:artifacts name should be a string")
1158
1159
      end

Kamil Trzcinski's avatar
Kamil Trzcinski committed
1160
1161
1162
1163
      it "returns errors if job artifacts:when is not an a predefined value" do
        config = YAML.dump({ types: ["build", "test"], rspec: { script: "test", artifacts: { when: 1 } } })
        expect do
          GitlabCiYamlProcessor.new(config)
1164
        end.to raise_error(GitlabCiYamlProcessor::ValidationError, "jobs:rspec:artifacts when should be on_success, on_failure or always")
Kamil Trzcinski's avatar
Kamil Trzcinski committed
1165
1166
      end

1167
1168
1169
1170
      it "returns errors if job artifacts:expire_in is not an a string" do
        config = YAML.dump({ types: ["build", "test"], rspec: { script: "test", artifacts: { expire_in: 1 } } })
        expect do
          GitlabCiYamlProcessor.new(config)
1171
        end.to raise_error(GitlabCiYamlProcessor::ValidationError, "jobs:rspec:artifacts expire in should be a duration")
1172
1173
1174
1175
1176
1177
      end

      it "returns errors if job artifacts:expire_in is not an a valid duration" do
        config = YAML.dump({ types: ["build", "test"], rspec: { script: "test", artifacts: { expire_in: "7 elephants" } } })
        expect do
          GitlabCiYamlProcessor.new(config)
1178
        end.to raise_error(GitlabCiYamlProcessor::ValidationError, "jobs:rspec:artifacts expire in should be a duration")
1179
1180
      end

1181
1182
      it "returns errors if job artifacts:untracked is not an array of strings" do
        config = YAML.dump({ types: ["build", "test"], rspec: { script: "test", artifacts: { untracked: "string" } } })
Kamil Trzcinski's avatar
Kamil Trzcinski committed
1183
1184
        expect do
          GitlabCiYamlProcessor.new(config)
1185
        end.to raise_error(GitlabCiYamlProcessor::ValidationError, "jobs:rspec:artifacts untracked should be a boolean value")
1186
1187
1188
1189
1190
1191
      end

      it "returns errors if job artifacts:paths is not an array of strings" do
        config = YAML.dump({ types: ["build", "test"], rspec: { script: "test", artifacts: { paths: "string" } } })
        expect do
          GitlabCiYamlProcessor.new(config)
1192
        end.to raise_error(GitlabCiYamlProcessor::ValidationError, "jobs:rspec:artifacts paths should be an array of strings")
Kamil Trzcinski's avatar
Kamil Trzcinski committed
1193
      end
1194
1195
1196
1197
1198

      it "returns errors if cache:untracked is not an array of strings" do
        config = YAML.dump({ cache: { untracked: "string" }, rspec: { script: "test" } })
        expect do
          GitlabCiYamlProcessor.new(config)
1199
        end.to raise_error(GitlabCiYamlProcessor::ValidationError, "cache:untracked config should be a boolean value")
1200
1201
1202
1203
1204
1205
      end

      it "returns errors if cache:paths is not an array of strings" do
        config = YAML.dump({ cache: { paths: "string" }, rspec: { script: "test" } })
        expect do
          GitlabCiYamlProcessor.new(config)
1206
        end.to raise_error(GitlabCiYamlProcessor::ValidationError, "cache:paths config should be an array of strings")
1207
1208
      end

1209
1210
1211
1212
      it "returns errors if cache:key is not a string" do
        config = YAML.dump({ cache: { key: 1 }, rspec: { script: "test" } })
        expect do
          GitlabCiYamlProcessor.new(config)
1213
        end.to raise_error(GitlabCiYamlProcessor::ValidationError, "cache:key config should be a string or symbol")
1214
1215
1216
1217
1218
1219
      end

      it "returns errors if job cache:key is not an a string" do
        config = YAML.dump({ types: ["build", "test"], rspec: { script: "test", cache: { key: 1 } } })
        expect do
          GitlabCiYamlProcessor.new(config)
1220
        end.to raise_error(GitlabCiYamlProcessor::ValidationError, "jobs:rspec:cache:key config should be a string or symbol")
1221
1222
      end

1223
1224
1225
1226
      it "returns errors if job cache:untracked is not an array of strings" do
        config = YAML.dump({ types: ["build", "test"], rspec: { script: "test", cache: { untracked: "string" } } })
        expect do
          GitlabCiYamlProcessor.new(config)
1227
        end.to raise_error(GitlabCiYamlProcessor::ValidationError, "jobs:rspec:cache:untracked config should be a boolean value")
1228
1229
1230
1231
1232
1233
      end

      it "returns errors if job cache:paths is not an array of strings" do
        config = YAML.dump({ types: ["build", "test"], rspec: { script: "test", cache: { paths: "string" } } })
        expect do
          GitlabCiYamlProcessor.new(config)
1234
        end.to raise_error(GitlabCiYamlProcessor::ValidationError, "jobs:rspec:cache:paths config should be an array of strings")
1235
      end
1236
1237
1238
1239
1240

      it "returns errors if job dependencies is not an array of strings" do
        config = YAML.dump({ types: ["build", "test"], rspec: { script: "test", dependencies: "string" } })
        expect do
          GitlabCiYamlProcessor.new(config)