retry_build_service_spec.rb 4.1 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 19 20 21
  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
       erased_at].freeze

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

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

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

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

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

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

      current_accessors.uniq!

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

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

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

78 79
      it_behaves_like 'build duplication'

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

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

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

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

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

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

124 125
      it_behaves_like 'build duplication'

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