retry_build_service_spec.rb 5.0 KB
Newer Older
1 2
require 'spec_helper'

3
describe Ci::RetryBuildService do
4
  let(:user) { create(:user) }
5
  let(:project) { create(:project) }
6 7 8 9
  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 15 16 17 18
  CLONE_ACCESSORS = described_class::CLONE_ACCESSORS

  REJECT_ACCESSORS =
    %i[id status user token coverage trace runner artifacts_expire_at
       artifacts_file artifacts_metadata artifacts_size created_at
       updated_at started_at finished_at queued_at erased_by
19
       erased_at auto_canceled_by].freeze
20 21

  IGNORE_ACCESSORS =
K
Kamil Trzciński 已提交
22
    %i[type lock_version target_url base_tags
23 24
       commit_id deployments erased_by_id last_deployment project_id
       runner_id tag_taggings taggings tags trigger_request_id
25
       user_id auto_canceled_by_id retried failure_reason].freeze
26

27
  shared_examples 'build duplication' do
28 29 30 31 32 33 34
    let(:stage) do
      # TODO, we still do not have factory for new stages, we will need to
      # switch existing factory to persist stages, instead of using LegacyStage
      #
      Ci::Stage.create!(project: project, pipeline: pipeline, name: 'test')
    end

35
    let(:build) do
36 37
      create(:ci_build, :failed, :artifacts_expired, :erased,
             :queued, :coverage, :tags, :allowed_to_fail, :on_tag,
38 39 40 41 42 43 44 45
             :triggered, :trace, :teardown_environment,
             description: 'my-job', stage: 'test',  pipeline: pipeline,
             auto_canceled_by: create(:ci_empty_pipeline)) do |build|
               ##
               # TODO, workaround for FactoryGirl limitation when having both
               # stage (text) and stage_id (integer) columns in the table.
               build.stage_id = stage.id
             end
46 47
    end

48 49
    describe 'clone accessors' do
      CLONE_ACCESSORS.each do |attribute|
50
        it "clones #{attribute} build attribute" do
S
Shinya Maeda 已提交
51
          expect(new_build.send(attribute)).not_to be_nil
52 53 54 55
          expect(new_build.send(attribute)).to eq build.send(attribute)
        end
      end
    end
56

57 58
    describe 'reject acessors' do
      REJECT_ACCESSORS.each do |attribute|
59 60 61
        it "does not clone #{attribute} build attribute" do
          expect(new_build.send(attribute)).not_to eq build.send(attribute)
        end
62 63 64
      end
    end

65
    it 'has correct number of known attributes' do
66
      known_accessors = CLONE_ACCESSORS + REJECT_ACCESSORS + IGNORE_ACCESSORS
67 68 69 70 71 72

      # :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 =
73 74
        Ci::Build.attribute_names.map(&:to_sym) +
        Ci::Build.reflect_on_all_associations.map(&:name) +
75 76 77 78 79
        [:tag_list]

      current_accessors.uniq!

      expect(known_accessors).to contain_exactly(*current_accessors)
80 81 82
    end
  end

83 84
  describe '#execute' do
    let(:new_build) { service.execute(build) }
85

86
    context 'when user has ability to execute build' do
87
      before do
88 89 90
        stub_not_protect_default_branch

        project.add_developer(user)
91 92
      end

93 94
      it_behaves_like 'build duplication'

95 96 97 98 99 100 101 102 103
      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
104
        expect(MergeRequests::AddTodoWhenBuildFailsService)
105 106
          .to receive_message_chain(:new, :close)

107
        service.execute(build)
108 109 110 111 112 113 114 115
      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
116
          service.execute(build)
117 118 119 120 121 122

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

123
    context 'when user does not have ability to execute build' do
124
      it 'raises an error' do
125
        expect { service.execute(build) }
126 127 128 129
          .to raise_error Gitlab::Access::AccessDeniedError
      end
    end
  end
130 131

  describe '#reprocess' do
132
    let(:new_build) { service.reprocess!(build) }
133 134 135

    context 'when user has ability to execute build' do
      before do
136 137 138
        stub_not_protect_default_branch

        project.add_developer(user)
139 140
      end

141 142
      it_behaves_like 'build duplication'

143 144 145 146 147 148 149
      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
150 151 152 153 154

      it 'does mark old build as retried' do
        expect(new_build).to be_latest
        expect(build.reload).to be_retried
      end
155 156 157 158
    end

    context 'when user does not have ability to execute build' do
      it 'raises an error' do
159
        expect { service.reprocess!(build) }
160 161 162 163
          .to raise_error Gitlab::Access::AccessDeniedError
      end
    end
  end
164
end