create_spec.rb 1.7 KB
Newer Older
1 2 3 4 5 6 7
require 'spec_helper'

describe Gitlab::Ci::Pipeline::Chain::Create do
  set(:project) { create(:project) }
  set(:user) { create(:user) }

  let(:pipeline) do
8
    build(:ci_empty_pipeline, project: project, ref: 'master')
9 10 11
  end

  let(:command) do
12
    Gitlab::Ci::Pipeline::Chain::Command.new(
13
      project: project, current_user: user)
14 15 16 17 18
  end

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

  context 'when pipeline is ready to be saved' do
19
    before do
20
      pipeline.stages.build(name: 'test', position: 0, project: project)
21 22 23 24

      step.perform!
    end

25 26 27 28 29 30 31 32 33 34
    it 'saves a pipeline' do
      expect(pipeline).to be_persisted
    end

    it 'does not break the chain' do
      expect(step.break?).to be false
    end

    it 'creates stages' do
      expect(pipeline.reload.stages).to be_one
35
      expect(pipeline.stages.first).to be_persisted
36 37 38 39
    end
  end

  context 'when pipeline has validation errors' do
40 41 42 43
    shared_examples_for 'expectations' do
      it 'breaks the chain' do
        expect(step.break?).to be true
      end
S
Shinya Maeda 已提交
44

45 46 47 48
      it 'appends validation error' do
        expect(pipeline.errors.to_a)
          .to include /Failed to persist the pipeline/
      end
49
    end
S
Shinya Maeda 已提交
50

51 52 53 54
    context 'when ref is nil' do
      let(:pipeline) do
        build(:ci_pipeline, project: project, ref: nil)
      end
55

56 57 58
      before do
        step.perform!
      end
59

60
      it_behaves_like 'expectations'
61 62
    end

63 64 65 66
    context 'when pipeline has a duplicate iid' do
      before do
        allow_any_instance_of(Ci::Pipeline).to receive(:ensure_project_iid!) { |p| p.send(:write_attribute, :iid, 1) }
        create(:ci_pipeline, project: project)
S
Shinya Maeda 已提交
67

68 69 70 71
        step.perform!
      end

      it_behaves_like 'expectations'
72 73 74
    end
  end
end