Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2020-01-18 00:08:29 +0300
committerGitLab Bot <gitlab-bot@gitlab.com>2020-01-18 00:08:29 +0300
commit40254b9ace2a74a3c9f1cc51a1b1d5e3e78c1ae9 (patch)
tree9b735ef933178be36d35088f3acab2d9b75dbbad /spec/lib/gitlab/import_export
parent22a0d312ae82e7dda3073d5d1a5a766d7641738d (diff)
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec/lib/gitlab/import_export')
-rw-r--r--spec/lib/gitlab/import_export/import_failure_service_spec.rb107
1 files changed, 107 insertions, 0 deletions
diff --git a/spec/lib/gitlab/import_export/import_failure_service_spec.rb b/spec/lib/gitlab/import_export/import_failure_service_spec.rb
new file mode 100644
index 00000000000..0351f88afdb
--- /dev/null
+++ b/spec/lib/gitlab/import_export/import_failure_service_spec.rb
@@ -0,0 +1,107 @@
+# frozen_string_literal: true
+
+require 'spec_helper'
+
+describe Gitlab::ImportExport::ImportFailureService do
+ let(:importable) { create(:project, :builds_enabled, :issues_disabled, name: 'project', path: 'project') }
+ let(:label) { create(:label) }
+ let(:subject) { described_class.new(importable) }
+ let(:relation_key) { "labels" }
+ let(:relation_index) { 0 }
+
+ describe '#log_import_failure' do
+ let(:standard_error_message) { "StandardError message" }
+ let(:exception) { StandardError.new(standard_error_message) }
+ let(:correlation_id) { 'my-correlation-id' }
+ let(:retry_count) { 2 }
+ let(:log_import_failure) do
+ subject.log_import_failure(relation_key, relation_index, exception, retry_count)
+ end
+
+ before do
+ # Import is running from the rake task, `correlation_id` is not assigned
+ allow(Labkit::Correlation::CorrelationId).to receive(:current_or_new_id).and_return(correlation_id)
+ end
+
+ context 'when importable is a group' do
+ let(:importable) { create(:group) }
+
+ it_behaves_like 'log import failure', :group_id
+ end
+
+ context 'when importable is a project' do
+ it_behaves_like 'log import failure', :project_id
+ end
+
+ context 'when ImportFailure does not support importable class' do
+ let(:importable) { create(:merge_request) }
+
+ it 'raise exception' do
+ expect { subject }.to raise_exception(ActiveRecord::AssociationNotFoundError, "Association named 'import_failures' was not found on MergeRequest; perhaps you misspelled it?")
+ end
+ end
+ end
+
+ describe '#with_retry' do
+ let(:perform_retry) do
+ subject.with_retry(relation_key, relation_index) do
+ label.save!
+ end
+ end
+
+ context 'when exceptions are retriable' do
+ where(:exception) { Gitlab::ImportExport::ImportFailureService::RETRIABLE_EXCEPTIONS }
+
+ with_them do
+ context 'when retry succeeds' do
+ before do
+ expect(label).to receive(:save!).and_raise(exception.new)
+ expect(label).to receive(:save!).and_return(true)
+ end
+
+ it 'retries and logs import failure once with correct params' do
+ expect(subject).to receive(:log_import_failure).with(relation_key, relation_index, instance_of(exception), 1).once
+
+ perform_retry
+ end
+ end
+
+ context 'when retry continues to fail with intermittent errors' do
+ let(:maximum_retry_count) do
+ Retriable.config.tries
+ end
+
+ before do
+ expect(label).to receive(:save!)
+ .exactly(maximum_retry_count).times
+ .and_raise(exception.new)
+ end
+
+ it 'retries the number of times allowed and raise exception', :aggregate_failures do
+ expect { perform_retry }.to raise_exception(exception)
+ end
+
+ it 'logs import failure each time and raise exception', :aggregate_failures do
+ maximum_retry_count.times do |index|
+ retry_count = index + 1
+
+ expect(subject).to receive(:log_import_failure).with(relation_key, relation_index, instance_of(exception), retry_count)
+ end
+
+ expect { perform_retry }.to raise_exception(exception)
+ end
+ end
+ end
+ end
+
+ context 'when exception is not retriable' do
+ let(:exception) { StandardError.new }
+
+ it 'raise the exception', :aggregate_failures do
+ expect(label).to receive(:save!).once.and_raise(exception)
+ expect(subject).not_to receive(:log_import_failure)
+ expect { perform_retry }.to raise_exception(exception)
+ end
+ end
+ end
+end