retry_build_service_spec.rb 3.6 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
L
Lin Jen-Shin 已提交
15
      create(:ci_build, :failed, :artifacts_expired, :erased, :trace,
16
             :queued, :coverage, :tags, pipeline: pipeline)
17 18
    end

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

27
    describe 'reject attributes' do
28
      described_class::REJECT_ACCESSORS.each do |attribute|
29 30 31
        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
    it 'has correct number of known attributes' do
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
      known_accessors =
        described_class::CLONE_ACCESSORS +
        described_class::IGNORE_ACCESSORS +
        described_class::REJECT_ACCESSORS

      # :tag_list is a special case, this accessor does not exist
      # in reflected associations, comes from `act_as_taggable` and
      # we use it to copy tags, instead of reusing tags.
      #
      current_accessors =
        build.attribute_names.map.map(&:to_sym) +
        build._reflections.map { |assoc| assoc.first.to_sym } +
        [:tag_list]

      current_accessors.uniq!

      expect(known_accessors).to contain_exactly(*current_accessors)
53 54 55
    end
  end

56 57
  describe '#execute' do
    let(:new_build) { service.execute(build) }
58

59
    context 'when user has ability to execute build' do
60
      before do
61
        project.add_developer(user)
62 63
      end

64 65
      it_behaves_like 'build duplication'

66 67 68 69 70 71 72 73 74
      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
75
        expect(MergeRequests::AddTodoWhenBuildFailsService)
76 77
          .to receive_message_chain(:new, :close)

78
        service.execute(build)
79 80 81 82 83 84 85 86
      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
87
          service.execute(build)
88 89 90 91 92 93

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

94
    context 'when user does not have ability to execute build' do
95
      it 'raises an error' do
96
        expect { service.execute(build) }
97 98 99 100
          .to raise_error Gitlab::Access::AccessDeniedError
      end
    end
  end
101 102 103 104 105 106

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

    context 'when user has ability to execute build' do
      before do
107
        project.add_developer(user)
108 109
      end

110 111
      it_behaves_like 'build duplication'

112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
      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
128
end