2019-04-11 12:17:24 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-03-30 15:45:59 +00:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe Projects::ImportExport::ExportService do
|
|
|
|
describe '#execute' do
|
|
|
|
let!(:user) { create(:user) }
|
|
|
|
let(:project) { create(:project) }
|
|
|
|
let(:shared) { project.import_export_shared }
|
|
|
|
let(:service) { described_class.new(project, user) }
|
|
|
|
let!(:after_export_strategy) { Gitlab::ImportExport::AfterExportStrategies::DownloadNotificationStrategy.new }
|
|
|
|
|
2020-01-30 21:08:47 +00:00
|
|
|
before do
|
|
|
|
project.add_maintainer(user)
|
|
|
|
end
|
|
|
|
|
2018-03-30 14:32:21 +00:00
|
|
|
it 'saves the version' do
|
|
|
|
expect(Gitlab::ImportExport::VersionSaver).to receive(:new).and_call_original
|
|
|
|
|
|
|
|
service.execute
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'saves the avatar' do
|
|
|
|
expect(Gitlab::ImportExport::AvatarSaver).to receive(:new).and_call_original
|
|
|
|
|
|
|
|
service.execute
|
|
|
|
end
|
|
|
|
|
2020-04-07 15:09:30 +00:00
|
|
|
it 'saves the models' do
|
|
|
|
expect(Gitlab::ImportExport::Project::TreeSaver).to receive(:new).and_call_original
|
2018-03-30 14:32:21 +00:00
|
|
|
|
2020-04-07 15:09:30 +00:00
|
|
|
service.execute
|
2018-03-30 14:32:21 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'saves the uploads' do
|
|
|
|
expect(Gitlab::ImportExport::UploadsSaver).to receive(:new).and_call_original
|
|
|
|
|
|
|
|
service.execute
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'saves the repo' do
|
2019-09-27 09:06:26 +00:00
|
|
|
# This spec errors when run against the EE codebase as there will be a third repository
|
|
|
|
# saved (the EE-specific design repository).
|
|
|
|
#
|
|
|
|
# Instead, skip this test when run within EE. There is a spec for the EE-specific design repo
|
|
|
|
# in the corresponding EE spec.
|
|
|
|
skip if Gitlab.ee?
|
|
|
|
|
2020-05-12 00:10:11 +00:00
|
|
|
# once for the normal repo, once for the wiki repo, and once for the design repo
|
|
|
|
expect(Gitlab::ImportExport::RepoSaver).to receive(:new).exactly(3).times.and_call_original
|
2018-03-30 14:32:21 +00:00
|
|
|
|
|
|
|
service.execute
|
|
|
|
end
|
|
|
|
|
2019-09-27 09:06:26 +00:00
|
|
|
it 'saves the wiki repo' do
|
|
|
|
expect(Gitlab::ImportExport::WikiRepoSaver).to receive(:new).and_call_original
|
2018-03-30 14:32:21 +00:00
|
|
|
|
|
|
|
service.execute
|
|
|
|
end
|
|
|
|
|
2020-05-12 00:10:11 +00:00
|
|
|
it 'saves the design repo' do
|
|
|
|
expect(Gitlab::ImportExport::DesignRepoSaver).to receive(:new).and_call_original
|
|
|
|
|
|
|
|
service.execute
|
|
|
|
end
|
|
|
|
|
2019-09-27 09:06:26 +00:00
|
|
|
it 'saves the lfs objects' do
|
|
|
|
expect(Gitlab::ImportExport::LfsSaver).to receive(:new).and_call_original
|
2018-03-30 14:32:21 +00:00
|
|
|
|
|
|
|
service.execute
|
|
|
|
end
|
|
|
|
|
2020-03-06 18:08:08 +00:00
|
|
|
it 'saves the snippets' do
|
|
|
|
expect_next_instance_of(Gitlab::ImportExport::SnippetsRepoSaver) do |instance|
|
|
|
|
expect(instance).to receive(:save).and_call_original
|
|
|
|
end
|
|
|
|
|
|
|
|
service.execute
|
|
|
|
end
|
|
|
|
|
2018-03-30 15:45:59 +00:00
|
|
|
context 'when all saver services succeed' do
|
|
|
|
before do
|
|
|
|
allow(service).to receive(:save_services).and_return(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'saves the project in the file system' do
|
2019-11-11 21:06:20 +00:00
|
|
|
expect(Gitlab::ImportExport::Saver).to receive(:save).with(exportable: project, shared: shared)
|
2018-03-30 15:45:59 +00:00
|
|
|
|
|
|
|
service.execute
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'calls the after export strategy' do
|
|
|
|
expect(after_export_strategy).to receive(:execute)
|
|
|
|
|
|
|
|
service.execute(after_export_strategy)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when after export strategy fails' do
|
|
|
|
before do
|
|
|
|
allow(after_export_strategy).to receive(:execute).and_return(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
|
|
|
service.execute(after_export_strategy)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'removes the remaining exported data' do
|
2020-02-26 18:09:24 +00:00
|
|
|
allow(shared).to receive(:archive_path).and_return('whatever')
|
2018-03-30 15:45:59 +00:00
|
|
|
allow(FileUtils).to receive(:rm_rf)
|
|
|
|
|
2020-02-26 18:09:24 +00:00
|
|
|
expect(FileUtils).to receive(:rm_rf).with(shared.archive_path)
|
2018-03-30 15:45:59 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'notifies the user' do
|
2020-01-17 21:08:29 +00:00
|
|
|
expect_next_instance_of(NotificationService) do |instance|
|
|
|
|
expect(instance).to receive(:project_not_exported)
|
|
|
|
end
|
2018-03-30 15:45:59 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'notifies logger' do
|
|
|
|
allow(Rails.logger).to receive(:error)
|
|
|
|
|
|
|
|
expect(Rails.logger).to receive(:error)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-09-27 09:06:26 +00:00
|
|
|
context 'when saving services fail' do
|
2018-03-30 15:45:59 +00:00
|
|
|
before do
|
2019-09-27 09:06:26 +00:00
|
|
|
allow(service).to receive(:save_exporters).and_return(false)
|
2018-03-30 15:45:59 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
|
|
|
expect { service.execute }.to raise_error(Gitlab::ImportExport::Error)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'removes the remaining exported data' do
|
2020-02-26 18:09:24 +00:00
|
|
|
allow(shared).to receive(:archive_path).and_return('whatever')
|
2018-03-30 15:45:59 +00:00
|
|
|
allow(FileUtils).to receive(:rm_rf)
|
|
|
|
|
2020-02-26 18:09:24 +00:00
|
|
|
expect(FileUtils).to receive(:rm_rf).with(shared.archive_path)
|
2018-03-30 15:45:59 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'notifies the user' do
|
2020-01-17 21:08:29 +00:00
|
|
|
expect_next_instance_of(NotificationService) do |instance|
|
|
|
|
expect(instance).to receive(:project_not_exported)
|
|
|
|
end
|
2018-03-30 15:45:59 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'notifies logger' do
|
|
|
|
expect(Rails.logger).to receive(:error)
|
|
|
|
end
|
|
|
|
|
2019-09-27 09:06:26 +00:00
|
|
|
it 'does not call the export strategy' do
|
2018-03-30 15:45:59 +00:00
|
|
|
expect(service).not_to receive(:execute_after_export_action)
|
|
|
|
end
|
|
|
|
end
|
2020-01-30 21:08:47 +00:00
|
|
|
|
2020-02-26 18:09:24 +00:00
|
|
|
context 'when one of the savers fail unexpectedly' do
|
|
|
|
let(:archive_path) { shared.archive_path }
|
|
|
|
|
|
|
|
before do
|
|
|
|
allow(service).to receive_message_chain(:uploads_saver, :save).and_return(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'removes the remaining exported data' do
|
|
|
|
expect { service.execute }.to raise_error(Gitlab::ImportExport::Error)
|
|
|
|
|
|
|
|
expect(project.import_export_upload).to be_nil
|
|
|
|
expect(File.exist?(shared.archive_path)).to eq(false)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-01-30 21:08:47 +00:00
|
|
|
context 'when user does not have admin_project permission' do
|
|
|
|
let!(:another_user) { create(:user) }
|
|
|
|
|
|
|
|
subject(:service) { described_class.new(project, another_user) }
|
|
|
|
|
|
|
|
it 'fails' do
|
|
|
|
expected_message =
|
2020-03-05 18:08:19 +00:00
|
|
|
"User with ID: %s does not have required permissions for Project: %s with ID: %s" %
|
2020-01-30 21:08:47 +00:00
|
|
|
[another_user.id, project.name, project.id]
|
|
|
|
expect { service.execute }.to raise_error(Gitlab::ImportExport::Error).with_message(expected_message)
|
|
|
|
end
|
|
|
|
end
|
2020-04-24 12:10:16 +00:00
|
|
|
|
|
|
|
context 'when measurable params are provided' do
|
|
|
|
let(:base_log_data) do
|
|
|
|
{
|
|
|
|
class: described_class.name,
|
|
|
|
current_user: user.name,
|
|
|
|
project_full_path: project.full_path,
|
|
|
|
file_path: shared.export_path
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
subject(:service) { described_class.new(project, user) }
|
|
|
|
|
|
|
|
context 'when measurement is enabled' do
|
|
|
|
let(:logger) { double(:logger) }
|
|
|
|
let(:measurable_options) do
|
|
|
|
{
|
|
|
|
measurement_enabled: true,
|
|
|
|
measurement_logger: logger
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
allow(logger).to receive(:info)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'measure service execution with Gitlab::Utils::Measuring' do
|
|
|
|
expect(Gitlab::Utils::Measuring).to receive(:execute_with).with(true, logger, base_log_data).and_call_original
|
|
|
|
expect_next_instance_of(Gitlab::Utils::Measuring) do |measuring|
|
|
|
|
expect(measuring).to receive(:with_measuring).and_call_original
|
|
|
|
end
|
|
|
|
|
|
|
|
service.execute(after_export_strategy, measurable_options)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when measurement is disabled' do
|
|
|
|
let(:measurable_options) do
|
|
|
|
{
|
|
|
|
measurement_enabled: false
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not measure service execution' do
|
|
|
|
expect(Gitlab::Utils::Measuring).to receive(:execute_with).with(false, nil, base_log_data).and_call_original
|
|
|
|
expect(Gitlab::Utils::Measuring).not_to receive(:new)
|
|
|
|
|
|
|
|
service.execute(after_export_strategy, measurable_options)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-03-30 15:45:59 +00:00
|
|
|
end
|
|
|
|
end
|