diff options
Diffstat (limited to 'spec/services/bulk_imports')
5 files changed, 222 insertions, 0 deletions
diff --git a/spec/services/bulk_imports/create_service_spec.rb b/spec/services/bulk_imports/create_service_spec.rb new file mode 100644 index 00000000000..67ec6fee1ae --- /dev/null +++ b/spec/services/bulk_imports/create_service_spec.rb @@ -0,0 +1,86 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe BulkImports::CreateService do + let(:user) { create(:user) } + let(:credentials) { { url: 'http://gitlab.example', access_token: 'token' } } + let(:params) do + [ + { + source_type: 'group_entity', + source_full_path: 'full/path/to/group1', + destination_name: 'destination group 1', + destination_namespace: 'full/path/to/destination1' + }, + { + source_type: 'group_entity', + source_full_path: 'full/path/to/group2', + destination_name: 'destination group 2', + destination_namespace: 'full/path/to/destination2' + }, + { + source_type: 'project_entity', + source_full_path: 'full/path/to/project1', + destination_name: 'destination project 1', + destination_namespace: 'full/path/to/destination1' + } + ] + end + + subject { described_class.new(user, params, credentials) } + + describe '#execute' do + let_it_be(:source_version) do + Gitlab::VersionInfo.new(::BulkImport::MIN_MAJOR_VERSION, + ::BulkImport::MIN_MINOR_VERSION_FOR_PROJECT) + end + + before do + allow_next_instance_of(BulkImports::Clients::HTTP) do |instance| + allow(instance).to receive(:instance_version).and_return(source_version) + end + end + + it 'creates bulk import' do + expect { subject.execute }.to change { BulkImport.count }.by(1) + + last_bulk_import = BulkImport.last + + expect(last_bulk_import.user).to eq(user) + expect(last_bulk_import.source_version).to eq(source_version.to_s) + expect(last_bulk_import.user).to eq(user) + end + + it 'creates bulk import entities' do + expect { subject.execute }.to change { BulkImports::Entity.count }.by(3) + end + + it 'creates bulk import configuration' do + expect { subject.execute }.to change { BulkImports::Configuration.count }.by(1) + end + + it 'enqueues BulkImportWorker' do + expect(BulkImportWorker).to receive(:perform_async) + + subject.execute + end + + it 'returns success ServiceResponse' do + result = subject.execute + + expect(result).to be_a(ServiceResponse) + expect(result).to be_success + end + + it 'returns ServiceResponse with error if validation fails' do + params[0][:source_full_path] = nil + + result = subject.execute + + expect(result).to be_a(ServiceResponse) + expect(result).to be_error + expect(result.message).to eq("Validation failed: Source full path can't be blank") + end + end +end diff --git a/spec/services/bulk_imports/file_export_service_spec.rb b/spec/services/bulk_imports/file_export_service_spec.rb new file mode 100644 index 00000000000..0d129c75384 --- /dev/null +++ b/spec/services/bulk_imports/file_export_service_spec.rb @@ -0,0 +1,37 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe BulkImports::FileExportService do + let_it_be(:project) { create(:project) } + let_it_be(:export_path) { Dir.mktmpdir } + let_it_be(:relation) { 'uploads' } + + subject(:service) { described_class.new(project, export_path, relation) } + + describe '#execute' do + it 'executes export service and archives exported data' do + expect_next_instance_of(BulkImports::UploadsExportService) do |service| + expect(service).to receive(:execute) + end + + expect(subject).to receive(:tar_cf).with(archive: File.join(export_path, 'uploads.tar'), dir: export_path) + + subject.execute + end + + context 'when unsupported relation is passed' do + it 'raises an error' do + service = described_class.new(project, export_path, 'unsupported') + + expect { service.execute }.to raise_error(BulkImports::Error, 'Unsupported relation export type') + end + end + end + + describe '#exported_filename' do + it 'returns filename of the exported file' do + expect(subject.exported_filename).to eq('uploads.tar') + end + end +end diff --git a/spec/services/bulk_imports/get_importable_data_service_spec.rb b/spec/services/bulk_imports/get_importable_data_service_spec.rb new file mode 100644 index 00000000000..eccd3e5f49d --- /dev/null +++ b/spec/services/bulk_imports/get_importable_data_service_spec.rb @@ -0,0 +1,46 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe BulkImports::GetImportableDataService do + describe '#execute' do + include_context 'bulk imports requests context', 'https://gitlab.example.com' + + let_it_be(:params) { { per_page: 20, page: 1 } } + let_it_be(:query_params) { { top_level_only: true, min_access_level: 50, search: '' } } + let_it_be(:credentials) { { url: 'https://gitlab.example.com', access_token: 'demo-pat' } } + let_it_be(:expected_version_validation) do + { + features: { + project_migration: { + available: true, + min_version: BulkImport.min_gl_version_for_project_migration.to_s + }, + 'source_instance_version': BulkImport.min_gl_version_for_project_migration.to_s + } + } + end + + let_it_be(:expected_parsed_response) do + [ + { + 'id' => 2595438, + 'web_url' => 'https://gitlab.com/groups/auto-breakfast', + 'name' => 'Stub', + 'path' => 'stub-group', + 'full_name' => 'Stub', + 'full_path' => 'stub-group' + } + ] + end + + subject do + described_class.new(params, query_params, credentials).execute + end + + it 'returns version_validation and a response' do + expect(subject[:version_validation]).to eq(expected_version_validation) + expect(subject[:response].parsed_response).to eq(expected_parsed_response) + end + end +end diff --git a/spec/services/bulk_imports/relation_export_service_spec.rb b/spec/services/bulk_imports/relation_export_service_spec.rb index 333cd9201d8..27a6ca60515 100644 --- a/spec/services/bulk_imports/relation_export_service_spec.rb +++ b/spec/services/bulk_imports/relation_export_service_spec.rb @@ -7,12 +7,14 @@ RSpec.describe BulkImports::RelationExportService do let_it_be(:relation) { 'labels' } let_it_be(:user) { create(:user) } let_it_be(:group) { create(:group) } + let_it_be(:project) { create(:project) } let_it_be(:label) { create(:group_label, group: group) } let_it_be(:export_path) { "#{Dir.tmpdir}/relation_export_service_spec/tree" } let_it_be_with_reload(:export) { create(:bulk_import_export, group: group, relation: relation) } before do group.add_owner(user) + project.add_maintainer(user) allow(export).to receive(:export_path).and_return(export_path) end @@ -25,6 +27,10 @@ RSpec.describe BulkImports::RelationExportService do describe '#execute' do it 'exports specified relation and marks export as finished' do + expect_next_instance_of(BulkImports::TreeExportService) do |service| + expect(service).to receive(:execute).and_call_original + end + subject.execute expect(export.reload.upload.export_file).to be_present @@ -43,6 +49,18 @@ RSpec.describe BulkImports::RelationExportService do expect(export.upload.export_file).to be_present end + context 'when exporting a file relation' do + it 'uses file export service' do + service = described_class.new(user, project, 'uploads', jid) + + expect_next_instance_of(BulkImports::FileExportService) do |service| + expect(service).to receive(:execute) + end + + service.execute + end + end + context 'when export record does not exist' do let(:another_group) { create(:group) } diff --git a/spec/services/bulk_imports/tree_export_service_spec.rb b/spec/services/bulk_imports/tree_export_service_spec.rb new file mode 100644 index 00000000000..f2ed747b64e --- /dev/null +++ b/spec/services/bulk_imports/tree_export_service_spec.rb @@ -0,0 +1,35 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe BulkImports::TreeExportService do + let_it_be(:project) { create(:project) } + let_it_be(:export_path) { Dir.mktmpdir } + let_it_be(:relation) { 'issues' } + + subject(:service) { described_class.new(project, export_path, relation) } + + describe '#execute' do + it 'executes export service and archives exported data' do + expect_next_instance_of(Gitlab::ImportExport::Json::StreamingSerializer) do |serializer| + expect(serializer).to receive(:serialize_relation) + end + + subject.execute + end + + context 'when unsupported relation is passed' do + it 'raises an error' do + service = described_class.new(project, export_path, 'unsupported') + + expect { service.execute }.to raise_error(BulkImports::Error, 'Unsupported relation export type') + end + end + end + + describe '#exported_filename' do + it 'returns filename of the exported file' do + expect(subject.exported_filename).to eq('issues.ndjson') + end + end +end |