config_spec.rb 3.68 KB
Newer Older
1 2 3
require 'spec_helper'

describe Gitlab::Ci::Pipeline::Chain::Validate::Config do
4
  set(:project) { create(:project, :repository) }
5 6 7
  set(:user) { create(:user) }

  let(:command) do
8 9 10 11
    Gitlab::Ci::Pipeline::Chain::Command.new(
      project: project,
      current_user: user,
      save_incompleted: true)
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
  end

  let!(:step) { described_class.new(pipeline, command) }

  before do
    step.perform!
  end

  context 'when pipeline has no YAML configuration' do
    let(:pipeline) do
      build_stubbed(:ci_pipeline, project: project)
    end

    it 'appends errors about missing configuration' do
      expect(pipeline.errors.to_a)
        .to include 'Missing .gitlab-ci.yml file'
    end

    it 'breaks the chain' do
      expect(step.break?).to be true
    end
  end

  context 'when YAML configuration contains errors' do
    let(:pipeline) do
      build(:ci_pipeline, project: project, config: 'invalid YAML')
    end

    it 'appends errors about YAML errors' do
      expect(pipeline.errors.to_a)
        .to include 'Invalid configuration format'
    end

    it 'breaks the chain' do
      expect(step.break?).to be true
    end

    context 'when saving incomplete pipeline is allowed' do
      let(:command) do
        double('command', project: project,
                          current_user: user,
                          save_incompleted: true)
      end

      it 'fails the pipeline' do
        expect(pipeline.reload).to be_failed
      end
59 60 61 62

      it 'sets a config error failure reason' do
        expect(pipeline.reload.config_error?).to eq true
      end
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
    end

    context 'when saving incomplete pipeline is not allowed' do
      let(:command) do
        double('command', project: project,
                          current_user: user,
                          save_incompleted: false)
      end

      it 'does not drop pipeline' do
        expect(pipeline).not_to be_failed
        expect(pipeline).not_to be_persisted
      end
    end
  end

  context 'when pipeline contains configuration validation errors' do
80 81 82 83 84 85 86 87
    let(:config) do
      {
        rspec: {
          before_script: 10,
          script: 'ls -al'
        }
      }
    end
88 89 90 91 92 93 94

    let(:pipeline) do
      build(:ci_pipeline, project: project, config: config)
    end

    it 'appends configuration validation errors to pipeline errors' do
      expect(pipeline.errors.to_a)
95
        .to include "jobs:rspec:before_script config should be an array of strings"
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
    end

    it 'breaks the chain' do
      expect(step.break?).to be true
    end
  end

  context 'when pipeline is correct and complete' do
    let(:pipeline) do
      build(:ci_pipeline_with_one_job, project: project)
    end

    it 'does not invalidate the pipeline' do
      expect(pipeline).to be_valid
    end

    it 'does not break the chain' do
      expect(step.break?).to be false
    end
  end
Shinya Maeda's avatar
Shinya Maeda committed
116 117 118 119 120 121 122 123 124

  context 'when pipeline source is merge request' do
    before do
      stub_ci_pipeline_yaml_file(YAML.dump(config))
    end

    let(:pipeline) { build_stubbed(:ci_pipeline, project: project) }

    let(:merge_request_pipeline) do
125
      build(:ci_pipeline, source: :merge_request_event, project: project)
Shinya Maeda's avatar
Shinya Maeda committed
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
    end

    let(:chain) { described_class.new(merge_request_pipeline, command).tap(&:perform!) }

    context "when config contains 'merge_requests' keyword" do
      let(:config) { { rspec: { script: 'echo', only: ['merge_requests'] } } }

      it 'does not break the chain' do
        expect(chain).not_to be_break
      end
    end

    context "when config contains 'merge_request' keyword" do
      let(:config) { { rspec: { script: 'echo', only: ['merge_request'] } } }

      it 'does not break the chain' do
        expect(chain).not_to be_break
      end
    end
  end
146
end