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

3
describe Ci::RetryBuildService do
4 5 6 7
  set(:user) { create(:user) }
  set(:project) { create(:project) }
  set(:pipeline) { create(:ci_pipeline, project: project) }

8 9 10
  let(:build) { create(:ci_build, pipeline: pipeline) }

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

14 15 16 17 18 19
  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
20
       erased_at auto_canceled_by job_artifacts artifacts_archive artifacts_metadata].freeze
21 22

  IGNORE_ACCESSORS =
23
    %i[type lock_version target_url base_tags trace_sections
24 25
       commit_id deployments erased_by_id last_deployment project_id
       runner_id tag_taggings taggings tags trigger_request_id
26
       user_id auto_canceled_by_id retried failure_reason].freeze
27

28
  shared_examples 'build duplication' do
29 30 31 32 33 34 35
    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

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

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

      context 'when job has nullified protected' do
        before do
          build.update_attribute(:protected, nil)
        end

        it "clones protected build attribute" do
S
Shinya Maeda 已提交
63
          expect(new_build.protected).to be_nil
S
Shinya Maeda 已提交
64 65 66
          expect(new_build.protected).to eq build.protected
        end
      end
67
    end
68

69 70
    describe 'reject acessors' do
      REJECT_ACCESSORS.each do |attribute|
71 72 73
        it "does not clone #{attribute} build attribute" do
          expect(new_build.send(attribute)).not_to eq build.send(attribute)
        end
74 75 76
      end
    end

77
    it 'has correct number of known attributes' do
78
      known_accessors = CLONE_ACCESSORS + REJECT_ACCESSORS + IGNORE_ACCESSORS
79 80 81 82 83 84

      # :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 =
85 86
        Ci::Build.attribute_names.map(&:to_sym) +
        Ci::Build.reflect_on_all_associations.map(&:name) +
87 88 89 90 91
        [:tag_list]

      current_accessors.uniq!

      expect(known_accessors).to contain_exactly(*current_accessors)
92 93 94
    end
  end

95 96
  describe '#execute' do
    let(:new_build) { service.execute(build) }
97

98
    context 'when user has ability to execute build' do
99
      before do
100 101 102
        stub_not_protect_default_branch

        project.add_developer(user)
103 104
      end

105 106
      it_behaves_like 'build duplication'

107 108 109 110 111 112 113 114 115
      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
116
        expect(MergeRequests::AddTodoWhenBuildFailsService)
117 118
          .to receive_message_chain(:new, :close)

119
        service.execute(build)
120 121 122 123 124 125 126 127
      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
128
          service.execute(build)
129 130 131 132 133 134

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

135
    context 'when user does not have ability to execute build' do
136
      it 'raises an error' do
137
        expect { service.execute(build) }
138 139 140 141
          .to raise_error Gitlab::Access::AccessDeniedError
      end
    end
  end
142 143

  describe '#reprocess' do
144
    let(:new_build) { service.reprocess!(build) }
145 146 147

    context 'when user has ability to execute build' do
      before do
148 149 150
        stub_not_protect_default_branch

        project.add_developer(user)
151 152
      end

153 154
      it_behaves_like 'build duplication'

155 156 157 158 159 160 161
      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
162

163
      it 'does mark old build as retried in the database and on the instance' do
164
        expect(new_build).to be_latest
165
        expect(build).to be_retried
166 167
        expect(build.reload).to be_retried
      end
168 169 170 171
    end

    context 'when user does not have ability to execute build' do
      it 'raises an error' do
172
        expect { service.reprocess!(build) }
173 174 175 176
          .to raise_error Gitlab::Access::AccessDeniedError
      end
    end
  end
177
end