retry_build_service_spec.rb 4.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 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].freeze
26

27 28
  shared_examples 'build duplication' do
    let(:build) do
29 30 31
      create(:ci_build, :failed, :artifacts_expired, :erased,
             :queued, :coverage, :tags, :allowed_to_fail, :on_tag,
             :teardown_environment, :triggered, :trace,
32 33
             description: 'some build', pipeline: pipeline,
             auto_canceled_by: create(:ci_empty_pipeline))
34 35
    end

36 37
    describe 'clone accessors' do
      CLONE_ACCESSORS.each do |attribute|
38
        it "clones #{attribute} build attribute" do
39
          expect(new_build.send(attribute)).to be_present
40 41 42 43
          expect(new_build.send(attribute)).to eq build.send(attribute)
        end
      end
    end
44

45 46
    describe 'reject acessors' do
      REJECT_ACCESSORS.each do |attribute|
47 48 49
        it "does not clone #{attribute} build attribute" do
          expect(new_build.send(attribute)).not_to eq build.send(attribute)
        end
50 51 52
      end
    end

53
    it 'has correct number of known attributes' do
54
      known_accessors = CLONE_ACCESSORS + REJECT_ACCESSORS + IGNORE_ACCESSORS
55 56 57 58 59 60

      # :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 =
61 62
        Ci::Build.attribute_names.map(&:to_sym) +
        Ci::Build.reflect_on_all_associations.map(&:name) +
63 64 65 66 67
        [:tag_list]

      current_accessors.uniq!

      expect(known_accessors).to contain_exactly(*current_accessors)
68 69 70
    end
  end

71 72
  describe '#execute' do
    let(:new_build) { service.execute(build) }
73

74
    context 'when user has ability to execute build' do
75
      before do
76
        project.add_developer(user)
77 78
      end

79 80
      it_behaves_like 'build duplication'

81 82 83 84 85 86 87 88 89
      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
90
        expect(MergeRequests::AddTodoWhenBuildFailsService)
91 92
          .to receive_message_chain(:new, :close)

93
        service.execute(build)
94 95 96 97 98 99 100 101
      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
102
          service.execute(build)
103 104 105 106 107 108

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

109
    context 'when user does not have ability to execute build' do
110
      it 'raises an error' do
111
        expect { service.execute(build) }
112 113 114 115
          .to raise_error Gitlab::Access::AccessDeniedError
      end
    end
  end
116 117 118 119 120 121

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

    context 'when user has ability to execute build' do
      before do
122
        project.add_developer(user)
123 124
      end

125 126
      it_behaves_like 'build duplication'

127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
      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
143
end