2019-04-11 12:17:24 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-09-06 05:16:26 +00:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 15:08:50 +00:00
|
|
|
RSpec.describe Projects::HashedStorage::MigrateRepositoryService do
|
2018-10-02 03:21:46 +00:00
|
|
|
include GitHelpers
|
|
|
|
|
2017-09-06 05:16:26 +00:00
|
|
|
let(:gitlab_shell) { Gitlab::Shell.new }
|
2020-05-13 18:08:47 +00:00
|
|
|
let(:project) { create(:project, :legacy_storage, :repository, :wiki_repo, :design_repo) }
|
2017-09-06 05:16:26 +00:00
|
|
|
let(:legacy_storage) { Storage::LegacyProject.new(project) }
|
2020-02-04 12:09:00 +00:00
|
|
|
let(:hashed_storage) { Storage::Hashed.new(project) }
|
2017-09-06 05:16:26 +00:00
|
|
|
|
2019-11-14 21:06:14 +00:00
|
|
|
subject(:service) { described_class.new(project: project, old_disk_path: project.disk_path) }
|
2018-08-03 14:34:28 +00:00
|
|
|
|
2017-09-06 05:16:26 +00:00
|
|
|
describe '#execute' do
|
2019-01-17 01:53:50 +00:00
|
|
|
let(:old_disk_path) { legacy_storage.disk_path }
|
|
|
|
let(:new_disk_path) { hashed_storage.disk_path }
|
|
|
|
|
2017-09-06 05:16:26 +00:00
|
|
|
before do
|
|
|
|
allow(service).to receive(:gitlab_shell) { gitlab_shell }
|
|
|
|
end
|
|
|
|
|
2018-12-22 03:01:24 +00:00
|
|
|
context 'repository lock' do
|
|
|
|
it 'tries to lock the repository' do
|
|
|
|
expect(service).to receive(:try_to_set_repository_read_only!)
|
|
|
|
|
|
|
|
service.execute
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'fails when a git operation is in progress' do
|
2020-11-26 15:09:30 +00:00
|
|
|
allow(project).to receive(:git_transfer_in_progress?) { true }
|
2018-12-22 03:01:24 +00:00
|
|
|
|
2019-03-01 06:57:11 +00:00
|
|
|
expect { service.execute }.to raise_error(Projects::HashedStorage::RepositoryInUseError)
|
2018-12-22 03:01:24 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-02-22 08:12:06 +00:00
|
|
|
context 'when repository doesnt exist on disk' do
|
|
|
|
let(:project) { create(:project, :legacy_storage) }
|
|
|
|
|
|
|
|
it 'skips the disk change but increase the version' do
|
|
|
|
service.execute
|
|
|
|
|
|
|
|
expect(project.hashed_storage?(:repository)).to be_truthy
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-09-06 05:16:26 +00:00
|
|
|
context 'when succeeds' do
|
2020-05-13 18:08:47 +00:00
|
|
|
it 'renames project, wiki and design repositories' do
|
2017-09-06 05:16:26 +00:00
|
|
|
service.execute
|
|
|
|
|
2019-10-09 09:06:19 +00:00
|
|
|
expect(gitlab_shell.repository_exists?(project.repository_storage, "#{new_disk_path}.git")).to be_truthy
|
|
|
|
expect(gitlab_shell.repository_exists?(project.repository_storage, "#{new_disk_path}.wiki.git")).to be_truthy
|
2020-05-13 18:08:47 +00:00
|
|
|
expect(gitlab_shell.repository_exists?(project.repository_storage, "#{new_disk_path}.design.git")).to be_truthy
|
2017-09-06 05:16:26 +00:00
|
|
|
end
|
|
|
|
|
2017-09-19 07:44:58 +00:00
|
|
|
it 'updates project to be hashed and not read-only' do
|
2017-09-06 05:16:26 +00:00
|
|
|
service.execute
|
|
|
|
|
2017-10-31 10:31:57 +00:00
|
|
|
expect(project.hashed_storage?(:repository)).to be_truthy
|
2017-09-06 05:16:26 +00:00
|
|
|
expect(project.repository_read_only).to be_falsey
|
|
|
|
end
|
|
|
|
|
2020-05-13 18:08:47 +00:00
|
|
|
it 'move operation is called for all repositories' do
|
2019-01-17 01:53:50 +00:00
|
|
|
expect_move_repository(old_disk_path, new_disk_path)
|
|
|
|
expect_move_repository("#{old_disk_path}.wiki", "#{new_disk_path}.wiki")
|
2020-05-13 18:08:47 +00:00
|
|
|
expect_move_repository("#{old_disk_path}.design", "#{new_disk_path}.design")
|
2017-09-06 05:16:26 +00:00
|
|
|
|
|
|
|
service.execute
|
|
|
|
end
|
2017-12-19 20:04:48 +00:00
|
|
|
|
|
|
|
it 'writes project full path to .git/config' do
|
|
|
|
service.execute
|
|
|
|
|
2018-10-02 03:21:46 +00:00
|
|
|
rugged_config = rugged_repo(project.repository).config['gitlab.fullpath']
|
2018-06-05 15:58:28 +00:00
|
|
|
|
|
|
|
expect(rugged_config).to eq project.full_path
|
2017-12-19 20:04:48 +00:00
|
|
|
end
|
2017-09-06 05:16:26 +00:00
|
|
|
end
|
|
|
|
|
2020-11-13 06:09:02 +00:00
|
|
|
context 'when exception happens' do
|
|
|
|
it 'handles OpenSSL::Cipher::CipherError' do
|
|
|
|
expect(project).to receive(:ensure_runners_token).and_raise(OpenSSL::Cipher::CipherError)
|
|
|
|
|
|
|
|
expect { service.execute }.not_to raise_exception
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'ensures rollback when OpenSSL::Cipher::CipherError' do
|
|
|
|
expect(project).to receive(:ensure_runners_token).and_raise(OpenSSL::Cipher::CipherError)
|
|
|
|
expect(service).to receive(:rollback_folder_move).and_call_original
|
|
|
|
|
|
|
|
service.execute
|
|
|
|
project.reload
|
|
|
|
|
|
|
|
expect(project.legacy_storage?).to be_truthy
|
|
|
|
expect(project.repository_read_only?).to be_falsey
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'handles Gitlab::Git::CommandError' do
|
|
|
|
expect(project).to receive(:write_repository_config).and_raise(Gitlab::Git::CommandError)
|
|
|
|
|
|
|
|
expect { service.execute }.not_to raise_exception
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'ensures rollback when Gitlab::Git::CommandError' do
|
|
|
|
expect(project).to receive(:write_repository_config).and_raise(Gitlab::Git::CommandError)
|
|
|
|
expect(service).to receive(:rollback_folder_move).and_call_original
|
|
|
|
|
|
|
|
service.execute
|
|
|
|
project.reload
|
|
|
|
|
|
|
|
expect(project.legacy_storage?).to be_truthy
|
|
|
|
expect(project.repository_read_only?).to be_falsey
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-09-06 05:16:26 +00:00
|
|
|
context 'when one move fails' do
|
|
|
|
it 'rollsback repositories to original name' do
|
|
|
|
allow(service).to receive(:move_repository).and_call_original
|
2019-01-17 01:53:50 +00:00
|
|
|
allow(service).to receive(:move_repository).with(old_disk_path, new_disk_path).once { false } # will disable first move only
|
2017-09-06 05:16:26 +00:00
|
|
|
|
|
|
|
expect(service).to receive(:rollback_folder_move).and_call_original
|
|
|
|
|
|
|
|
service.execute
|
|
|
|
|
2019-10-09 09:06:19 +00:00
|
|
|
expect(gitlab_shell.repository_exists?(project.repository_storage, "#{new_disk_path}.git")).to be_falsey
|
|
|
|
expect(gitlab_shell.repository_exists?(project.repository_storage, "#{new_disk_path}.wiki.git")).to be_falsey
|
2020-05-13 18:08:47 +00:00
|
|
|
expect(gitlab_shell.repository_exists?(project.repository_storage, "#{new_disk_path}.design.git")).to be_falsey
|
2017-11-21 15:38:32 +00:00
|
|
|
expect(project.repository_read_only?).to be_falsey
|
2017-09-06 05:16:26 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when rollback fails' do
|
2017-11-21 15:38:32 +00:00
|
|
|
before do
|
2019-01-17 01:53:50 +00:00
|
|
|
gitlab_shell.mv_repository(project.repository_storage, old_disk_path, new_disk_path)
|
2017-09-06 05:16:26 +00:00
|
|
|
end
|
|
|
|
|
2019-01-17 01:53:50 +00:00
|
|
|
it 'does not try to move nil repository over existing' do
|
|
|
|
expect(gitlab_shell).not_to receive(:mv_repository).with(project.repository_storage, old_disk_path, new_disk_path)
|
|
|
|
expect_move_repository("#{old_disk_path}.wiki", "#{new_disk_path}.wiki")
|
2020-05-13 18:08:47 +00:00
|
|
|
expect_move_repository("#{old_disk_path}.design", "#{new_disk_path}.design")
|
2017-09-06 05:16:26 +00:00
|
|
|
|
|
|
|
service.execute
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-03-05 02:59:17 +00:00
|
|
|
it 'works even when project validation fails' do
|
|
|
|
allow(project).to receive(:valid?) { false }
|
|
|
|
|
|
|
|
expect { service.execute }.to change { project.hashed_storage?(:repository) }.to(true)
|
|
|
|
end
|
|
|
|
|
2017-09-06 05:16:26 +00:00
|
|
|
def expect_move_repository(from_name, to_name)
|
2018-04-13 10:57:19 +00:00
|
|
|
expect(gitlab_shell).to receive(:mv_repository).with(project.repository_storage, from_name, to_name).and_call_original
|
2017-09-06 05:16:26 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|