retry_build_service_spec.rb 3.2 KB
Newer Older
1 2 3 4 5 6 7 8 9
require 'spec_helper'

describe Ci::RetryBuildService, :services do
  let(:user) { create(:user) }
  let(:project) { create(:empty_project) }
  let(:pipeline) { create(:ci_pipeline, project: project) }
  let(:build) { create(:ci_build, pipeline: pipeline) }

  let(:service) do
10
    described_class.new(project, user)
11 12
  end

13 14
  shared_examples 'build duplication' do
    let(:build) do
15 16
      create(:ci_build, :failed, :artifacts, :erased, :trace,
             :queued, :coverage, pipeline: pipeline)
17 18
    end

19 20 21 22 23 24 25
    describe 'clone attributes' do
      described_class::CLONE_ATTRIBUTES.each do |attribute|
        it "clones #{attribute} build attribute" do
          expect(new_build.send(attribute)).to eq build.send(attribute)
        end
      end
    end
26

27 28 29 30 31
    describe 'reject attributes' do
      described_class::REJECT_ATTRIBUTES.each do |attribute|
        it "does not clone #{attribute} build attribute" do
          expect(new_build.send(attribute)).not_to eq build.send(attribute)
        end
32 33 34
      end
    end

35 36 37 38 39
    it 'has correct number of known attributes' do
      attributes =
        described_class::CLONE_ATTRIBUTES +
        described_class::IGNORE_ATTRIBUTES +
        described_class::REJECT_ATTRIBUTES
40

41
      expect(attributes.size).to eq build.attributes.size
42 43 44
    end
  end

45 46
  describe '#execute' do
    let(:new_build) { service.execute(build) }
47

48
    context 'when user has ability to execute build' do
49
      before do
50
        project.add_developer(user)
51 52
      end

53 54
      it_behaves_like 'build duplication'

55 56 57 58 59 60 61 62 63
      it 'creates a new build that represents the old one' do
        expect(new_build.name).to eq build.name
      end

      it 'enqueues the new build' do
        expect(new_build).to be_pending
      end

      it 'resolves todos for old build that failed' do
64
        expect(MergeRequests::AddTodoWhenBuildFailsService)
65 66
          .to receive_message_chain(:new, :close)

67
        service.execute(build)
68 69 70 71 72 73 74 75
      end

      context 'when there are subsequent builds that are skipped' do
        let!(:subsequent_build) do
          create(:ci_build, :skipped, stage_idx: 1, pipeline: pipeline)
        end

        it 'resumes pipeline processing in subsequent stages' do
76
          service.execute(build)
77 78 79 80 81 82

          expect(subsequent_build.reload).to be_created
        end
      end
    end

83
    context 'when user does not have ability to execute build' do
84
      it 'raises an error' do
85
        expect { service.execute(build) }
86 87 88 89
          .to raise_error Gitlab::Access::AccessDeniedError
      end
    end
  end
90 91 92 93 94 95

  describe '#reprocess' do
    let(:new_build) { service.reprocess(build) }

    context 'when user has ability to execute build' do
      before do
96
        project.add_developer(user)
97 98
      end

99 100
      it_behaves_like 'build duplication'

101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
      it 'creates a new build that represents the old one' do
        expect(new_build.name).to eq build.name
      end

      it 'does not enqueue the new build' do
        expect(new_build).to be_created
      end
    end

    context 'when user does not have ability to execute build' do
      it 'raises an error' do
        expect { service.reprocess(build) }
          .to raise_error Gitlab::Access::AccessDeniedError
      end
    end
  end
117
end