2020-04-08 14:13:33 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe Projects::UpdateRepositoryStorageService do
|
|
|
|
include Gitlab::ShellAdapter
|
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
subject { described_class.new(repository_storage_move) }
|
2020-04-08 14:13:33 +05:30
|
|
|
|
|
|
|
describe "#execute" do
|
2020-05-24 23:13:21 +05:30
|
|
|
let(:time) { Time.current }
|
2020-04-08 14:13:33 +05:30
|
|
|
|
|
|
|
before do
|
|
|
|
allow(Time).to receive(:now).and_return(time)
|
2020-05-24 23:13:21 +05:30
|
|
|
allow(Gitlab.config.repositories.storages).to receive(:keys).and_return(%w[default test_second_storage])
|
2020-04-08 14:13:33 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
context 'without wiki and design repository' do
|
2020-06-23 00:09:42 +05:30
|
|
|
let(:project) { create(:project, :repository, wiki_enabled: false) }
|
2020-05-24 23:13:21 +05:30
|
|
|
let(:destination) { 'test_second_storage' }
|
|
|
|
let(:repository_storage_move) { create(:project_repository_storage_move, :scheduled, project: project, destination_storage_name: destination) }
|
2020-04-08 14:13:33 +05:30
|
|
|
let!(:checksum) { project.repository.checksum }
|
|
|
|
let(:project_repository_double) { double(:repository) }
|
|
|
|
|
|
|
|
before do
|
2020-04-22 19:07:51 +05:30
|
|
|
allow(Gitlab::GitalyClient).to receive(:filesystem_id).with('default').and_call_original
|
|
|
|
allow(Gitlab::GitalyClient).to receive(:filesystem_id).with('test_second_storage').and_return(SecureRandom.uuid)
|
2020-04-08 14:13:33 +05:30
|
|
|
allow(Gitlab::Git::Repository).to receive(:new).and_call_original
|
|
|
|
allow(Gitlab::Git::Repository).to receive(:new)
|
|
|
|
.with('test_second_storage', project.repository.raw.relative_path, project.repository.gl_repository, project.repository.full_path)
|
|
|
|
.and_return(project_repository_double)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the move succeeds' do
|
|
|
|
it 'moves the repository to the new storage and unmarks the repository as read only' do
|
|
|
|
old_path = Gitlab::GitalyClient::StorageSettings.allow_disk_access do
|
|
|
|
project.repository.path_to_repo
|
|
|
|
end
|
|
|
|
|
|
|
|
expect(project_repository_double).to receive(:create_repository)
|
|
|
|
.and_return(true)
|
|
|
|
expect(project_repository_double).to receive(:replicate)
|
|
|
|
.with(project.repository.raw)
|
|
|
|
expect(project_repository_double).to receive(:checksum)
|
|
|
|
.and_return(checksum)
|
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
result = subject.execute
|
2020-04-08 14:13:33 +05:30
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
expect(result).to be_success
|
2020-04-08 14:13:33 +05:30
|
|
|
expect(project).not_to be_repository_read_only
|
|
|
|
expect(project.repository_storage).to eq('test_second_storage')
|
|
|
|
expect(gitlab_shell.repository_exists?('default', old_path)).to be(false)
|
|
|
|
expect(project.project_repository.shard_name).to eq('test_second_storage')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-04-22 19:07:51 +05:30
|
|
|
context 'when the filesystems are the same' do
|
2020-05-24 23:13:21 +05:30
|
|
|
let(:destination) { project.repository_storage }
|
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
it 'bails out and does nothing' do
|
2020-05-24 23:13:21 +05:30
|
|
|
result = subject.execute
|
2020-04-08 14:13:33 +05:30
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
expect(result).to be_error
|
|
|
|
expect(result.message).to match(/SameFilesystemError/)
|
2020-04-08 14:13:33 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the move fails' do
|
|
|
|
it 'unmarks the repository as read-only without updating the repository storage' do
|
2020-04-22 19:07:51 +05:30
|
|
|
allow(Gitlab::GitalyClient).to receive(:filesystem_id).with('default').and_call_original
|
|
|
|
allow(Gitlab::GitalyClient).to receive(:filesystem_id).with('test_second_storage').and_return(SecureRandom.uuid)
|
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
expect(project_repository_double).to receive(:create_repository)
|
|
|
|
.and_return(true)
|
|
|
|
expect(project_repository_double).to receive(:replicate)
|
|
|
|
.with(project.repository.raw)
|
|
|
|
.and_raise(Gitlab::Git::CommandError)
|
|
|
|
expect(GitlabShellWorker).not_to receive(:perform_async)
|
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
result = subject.execute
|
2020-04-08 14:13:33 +05:30
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
expect(result).to be_error
|
2020-04-08 14:13:33 +05:30
|
|
|
expect(project).not_to be_repository_read_only
|
|
|
|
expect(project.repository_storage).to eq('default')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the checksum does not match' do
|
|
|
|
it 'unmarks the repository as read-only without updating the repository storage' do
|
2020-04-22 19:07:51 +05:30
|
|
|
allow(Gitlab::GitalyClient).to receive(:filesystem_id).with('default').and_call_original
|
|
|
|
allow(Gitlab::GitalyClient).to receive(:filesystem_id).with('test_second_storage').and_return(SecureRandom.uuid)
|
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
expect(project_repository_double).to receive(:create_repository)
|
|
|
|
.and_return(true)
|
|
|
|
expect(project_repository_double).to receive(:replicate)
|
|
|
|
.with(project.repository.raw)
|
|
|
|
expect(project_repository_double).to receive(:checksum)
|
|
|
|
.and_return('not matching checksum')
|
|
|
|
expect(GitlabShellWorker).not_to receive(:perform_async)
|
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
result = subject.execute
|
2020-04-08 14:13:33 +05:30
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
expect(result).to be_error
|
2020-04-08 14:13:33 +05:30
|
|
|
expect(project).not_to be_repository_read_only
|
|
|
|
expect(project.repository_storage).to eq('default')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when a object pool was joined' do
|
|
|
|
let!(:pool) { create(:pool_repository, :ready, source_project: project) }
|
|
|
|
|
|
|
|
it 'leaves the pool' do
|
2020-04-22 19:07:51 +05:30
|
|
|
allow(Gitlab::GitalyClient).to receive(:filesystem_id).with('default').and_call_original
|
|
|
|
allow(Gitlab::GitalyClient).to receive(:filesystem_id).with('test_second_storage').and_return(SecureRandom.uuid)
|
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
expect(project_repository_double).to receive(:create_repository)
|
|
|
|
.and_return(true)
|
|
|
|
expect(project_repository_double).to receive(:replicate)
|
|
|
|
.with(project.repository.raw)
|
|
|
|
expect(project_repository_double).to receive(:checksum)
|
|
|
|
.and_return(checksum)
|
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
result = subject.execute
|
2020-04-08 14:13:33 +05:30
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
expect(result).to be_success
|
2020-04-08 14:13:33 +05:30
|
|
|
expect(project.repository_storage).to eq('test_second_storage')
|
|
|
|
expect(project.reload_pool_repository).to be_nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with wiki repository' do
|
|
|
|
include_examples 'moves repository to another storage', 'wiki' do
|
2020-06-23 00:09:42 +05:30
|
|
|
let(:project) { create(:project, :repository, wiki_enabled: true) }
|
2020-04-08 14:13:33 +05:30
|
|
|
let(:repository) { project.wiki.repository }
|
2020-05-24 23:13:21 +05:30
|
|
|
let(:destination) { 'test_second_storage' }
|
|
|
|
let(:repository_storage_move) { create(:project_repository_storage_move, :scheduled, project: project, destination_storage_name: destination) }
|
2020-04-08 14:13:33 +05:30
|
|
|
|
|
|
|
before do
|
|
|
|
project.create_wiki
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-05-24 23:13:21 +05:30
|
|
|
|
|
|
|
context 'with design repository' do
|
|
|
|
include_examples 'moves repository to another storage', 'design' do
|
2020-06-23 00:09:42 +05:30
|
|
|
let(:project) { create(:project, :repository) }
|
2020-05-24 23:13:21 +05:30
|
|
|
let(:repository) { project.design_repository }
|
|
|
|
let(:destination) { 'test_second_storage' }
|
|
|
|
let(:repository_storage_move) { create(:project_repository_storage_move, :scheduled, project: project, destination_storage_name: destination) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
project.design_repository.create_if_not_exists
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-04-08 14:13:33 +05:30
|
|
|
end
|
|
|
|
end
|