debian-mirror-gitlab/spec/workers/projects/after_import_worker_spec.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

123 lines
3.6 KiB
Ruby
Raw Normal View History

2019-07-31 22:56:46 +05:30
# frozen_string_literal: true
2018-03-17 18:26:18 +05:30
require 'spec_helper'
2022-07-16 23:28:13 +05:30
RSpec.describe Projects::AfterImportWorker do
subject { worker.perform(project.id) }
2018-03-17 18:26:18 +05:30
2022-07-16 23:28:13 +05:30
let(:worker) { described_class.new }
2018-03-17 18:26:18 +05:30
let(:project) { create(:project, :repository) }
let(:repository) { project.repository }
let(:sha) { project.commit.sha }
let(:housekeeping_service) { double(:housekeeping_service) }
describe '#execute' do
before do
2021-03-08 18:12:59 +05:30
allow(Repositories::HousekeepingService)
2019-05-30 16:15:17 +05:30
.to receive(:new).with(project).and_return(housekeeping_service)
2018-03-17 18:26:18 +05:30
allow(housekeeping_service)
.to receive(:execute).and_yield
2019-12-21 20:55:43 +05:30
2020-03-13 15:44:24 +05:30
allow(housekeeping_service).to receive(:increment!)
2018-03-17 18:26:18 +05:30
end
it 'performs housekeeping' do
2022-07-16 23:28:13 +05:30
subject
2018-03-17 18:26:18 +05:30
expect(housekeeping_service).to have_received(:execute)
end
context 'with some refs in refs/pull/**/*' do
before do
repository.write_ref('refs/pull/1/head', sha)
repository.write_ref('refs/pull/1/merge', sha)
2022-07-16 23:28:13 +05:30
subject
2018-03-17 18:26:18 +05:30
end
it 'removes refs/pull/**/*' do
2022-08-13 15:12:31 +05:30
expect(repository.list_refs(['refs/pull/'])).to be_empty
2018-03-17 18:26:18 +05:30
end
end
Repository::RESERVED_REFS_NAMES.each do |name|
context "with a ref in refs/#{name}/tmp" do
before do
repository.write_ref("refs/#{name}/tmp", sha)
2022-07-16 23:28:13 +05:30
subject
2018-03-17 18:26:18 +05:30
end
it "does not remove refs/#{name}/tmp" do
2022-08-13 15:12:31 +05:30
expect(repository.list_refs(["refs/#{name}/tmp"]).length).to be(1)
2018-03-17 18:26:18 +05:30
end
end
end
2018-11-08 19:23:39 +05:30
2020-03-13 15:44:24 +05:30
context 'when after import action throw non-retriable exception' do
let(:exception) { StandardError.new('after import error') }
before do
2022-07-16 23:28:13 +05:30
allow_next_instance_of(Repository) do |repository|
allow(repository).to receive(:delete_all_refs_except)
2020-03-13 15:44:24 +05:30
.and_raise(exception)
2022-07-16 23:28:13 +05:30
end
2020-03-13 15:44:24 +05:30
end
it 'throws after import error' do
2022-07-16 23:28:13 +05:30
expect { subject }.to raise_exception('after import error')
2020-03-13 15:44:24 +05:30
end
end
2020-07-28 23:09:34 +05:30
context 'when housekeeping service lease is taken' do
2021-03-08 18:12:59 +05:30
let(:exception) { Repositories::HousekeepingService::LeaseTaken.new }
2020-07-28 23:09:34 +05:30
it 'logs the error message' do
2021-03-08 18:12:59 +05:30
allow_next_instance_of(Repositories::HousekeepingService) do |instance|
2020-07-28 23:09:34 +05:30
expect(instance).to receive(:execute).and_raise(exception)
end
expect(Gitlab::Import::Logger).to receive(:info).with(
{
message: 'Project housekeeping failed',
project_full_path: project.full_path,
project_id: project.id,
'error.message' => exception.to_s
}).and_call_original
2022-07-16 23:28:13 +05:30
subject
2020-07-28 23:09:34 +05:30
end
end
2020-03-13 15:44:24 +05:30
context 'when after import action throw retriable exception one time' do
let(:exception) { GRPC::DeadlineExceeded.new }
it 'removes refs/pull/**/*' do
2022-07-16 23:28:13 +05:30
subject
2022-08-13 15:12:31 +05:30
expect(repository.list_refs(['refs/pull/'])).to be_empty
2020-03-13 15:44:24 +05:30
end
it 'records the failures in the database', :aggregate_failures do
2022-07-16 23:28:13 +05:30
expect_next_instance_of(Repository) do |repository|
expect(repository).to receive(:delete_all_refs_except).and_raise(exception)
expect(repository).to receive(:delete_all_refs_except).and_call_original
end
subject
2020-03-13 15:44:24 +05:30
import_failure = ImportFailure.last
expect(import_failure.source).to eq('delete_all_refs')
expect(import_failure.project_id).to eq(project.id)
expect(import_failure.relation_key).to be_nil
expect(import_failure.relation_index).to be_nil
expect(import_failure.exception_class).to eq('GRPC::DeadlineExceeded')
expect(import_failure.exception_message).to be_present
expect(import_failure.correlation_id_value).not_to be_empty
end
end
2018-03-17 18:26:18 +05:30
end
end