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

pipeline_batch_worker_spec.rb « bulk_imports « workers « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c10e1b486ab859cb2f62307f8ffe0425512b4e46 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe BulkImports::PipelineBatchWorker, feature_category: :importers do
  let_it_be(:bulk_import) { create(:bulk_import) }
  let_it_be(:config) { create(:bulk_import_configuration, bulk_import: bulk_import) }
  let_it_be(:entity) { create(:bulk_import_entity, bulk_import: bulk_import) }

  let(:pipeline_class) do
    Class.new do
      def initialize(_); end

      def run; end

      def self.file_extraction_pipeline?
        false
      end
    end
  end

  let(:tracker) do
    create(
      :bulk_import_tracker,
      entity: entity,
      pipeline_name: 'FakePipeline',
      status_event: 'enqueue'
    )
  end

  let(:batch) { create(:bulk_import_batch_tracker, :created, tracker: tracker) }

  subject(:worker) { described_class.new }

  before do
    stub_const('FakePipeline', pipeline_class)

    allow(subject).to receive(:jid).and_return('jid')
    allow(entity).to receive(:pipeline_exists?).with('FakePipeline').and_return(true)
    allow_next_instance_of(BulkImports::Groups::Stage) do |instance|
      allow(instance)
        .to receive(:pipelines)
        .and_return([{ stage: 0, pipeline: pipeline_class }])
    end
  end

  describe '#perform' do
    it 'runs the given pipeline batch successfully' do
      expect(BulkImports::FinishBatchedPipelineWorker).to receive(:perform_async).with(tracker.id)

      subject.perform(batch.id)

      expect(batch.reload).to be_finished
    end

    context 'when tracker is failed' do
      let(:tracker) { create(:bulk_import_tracker, :failed) }

      it 'skips the batch' do
        subject.perform(batch.id)

        expect(batch.reload).to be_skipped
      end
    end

    context 'when tracker is finished' do
      let(:tracker) { create(:bulk_import_tracker, :finished) }

      it 'skips the batch' do
        subject.perform(batch.id)

        expect(batch.reload).to be_skipped
      end
    end

    context 'when exclusive lease cannot be obtained' do
      it 'does not run the pipeline' do
        expect(subject).to receive(:try_obtain_lease).and_return(false)
        expect(subject).not_to receive(:run)

        subject.perform(batch.id)
      end
    end

    context 'when pipeline raises an exception' do
      context 'when pipeline is retryable' do
        it 'retries the batch' do
          allow_next_instance_of(pipeline_class) do |instance|
            allow(instance)
              .to receive(:run)
              .and_raise(BulkImports::RetryPipelineError.new('Error!', 60))
          end

          expect(described_class).to receive(:perform_in).with(60, batch.id)

          subject.perform(batch.id)

          expect(batch.reload).to be_created
        end
      end

      context 'when pipeline is not retryable' do
        it 'fails the batch and creates a failure record' do
          allow_next_instance_of(pipeline_class) do |instance|
            allow(instance).to receive(:run).and_raise(StandardError, 'Something went wrong')
          end

          expect(Gitlab::ErrorTracking).to receive(:track_exception).with(
            instance_of(StandardError),
            hash_including(
              batch_id: batch.id,
              tracker_id: tracker.id,
              pipeline_class: 'FakePipeline',
              pipeline_step: 'pipeline_batch_worker_run'
            )
          )

          expect(BulkImports::Failure).to receive(:create).with(
            bulk_import_entity_id: entity.id,
            pipeline_class: 'FakePipeline',
            pipeline_step: 'pipeline_batch_worker_run',
            exception_class: 'StandardError',
            exception_message: 'Something went wrong',
            correlation_id_value: anything
          )

          expect(BulkImports::FinishBatchedPipelineWorker).to receive(:perform_async).with(tracker.id)

          subject.perform(batch.id)

          expect(batch.reload).to be_failed
        end
      end
    end
  end
end