diff options
Diffstat (limited to 'spec/workers/bulk_imports/entity_worker_spec.rb')
-rw-r--r-- | spec/workers/bulk_imports/entity_worker_spec.rb | 124 |
1 files changed, 90 insertions, 34 deletions
diff --git a/spec/workers/bulk_imports/entity_worker_spec.rb b/spec/workers/bulk_imports/entity_worker_spec.rb index cd9a6f605b9..deae15a3ca2 100644 --- a/spec/workers/bulk_imports/entity_worker_spec.rb +++ b/spec/workers/bulk_imports/entity_worker_spec.rb @@ -3,51 +3,107 @@ require 'spec_helper' RSpec.describe BulkImports::EntityWorker do - describe '#execute' do - let(:bulk_import) { create(:bulk_import) } - - context 'when started entity exists' do - let(:entity) { create(:bulk_import_entity, :started, bulk_import: bulk_import) } - - it 'executes BulkImports::Importers::GroupImporter' do - expect(BulkImports::Importers::GroupImporter).to receive(:new).with(entity).and_call_original + let_it_be(:entity) { create(:bulk_import_entity) } + + let_it_be(:pipeline_tracker) do + create( + :bulk_import_tracker, + entity: entity, + pipeline_name: 'Stage0::Pipeline', + stage: 0 + ) + end - subject.perform(entity.id) - end + it 'enqueues the first stage pipelines work' do + expect_next_instance_of(Gitlab::Import::Logger) do |logger| + expect(logger) + .to receive(:info) + .with( + worker: described_class.name, + entity_id: entity.id, + current_stage: nil + ) + end - it 'sets jid' do - jid = 'jid' + expect(BulkImports::PipelineWorker) + .to receive(:perform_async) + .with( + pipeline_tracker.id, + pipeline_tracker.stage, + entity.id + ) - allow(subject).to receive(:jid).and_return(jid) + subject.perform(entity.id) + end - subject.perform(entity.id) + it 'do not enqueue a new pipeline job if the current stage still running' do + expect(BulkImports::PipelineWorker) + .not_to receive(:perform_async) - expect(entity.reload.jid).to eq(jid) - end + subject.perform(entity.id, 0) + end - context 'when exception occurs' do - it 'tracks the exception & marks entity as failed' do - allow(BulkImports::Importers::GroupImporter).to receive(:new) { raise StandardError } + it 'enqueues the next stage pipelines when the current stage is finished' do + next_stage_pipeline_tracker = create( + :bulk_import_tracker, + entity: entity, + pipeline_name: 'Stage1::Pipeline', + stage: 1 + ) + + pipeline_tracker.fail_op! + + expect_next_instance_of(Gitlab::Import::Logger) do |logger| + expect(logger) + .to receive(:info) + .with( + worker: described_class.name, + entity_id: entity.id, + current_stage: 0 + ) + end - expect(Gitlab::ErrorTracking) - .to receive(:track_exception) - .with(kind_of(StandardError), bulk_import_id: bulk_import.id, entity_id: entity.id) + expect(BulkImports::PipelineWorker) + .to receive(:perform_async) + .with( + next_stage_pipeline_tracker.id, + next_stage_pipeline_tracker.stage, + entity.id + ) - subject.perform(entity.id) + subject.perform(entity.id, 0) + end - expect(entity.reload.failed?).to eq(true) - end - end + it 'logs and tracks the raised exceptions' do + exception = StandardError.new('Error!') + + expect(BulkImports::PipelineWorker) + .to receive(:perform_async) + .and_raise(exception) + + expect_next_instance_of(Gitlab::Import::Logger) do |logger| + expect(logger) + .to receive(:info) + .with( + worker: described_class.name, + entity_id: entity.id, + current_stage: nil + ) + + expect(logger) + .to receive(:error) + .with( + worker: described_class.name, + entity_id: entity.id, + current_stage: nil, + error_message: 'Error!' + ) end - context 'when started entity does not exist' do - it 'does not execute BulkImports::Importers::GroupImporter' do - entity = create(:bulk_import_entity, bulk_import: bulk_import) + expect(Gitlab::ErrorTracking) + .to receive(:track_exception) + .with(exception, entity_id: entity.id) - expect(BulkImports::Importers::GroupImporter).not_to receive(:new) - - subject.perform(entity.id) - end - end + subject.perform(entity.id) end end |