build_spec.rb 3.02 KB
Newer Older
1 2 3 4 5 6 7
require 'spec_helper'

describe Gitlab::Ci::Pipeline::Chain::Build do
  set(:project) { create(:project, :repository) }
  set(:user) { create(:user) }
  let(:pipeline) { Ci::Pipeline.new }

8 9 10 11
  let(:variables_attributes) do
    [{ key: 'first', secret_value: 'world' },
     { key: 'second', secret_value: 'second_world' }]
  end
12
  let(:command) do
13 14 15 16 17 18 19 20
    Gitlab::Ci::Pipeline::Chain::Command.new(
      source: :push,
      origin_ref: 'master',
      checkout_sha: project.commit.id,
      after_sha: nil,
      before_sha: nil,
      trigger_request: nil,
      schedule: nil,
Shinya Maeda's avatar
Shinya Maeda committed
21
      merge_request: nil,
22
      project: project,
23 24
      current_user: user,
      variables_attributes: variables_attributes)
25 26 27 28 29
  end

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

  before do
30
    stub_repository_ci_yaml_file(sha: anything)
31 32 33
  end

  it 'never breaks the chain' do
34 35
    step.perform!

36 37 38 39
    expect(step.break?).to be false
  end

  it 'fills pipeline object with data' do
40 41
    step.perform!

42
    expect(pipeline.sha).not_to be_empty
43 44
    expect(pipeline.sha).to eq project.commit.id
    expect(pipeline.ref).to eq 'master'
45
    expect(pipeline.tag).to be false
46 47
    expect(pipeline.user).to eq user
    expect(pipeline.project).to eq project
48 49
    expect(pipeline.variables.map { |var| var.slice(:key, :secret_value) })
      .to eq variables_attributes.map(&:with_indifferent_access)
50 51 52
  end

  it 'sets a valid config source' do
53 54
    step.perform!

55 56 57 58
    expect(pipeline.repository_source?).to be true
  end

  it 'returns a valid pipeline' do
59 60
    step.perform!

61 62
    expect(pipeline).to be_valid
  end
63 64

  it 'does not persist a pipeline' do
65 66
    step.perform!

67 68
    expect(pipeline).not_to be_persisted
  end
69 70 71

  context 'when pipeline is running for a tag' do
    let(:command) do
72 73 74 75 76 77 78 79
      Gitlab::Ci::Pipeline::Chain::Command.new(
        source: :push,
        origin_ref: 'mytag',
        checkout_sha: project.commit.id,
        after_sha: nil,
        before_sha: nil,
        trigger_request: nil,
        schedule: nil,
Shinya Maeda's avatar
Shinya Maeda committed
80
        merge_request: nil,
81 82
        project: project,
        current_user: user)
83 84 85
    end

    before do
86
      allow_any_instance_of(Repository).to receive(:tag_exists?).with('mytag').and_return(true)
87 88 89 90 91 92 93 94

      step.perform!
    end

    it 'correctly indicated that this is a tagged pipeline' do
      expect(pipeline).to be_tag
    end
  end
Shinya Maeda's avatar
Shinya Maeda committed
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121

  context 'when pipeline is running for a merge request' do
    let(:command) do
      Gitlab::Ci::Pipeline::Chain::Command.new(
        source: :merge_request,
        origin_ref: 'feature',
        checkout_sha: project.commit.id,
        after_sha: nil,
        before_sha: nil,
        trigger_request: nil,
        schedule: nil,
        merge_request: merge_request,
        project: project,
        current_user: user)
    end

    let(:merge_request) { build(:merge_request, target_project: project) }

    before do
      step.perform!
    end

    it 'correctly indicated that this is a merge request pipeline' do
      expect(pipeline).to be_merge_request
      expect(pipeline.merge_request).to eq(merge_request)
    end
  end
122
end