diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-01-28 06:08:39 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-01-28 06:08:39 +0300 |
commit | 5cef625594aedbac12011d870719fe81a1587a98 (patch) | |
tree | 147d465fb4275ab2d14be99ed58888ca23e10111 /spec/services/repositories | |
parent | ee7de3a24d62376916d78649d7e477a184b2e203 (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec/services/repositories')
3 files changed, 178 insertions, 0 deletions
diff --git a/spec/services/repositories/destroy_rollback_service_spec.rb b/spec/services/repositories/destroy_rollback_service_spec.rb new file mode 100644 index 00000000000..c3cdae17de7 --- /dev/null +++ b/spec/services/repositories/destroy_rollback_service_spec.rb @@ -0,0 +1,73 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe Repositories::DestroyRollbackService do + let_it_be(:user) { create(:user) } + let!(:project) { create(:project, :repository, namespace: user.namespace) } + let(:repository) { project.repository } + let(:path) { repository.disk_path } + let(:remove_path) { "#{path}+#{project.id}#{described_class::DELETED_FLAG}" } + + subject { described_class.new(repository).execute } + + before do + # Dont run sidekiq to check if renamed repository exists + Sidekiq::Testing.fake! { destroy_project(project, user) } + end + + it 'moves the repository from the +deleted folder' do + expect(project.gitlab_shell.repository_exists?(project.repository_storage, remove_path + '.git')).to be_truthy + expect(project.gitlab_shell.repository_exists?(project.repository_storage, path + '.git')).to be_falsey + + subject + + expect(project.gitlab_shell.repository_exists?(project.repository_storage, remove_path + '.git')).to be_falsey + expect(project.gitlab_shell.repository_exists?(project.repository_storage, path + '.git')).to be_truthy + end + + it 'logs the successful action' do + expect(Gitlab::AppLogger).to receive(:info) + + subject + end + + it 'flushes the repository cache' do + expect(repository).to receive(:before_delete) + + subject + end + + it 'returns success and does not perform any action if repository path does not exist' do + expect(repository).to receive(:disk_path).and_return('foo') + expect(repository).not_to receive(:before_delete) + + result = subject + + expect(result[:status]).to eq :success + end + + context 'when move operation cannot be performed' do + let(:service) { described_class.new(repository) } + + before do + allow(service).to receive(:mv_repository).and_return(false) + end + + it 'returns error' do + result = service.execute + + expect(result[:status]).to eq :error + end + + it 'logs the error' do + expect(Gitlab::AppLogger).to receive(:error) + + service.execute + end + end + + def destroy_project(project, user) + Projects::DestroyService.new(project, user, {}).execute + end +end diff --git a/spec/services/repositories/destroy_service_spec.rb b/spec/services/repositories/destroy_service_spec.rb new file mode 100644 index 00000000000..9c2694483c1 --- /dev/null +++ b/spec/services/repositories/destroy_service_spec.rb @@ -0,0 +1,80 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe Repositories::DestroyService do + let_it_be(:user) { create(:user) } + let!(:project) { create(:project, :repository, namespace: user.namespace) } + let(:repository) { project.repository } + let(:path) { repository.disk_path } + let(:remove_path) { "#{path}+#{project.id}#{described_class::DELETED_FLAG}" } + + subject { described_class.new(project.repository).execute } + + it 'moves the repository to a +deleted folder' do + expect(project.gitlab_shell.repository_exists?(project.repository_storage, path + '.git')).to be_truthy + expect(project.gitlab_shell.repository_exists?(project.repository_storage, remove_path + '.git')).to be_falsey + + subject + + expect(project.gitlab_shell.repository_exists?(project.repository_storage, path + '.git')).to be_falsey + expect(project.gitlab_shell.repository_exists?(project.repository_storage, remove_path + '.git')).to be_truthy + end + + it 'schedules the repository deletion' do + subject + + expect(Repositories::ShellDestroyService).to receive(:new).with(repository).and_call_original + + expect(GitlabShellWorker).to receive(:perform_in) + .with(Repositories::ShellDestroyService::REPO_REMOVAL_DELAY, :remove_repository, project.repository_storage, remove_path) + + # Because GitlabShellWorker is inside a run_after_commit callback we need to + # trigger the callback + project.touch + end + + it 'removes the repository', :sidekiq_inline do + subject + + project.touch + + expect(project.gitlab_shell.repository_exists?(project.repository_storage, path + '.git')).to be_falsey + expect(project.gitlab_shell.repository_exists?(project.repository_storage, remove_path + '.git')).to be_falsey + end + + it 'flushes the repository cache' do + expect(repository).to receive(:before_delete) + + subject + end + + it 'does not perform any action if repository path does not exist and returns success' do + expect(repository).to receive(:disk_path).and_return('foo') + expect(repository).not_to receive(:before_delete) + + result = subject + + expect(result[:status]).to eq :success + end + + context 'when move operation cannot be performed' do + let(:service) { described_class.new(repository) } + + before do + allow(service).to receive(:mv_repository).and_return(false) + end + + it 'returns error' do + result = service.execute + + expect(result[:status]).to eq :error + end + + it 'logs the error' do + expect(Gitlab::AppLogger).to receive(:error) + + service.execute + end + end +end diff --git a/spec/services/repositories/shell_destroy_service_spec.rb b/spec/services/repositories/shell_destroy_service_spec.rb new file mode 100644 index 00000000000..9419977f6fe --- /dev/null +++ b/spec/services/repositories/shell_destroy_service_spec.rb @@ -0,0 +1,25 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe Repositories::ShellDestroyService do + let_it_be(:user) { create(:user) } + let!(:project) { create(:project, :repository, namespace: user.namespace) } + let(:path) { project.repository.disk_path } + let(:remove_path) { "#{path}+#{project.id}#{described_class::DELETED_FLAG}" } + + it 'returns success if the repository is nil' do + expect(GitlabShellWorker).not_to receive(:perform_in) + + result = described_class.new(nil).execute + + expect(result[:status]).to eq :success + end + + it 'schedules the repository deletion' do + expect(GitlabShellWorker).to receive(:perform_in) + .with(described_class::REPO_REMOVAL_DELAY, :remove_repository, project.repository_storage, remove_path) + + described_class.new(project.repository).execute + end +end |