create_pipeline_service_spec.rb 7.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
require 'spec_helper'

describe Ci::CreatePipelineService, services: true do
  let(:project) { FactoryGirl.create(:project) }
  let(:user) { create(:admin) }

  before do
    stub_ci_pipeline_to_return_yaml_file
  end

  describe '#execute' do
    def execute(params)
      described_class.new(project, user, params).execute
    end

    context 'valid params' do
      let(:pipeline) do
        execute(ref: 'refs/heads/master',
                before: '00000000',
                after: project.commit.id,
                commits: [{ message: "Message" }])
      end

      it { expect(pipeline).to be_kind_of(Ci::Pipeline) }
      it { expect(pipeline).to be_valid }
      it { expect(pipeline).to be_persisted }
      it { expect(pipeline).to eq(project.pipelines.last) }
      it { expect(pipeline).to have_attributes(user: user) }
      it { expect(pipeline.builds.first).to be_kind_of(Ci::Build) }
    end

    context "skip tag if there is no build for it" do
      it "creates commit if there is appropriate job" do
        result = execute(ref: 'refs/heads/master',
                         before: '00000000',
                         after: project.commit.id,
                         commits: [{ message: "Message" }])
        expect(result).to be_persisted
      end

      it "creates commit if there is no appropriate job but deploy job has right ref setting" do
        config = YAML.dump({ deploy: { script: "ls", only: ["master"] } })
        stub_ci_pipeline_yaml_file(config)
        result = execute(ref: 'refs/heads/master',
                         before: '00000000',
                         after: project.commit.id,
                         commits: [{ message: "Message" }])

        expect(result).to be_persisted
      end
    end

    it 'skips creating pipeline for refs without .gitlab-ci.yml' do
      stub_ci_pipeline_yaml_file(nil)
      result = execute(ref: 'refs/heads/master',
                       before: '00000000',
                       after: project.commit.id,
                       commits: [{ message: 'Message' }])

      expect(result).not_to be_persisted
      expect(Ci::Pipeline.count).to eq(0)
    end

    it 'fails commits if yaml is invalid' do
      message = 'message'
      allow_any_instance_of(Ci::Pipeline).to receive(:git_commit_message) { message }
      stub_ci_pipeline_yaml_file('invalid: file: file')
      commits = [{ message: message }]
      pipeline = execute(ref: 'refs/heads/master',
                         before: '00000000',
                         after: project.commit.id,
                         commits: commits)

      expect(pipeline).to be_persisted
      expect(pipeline.builds.any?).to be false
      expect(pipeline.status).to eq('failed')
      expect(pipeline.yaml_errors).not_to be_nil
    end

    context 'when commit contains a [ci skip] directive' do
      let(:message) { "some message[ci skip]" }
82 83 84 85 86 87 88 89 90 91 92

      ci_messages = [
        "some message[ci skip]",
        "some message[skip ci]",
        "some message[CI SKIP]",
        "some message[SKIP CI]",
        "some message[ci_skip]",
        "some message[skip_ci]",
        "some message[ci-skip]",
        "some message[skip-ci]"
      ]
93 94 95 96 97

      before do
        allow_any_instance_of(Ci::Pipeline).to receive(:git_commit_message) { message }
      end

98 99 100 101 102 103 104 105 106 107 108 109
      ci_messages.each do |ci_message|
        it "skips builds creation if the commit message is #{ci_message}" do
          commits = [{ message: ci_message }]
          pipeline = execute(ref: 'refs/heads/master',
                             before: '00000000',
                             after: project.commit.id,
                             commits: commits)

          expect(pipeline).to be_persisted
          expect(pipeline.builds.any?).to be false
          expect(pipeline.status).to eq("skipped")
        end
110 111
      end

112 113
      it "does not skips builds creation if there is no [ci skip] or [skip ci] tag in commit message" do
        allow_any_instance_of(Ci::Pipeline).to receive(:git_commit_message) { "some message" }
114

115
        commits = [{ message: "some message" }]
116 117 118 119 120 121
        pipeline = execute(ref: 'refs/heads/master',
                           before: '00000000',
                           after: project.commit.id,
                           commits: commits)

        expect(pipeline).to be_persisted
122
        expect(pipeline.builds.first.name).to eq("rspec")
123 124
      end

125 126
      it "does not skip builds creation if the commit message is nil" do
        allow_any_instance_of(Ci::Pipeline).to receive(:git_commit_message) { nil }
127

128
        commits = [{ message: nil }]
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
        pipeline = execute(ref: 'refs/heads/master',
                           before: '00000000',
                           after: project.commit.id,
                           commits: commits)

        expect(pipeline).to be_persisted
        expect(pipeline.builds.first.name).to eq("rspec")
      end

      it "fails builds creation if there is [ci skip] tag in commit message and yaml is invalid" do
        stub_ci_pipeline_yaml_file('invalid: file: fiile')
        commits = [{ message: message }]
        pipeline = execute(ref: 'refs/heads/master',
                           before: '00000000',
                           after: project.commit.id,
                           commits: commits)

        expect(pipeline).to be_persisted
        expect(pipeline.builds.any?).to be false
        expect(pipeline.status).to eq("failed")
        expect(pipeline.yaml_errors).not_to be_nil
      end
    end

    it "creates commit with failed status if yaml is invalid" do
      stub_ci_pipeline_yaml_file('invalid: file')
      commits = [{ message: "some message" }]
      pipeline = execute(ref: 'refs/heads/master',
                         before: '00000000',
                         after: project.commit.id,
                         commits: commits)

      expect(pipeline).to be_persisted
      expect(pipeline.status).to eq("failed")
      expect(pipeline.builds.any?).to be false
    end

    context 'when there are no jobs for this pipeline' do
      before do
        config = YAML.dump({ test: { script: 'ls', only: ['feature'] } })
        stub_ci_pipeline_yaml_file(config)
      end

      it 'does not create a new pipeline' do
        result = execute(ref: 'refs/heads/master',
                         before: '00000000',
                         after: project.commit.id,
                         commits: [{ message: 'some msg' }])

        expect(result).not_to be_persisted
        expect(Ci::Build.all).to be_empty
        expect(Ci::Pipeline.count).to eq(0)
      end
    end

    context 'with manual actions' do
      before do
        config = YAML.dump({ deploy: { script: 'ls', when: 'manual' } })
        stub_ci_pipeline_yaml_file(config)
      end

      it 'does not create a new pipeline' do
        result = execute(ref: 'refs/heads/master',
                         before: '00000000',
                         after: project.commit.id,
                         commits: [{ message: 'some msg' }])

        expect(result).to be_persisted
        expect(result.manual_actions).not_to be_empty
      end
    end
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216

    context 'with environment' do
      before do
        config = YAML.dump(deploy: { environment: { name: "review/$CI_BUILD_REF_NAME" }, script: 'ls' })
        stub_ci_pipeline_yaml_file(config)
      end

      it 'creates the environment' do
        result = execute(ref: 'refs/heads/master',
                         before: '00000000',
                         after: project.commit.id,
                         commits: [{ message: 'some msg' }])

        expect(result).to be_persisted
        expect(Environment.find_by(name: "review/master")).not_to be_nil
      end
    end
217 218
  end
end