summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/ci/pipeline/chain/create_spec.rb
blob: b8ab01350929f9310d6b78ac74b123f22dba2760 (plain)
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
require 'spec_helper'

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

  let(:pipeline) do
    build(:ci_empty_pipeline, project: project, ref: 'master')
  end

  let(:command) do
    Gitlab::Ci::Pipeline::Chain::Command.new(
      project: project, current_user: user)
  end

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

  context 'when pipeline is ready to be saved' do
    before do
      pipeline.stages.build(name: 'test', position: 0, project: project)

      step.perform!
    end

    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
      expect(pipeline.stages.first).to be_persisted
    end
  end

  context 'when pipeline has validation errors' do
    shared_examples_for 'expectations' do
      it 'breaks the chain' do
        expect(step.break?).to be true
      end
  
      it 'appends validation error' do
        expect(pipeline.errors.to_a)
          .to include /Failed to persist the pipeline/
      end
    end
    
    context 'when ref is nil' do
      let(:pipeline) do
        build(:ci_pipeline, project: project, ref: nil)
      end

      before do
        step.perform!
      end

      it_behaves_like 'expectations'
    end

    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)
  
        step.perform!
      end

      it_behaves_like 'expectations'
    end
  end
end