2020-03-13 15:44:24 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
RSpec.describe Groups::ImportExport::ImportService do
|
2020-06-23 00:09:42 +05:30
|
|
|
describe '#async_execute' do
|
|
|
|
let_it_be(:user) { create(:user) }
|
|
|
|
let_it_be(:group) { create(:group) }
|
|
|
|
|
2021-12-11 22:18:48 +05:30
|
|
|
before do
|
|
|
|
allow(GroupImportWorker).to receive(:with_status).and_return(GroupImportWorker)
|
|
|
|
end
|
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
context 'when the job can be successfully scheduled' do
|
|
|
|
subject(:import_service) { described_class.new(group: group, user: user) }
|
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
it 'creates group import state' do
|
|
|
|
import_service.async_execute
|
|
|
|
|
|
|
|
import_state = group.import_state
|
|
|
|
|
|
|
|
expect(import_state.user).to eq(user)
|
|
|
|
expect(import_state.group).to eq(group)
|
|
|
|
end
|
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
it 'enqueues an import job' do
|
2021-12-11 22:18:48 +05:30
|
|
|
allow(GroupImportWorker).to receive(:with_status).and_return(GroupImportWorker)
|
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
expect(GroupImportWorker).to receive(:perform_async).with(user.id, group.id)
|
|
|
|
|
|
|
|
import_service.async_execute
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'marks the group import as in progress' do
|
|
|
|
import_service.async_execute
|
|
|
|
|
|
|
|
expect(group.import_state.in_progress?).to eq true
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns truthy' do
|
|
|
|
expect(import_service.async_execute).to be_truthy
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the job cannot be scheduled' do
|
|
|
|
subject(:import_service) { described_class.new(group: group, user: user) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
allow(GroupImportWorker).to receive(:perform_async).and_return(nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns falsey' do
|
|
|
|
expect(import_service.async_execute).to be_falsey
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not mark the group import as created' do
|
|
|
|
expect { import_service.async_execute }.not_to change { group.import_state }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
context 'with group_import_ndjson feature flag disabled' do
|
2021-04-17 20:07:23 +05:30
|
|
|
let(:user) { create(:user) }
|
2020-03-13 15:44:24 +05:30
|
|
|
let(:group) { create(:group) }
|
2020-04-08 14:13:33 +05:30
|
|
|
let(:import_logger) { instance_double(Gitlab::Import::Logger) }
|
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
subject(:service) { described_class.new(group: group, user: user) }
|
2020-03-13 15:44:24 +05:30
|
|
|
|
|
|
|
before do
|
2020-05-24 23:13:21 +05:30
|
|
|
stub_feature_flags(group_import_ndjson: false)
|
|
|
|
|
2021-04-17 20:07:23 +05:30
|
|
|
group.add_owner(user)
|
|
|
|
|
2021-01-29 00:20:46 +05:30
|
|
|
ImportExportUpload.create!(group: group, import_file: import_file)
|
2020-04-22 19:07:51 +05:30
|
|
|
|
|
|
|
allow(Gitlab::Import::Logger).to receive(:build).and_return(import_logger)
|
|
|
|
allow(import_logger).to receive(:error)
|
|
|
|
allow(import_logger).to receive(:info)
|
2020-03-13 15:44:24 +05:30
|
|
|
end
|
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
context 'with a json file' do
|
|
|
|
let(:import_file) { fixture_file_upload('spec/fixtures/legacy_group_export.tar.gz') }
|
2020-03-13 15:44:24 +05:30
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
it 'uses LegacyTreeRestorer to import the file' do
|
|
|
|
expect(Gitlab::ImportExport::Group::LegacyTreeRestorer).to receive(:new).and_call_original
|
2020-03-13 15:44:24 +05:30
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
service.execute
|
2020-03-13 15:44:24 +05:30
|
|
|
end
|
2020-05-24 23:13:21 +05:30
|
|
|
end
|
2020-04-08 14:13:33 +05:30
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
context 'with a ndjson file' do
|
|
|
|
let(:import_file) { fixture_file_upload('spec/fixtures/group_export.tar.gz') }
|
2020-04-08 14:13:33 +05:30
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
it 'fails to import' do
|
|
|
|
expect { service.execute }.to raise_error(Gitlab::ImportExport::Error, 'Incorrect JSON format')
|
2020-04-08 14:13:33 +05:30
|
|
|
end
|
2020-03-13 15:44:24 +05:30
|
|
|
end
|
2020-05-24 23:13:21 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
context 'with group_import_ndjson feature flag enabled' do
|
|
|
|
before do
|
|
|
|
stub_feature_flags(group_import_ndjson: true)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when importing a ndjson export' do
|
2021-04-17 20:07:23 +05:30
|
|
|
let(:user) { create(:user) }
|
2020-05-24 23:13:21 +05:30
|
|
|
let(:group) { create(:group) }
|
|
|
|
let(:service) { described_class.new(group: group, user: user) }
|
|
|
|
let(:import_file) { fixture_file_upload('spec/fixtures/group_export.tar.gz') }
|
2020-03-13 15:44:24 +05:30
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
let(:import_logger) { instance_double(Gitlab::Import::Logger) }
|
2020-03-13 15:44:24 +05:30
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
subject { service.execute }
|
2020-04-08 14:13:33 +05:30
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
before do
|
2021-01-29 00:20:46 +05:30
|
|
|
ImportExportUpload.create!(group: group, import_file: import_file)
|
2020-05-24 23:13:21 +05:30
|
|
|
|
|
|
|
allow(Gitlab::Import::Logger).to receive(:build).and_return(import_logger)
|
|
|
|
allow(import_logger).to receive(:error)
|
|
|
|
allow(import_logger).to receive(:info)
|
2021-02-22 17:27:13 +05:30
|
|
|
allow(import_logger).to receive(:warn)
|
2020-06-23 00:09:42 +05:30
|
|
|
allow(FileUtils).to receive(:rm_rf).and_call_original
|
2020-04-08 14:13:33 +05:30
|
|
|
end
|
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
context 'when user has correct permissions' do
|
2021-04-17 20:07:23 +05:30
|
|
|
before do
|
|
|
|
group.add_owner(user)
|
|
|
|
end
|
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
it 'imports group structure successfully' do
|
|
|
|
expect(subject).to be_truthy
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'removes import file' do
|
|
|
|
subject
|
2020-04-08 14:13:33 +05:30
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
expect(group.import_export_upload.import_file.file).to be_nil
|
2020-04-08 14:13:33 +05:30
|
|
|
end
|
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
it 'removes tmp files' do
|
|
|
|
shared = Gitlab::ImportExport::Shared.new(group)
|
|
|
|
allow(Gitlab::ImportExport::Shared).to receive(:new).and_return(shared)
|
|
|
|
|
|
|
|
subject
|
|
|
|
|
|
|
|
expect(FileUtils).to have_received(:rm_rf).with(shared.base_path)
|
|
|
|
expect(Dir.exist?(shared.base_path)).to eq(false)
|
|
|
|
end
|
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
it 'logs the import success' do
|
|
|
|
expect(import_logger).to receive(:info).with(
|
|
|
|
group_id: group.id,
|
|
|
|
group_name: group.name,
|
|
|
|
message: 'Group Import/Export: Import succeeded'
|
|
|
|
).once
|
|
|
|
|
|
|
|
subject
|
|
|
|
end
|
2020-04-08 14:13:33 +05:30
|
|
|
end
|
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
context 'when user does not have correct permissions' do
|
|
|
|
it 'logs the error and raises an exception' do
|
|
|
|
expect(import_logger).to receive(:error).with(
|
|
|
|
group_id: group.id,
|
|
|
|
group_name: group.name,
|
|
|
|
message: a_string_including('Errors occurred')
|
|
|
|
)
|
2020-04-08 14:13:33 +05:30
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
expect { subject }.to raise_error(Gitlab::ImportExport::Error)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'tracks the error' do
|
|
|
|
shared = Gitlab::ImportExport::Shared.new(group)
|
|
|
|
allow(Gitlab::ImportExport::Shared).to receive(:new).and_return(shared)
|
|
|
|
|
|
|
|
expect(shared).to receive(:error) do |param|
|
|
|
|
expect(param.message).to include 'does not have required permissions for'
|
|
|
|
end
|
|
|
|
|
|
|
|
expect { subject }.to raise_error(Gitlab::ImportExport::Error)
|
|
|
|
end
|
2020-04-08 14:13:33 +05:30
|
|
|
end
|
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
context 'when there are errors with the import file' do
|
|
|
|
let(:import_file) { fixture_file_upload('spec/fixtures/symlink_export.tar.gz') }
|
|
|
|
|
|
|
|
it 'logs the error and raises an exception' do
|
|
|
|
expect(import_logger).to receive(:error).with(
|
|
|
|
group_id: group.id,
|
|
|
|
group_name: group.name,
|
|
|
|
message: a_string_including('Errors occurred')
|
|
|
|
).once
|
|
|
|
|
|
|
|
expect { subject }.to raise_error(Gitlab::ImportExport::Error)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when there are errors with the sub-relations' do
|
|
|
|
let(:import_file) { fixture_file_upload('spec/fixtures/group_export_invalid_subrelations.tar.gz') }
|
|
|
|
|
2021-04-17 20:07:23 +05:30
|
|
|
before do
|
|
|
|
group.add_owner(user)
|
|
|
|
end
|
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
it 'successfully imports the group' do
|
|
|
|
expect(subject).to be_truthy
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'logs the import success' do
|
|
|
|
allow(Gitlab::Import::Logger).to receive(:build).and_return(import_logger)
|
|
|
|
|
|
|
|
expect(import_logger).to receive(:info).with(
|
|
|
|
group_id: group.id,
|
|
|
|
group_name: group.name,
|
|
|
|
message: 'Group Import/Export: Import succeeded'
|
|
|
|
)
|
2020-04-08 14:13:33 +05:30
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
subject
|
|
|
|
end
|
2020-04-08 14:13:33 +05:30
|
|
|
end
|
2020-05-24 23:13:21 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
context 'when importing a json export' do
|
2021-04-17 20:07:23 +05:30
|
|
|
let(:user) { create(:user) }
|
2020-05-24 23:13:21 +05:30
|
|
|
let(:group) { create(:group) }
|
|
|
|
let(:service) { described_class.new(group: group, user: user) }
|
|
|
|
let(:import_file) { fixture_file_upload('spec/fixtures/legacy_group_export.tar.gz') }
|
|
|
|
|
|
|
|
let(:import_logger) { instance_double(Gitlab::Import::Logger) }
|
|
|
|
|
|
|
|
subject { service.execute }
|
|
|
|
|
|
|
|
before do
|
2021-01-29 00:20:46 +05:30
|
|
|
ImportExportUpload.create!(group: group, import_file: import_file)
|
2020-04-08 14:13:33 +05:30
|
|
|
|
|
|
|
allow(Gitlab::Import::Logger).to receive(:build).and_return(import_logger)
|
2020-05-24 23:13:21 +05:30
|
|
|
allow(import_logger).to receive(:error)
|
2021-02-22 17:27:13 +05:30
|
|
|
allow(import_logger).to receive(:warn)
|
2020-05-24 23:13:21 +05:30
|
|
|
allow(import_logger).to receive(:info)
|
2020-06-23 00:09:42 +05:30
|
|
|
allow(FileUtils).to receive(:rm_rf).and_call_original
|
2020-05-24 23:13:21 +05:30
|
|
|
end
|
2020-04-08 14:13:33 +05:30
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
context 'when user has correct permissions' do
|
2021-04-17 20:07:23 +05:30
|
|
|
before do
|
|
|
|
group.add_owner(user)
|
|
|
|
end
|
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
it 'imports group structure successfully' do
|
|
|
|
expect(subject).to be_truthy
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'removes import file' do
|
|
|
|
subject
|
|
|
|
|
|
|
|
expect(group.import_export_upload.import_file.file).to be_nil
|
|
|
|
end
|
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
it 'removes tmp files' do
|
|
|
|
shared = Gitlab::ImportExport::Shared.new(group)
|
|
|
|
allow(Gitlab::ImportExport::Shared).to receive(:new).and_return(shared)
|
|
|
|
|
|
|
|
subject
|
|
|
|
|
|
|
|
expect(FileUtils).to have_received(:rm_rf).with(shared.base_path)
|
|
|
|
expect(Dir.exist?(shared.base_path)).to eq(false)
|
|
|
|
end
|
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
it 'logs the import success' do
|
|
|
|
expect(import_logger).to receive(:info).with(
|
|
|
|
group_id: group.id,
|
|
|
|
group_name: group.name,
|
|
|
|
message: 'Group Import/Export: Import succeeded'
|
|
|
|
).once
|
|
|
|
|
|
|
|
subject
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when user does not have correct permissions' do
|
|
|
|
it 'logs the error and raises an exception' do
|
|
|
|
expect(import_logger).to receive(:error).with(
|
|
|
|
group_id: group.id,
|
|
|
|
group_name: group.name,
|
|
|
|
message: a_string_including('Errors occurred')
|
|
|
|
)
|
|
|
|
|
|
|
|
expect { subject }.to raise_error(Gitlab::ImportExport::Error)
|
|
|
|
end
|
2020-04-08 14:13:33 +05:30
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
it 'tracks the error' do
|
|
|
|
shared = Gitlab::ImportExport::Shared.new(group)
|
|
|
|
allow(Gitlab::ImportExport::Shared).to receive(:new).and_return(shared)
|
|
|
|
|
|
|
|
expect(shared).to receive(:error) do |param|
|
|
|
|
expect(param.message).to include 'does not have required permissions for'
|
|
|
|
end
|
|
|
|
|
|
|
|
expect { subject }.to raise_error(Gitlab::ImportExport::Error)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when there are errors with the import file' do
|
|
|
|
let(:import_file) { fixture_file_upload('spec/fixtures/legacy_symlink_export.tar.gz') }
|
|
|
|
|
|
|
|
it 'logs the error and raises an exception' do
|
|
|
|
expect(import_logger).to receive(:error).with(
|
|
|
|
group_id: group.id,
|
|
|
|
group_name: group.name,
|
|
|
|
message: a_string_including('Errors occurred')
|
|
|
|
).once
|
|
|
|
|
|
|
|
expect { subject }.to raise_error(Gitlab::ImportExport::Error)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when there are errors with the sub-relations' do
|
|
|
|
let(:import_file) { fixture_file_upload('spec/fixtures/legacy_group_export_invalid_subrelations.tar.gz') }
|
|
|
|
|
2021-04-17 20:07:23 +05:30
|
|
|
before do
|
|
|
|
group.add_owner(user)
|
|
|
|
end
|
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
it 'successfully imports the group' do
|
|
|
|
expect(subject).to be_truthy
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'logs the import success' do
|
|
|
|
allow(Gitlab::Import::Logger).to receive(:build).and_return(import_logger)
|
|
|
|
|
|
|
|
expect(import_logger).to receive(:info).with(
|
|
|
|
group_id: group.id,
|
|
|
|
group_name: group.name,
|
|
|
|
message: 'Group Import/Export: Import succeeded'
|
|
|
|
)
|
|
|
|
|
|
|
|
subject
|
|
|
|
end
|
2020-03-13 15:44:24 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|