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

V
Valery Sizov 已提交
3
module Ci
D
Douwe Maan 已提交
4
  describe GitlabCiYamlProcessor, lib: true do
5
    let(:path) { 'path' }
6

V
Valery Sizov 已提交
7 8 9 10 11 12 13 14 15
    describe "#builds_for_ref" do
      let(:type) { 'test' }

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

16
        config_processor = GitlabCiYamlProcessor.new(config, path)
V
Valery Sizov 已提交
17 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,
V
Valery Sizov 已提交
22 23 24
          except: nil,
          name: :rspec,
          only: nil,
K
Kamil Trzcinski 已提交
25 26
          commands: "pwd\nrspec",
          tag_list: [],
V
Valery Sizov 已提交
27
          options: {},
K
Kamil Trzcinski 已提交
28 29
          allow_failure: false,
          when: "on_success"
V
Valery Sizov 已提交
30 31
        })
      end
32

33 34 35 36 37 38
      describe :only do
        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 已提交
39

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

42 43
          expect(config_processor.builds_for_stage_and_ref(type, "master").size).to eq(0)
        end
V
Valery Sizov 已提交
44

45 46 47 48 49
        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 已提交
50

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

53 54
          expect(config_processor.builds_for_stage_and_ref(type, "master").size).to eq(0)
        end
V
Valery Sizov 已提交
55

56 57 58 59 60
        it "returns builds if only has specified this branch" do
          config = YAML.dump({
                               before_script: ["pwd"],
                               rspec: { script: "rspec", only: ["master"] }
                             })
V
Valery Sizov 已提交
61

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

64 65
          expect(config_processor.builds_for_stage_and_ref(type, "master").size).to eq(1)
        end
V
Valery Sizov 已提交
66

67 68 69 70 71
        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"] }
                             })
V
Valery Sizov 已提交
72

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

75 76
          expect(config_processor.builds_for_stage_and_ref(type, "deploy").size).to eq(1)
        end
V
Valery Sizov 已提交
77

78 79 80 81 82
        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 已提交
83

84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
          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

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 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
        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"],
K
Kamil Trzcinski 已提交
148
                               rspec: { script: "rspec", type: "test", only: ["master", "deploy"] },
149 150 151
                               staging: { script: "deploy", type: "deploy", only: ["master", "deploy"] },
                               production: { script: "deploy", type: "deploy", only: ["master@path", "deploy"] },
                             })
V
Valery Sizov 已提交
152

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

155
          expect(config_processor.builds_for_stage_and_ref("deploy", "deploy").size).to eq(2)
K
Kamil Trzcinski 已提交
156 157
          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)
158
        end
V
Valery Sizov 已提交
159 160
      end

161 162 163 164 165 166
      describe :except do
        it "returns builds if except has another branch" do
          config = YAML.dump({
                               before_script: ["pwd"],
                               rspec: { script: "rspec", except: ["deploy"] }
                             })
V
Valery Sizov 已提交
167

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

170 171 172 173 174 175 176 177 178 179 180 181 182
          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 已提交
183

184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
        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)

          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

228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
        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

250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
        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 已提交
275 276 277
                               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"] },
278 279
                             })

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

K
Kamil Trzcinski 已提交
282 283 284
          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)
285
        end
V
Valery Sizov 已提交
286
      end
287

D
Douwe Maan 已提交
288 289
    end

V
Valery Sizov 已提交
290 291 292 293 294 295 296 297 298
    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" }
                           })

299
        config_processor = GitlabCiYamlProcessor.new(config, path)
V
Valery Sizov 已提交
300 301 302 303 304

        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({
          except: nil,
          stage: "test",
305
          stage_idx: 1,
V
Valery Sizov 已提交
306 307
          name: :rspec,
          only: nil,
K
Kamil Trzcinski 已提交
308 309
          commands: "pwd\nrspec",
          tag_list: [],
V
Valery Sizov 已提交
310 311 312 313
          options: {
            image: "ruby:2.1",
            services: ["mysql"]
          },
314 315
          allow_failure: false,
          when: "on_success"
V
Valery Sizov 已提交
316 317 318 319 320 321 322 323 324 325 326
        })
      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"], script: "rspec" }
                           })

327
        config_processor = GitlabCiYamlProcessor.new(config, path)
V
Valery Sizov 已提交
328 329 330 331 332

        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({
          except: nil,
          stage: "test",
333
          stage_idx: 1,
V
Valery Sizov 已提交
334 335
          name: :rspec,
          only: nil,
K
Kamil Trzcinski 已提交
336 337
          commands: "pwd\nrspec",
          tag_list: [],
V
Valery Sizov 已提交
338 339 340 341
          options: {
            image: "ruby:2.5",
            services: ["postgresql"]
          },
342 343
          allow_failure: false,
          when: "on_success"
V
Valery Sizov 已提交
344 345
        })
      end
D
Douwe Maan 已提交
346 347
    end

348
    describe 'Variables' do
349
      context 'when global variables are defined' do
350
        it 'returns global variables' do
351
          variables = {
352 353
            VAR1: 'value1',
            VAR2: 'value2',
354
          }
355

356
          config = YAML.dump({
357 358 359 360
            variables: variables,
            before_script: ['pwd'],
            rspec: { script: 'rspec' }
          })
V
Valery Sizov 已提交
361

362
          config_processor = GitlabCiYamlProcessor.new(config, path)
363

364
          expect(config_processor.global_variables).to eq(variables)
365 366 367 368
        end
      end

      context 'when job variables are defined' do
369 370 371 372 373 374 375
        it 'returns job variables' do
          variables =  {
            KEY1: 'value1',
            SOME_KEY_2: 'value2'
          }

          config =  YAML.dump(
376 377
            { before_script: ['pwd'],
              rspec: {
378
                variables: variables,
379 380 381
                script: 'rspec' }
            })

382
          config_processor = GitlabCiYamlProcessor.new(config, path)
383

384
          expect(config_processor.job_variables(:rspec)).to eq variables
385
        end
V
Valery Sizov 已提交
386
      end
387 388 389 390

      context 'when job variables are not defined' do
        it 'returns empty array' do
          config = YAML.dump({
391 392 393
            before_script: ['pwd'],
            rspec: { script: 'rspec' }
          })
394 395

          config_processor = GitlabCiYamlProcessor.new(config, path)
396

397 398 399
          expect(config_processor.job_variables(:rspec)).to eq []
        end
      end
D
Douwe Maan 已提交
400 401
    end

402 403 404 405 406 407 408
    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 }
                             })

409
          config_processor = GitlabCiYamlProcessor.new(config, path)
410 411 412 413 414 415 416
          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

417 418 419
    describe "Caches" do
      it "returns cache when defined globally" do
        config = YAML.dump({
420
                             cache: { paths: ["logs/", "binaries/"], untracked: true, key: 'key' },
421 422 423 424 425 426 427 428 429 430 431
                             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,
432
          key: 'key',
433 434 435 436 437 438
        )
      end

      it "returns cache when defined in a job" do
        config = YAML.dump({
                             rspec: {
439
                               cache: { paths: ["logs/", "binaries/"], untracked: true, key: 'key' },
440 441 442 443 444 445 446 447 448 449
                               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,
450
          key: 'key',
451 452 453 454 455
        )
      end

      it "overwrite cache when defined for a job and globally" do
        config = YAML.dump({
456
                             cache: { paths: ["logs/", "binaries/"], untracked: true, key: 'global' },
457 458
                             rspec: {
                               script: "rspec",
459
                               cache: { paths: ["test/"], untracked: false, key: 'local' },
460 461 462 463 464 465 466 467 468
                             }
                           })

        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,
469
          key: 'local',
470 471 472 473
        )
      end
    end

K
Kamil Trzcinski 已提交
474 475 476 477 478 479
    describe "Artifacts" do
      it "returns artifacts when defined" do
        config = YAML.dump({
                             image:         "ruby:2.1",
                             services:      ["mysql"],
                             before_script: ["pwd"],
480
                             rspec:         {
481
                               artifacts: { paths: ["logs/", "binaries/"], untracked: true, name: "custom_name" },
482 483
                               script: "rspec"
                             }
K
Kamil Trzcinski 已提交
484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499
                           })

        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({
          except: nil,
          stage: "test",
          stage_idx: 1,
          name: :rspec,
          only: nil,
          commands: "pwd\nrspec",
          tag_list: [],
          options: {
            image: "ruby:2.1",
            services: ["mysql"],
500
            artifacts: {
501
              name: "custom_name",
502 503 504
              paths: ["logs/", "binaries/"],
              untracked: true
            }
K
Kamil Trzcinski 已提交
505 506 507 508 509 510 511
          },
          when: "on_success",
          allow_failure: false
        })
      end
    end

512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531
    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) { }

        it { expect { subject }.to_not raise_error }
      end

      context 'dependencies to builds' do
532
        let(:dependencies) { ['build1', 'build2'] }
533 534 535 536

        it { expect { subject }.to_not raise_error }
      end

537 538 539 540 541 542
      context 'dependencies to builds defined as symbols' do
        let(:dependencies) { [:build1, :build2] }

        it { expect { subject }.to_not raise_error }
      end

543
      context 'undefined dependency' do
544
        let(:dependencies) { ['undefined'] }
545 546 547 548 549

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

      context 'dependencies to deploy' do
550
        let(:dependencies) { ['deploy'] }
551 552 553 554 555

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

556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584
    describe "Hidden jobs" do
      let(:config) do
        YAML.dump({
                    '.hidden_job' => { script: 'test' },
                    'normal_job' => { script: 'test' }
                  })
      end

      let(:config_processor) { GitlabCiYamlProcessor.new(config) }

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

      it "doesn't create jobs that starts with dot" do
        expect(subject.size).to eq(1)
        expect(subject.first).to eq({
          except: nil,
          stage: "test",
          stage_idx: 1,
          name: :normal_job,
          only: nil,
          commands: "\ntest",
          tag_list: [],
          options: {},
          when: "on_success",
          allow_failure: false
        })
      end
    end

585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623
    describe "YAML Alias/Anchor" do
      it "is correctly supported for jobs" do
        config = <<EOT
job1: &JOBTMPL
  script: execute-script-for-job

job2: *JOBTMPL
EOT

        config_processor = GitlabCiYamlProcessor.new(config)

        expect(config_processor.builds_for_stage_and_ref("test", "master").size).to eq(2)
        expect(config_processor.builds_for_stage_and_ref("test", "master").first).to eq({
          except: nil,
          stage: "test",
          stage_idx: 1,
          name: :job1,
          only: nil,
          commands: "\nexecute-script-for-job",
          tag_list: [],
          options: {},
          when: "on_success",
          allow_failure: false
        })
        expect(config_processor.builds_for_stage_and_ref("test", "master").second).to eq({
          except: nil,
          stage: "test",
          stage_idx: 1,
          name: :job2,
          only: nil,
          commands: "\nexecute-script-for-job",
          tag_list: [],
          options: {},
          when: "on_success",
          allow_failure: false
        })
      end
    end

V
Valery Sizov 已提交
624
    describe "Error handling" do
625 626 627 628
      it "fails to parse YAML" do
        expect{GitlabCiYamlProcessor.new("invalid: yaml: test")}.to raise_error(Psych::SyntaxError)
      end

V
Valery Sizov 已提交
629
      it "indicates that object is invalid" do
630
        expect{GitlabCiYamlProcessor.new("invalid_yaml")}.to raise_error(GitlabCiYamlProcessor::ValidationError)
V
Valery Sizov 已提交
631 632 633 634 635
      end

      it "returns errors if tags parameter is invalid" do
        config = YAML.dump({ rspec: { script: "test", tags: "mysql" } })
        expect do
636
          GitlabCiYamlProcessor.new(config, path)
V
Valery Sizov 已提交
637 638 639 640 641 642
        end.to raise_error(GitlabCiYamlProcessor::ValidationError, "rspec job: tags parameter should be an array of strings")
      end

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

      it "returns errors if image parameter is invalid" do
        config = YAML.dump({ image: ["test"], rspec: { script: "test" } })
        expect do
650
          GitlabCiYamlProcessor.new(config, path)
V
Valery Sizov 已提交
651 652 653
        end.to raise_error(GitlabCiYamlProcessor::ValidationError, "image should be a string")
      end

K
Kamil Trzcinski 已提交
654 655 656
      it "returns errors if job name is blank" do
        config = YAML.dump({ '' => { script: "test" } })
        expect do
657
          GitlabCiYamlProcessor.new(config, path)
K
Kamil Trzcinski 已提交
658 659 660 661 662 663
        end.to raise_error(GitlabCiYamlProcessor::ValidationError, "job name should be non-empty string")
      end

      it "returns errors if job name is non-string" do
        config = YAML.dump({ 10 => { script: "test" } })
        expect do
664
          GitlabCiYamlProcessor.new(config, path)
K
Kamil Trzcinski 已提交
665 666 667
        end.to raise_error(GitlabCiYamlProcessor::ValidationError, "job name should be non-empty string")
      end

V
Valery Sizov 已提交
668 669 670
      it "returns errors if job image parameter is invalid" do
        config = YAML.dump({ rspec: { script: "test", image: ["test"] } })
        expect do
671
          GitlabCiYamlProcessor.new(config, path)
V
Valery Sizov 已提交
672 673 674 675 676 677
        end.to raise_error(GitlabCiYamlProcessor::ValidationError, "rspec job: image should be a string")
      end

      it "returns errors if services parameter is not an array" do
        config = YAML.dump({ services: "test", rspec: { script: "test" } })
        expect do
678
          GitlabCiYamlProcessor.new(config, path)
V
Valery Sizov 已提交
679 680 681 682 683 684
        end.to raise_error(GitlabCiYamlProcessor::ValidationError, "services should be an array of strings")
      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
685
          GitlabCiYamlProcessor.new(config, path)
V
Valery Sizov 已提交
686 687 688 689 690 691
        end.to raise_error(GitlabCiYamlProcessor::ValidationError, "services should be an array of strings")
      end

      it "returns errors if job services parameter is not an array" do
        config = YAML.dump({ rspec: { script: "test", services: "test" } })
        expect do
692
          GitlabCiYamlProcessor.new(config, path)
V
Valery Sizov 已提交
693 694 695 696 697 698
        end.to raise_error(GitlabCiYamlProcessor::ValidationError, "rspec job: services should be an array of strings")
      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
699
          GitlabCiYamlProcessor.new(config, path)
V
Valery Sizov 已提交
700 701 702 703 704 705
        end.to raise_error(GitlabCiYamlProcessor::ValidationError, "rspec job: services should be an array of strings")
      end

      it "returns errors if there are unknown parameters" do
        config = YAML.dump({ extra: "bundle update" })
        expect do
706
          GitlabCiYamlProcessor.new(config, path)
V
Valery Sizov 已提交
707 708 709 710 711 712
        end.to raise_error(GitlabCiYamlProcessor::ValidationError, "Unknown parameter: extra")
      end

      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
713
          GitlabCiYamlProcessor.new(config, path)
V
Valery Sizov 已提交
714 715 716
        end.to raise_error(GitlabCiYamlProcessor::ValidationError, "Unknown parameter: extra")
      end

717
      it "returns errors if there are no jobs defined" do
V
Valery Sizov 已提交
718 719
        config = YAML.dump({ before_script: ["bundle update"] })
        expect do
720
          GitlabCiYamlProcessor.new(config, path)
V
Valery Sizov 已提交
721 722 723 724 725 726
        end.to raise_error(GitlabCiYamlProcessor::ValidationError, "Please define at least one job")
      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
727
          GitlabCiYamlProcessor.new(config, path)
V
Valery Sizov 已提交
728 729 730 731
        end.to raise_error(GitlabCiYamlProcessor::ValidationError, "rspec job: allow_failure parameter should be an boolean")
      end

      it "returns errors if job stage is not a string" do
732
        config = YAML.dump({ rspec: { script: "test", type: 1 } })
V
Valery Sizov 已提交
733
        expect do
734
          GitlabCiYamlProcessor.new(config, path)
V
Valery Sizov 已提交
735 736 737 738
        end.to raise_error(GitlabCiYamlProcessor::ValidationError, "rspec job: stage parameter should be build, test, deploy")
      end

      it "returns errors if job stage is not a pre-defined stage" do
739
        config = YAML.dump({ rspec: { script: "test", type: "acceptance" } })
V
Valery Sizov 已提交
740
        expect do
741
          GitlabCiYamlProcessor.new(config, path)
V
Valery Sizov 已提交
742 743 744 745
        end.to raise_error(GitlabCiYamlProcessor::ValidationError, "rspec job: stage parameter should be build, test, deploy")
      end

      it "returns errors if job stage is not a defined stage" do
746
        config = YAML.dump({ types: ["build", "test"], rspec: { script: "test", type: "acceptance" } })
V
Valery Sizov 已提交
747
        expect do
748
          GitlabCiYamlProcessor.new(config, path)
V
Valery Sizov 已提交
749 750 751 752 753 754
        end.to raise_error(GitlabCiYamlProcessor::ValidationError, "rspec job: stage parameter should be build, test")
      end

      it "returns errors if stages is not an array" do
        config = YAML.dump({ types: "test", rspec: { script: "test" } })
        expect do
755
          GitlabCiYamlProcessor.new(config, path)
V
Valery Sizov 已提交
756 757 758 759 760 761
        end.to raise_error(GitlabCiYamlProcessor::ValidationError, "stages should be an array of strings")
      end

      it "returns errors if stages is not an array of strings" do
        config = YAML.dump({ types: [true, "test"], rspec: { script: "test" } })
        expect do
762
          GitlabCiYamlProcessor.new(config, path)
V
Valery Sizov 已提交
763 764 765 766 767 768
        end.to raise_error(GitlabCiYamlProcessor::ValidationError, "stages should be an array of strings")
      end

      it "returns errors if variables is not a map" do
        config = YAML.dump({ variables: "test", rspec: { script: "test" } })
        expect do
769
          GitlabCiYamlProcessor.new(config, path)
V
Valery Sizov 已提交
770 771 772 773 774 775
        end.to raise_error(GitlabCiYamlProcessor::ValidationError, "variables should be a map of key-valued strings")
      end

      it "returns errors if variables is not a map of key-valued strings" do
        config = YAML.dump({ variables: { test: false }, rspec: { script: "test" } })
        expect do
776
          GitlabCiYamlProcessor.new(config, path)
V
Valery Sizov 已提交
777 778
        end.to raise_error(GitlabCiYamlProcessor::ValidationError, "variables should be a map of key-valued strings")
      end
779 780

      it "returns errors if job when is not on_success, on_failure or always" do
K
Kamil Trzcinski 已提交
781
        config = YAML.dump({ rspec: { script: "test", when: 1 } })
782
        expect do
783
          GitlabCiYamlProcessor.new(config, path)
784 785
        end.to raise_error(GitlabCiYamlProcessor::ValidationError, "rspec job: when parameter should be on_success, on_failure or always")
      end
K
Kamil Trzcinski 已提交
786

787 788 789 790 791 792 793
      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)
        end.to raise_error(GitlabCiYamlProcessor::ValidationError, "rspec job: artifacts:name parameter should be a string")
      end

794 795
      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" } } })
K
Kamil Trzcinski 已提交
796 797
        expect do
          GitlabCiYamlProcessor.new(config)
798 799 800 801 802 803 804 805
        end.to raise_error(GitlabCiYamlProcessor::ValidationError, "rspec job: artifacts:untracked parameter should be an boolean")
      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)
        end.to raise_error(GitlabCiYamlProcessor::ValidationError, "rspec job: artifacts:paths parameter should be an array of strings")
K
Kamil Trzcinski 已提交
806
      end
807 808 809 810 811 812 813 814 815 816 817 818 819 820 821

      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)
        end.to raise_error(GitlabCiYamlProcessor::ValidationError, "cache:untracked parameter should be an boolean")
      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)
        end.to raise_error(GitlabCiYamlProcessor::ValidationError, "cache:paths parameter should be an array of strings")
      end

822 823 824 825 826 827 828 829 830 831 832 833 834 835
      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)
        end.to raise_error(GitlabCiYamlProcessor::ValidationError, "cache:key parameter should be a string")
      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)
        end.to raise_error(GitlabCiYamlProcessor::ValidationError, "rspec job: cache:key parameter should be a string")
      end

836 837 838 839 840 841 842 843 844 845 846 847 848
      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)
        end.to raise_error(GitlabCiYamlProcessor::ValidationError, "rspec job: cache:untracked parameter should be an boolean")
      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)
        end.to raise_error(GitlabCiYamlProcessor::ValidationError, "rspec job: cache:paths parameter should be an array of strings")
      end
849 850 851 852 853 854 855

      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)
        end.to raise_error(GitlabCiYamlProcessor::ValidationError, "rspec job: dependencies parameter should be an array of strings")
      end
D
Douwe Maan 已提交
856 857 858
    end
  end
end