diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2021-05-19 15:44:42 +0000 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2021-05-19 15:44:42 +0000 |
commit | 4555e1b21c365ed8303ffb7a3325d773c9b8bf31 (patch) | |
tree | 5423a1c7516cffe36384133ade12572cf709398d /spec/services/bulk_imports | |
parent | e570267f2f6b326480d284e0164a6464ba4081bc (diff) | |
download | gitlab-ce-4555e1b21c365ed8303ffb7a3325d773c9b8bf31.tar.gz |
Add latest changes from gitlab-org/gitlab@13-12-stable-eev13.12.0-rc42
Diffstat (limited to 'spec/services/bulk_imports')
-rw-r--r-- | spec/services/bulk_imports/export_service_spec.rb | 43 | ||||
-rw-r--r-- | spec/services/bulk_imports/relation_export_service_spec.rb | 116 |
2 files changed, 159 insertions, 0 deletions
diff --git a/spec/services/bulk_imports/export_service_spec.rb b/spec/services/bulk_imports/export_service_spec.rb new file mode 100644 index 00000000000..2414f7c5ca7 --- /dev/null +++ b/spec/services/bulk_imports/export_service_spec.rb @@ -0,0 +1,43 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe BulkImports::ExportService do + let_it_be(:group) { create(:group) } + let_it_be(:user) { create(:user) } + + before do + group.add_owner(user) + end + + subject { described_class.new(portable: group, user: user) } + + describe '#execute' do + it 'schedules RelationExportWorker for each top level relation' do + expect(subject).to receive(:execute).and_return(ServiceResponse.success).and_call_original + top_level_relations = BulkImports::FileTransfer.config_for(group).portable_relations + + top_level_relations.each do |relation| + expect(BulkImports::RelationExportWorker) + .to receive(:perform_async) + .with(user.id, group.id, group.class.name, relation) + end + + subject.execute + end + + context 'when exception occurs' do + it 'does not schedule RelationExportWorker' do + service = described_class.new(portable: nil, user: user) + + expect(service) + .to receive(:execute) + .and_return(ServiceResponse.error(message: 'Gitlab::ImportExport::Error', http_status: :unprocessible_entity)) + .and_call_original + expect(BulkImports::RelationExportWorker).not_to receive(:perform_async) + + service.execute + end + 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 new file mode 100644 index 00000000000..bf286998df2 --- /dev/null +++ b/spec/services/bulk_imports/relation_export_service_spec.rb @@ -0,0 +1,116 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe BulkImports::RelationExportService do + let_it_be(:jid) { 'jid' } + let_it_be(:relation) { 'labels' } + let_it_be(:user) { create(:user) } + let_it_be(:group) { create(:group) } + 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) + + allow(export).to receive(:export_path).and_return(export_path) + end + + after :all do + FileUtils.rm_rf(export_path) + end + + subject { described_class.new(user, group, relation, jid) } + + describe '#execute' do + it 'exports specified relation and marks export as finished' do + subject.execute + + expect(export.reload.upload.export_file).to be_present + expect(export.finished?).to eq(true) + end + + it 'removes temp export files' do + subject.execute + + expect(Dir.exist?(export_path)).to eq(false) + end + + it 'exports specified relation and marks export as finished' do + subject.execute + + expect(export.upload.export_file).to be_present + end + + context 'when export record does not exist' do + let(:another_group) { create(:group) } + + subject { described_class.new(user, another_group, relation, jid) } + + it 'creates export record' do + another_group.add_owner(user) + + expect { subject.execute } + .to change { another_group.bulk_import_exports.count } + .from(0) + .to(1) + end + end + + context 'when there is existing export present' do + let(:upload) { create(:bulk_import_export_upload, export: export) } + + it 'removes existing export before exporting' do + upload.update!(export_file: fixture_file_upload('spec/fixtures/bulk_imports/labels.ndjson.gz')) + + expect_any_instance_of(BulkImports::ExportUpload) do |upload| + expect(upload).to receive(:remove_export_file!) + end + + subject.execute + end + end + + context 'when exception occurs during export' do + shared_examples 'tracks exception' do |exception_class| + it 'tracks exception' do + expect(Gitlab::ErrorTracking) + .to receive(:track_exception) + .with(exception_class, portable_id: group.id, portable_type: group.class.name) + .and_call_original + + subject.execute + end + end + + before do + allow_next_instance_of(BulkImports::ExportUpload) do |upload| + allow(upload).to receive(:save!).and_raise(StandardError) + end + end + + it 'marks export as failed' do + subject.execute + + expect(export.reload.failed?).to eq(true) + end + + include_examples 'tracks exception', StandardError + + context 'when passed relation is not supported' do + let(:relation) { 'unsupported' } + + include_examples 'tracks exception', ActiveRecord::RecordInvalid + end + + context 'when user is not allowed to perform export' do + let(:another_user) { create(:user) } + + subject { described_class.new(another_user, group, relation, jid) } + + include_examples 'tracks exception', Gitlab::ImportExport::Error + end + end + end +end |