gitlab_ci_yaml_processor_spec.rb 63.1 KB
Newer Older
D
Douwe Maan 已提交
1 2
require 'spec_helper'

V
Valery Sizov 已提交
3
module Ci
4 5
  describe GitlabCiYamlProcessor, :lib do
    subject { described_class.new(config, path) }
6
    let(:path) { 'path' }
7

8 9 10 11 12 13 14 15 16 17
    describe 'our current .gitlab-ci.yml' do
      let(:config) { File.read("#{Rails.root}/.gitlab-ci.yml") }

      it 'is valid' do
        error_message = described_class.validation_message(config)

        expect(error_message).to be_nil
      end
    end

18
    describe '#build_attributes' do
19
      subject { described_class.new(config, path).build_attributes(:rspec) }
20

21
      describe 'coverage entry' do
22 23 24
        describe 'code coverage regexp' do
          let(:config) do
            YAML.dump(rspec: { script: 'rspec',
25
                               coverage: '/Code coverage: \d+\.\d+/' })
26 27
          end

28
          it 'includes coverage regexp in build attributes' do
D
Douwe Maan 已提交
29 30
            expect(subject)
              .to include(coverage_regex: 'Code coverage: \d+\.\d+')
31 32 33
          end
        end
      end
34

35 36 37
      describe 'retry entry' do
        context 'when retry count is specified' do
          let(:config) do
G
Grzegorz Bizon 已提交
38
            YAML.dump(rspec: { script: 'rspec', retry: 1 })
39 40 41
          end

          it 'includes retry count in build options attribute' do
G
Grzegorz Bizon 已提交
42
            expect(subject[:options]).to include(retry: 1)
43 44 45 46 47 48 49 50 51 52 53 54 55 56
          end
        end

        context 'when retry count is not specified' do
          let(:config) do
            YAML.dump(rspec: { script: 'rspec' })
          end

          it 'does not persist retry count in the database' do
            expect(subject[:options]).not_to have_key(:retry)
          end
        end
      end

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
      describe 'allow failure entry' do
        context 'when job is a manual action' do
          context 'when allow_failure is defined' do
            let(:config) do
              YAML.dump(rspec: { script: 'rspec',
                                 when: 'manual',
                                 allow_failure: false })
            end

            it 'is not allowed to fail' do
              expect(subject[:allow_failure]).to be false
            end
          end

          context 'when allow_failure is not defined' do
            let(:config) do
              YAML.dump(rspec: { script: 'rspec',
                                 when: 'manual' })
            end

            it 'is allowed to fail' do
              expect(subject[:allow_failure]).to be true
            end
          end
        end

        context 'when job is not a manual action' do
          context 'when allow_failure is defined' do
            let(:config) do
              YAML.dump(rspec: { script: 'rspec',
                                 allow_failure: false })
            end

            it 'is not allowed to fail' do
              expect(subject[:allow_failure]).to be false
            end
          end

          context 'when allow_failure is not defined' do
            let(:config) do
              YAML.dump(rspec: { script: 'rspec' })
            end

            it 'is not allowed to fail' do
              expect(subject[:allow_failure]).to be false
            end
          end
        end
      end
106 107
    end

108
    describe '#stage_seeds' do
109 110 111 112 113 114 115
      context 'when no refs policy is specified' do
        let(:config) do
          YAML.dump(production: { stage: 'deploy', script: 'cap prod' },
                    rspec: { stage: 'test', script: 'rspec' },
                    spinach: { stage: 'test', script: 'spinach' })
        end

116 117 118 119 120 121 122 123 124 125 126
        let(:pipeline) { create(:ci_empty_pipeline) }

        it 'correctly fabricates a stage seeds object' do
          seeds = subject.stage_seeds(pipeline)

          expect(seeds.size).to eq 2
          expect(seeds.first.stage[:name]).to eq 'test'
          expect(seeds.second.stage[:name]).to eq 'deploy'
          expect(seeds.first.builds.dig(0, :name)).to eq 'rspec'
          expect(seeds.first.builds.dig(1, :name)).to eq 'spinach'
          expect(seeds.second.builds.dig(0, :name)).to eq 'production'
127 128 129 130 131 132 133 134 135
        end
      end

      context 'when refs policy is specified' do
        let(:config) do
          YAML.dump(production: { stage: 'deploy', script: 'cap prod', only: ['master'] },
                    spinach: { stage: 'test', script: 'spinach', only: ['tags'] })
        end

136 137 138 139
        let(:pipeline) do
          create(:ci_empty_pipeline, ref: 'feature', tag: true)
        end

140
        it 'returns stage seeds only assigned to master to master' do
141
          seeds = subject.stage_seeds(pipeline)
142

143 144 145
          expect(seeds.size).to eq 1
          expect(seeds.first.stage[:name]).to eq 'test'
          expect(seeds.first.builds.dig(0, :name)).to eq 'spinach'
146 147
        end
      end
S
Shinya Maeda 已提交
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166

      context 'when source policy is specified' do
        let(:config) do
          YAML.dump(production: { stage: 'deploy', script: 'cap prod', only: ['triggers'] },
                    spinach: { stage: 'test', script: 'spinach', only: ['schedules'] })
        end

        let(:pipeline) do
          create(:ci_empty_pipeline, source: :schedule)
        end

        it 'returns stage seeds only assigned to schedules' do
          seeds = subject.stage_seeds(pipeline)

          expect(seeds.size).to eq 1
          expect(seeds.first.stage[:name]).to eq 'test'
          expect(seeds.first.builds.dig(0, :name)).to eq 'spinach'
        end
      end
167 168 169 170 171 172 173

      context 'when kubernetes policy is specified' do
        let(:pipeline) { create(:ci_empty_pipeline) }

        let(:config) do
          YAML.dump(
            spinach: { stage: 'test', script: 'spinach' },
174 175 176 177 178
            production: {
              stage: 'deploy',
              script: 'cap',
              only: { kubernetes: 'active' }
            }
179 180 181
          )
        end

182
        context 'when kubernetes is active' do
183 184 185 186 187 188 189 190 191 192 193 194
          let(:project) { create(:kubernetes_project) }
          let(:pipeline) { create(:ci_empty_pipeline, project: project) }

          it 'returns seeds for kubernetes dependent job' do
            seeds = subject.stage_seeds(pipeline)

            expect(seeds.size).to eq 2
            expect(seeds.first.builds.dig(0, :name)).to eq 'spinach'
            expect(seeds.second.builds.dig(0, :name)).to eq 'production'
          end
        end

195
        context 'when kubernetes is not active' do
196 197 198 199 200 201 202 203
          it 'does not return seeds for kubernetes dependent job' do
            seeds = subject.stage_seeds(pipeline)

            expect(seeds.size).to eq 1
            expect(seeds.first.builds.dig(0, :name)).to eq 'spinach'
          end
        end
      end
204 205
    end

206
    describe "#builds_for_stage_and_ref" do
V
Valery Sizov 已提交
207 208 209 210 211 212 213 214
      let(:type) { 'test' }

      it "returns builds if no branch specified" do
        config = YAML.dump({
          before_script: ["pwd"],
          rspec: { script: "rspec" }
        })

215
        config_processor = GitlabCiYamlProcessor.new(config, path)
V
Valery Sizov 已提交
216 217 218 219

        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",
220
          stage_idx: 1,
221
          name: "rspec",
K
Kamil Trzcinski 已提交
222
          commands: "pwd\nrspec",
223
          coverage_regex: nil,
K
Kamil Trzcinski 已提交
224
          tag_list: [],
225 226 227 228
          options: {
            before_script: ["pwd"],
            script: ["rspec"]
          },
K
Kamil Trzcinski 已提交
229
          allow_failure: false,
230 231
          when: "on_success",
          environment: nil,
232
          yaml_variables: []
V
Valery Sizov 已提交
233 234
        })
      end
235

236
      describe 'only' do
237 238 239 240 241
        it "does not return builds if only has another branch" do
          config = YAML.dump({
                               before_script: ["pwd"],
                               rspec: { script: "rspec", only: ["deploy"] }
                             })
V
Valery Sizov 已提交
242

243
          config_processor = GitlabCiYamlProcessor.new(config, path)
V
Valery Sizov 已提交
244

245 246
          expect(config_processor.builds_for_stage_and_ref(type, "master").size).to eq(0)
        end
V
Valery Sizov 已提交
247

248 249 250 251 252
        it "does not return builds if only has regexp with another branch" do
          config = YAML.dump({
                               before_script: ["pwd"],
                               rspec: { script: "rspec", only: ["/^deploy$/"] }
                             })
V
Valery Sizov 已提交
253

254
          config_processor = GitlabCiYamlProcessor.new(config, path)
V
Valery Sizov 已提交
255

256 257
          expect(config_processor.builds_for_stage_and_ref(type, "master").size).to eq(0)
        end
V
Valery Sizov 已提交
258

259 260 261 262 263
        it "returns builds if only has specified this branch" do
          config = YAML.dump({
                               before_script: ["pwd"],
                               rspec: { script: "rspec", only: ["master"] }
                             })
V
Valery Sizov 已提交
264

265
          config_processor = GitlabCiYamlProcessor.new(config, path)
V
Valery Sizov 已提交
266

267 268
          expect(config_processor.builds_for_stage_and_ref(type, "master").size).to eq(1)
        end
V
Valery Sizov 已提交
269

270 271 272
        it "returns builds if only has a list of branches including specified" do
          config = YAML.dump({
                               before_script: ["pwd"],
D
Douwe Maan 已提交
273
                               rspec: { script: "rspec", type: type, only: %w(master deploy) }
274
                             })
V
Valery Sizov 已提交
275

276
          config_processor = GitlabCiYamlProcessor.new(config, path)
V
Valery Sizov 已提交
277

278 279
          expect(config_processor.builds_for_stage_and_ref(type, "deploy").size).to eq(1)
        end
V
Valery Sizov 已提交
280

281 282 283 284 285
        it "returns builds if only has a branches keyword specified" do
          config = YAML.dump({
                               before_script: ["pwd"],
                               rspec: { script: "rspec", type: type, only: ["branches"] }
                             })
V
Valery Sizov 已提交
286

287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
          config_processor = GitlabCiYamlProcessor.new(config, path)

          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)

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

303
        it "returns builds if only has special keywords specified and source matches" do
304 305 306 307 308 309
          possibilities = [{ keyword: 'pushes', source: 'push' },
                           { keyword: 'web', source: 'web' },
                           { keyword: 'triggers', source: 'trigger' },
                           { keyword: 'schedules', source: 'schedule' },
                           { keyword: 'api', source: 'api' },
                           { keyword: 'external', source: 'external' }]
310 311 312 313 314 315 316 317 318 319 320

          possibilities.each do |possibility|
            config = YAML.dump({
                                 before_script: ["pwd"],
                                 rspec: { script: "rspec", type: type, only: [possibility[:keyword]] }
                               })

            config_processor = GitlabCiYamlProcessor.new(config, path)

            expect(config_processor.builds_for_stage_and_ref(type, "deploy", false, possibility[:source]).size).to eq(1)
          end
321 322
        end

323
        it "does not return builds if only has special keywords specified and source doesn't match" do
324 325 326 327 328 329
          possibilities = [{ keyword: 'pushes', source: 'web' },
                           { keyword: 'web', source: 'push' },
                           { keyword: 'triggers', source: 'schedule' },
                           { keyword: 'schedules', source: 'external' },
                           { keyword: 'api', source: 'trigger' },
                           { keyword: 'external', source: 'api' }]
330

331 332 333 334 335
          possibilities.each do |possibility|
            config = YAML.dump({
                                 before_script: ["pwd"],
                                 rspec: { script: "rspec", type: type, only: [possibility[:keyword]] }
                               })
336

337
            config_processor = GitlabCiYamlProcessor.new(config, path)
338

339 340
            expect(config_processor.builds_for_stage_and_ref(type, "deploy", false, possibility[:source]).size).to eq(0)
          end
341 342
        end

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
        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

        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"],
D
Douwe Maan 已提交
368 369
                               rspec: { script: "rspec", type: "test", only: %w(master deploy) },
                               staging: { script: "deploy", type: "deploy", only: %w(master deploy) },
370
                               production: { script: "deploy", type: "deploy", only: ["master@path", "deploy"] }
371
                             })
V
Valery Sizov 已提交
372

K
Kamil Trzcinski 已提交
373
          config_processor = GitlabCiYamlProcessor.new(config, 'fork')
V
Valery Sizov 已提交
374

375
          expect(config_processor.builds_for_stage_and_ref("deploy", "deploy").size).to eq(2)
K
Kamil Trzcinski 已提交
376 377
          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)
378
        end
379 380 381 382 383 384 385 386

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

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

387 388 389 390
            it do
              expect { processor }.to raise_error(GitlabCiYamlProcessor::ValidationError,
                                                  'jobs:rspec:only has to be either an array of conditions or a hash')
            end
391 392 393 394 395
          end

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

396 397 398 399
            it do
              expect { processor }.to raise_error(GitlabCiYamlProcessor::ValidationError,
                                                  'jobs:rspec:only config should be an array of strings or regexps')
            end
400 401 402 403 404
          end

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

405 406 407 408
            it do
              expect { processor }.to raise_error(GitlabCiYamlProcessor::ValidationError,
                                                  'jobs:rspec:only config should be an array of strings or regexps')
            end
409 410
          end
        end
V
Valery Sizov 已提交
411 412
      end

413
      describe 'except' do
414 415 416 417 418
        it "returns builds if except has another branch" do
          config = YAML.dump({
                               before_script: ["pwd"],
                               rspec: { script: "rspec", except: ["deploy"] }
                             })
V
Valery Sizov 已提交
419

420
          config_processor = GitlabCiYamlProcessor.new(config, path)
V
Valery Sizov 已提交
421

422 423 424 425 426 427 428 429 430 431 432 433 434
          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
V
Valery Sizov 已提交
435

436 437 438 439 440 441 442 443 444 445 446 447 448 449
        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"],
D
Douwe Maan 已提交
450
                               rspec: { script: "rspec", type: type, except: %w(master deploy) }
451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
                             })

          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)

          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

480
        it "does not return builds if except has special keywords specified and source matches" do
F
Filip Krakowski 已提交
481 482 483 484 485 486
          possibilities = [{ keyword: 'pushes', source: 'push' },
                           { keyword: 'web', source: 'web' },
                           { keyword: 'triggers', source: 'trigger' },
                           { keyword: 'schedules', source: 'schedule' },
                           { keyword: 'api', source: 'api' },
                           { keyword: 'external', source: 'external' }]
487

488 489 490 491 492
          possibilities.each do |possibility|
            config = YAML.dump({
                                 before_script: ["pwd"],
                                 rspec: { script: "rspec", type: type, except: [possibility[:keyword]] }
                               })
493

494
            config_processor = GitlabCiYamlProcessor.new(config, path)
495

496 497
            expect(config_processor.builds_for_stage_and_ref(type, "deploy", false, possibility[:source]).size).to eq(0)
          end
498 499
        end

500
        it "returns builds if except has special keywords specified and source doesn't match" do
501 502 503 504 505 506
          possibilities = [{ keyword: 'pushes', source: 'web' },
                           { keyword: 'web', source: 'push' },
                           { keyword: 'triggers', source: 'schedule' },
                           { keyword: 'schedules', source: 'external' },
                           { keyword: 'api', source: 'trigger' },
                           { keyword: 'external', source: 'api' }]
507

508 509 510
          possibilities.each do |possibility|
            config = YAML.dump({
                                 before_script: ["pwd"],
F
Filip Krakowski 已提交
511
                                 rspec: { script: "rspec", type: type, except: [possibility[:keyword]] }
512
                               })
513

514
            config_processor = GitlabCiYamlProcessor.new(config, path)
515

516 517
            expect(config_processor.builds_for_stage_and_ref(type, "deploy", false, possibility[:source]).size).to eq(1)
          end
518 519
        end

520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544
        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

        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"],
K
Kamil Trzcinski 已提交
545 546
                               rspec: { script: "rspec", type: "test", except: ["master", "deploy", "test@fork"] },
                               staging: { script: "deploy", type: "deploy", except: ["master"] },
547
                               production: { script: "deploy", type: "deploy", except: ["master@fork"] }
548 549
                             })

K
Kamil Trzcinski 已提交
550
          config_processor = GitlabCiYamlProcessor.new(config, 'fork')
551

K
Kamil Trzcinski 已提交
552 553 554
          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)
555 556
        end

557 558 559 560 561 562 563
        context 'for invalid value' do
          let(:config) { { rspec: { script: "rspec", except: except } } }
          let(:processor) { GitlabCiYamlProcessor.new(YAML.dump(config)) }

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

564 565 566 567
            it do
              expect { processor }.to raise_error(GitlabCiYamlProcessor::ValidationError,
                                                  'jobs:rspec:except has to be either an array of conditions or a hash')
            end
568 569 570 571 572
          end

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

573 574 575 576
            it do
              expect { processor }.to raise_error(GitlabCiYamlProcessor::ValidationError,
                                                  'jobs:rspec:except config should be an array of strings or regexps')
            end
577 578 579 580 581
          end

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

582 583 584 585
            it do
              expect { processor }.to raise_error(GitlabCiYamlProcessor::ValidationError,
                                                  'jobs:rspec:except config should be an array of strings or regexps')
            end
586 587 588
          end
        end
      end
D
Douwe Maan 已提交
589
    end
590

591 592 593
    describe "Scripts handling" do
      let(:config_data) { YAML.dump(config) }
      let(:config_processor) { GitlabCiYamlProcessor.new(config_data, path) }
594

595
      subject { config_processor.builds_for_stage_and_ref("test", "master").first }
596

597 598
      describe "before_script" do
        context "in global context" do
K
Kamil Trzcinski 已提交
599
          let(:config) do
600 601 602 603
            {
              before_script: ["global script"],
              test: { script: ["script"] }
            }
K
Kamil Trzcinski 已提交
604
          end
605

606 607 608 609
          it "return commands with scripts concencaced" do
            expect(subject[:commands]).to eq("global script\nscript")
          end
        end
610

611
        context "overwritten in local context" do
K
Kamil Trzcinski 已提交
612
          let(:config) do
613 614 615 616
            {
              before_script: ["global script"],
              test: { before_script: ["local script"], script: ["script"] }
            }
K
Kamil Trzcinski 已提交
617
          end
618 619 620 621 622 623 624 625

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

      describe "script" do
K
Kamil Trzcinski 已提交
626
        let(:config) do
627 628 629
          {
            test: { script: ["script"] }
          }
K
Kamil Trzcinski 已提交
630
        end
631 632 633 634 635 636

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

637
      describe "after_script" do
638
        context "in global context" do
K
Kamil Trzcinski 已提交
639
          let(:config) do
640
            {
641
              after_script: ["after_script"],
642 643
              test: { script: ["script"] }
            }
K
Kamil Trzcinski 已提交
644
          end
645

646 647
          it "return after_script in options" do
            expect(subject[:options][:after_script]).to eq(["after_script"])
648 649
          end
        end
650 651

        context "overwritten in local context" do
K
Kamil Trzcinski 已提交
652
          let(:config) do
653
            {
654 655
              after_script: ["local after_script"],
              test: { after_script: ["local after_script"], script: ["script"] }
656
            }
K
Kamil Trzcinski 已提交
657
          end
658

659 660
          it "return after_script in options" do
            expect(subject[:options][:after_script]).to eq(["local after_script"])
661 662
          end
        end
663 664
      end
    end
D
Douwe Maan 已提交
665

V
Valery Sizov 已提交
666
    describe "Image and service handling" do
667 668
      context "when extended docker configuration is used" do
        it "returns image and service when defined" do
669 670 671 672
          config = YAML.dump({ image: { name: "ruby:2.1", entrypoint: ["/usr/local/bin/init", "run"] },
                               services: ["mysql", { name: "docker:dind", alias: "docker",
                                                     entrypoint: ["/usr/local/bin/init", "run"],
                                                     command: ["/usr/local/bin/init", "run"] }],
673 674
                               before_script: ["pwd"],
                               rspec: { script: "rspec" } })
V
Valery Sizov 已提交
675

676
          config_processor = GitlabCiYamlProcessor.new(config, path)
V
Valery Sizov 已提交
677

678 679 680 681 682 683 684 685 686
          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",
            coverage_regex: nil,
            tag_list: [],
            options: {
687 688 689 690 691 692
              before_script: ["pwd"],
              script: ["rspec"],
              image: { name: "ruby:2.1", entrypoint: ["/usr/local/bin/init", "run"] },
              services: [{ name: "mysql" },
                         { name: "docker:dind", alias: "docker", entrypoint: ["/usr/local/bin/init", "run"],
                           command: ["/usr/local/bin/init", "run"] }]
693 694 695 696 697 698 699 700 701 702 703 704
            },
            allow_failure: false,
            when: "on_success",
            environment: nil,
            yaml_variables: []
          })
        end

        it "returns image and service when overridden for job" do
          config = YAML.dump({ image: "ruby:2.1",
                               services: ["mysql"],
                               before_script: ["pwd"],
705 706 707 708 709
                               rspec: { image: { name: "ruby:2.5", entrypoint: ["/usr/local/bin/init", "run"] },
                                        services: [{ name: "postgresql", alias: "db-pg",
                                                     entrypoint: ["/usr/local/bin/init", "run"],
                                                     command: ["/usr/local/bin/init", "run"] }, "docker:dind"],
                                        script: "rspec" } })
710 711 712 713 714 715 716 717 718 719 720 721

          config_processor = GitlabCiYamlProcessor.new(config, path)

          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",
            coverage_regex: nil,
            tag_list: [],
            options: {
722 723 724 725 726 727
              before_script: ["pwd"],
              script: ["rspec"],
              image: { name: "ruby:2.5", entrypoint: ["/usr/local/bin/init", "run"] },
              services: [{ name: "postgresql", alias: "db-pg", entrypoint: ["/usr/local/bin/init", "run"],
                           command: ["/usr/local/bin/init", "run"] },
                         { name: "docker:dind" }]
728 729 730 731 732 733 734
            },
            allow_failure: false,
            when: "on_success",
            environment: nil,
            yaml_variables: []
          })
        end
V
Valery Sizov 已提交
735 736
      end

737 738 739 740 741 742
      context "when etended docker configuration is not used" do
        it "returns image and service when defined" do
          config = YAML.dump({ image: "ruby:2.1",
                               services: ["mysql", "docker:dind"],
                               before_script: ["pwd"],
                               rspec: { script: "rspec" } })
V
Valery Sizov 已提交
743

744
          config_processor = GitlabCiYamlProcessor.new(config, path)
V
Valery Sizov 已提交
745

746 747 748 749 750 751 752 753 754
          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",
            coverage_regex: nil,
            tag_list: [],
            options: {
755 756
              before_script: ["pwd"],
              script: ["rspec"],
757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783
              image: { name: "ruby:2.1" },
              services: [{ name: "mysql" }, { name: "docker:dind" }]
            },
            allow_failure: false,
            when: "on_success",
            environment: nil,
            yaml_variables: []
          })
        end

        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", "docker:dind"], script: "rspec" } })

          config_processor = GitlabCiYamlProcessor.new(config, path)

          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",
            coverage_regex: nil,
            tag_list: [],
            options: {
784 785 786 787
              before_script: ["pwd"],
              script: ["rspec"],
              image: { name: "ruby:2.5" },
              services: [{ name: "postgresql" }, { name: "docker:dind" }]
788 789 790 791 792 793 794
            },
            allow_failure: false,
            when: "on_success",
            environment: nil,
            yaml_variables: []
          })
        end
V
Valery Sizov 已提交
795
      end
D
Douwe Maan 已提交
796 797
    end

798
    describe 'Variables' do
799
      let(:config_processor) { GitlabCiYamlProcessor.new(YAML.dump(config), path) }
V
Valery Sizov 已提交
800

801 802 803 804
      subject { config_processor.builds.first[:yaml_variables] }

      context 'when global variables are defined' do
        let(:variables) do
805
          { 'VAR1' => 'value1', 'VAR2' => 'value2' }
806 807 808
        end
        let(:config) do
          {
809 810 811
            variables: variables,
            before_script: ['pwd'],
            rspec: { script: 'rspec' }
812 813
          }
        end
V
Valery Sizov 已提交
814

815 816
        it 'returns global variables' do
          expect(subject).to contain_exactly(
817 818
            { key: 'VAR1', value: 'value1', public: true },
            { key: 'VAR2', value: 'value2', public: true }
819 820 821
          )
        end
      end
822

823 824
      context 'when job and global variables are defined' do
        let(:global_variables) do
825
          { 'VAR1' => 'global1', 'VAR3' => 'global3' }
826 827
        end
        let(:job_variables) do
828
          { 'VAR1' => 'value1', 'VAR2' => 'value2' }
829 830 831 832 833 834 835 836
        end
        let(:config) do
          {
            before_script: ['pwd'],
            variables: global_variables,
            rspec: { script: 'rspec', variables: job_variables }
          }
        end
837

838 839
        it 'returns all unique variables' do
          expect(subject).to contain_exactly(
840 841 842
            { key: 'VAR3', value: 'global3', public: true },
            { key: 'VAR1', value: 'value1', public: true },
            { key: 'VAR2', value: 'value2', public: true }
843
          )
844 845 846 847
        end
      end

      context 'when job variables are defined' do
848 849 850 851 852 853
        let(:config) do
          {
            before_script: ['pwd'],
            rspec: { script: 'rspec', variables: variables }
          }
        end
854

855 856
        context 'when syntax is correct' do
          let(:variables) do
857
            { 'VAR1' => 'value1', 'VAR2' => 'value2' }
858
          end
859

860 861
          it 'returns job variables' do
            expect(subject).to contain_exactly(
862 863
              { key: 'VAR1', value: 'value1', public: true },
              { key: 'VAR2', value: 'value2', public: true }
864
            )
865 866
          end
        end
867

868
        context 'when syntax is incorrect' do
869
          context 'when variables defined but invalid' do
870
            let(:variables) do
D
Douwe Maan 已提交
871
              %w(VAR1 value1 VAR2 value2)
872
            end
873

874
            it 'raises error' do
D
Douwe Maan 已提交
875 876
              expect { subject }
                .to raise_error(GitlabCiYamlProcessor::ValidationError,
877
                                 /jobs:rspec:variables config should be a hash of key value pairs/)
878 879
            end
          end
880

881
          context 'when variables key defined but value not specified' do
882 883 884
            let(:variables) do
              nil
            end
885

886
            it 'returns empty array' do
887
              ##
888 889
              # When variables config is empty, we assume this is a valid
              # configuration, see issue #18775
890
              #
891 892
              expect(subject).to be_an_instance_of(Array)
              expect(subject).to be_empty
893
            end
894
          end
895
        end
V
Valery Sizov 已提交
896
      end
897 898

      context 'when job variables are not defined' do
899 900
        let(:config) do
          {
901 902
            before_script: ['pwd'],
            rspec: { script: 'rspec' }
903 904
          }
        end
905

906 907 908
        it 'returns empty array' do
          expect(subject).to be_an_instance_of(Array)
          expect(subject).to be_empty
909
        end
V
Valery Sizov 已提交
910
      end
D
Douwe Maan 已提交
911 912
    end

913 914 915 916 917 918 919
    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 }
                             })

920
          config_processor = GitlabCiYamlProcessor.new(config, path)
K
Kamil Trzcinski 已提交
921

922 923 924 925 926 927 928
          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

929 930 931 932 933 934 935 936 937
    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,
938
            'cache config contains unknown keys: invalid'
939 940 941 942
          )
        end
      end

943 944
      it "returns cache when defined globally" do
        config = YAML.dump({
945
                             cache: { paths: ["logs/", "binaries/"], untracked: true, key: 'key' },
946 947 948 949 950 951 952 953 954 955 956
                             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,
957 958
          key: 'key',
          policy: 'pull-push'
959 960 961 962 963 964
        )
      end

      it "returns cache when defined in a job" do
        config = YAML.dump({
                             rspec: {
965
                               cache: { paths: ["logs/", "binaries/"], untracked: true, key: 'key' },
966 967 968 969 970 971 972 973 974 975
                               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,
976 977
          key: 'key',
          policy: 'pull-push'
978 979 980 981 982
        )
      end

      it "overwrite cache when defined for a job and globally" do
        config = YAML.dump({
983
                             cache: { paths: ["logs/", "binaries/"], untracked: true, key: 'global' },
984 985
                             rspec: {
                               script: "rspec",
986
                               cache: { paths: ["test/"], untracked: false, key: 'local' }
987 988 989 990 991 992 993 994 995
                             }
                           })

        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,
996 997
          key: 'local',
          policy: 'pull-push'
998 999 1000 1001
        )
      end
    end

K
Kamil Trzcinski 已提交
1002 1003 1004 1005 1006 1007
    describe "Artifacts" do
      it "returns artifacts when defined" do
        config = YAML.dump({
                             image:         "ruby:2.1",
                             services:      ["mysql"],
                             before_script: ["pwd"],
1008
                             rspec:         {
1009 1010 1011 1012 1013 1014
                               artifacts: {
                                 paths: ["logs/", "binaries/"],
                                 untracked: true,
                                 name: "custom_name",
                                 expire_in: "7d"
                               },
1015 1016
                               script: "rspec"
                             }
K
Kamil Trzcinski 已提交
1017 1018 1019 1020 1021 1022 1023 1024
                           })

        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,
1025
          name: "rspec",
K
Kamil Trzcinski 已提交
1026
          commands: "pwd\nrspec",
1027
          coverage_regex: nil,
K
Kamil Trzcinski 已提交
1028 1029
          tag_list: [],
          options: {
1030 1031
            before_script: ["pwd"],
            script: ["rspec"],
1032 1033
            image: { name: "ruby:2.1" },
            services: [{ name: "mysql" }],
1034
            artifacts: {
1035
              name: "custom_name",
1036
              paths: ["logs/", "binaries/"],
1037 1038
              untracked: true,
              expire_in: "7d"
1039
            }
K
Kamil Trzcinski 已提交
1040 1041
          },
          when: "on_success",
1042 1043
          allow_failure: false,
          environment: nil,
1044
          yaml_variables: []
K
Kamil Trzcinski 已提交
1045 1046
        })
      end
K
Kamil Trzcinski 已提交
1047

K
Kamil Trzcinski 已提交
1048
      %w[on_success on_failure always].each do |when_state|
K
Kamil Trzcinski 已提交
1049 1050 1051 1052 1053 1054 1055 1056 1057
        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)
K
Kamil Trzcinski 已提交
1058

K
Kamil Trzcinski 已提交
1059 1060 1061 1062 1063
          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
K
Kamil Trzcinski 已提交
1064 1065
    end

1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081
    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)
K
Kamil Trzcinski 已提交
1082
          expect(builds.first[:options]).to include(environment: { name: environment, action: "start" })
1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094
        end
      end

      context 'when hash is specified' do
        let(:environment) do
          { name: 'production',
            url: 'http://production.gitlab.com' }
        end

        it 'does return production and URL' do
          expect(builds.size).to eq(1)
          expect(builds.first[:environment]).to eq(environment[:name])
1095
          expect(builds.first[:options]).to include(environment: environment)
1096
        end
1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109

        context 'the url has a port as variable' do
          let(:environment) do
            { name: 'production',
              url: 'http://production.gitlab.com:$PORT' }
          end

          it 'allows a variable for the port' do
            expect(builds.size).to eq(1)
            expect(builds.first[:environment]).to eq(environment[:name])
            expect(builds.first[:options]).to include(environment: environment)
          end
        end
1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124
      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
1125 1126
          expect { builds }.to raise_error(
            'jobs:deploy_to_production:environment config should be a hash or a string')
1127 1128 1129 1130
        end
      end

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

        it 'raises error' do
1134
          expect { builds }.to raise_error("jobs:deploy_to_production:environment name #{Gitlab::Regex.environment_name_regex_message}")
1135
        end
1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181
      end

      context 'when on_stop is specified' do
        let(:review) { { stage: 'deploy', script: 'test', environment: { name: 'review', on_stop: 'close_review' } } }
        let(:config) { { review: review, close_review: close_review }.compact }

        context 'with matching job' do
          let(:close_review) { { stage: 'deploy', script: 'test', environment: { name: 'review', action: 'stop' } } }

          it 'does return a list of builds' do
            expect(builds.size).to eq(2)
            expect(builds.first[:environment]).to eq('review')
          end
        end

        context 'without matching job' do
          let(:close_review) { nil  }

          it 'raises error' do
            expect { builds }.to raise_error('review job: on_stop job close_review is not defined')
          end
        end

        context 'with close job without environment' do
          let(:close_review) { { stage: 'deploy', script: 'test' } }

          it 'raises error' do
            expect { builds }.to raise_error('review job: on_stop job close_review does not have environment defined')
          end
        end

        context 'with close job for different environment' do
          let(:close_review) { { stage: 'deploy', script: 'test', environment: 'production' } }

          it 'raises error' do
            expect { builds }.to raise_error('review job: on_stop job close_review have different environment name')
          end
        end

        context 'with close job without stop action' do
          let(:close_review) { { stage: 'deploy', script: 'test', environment: { name: 'review' } } }

          it 'raises error' do
            expect { builds }.to raise_error('review job: on_stop job close_review needs to have action stop defined')
          end
        end
1182 1183 1184
      end
    end

1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200
    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) { }

1201
        it { expect { subject }.not_to raise_error }
1202 1203 1204
      end

      context 'dependencies to builds' do
D
Douwe Maan 已提交
1205
        let(:dependencies) { %w(build1 build2) }
1206

1207
        it { expect { subject }.not_to raise_error }
1208 1209
      end

1210 1211 1212
      context 'dependencies to builds defined as symbols' do
        let(:dependencies) { [:build1, :build2] }

1213
        it { expect { subject }.not_to raise_error }
1214 1215
      end

1216
      context 'undefined dependency' do
1217
        let(:dependencies) { ['undefined'] }
1218 1219 1220 1221 1222

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

      context 'dependencies to deploy' do
1223
        let(:dependencies) { ['deploy'] }
1224 1225 1226 1227 1228

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

1229
    describe "Hidden jobs" do
1230 1231 1232 1233
      let(:config_processor) { GitlabCiYamlProcessor.new(config) }
      subject { config_processor.builds_for_stage_and_ref("test", "master") }

      shared_examples 'hidden_job_handling' do
T
Tomasz Maczukin 已提交
1234
        it "doesn't create jobs that start with dot" do
1235 1236 1237 1238
          expect(subject.size).to eq(1)
          expect(subject.first).to eq({
            stage: "test",
            stage_idx: 1,
1239
            name: "normal_job",
1240
            commands: "test",
1241
            coverage_regex: nil,
1242
            tag_list: [],
1243 1244 1245
            options: {
              script: ["test"]
            },
1246
            when: "on_success",
1247 1248
            allow_failure: false,
            environment: nil,
1249
            yaml_variables: []
1250 1251
          })
        end
1252 1253
      end

T
Tomasz Maczukin 已提交
1254
      context 'when hidden job have a script definition' do
1255 1256 1257 1258 1259 1260
        let(:config) do
          YAML.dump({
                      '.hidden_job' => { image: 'ruby:2.1', script: 'test' },
                      'normal_job' => { script: 'test' }
                    })
        end
1261

1262 1263
        it_behaves_like 'hidden_job_handling'
      end
1264

T
Tomasz Maczukin 已提交
1265
      context "when hidden job doesn't have a script definition" do
1266 1267 1268 1269 1270 1271 1272 1273
        let(:config) do
          YAML.dump({
                      '.hidden_job' => { image: 'ruby:2.1' },
                      'normal_job' => { script: 'test' }
                    })
        end

        it_behaves_like 'hidden_job_handling'
1274 1275 1276
      end
    end

1277
    describe "YAML Alias/Anchor" do
1278 1279 1280 1281 1282 1283 1284 1285 1286
      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,
1287
            name: "job1",
1288
            commands: "execute-script-for-job",
1289
            coverage_regex: nil,
1290
            tag_list: [],
1291 1292 1293
            options: {
              script: ["execute-script-for-job"]
            },
1294
            when: "on_success",
1295 1296
            allow_failure: false,
            environment: nil,
1297
            yaml_variables: []
1298 1299 1300 1301
          })
          expect(subject.second).to eq({
            stage: "build",
            stage_idx: 0,
1302
            name: "job2",
1303
            commands: "execute-script-for-job",
1304
            coverage_regex: nil,
1305
            tag_list: [],
1306 1307 1308
            options: {
              script: ["execute-script-for-job"]
            },
1309
            when: "on_success",
1310 1311
            allow_failure: false,
            environment: nil,
1312
            yaml_variables: []
1313 1314 1315 1316
          })
        end
      end

T
Tomasz Maczukin 已提交
1317
      context 'when template is a job' do
T
Tomasz Maczukin 已提交
1318
        let(:config) do
1319
          <<EOT
1320
job1: &JOBTMPL
1321
  stage: build
1322 1323 1324 1325
  script: execute-script-for-job

job2: *JOBTMPL
EOT
1326
        end
1327

1328 1329
        it_behaves_like 'job_templates_handling'
      end
1330

T
Tomasz Maczukin 已提交
1331
      context 'when template is a hidden job' do
T
Tomasz Maczukin 已提交
1332
        let(:config) do
1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346
          <<EOT
.template: &JOBTMPL
  stage: build
  script: execute-script-for-job

job1: *JOBTMPL

job2: *JOBTMPL
EOT
        end

        it_behaves_like 'job_templates_handling'
      end

T
Tomasz Maczukin 已提交
1347
      context 'when job adds its own keys to a template definition' do
T
Tomasz Maczukin 已提交
1348
        let(:config) do
1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363
          <<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'
1364 1365 1366
      end
    end

V
Valery Sizov 已提交
1367
    describe "Error handling" do
1368
      it "fails to parse YAML" do
1369
        expect {GitlabCiYamlProcessor.new("invalid: yaml: test")}.to raise_error(Psych::SyntaxError)
1370 1371
      end

V
Valery Sizov 已提交
1372
      it "indicates that object is invalid" do
1373
        expect {GitlabCiYamlProcessor.new("invalid_yaml")}.to raise_error(GitlabCiYamlProcessor::ValidationError)
V
Valery Sizov 已提交
1374 1375 1376 1377 1378
      end

      it "returns errors if tags parameter is invalid" do
        config = YAML.dump({ rspec: { script: "test", tags: "mysql" } })
        expect do
1379
          GitlabCiYamlProcessor.new(config, path)
1380
        end.to raise_error(GitlabCiYamlProcessor::ValidationError, "jobs:rspec tags should be an array of strings")
V
Valery Sizov 已提交
1381 1382 1383 1384 1385
      end

      it "returns errors if before_script parameter is invalid" do
        config = YAML.dump({ before_script: "bundle update", rspec: { script: "test" } })
        expect do
1386
          GitlabCiYamlProcessor.new(config, path)
1387
        end.to raise_error(GitlabCiYamlProcessor::ValidationError, "before_script config should be an array of strings")
V
Valery Sizov 已提交
1388 1389
      end

1390 1391 1392 1393
      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)
1394
        end.to raise_error(GitlabCiYamlProcessor::ValidationError, "jobs:rspec:before_script config should be an array of strings")
1395 1396
      end

1397 1398
      it "returns errors if after_script parameter is invalid" do
        config = YAML.dump({ after_script: "bundle update", rspec: { script: "test" } })
1399 1400
        expect do
          GitlabCiYamlProcessor.new(config, path)
1401
        end.to raise_error(GitlabCiYamlProcessor::ValidationError, "after_script config should be an array of strings")
1402 1403
      end

1404 1405
      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"] } })
1406 1407
        expect do
          GitlabCiYamlProcessor.new(config, path)
1408
        end.to raise_error(GitlabCiYamlProcessor::ValidationError, "jobs:rspec:after_script config should be an array of strings")
1409 1410
      end

V
Valery Sizov 已提交
1411 1412 1413
      it "returns errors if image parameter is invalid" do
        config = YAML.dump({ image: ["test"], rspec: { script: "test" } })
        expect do
1414
          GitlabCiYamlProcessor.new(config, path)
1415
        end.to raise_error(GitlabCiYamlProcessor::ValidationError, "image config should be a hash or a string")
V
Valery Sizov 已提交
1416 1417
      end

K
Kamil Trzcinski 已提交
1418 1419 1420
      it "returns errors if job name is blank" do
        config = YAML.dump({ '' => { script: "test" } })
        expect do
1421
          GitlabCiYamlProcessor.new(config, path)
1422
        end.to raise_error(GitlabCiYamlProcessor::ValidationError, "jobs:job name can't be blank")
K
Kamil Trzcinski 已提交
1423 1424 1425 1426 1427
      end

      it "returns errors if job name is non-string" do
        config = YAML.dump({ 10 => { script: "test" } })
        expect do
1428
          GitlabCiYamlProcessor.new(config, path)
1429
        end.to raise_error(GitlabCiYamlProcessor::ValidationError, "jobs:10 name should be a symbol")
K
Kamil Trzcinski 已提交
1430 1431
      end

V
Valery Sizov 已提交
1432 1433 1434
      it "returns errors if job image parameter is invalid" do
        config = YAML.dump({ rspec: { script: "test", image: ["test"] } })
        expect do
1435
          GitlabCiYamlProcessor.new(config, path)
1436
        end.to raise_error(GitlabCiYamlProcessor::ValidationError, "jobs:rspec:image config should be a hash or a string")
V
Valery Sizov 已提交
1437 1438 1439 1440 1441
      end

      it "returns errors if services parameter is not an array" do
        config = YAML.dump({ services: "test", rspec: { script: "test" } })
        expect do
1442
          GitlabCiYamlProcessor.new(config, path)
1443
        end.to raise_error(GitlabCiYamlProcessor::ValidationError, "services config should be a array")
V
Valery Sizov 已提交
1444 1445 1446 1447 1448
      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
1449
          GitlabCiYamlProcessor.new(config, path)
1450
        end.to raise_error(GitlabCiYamlProcessor::ValidationError, "service config should be a hash or a string")
V
Valery Sizov 已提交
1451 1452 1453 1454 1455
      end

      it "returns errors if job services parameter is not an array" do
        config = YAML.dump({ rspec: { script: "test", services: "test" } })
        expect do
1456
          GitlabCiYamlProcessor.new(config, path)
1457
        end.to raise_error(GitlabCiYamlProcessor::ValidationError, "jobs:rspec:services config should be a array")
V
Valery Sizov 已提交
1458 1459 1460 1461 1462
      end

      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
1463
          GitlabCiYamlProcessor.new(config, path)
1464
        end.to raise_error(GitlabCiYamlProcessor::ValidationError, "service config should be a hash or a string")
V
Valery Sizov 已提交
1465 1466
      end

1467
      it "returns error if job configuration is invalid" do
V
Valery Sizov 已提交
1468 1469
        config = YAML.dump({ extra: "bundle update" })
        expect do
1470
          GitlabCiYamlProcessor.new(config, path)
1471
        end.to raise_error(GitlabCiYamlProcessor::ValidationError, "jobs:extra config should be a hash")
V
Valery Sizov 已提交
1472 1473
      end

1474 1475
      it "returns errors if services configuration is not correct" do
        config = YAML.dump({ extra: { script: 'rspec', services: "test" } })
V
Valery Sizov 已提交
1476
        expect do
1477
          GitlabCiYamlProcessor.new(config, path)
1478
        end.to raise_error(GitlabCiYamlProcessor::ValidationError, "jobs:extra:services config should be a array")
V
Valery Sizov 已提交
1479 1480
      end

1481
      it "returns errors if there are no jobs defined" do
V
Valery Sizov 已提交
1482 1483
        config = YAML.dump({ before_script: ["bundle update"] })
        expect do
1484
          GitlabCiYamlProcessor.new(config, path)
1485 1486 1487 1488
        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
1489
        config = YAML.dump({ before_script: ["bundle update"], '.hidden'.to_sym => { script: 'ls' } })
1490 1491 1492
        expect do
          GitlabCiYamlProcessor.new(config, path)
        end.to raise_error(GitlabCiYamlProcessor::ValidationError, "jobs config should contain at least one visible job")
V
Valery Sizov 已提交
1493 1494 1495 1496 1497
      end

      it "returns errors if job allow_failure parameter is not an boolean" do
        config = YAML.dump({ rspec: { script: "test", allow_failure: "string" } })
        expect do
1498
          GitlabCiYamlProcessor.new(config, path)
1499
        end.to raise_error(GitlabCiYamlProcessor::ValidationError, "jobs:rspec allow failure should be a boolean value")
V
Valery Sizov 已提交
1500 1501 1502
      end

      it "returns errors if job stage is not a string" do
1503
        config = YAML.dump({ rspec: { script: "test", type: 1 } })
V
Valery Sizov 已提交
1504
        expect do
1505
          GitlabCiYamlProcessor.new(config, path)
1506
        end.to raise_error(GitlabCiYamlProcessor::ValidationError, "jobs:rspec:type config should be a string")
V
Valery Sizov 已提交
1507 1508 1509
      end

      it "returns errors if job stage is not a pre-defined stage" do
1510
        config = YAML.dump({ rspec: { script: "test", type: "acceptance" } })
V
Valery Sizov 已提交
1511
        expect do
1512
          GitlabCiYamlProcessor.new(config, path)
1513
        end.to raise_error(GitlabCiYamlProcessor::ValidationError, "rspec job: stage parameter should be build, test, deploy")
V
Valery Sizov 已提交
1514 1515 1516
      end

      it "returns errors if job stage is not a defined stage" do
D
Douwe Maan 已提交
1517
        config = YAML.dump({ types: %w(build test), rspec: { script: "test", type: "acceptance" } })
V
Valery Sizov 已提交
1518
        expect do
1519
          GitlabCiYamlProcessor.new(config, path)
1520
        end.to raise_error(GitlabCiYamlProcessor::ValidationError, "rspec job: stage parameter should be build, test")
V
Valery Sizov 已提交
1521 1522 1523
      end

      it "returns errors if stages is not an array" do
1524
        config = YAML.dump({ stages: "test", rspec: { script: "test" } })
V
Valery Sizov 已提交
1525
        expect do
1526
          GitlabCiYamlProcessor.new(config, path)
1527
        end.to raise_error(GitlabCiYamlProcessor::ValidationError, "stages config should be an array of strings")
V
Valery Sizov 已提交
1528 1529 1530
      end

      it "returns errors if stages is not an array of strings" do
1531
        config = YAML.dump({ stages: [true, "test"], rspec: { script: "test" } })
V
Valery Sizov 已提交
1532
        expect do
1533
          GitlabCiYamlProcessor.new(config, path)
1534
        end.to raise_error(GitlabCiYamlProcessor::ValidationError, "stages config should be an array of strings")
V
Valery Sizov 已提交
1535 1536 1537 1538 1539
      end

      it "returns errors if variables is not a map" do
        config = YAML.dump({ variables: "test", rspec: { script: "test" } })
        expect do
1540
          GitlabCiYamlProcessor.new(config, path)
1541
        end.to raise_error(GitlabCiYamlProcessor::ValidationError, "variables config should be a hash of key value pairs")
V
Valery Sizov 已提交
1542 1543
      end

G
Grzegorz Bizon 已提交
1544
      it "returns errors if variables is not a map of key-value strings" do
V
Valery Sizov 已提交
1545 1546
        config = YAML.dump({ variables: { test: false }, rspec: { script: "test" } })
        expect do
1547
          GitlabCiYamlProcessor.new(config, path)
1548
        end.to raise_error(GitlabCiYamlProcessor::ValidationError, "variables config should be a hash of key value pairs")
V
Valery Sizov 已提交
1549
      end
1550 1551

      it "returns errors if job when is not on_success, on_failure or always" do
K
Kamil Trzcinski 已提交
1552
        config = YAML.dump({ rspec: { script: "test", when: 1 } })
1553
        expect do
1554
          GitlabCiYamlProcessor.new(config, path)
1555
        end.to raise_error(GitlabCiYamlProcessor::ValidationError, "jobs:rspec when should be on_success, on_failure, always or manual")
1556
      end
K
Kamil Trzcinski 已提交
1557

1558
      it "returns errors if job artifacts:name is not an a string" do
D
Douwe Maan 已提交
1559
        config = YAML.dump({ types: %w(build test), rspec: { script: "test", artifacts: { name: 1 } } })
1560 1561
        expect do
          GitlabCiYamlProcessor.new(config)
1562
        end.to raise_error(GitlabCiYamlProcessor::ValidationError, "jobs:rspec:artifacts name should be a string")
1563 1564
      end

K
Kamil Trzcinski 已提交
1565
      it "returns errors if job artifacts:when is not an a predefined value" do
D
Douwe Maan 已提交
1566
        config = YAML.dump({ types: %w(build test), rspec: { script: "test", artifacts: { when: 1 } } })
K
Kamil Trzcinski 已提交
1567 1568
        expect do
          GitlabCiYamlProcessor.new(config)
1569
        end.to raise_error(GitlabCiYamlProcessor::ValidationError, "jobs:rspec:artifacts when should be on_success, on_failure or always")
K
Kamil Trzcinski 已提交
1570 1571
      end

1572
      it "returns errors if job artifacts:expire_in is not an a string" do
D
Douwe Maan 已提交
1573
        config = YAML.dump({ types: %w(build test), rspec: { script: "test", artifacts: { expire_in: 1 } } })
1574 1575
        expect do
          GitlabCiYamlProcessor.new(config)
1576
        end.to raise_error(GitlabCiYamlProcessor::ValidationError, "jobs:rspec:artifacts expire in should be a duration")
1577 1578 1579
      end

      it "returns errors if job artifacts:expire_in is not an a valid duration" do
D
Douwe Maan 已提交
1580
        config = YAML.dump({ types: %w(build test), rspec: { script: "test", artifacts: { expire_in: "7 elephants" } } })
1581 1582
        expect do
          GitlabCiYamlProcessor.new(config)
1583
        end.to raise_error(GitlabCiYamlProcessor::ValidationError, "jobs:rspec:artifacts expire in should be a duration")
1584 1585
      end

1586
      it "returns errors if job artifacts:untracked is not an array of strings" do
D
Douwe Maan 已提交
1587
        config = YAML.dump({ types: %w(build test), rspec: { script: "test", artifacts: { untracked: "string" } } })
K
Kamil Trzcinski 已提交
1588 1589
        expect do
          GitlabCiYamlProcessor.new(config)
1590
        end.to raise_error(GitlabCiYamlProcessor::ValidationError, "jobs:rspec:artifacts untracked should be a boolean value")
1591 1592 1593
      end

      it "returns errors if job artifacts:paths is not an array of strings" do
D
Douwe Maan 已提交
1594
        config = YAML.dump({ types: %w(build test), rspec: { script: "test", artifacts: { paths: "string" } } })
1595 1596
        expect do
          GitlabCiYamlProcessor.new(config)
1597
        end.to raise_error(GitlabCiYamlProcessor::ValidationError, "jobs:rspec:artifacts paths should be an array of strings")
K
Kamil Trzcinski 已提交
1598
      end
1599 1600 1601 1602 1603

      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)
1604
        end.to raise_error(GitlabCiYamlProcessor::ValidationError, "cache:untracked config should be a boolean value")
1605 1606 1607 1608 1609 1610
      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)
1611
        end.to raise_error(GitlabCiYamlProcessor::ValidationError, "cache:paths config should be an array of strings")
1612 1613
      end

1614 1615 1616 1617
      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)
1618
        end.to raise_error(GitlabCiYamlProcessor::ValidationError, "cache:key config should be a string or symbol")
1619 1620 1621
      end

      it "returns errors if job cache:key is not an a string" do
D
Douwe Maan 已提交
1622
        config = YAML.dump({ types: %w(build test), rspec: { script: "test", cache: { key: 1 } } })
1623 1624
        expect do
          GitlabCiYamlProcessor.new(config)
1625
        end.to raise_error(GitlabCiYamlProcessor::ValidationError, "jobs:rspec:cache:key config should be a string or symbol")
1626 1627
      end

1628
      it "returns errors if job cache:untracked is not an array of strings" do
D
Douwe Maan 已提交
1629
        config = YAML.dump({ types: %w(build test), rspec: { script: "test", cache: { untracked: "string" } } })
1630 1631
        expect do
          GitlabCiYamlProcessor.new(config)
1632
        end.to raise_error(GitlabCiYamlProcessor::ValidationError, "jobs:rspec:cache:untracked config should be a boolean value")
1633 1634 1635
      end

      it "returns errors if job cache:paths is not an array of strings" do
D
Douwe Maan 已提交
1636
        config = YAML.dump({ types: %w(build test), rspec: { script: "test", cache: { paths: "string" } } })
1637 1638
        expect do
          GitlabCiYamlProcessor.new(config)
1639
        end.to raise_error(GitlabCiYamlProcessor::ValidationError, "jobs:rspec:cache:paths config should be an array of strings")
1640
      end
1641 1642

      it "returns errors if job dependencies is not an array of strings" do
D
Douwe Maan 已提交
1643
        config = YAML.dump({ types: %w(build test), rspec: { script: "test", dependencies: "string" } })
1644 1645
        expect do
          GitlabCiYamlProcessor.new(config)
1646
        end.to raise_error(GitlabCiYamlProcessor::ValidationError, "jobs:rspec dependencies should be an array of strings")
1647
      end
D
Douwe Maan 已提交
1648
    end
1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660

    describe "Validate configuration templates" do
      templates = Dir.glob("#{Rails.root.join('vendor/gitlab-ci-yml')}/**/*.gitlab-ci.yml")

      templates.each do |file|
        it "does not return errors for #{file}" do
          file = File.read(file)

          expect { GitlabCiYamlProcessor.new(file) }.not_to raise_error
        end
      end
    end
1661

K
Katarzyna Kobierska 已提交
1662
    describe "#validation_message" do
K
Katarzyna Kobierska 已提交
1663 1664
      context "when the YAML could not be parsed" do
        it "returns an error about invalid configutaion" do
K
Katarzyna Kobierska 已提交
1665
          content = YAML.dump("invalid: yaml: test")
K
Katarzyna Kobierska 已提交
1666

D
Douwe Maan 已提交
1667 1668
          expect(GitlabCiYamlProcessor.validation_message(content))
            .to eq "Invalid configuration format"
1669
        end
K
Katarzyna Kobierska 已提交
1670
      end
1671

K
Katarzyna Kobierska 已提交
1672 1673
      context "when the tags parameter is invalid" do
        it "returns an error about invalid tags" do
K
Katarzyna Kobierska 已提交
1674
          content = YAML.dump({ rspec: { script: "test", tags: "mysql" } })
K
Katarzyna Kobierska 已提交
1675

D
Douwe Maan 已提交
1676 1677
          expect(GitlabCiYamlProcessor.validation_message(content))
            .to eq "jobs:rspec tags should be an array of strings"
K
Katarzyna Kobierska 已提交
1678 1679 1680
        end
      end

K
Katarzyna Kobierska 已提交
1681
      context "when YAML content is empty" do
K
Katarzyna Kobierska 已提交
1682
        it "returns an error about missing content" do
D
Douwe Maan 已提交
1683 1684
          expect(GitlabCiYamlProcessor.validation_message(''))
            .to eq "Please provide content of .gitlab-ci.yml"
1685
        end
K
Katarzyna Kobierska 已提交
1686
      end
1687

K
Katarzyna Kobierska 已提交
1688 1689
      context "when the YAML is valid" do
        it "does not return any errors" do
K
Katarzyna Kobierska 已提交
1690
          content = File.read(Rails.root.join('spec/support/gitlab_stubs/gitlab_ci.yml'))
K
Katarzyna Kobierska 已提交
1691 1692

          expect(GitlabCiYamlProcessor.validation_message(content)).to be_nil
1693 1694 1695
        end
      end
    end
D
Douwe Maan 已提交
1696 1697
  end
end