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

entity_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: 8238721df01cdf3dfeb8223f0cbf2bec471ff841 (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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe BulkImports::EntityWorker, feature_category: :importers do
  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

  let(:job_args) { entity.id }

  it 'updates pipeline trackers to enqueued state when selected' do
    worker = described_class.new

    next_tracker = worker.send(:next_pipeline_trackers_for, entity.id).first

    next_tracker.reload

    expect(next_tracker.enqueued?).to be_truthy

    expect(worker.send(:next_pipeline_trackers_for, entity.id))
      .not_to include(next_tracker)
  end

  include_examples 'an idempotent worker' do
    it 'enqueues the first stage pipelines work' do
      expect_next_instance_of(Gitlab::Import::Logger) do |logger|
        # the worker runs twice but only executes once
        expect(logger)
          .to receive(:info).twice
          .with(
            hash_including(
              'bulk_import_entity_id' => entity.id,
              'bulk_import_id' => entity.bulk_import_id,
              'bulk_import_entity_type' => entity.source_type,
              'source_full_path' => entity.source_full_path,
              'current_stage' => nil,
              'message' => 'Stage starting',
              'source_version' => entity.bulk_import.source_version_info.to_s,
              'importer' => 'gitlab_migration'
            )
          )
      end

      expect(BulkImports::PipelineWorker)
        .to receive(:perform_async)
        .with(
          pipeline_tracker.id,
          pipeline_tracker.stage,
          entity.id
        )

      subject
    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).twice
          .with(
            hash_including(
              'bulk_import_entity_id' => entity.id,
              'bulk_import_id' => entity.bulk_import_id,
              'bulk_import_entity_type' => entity.source_type,
              'source_full_path' => entity.source_full_path,
              'current_stage' => nil,
              'source_version' => entity.bulk_import.source_version_info.to_s,
              'importer' => 'gitlab_migration'
            )
          )

        expect(logger)
          .to receive(:error)
          .with(
            hash_including(
              'bulk_import_entity_id' => entity.id,
              'bulk_import_id' => entity.bulk_import_id,
              'bulk_import_entity_type' => entity.source_type,
              'source_full_path' => entity.source_full_path,
              'current_stage' => nil,
              'message' => 'Entity failed',
              'exception.backtrace' => anything,
              'exception.class' => 'StandardError',
              'exception.message' => 'Error!',
              'importer' => 'gitlab_migration',
              'source_version' => entity.bulk_import.source_version_info.to_s
            )
          )
      end

      expect(Gitlab::ErrorTracking)
        .to receive(:track_exception)
              .with(
                exception,
                bulk_import_entity_id: entity.id,
                bulk_import_id: entity.bulk_import_id,
                bulk_import_entity_type: entity.source_type,
                source_full_path: entity.source_full_path,
                source_version: entity.bulk_import.source_version_info.to_s,
                importer: 'gitlab_migration'
              )

      subject

      expect(entity.reload.failed?).to eq(true)
    end

    context 'in first stage' do
      let(:job_args) { [entity.id, 0] }

      it 'do not enqueue a new pipeline job if the current stage still running' do
        expect_next_instance_of(Gitlab::Import::Logger) do |logger|
          expect(logger)
            .to receive(:info).twice
            .with(
              hash_including(
                'bulk_import_entity_id' => entity.id,
                'bulk_import_id' => entity.bulk_import_id,
                'bulk_import_entity_type' => entity.source_type,
                'source_full_path' => entity.source_full_path,
                'current_stage' => 0,
                'message' => 'Stage running',
                'source_version' => entity.bulk_import.source_version_info.to_s,
                'importer' => 'gitlab_migration'
              )
            )
        end

        expect(BulkImports::PipelineWorker)
          .not_to receive(:perform_async)

        subject
      end

      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).twice
            .with(
              hash_including(
                'bulk_import_entity_id' => entity.id,
                'bulk_import_id' => entity.bulk_import_id,
                'bulk_import_entity_type' => entity.source_type,
                'source_full_path' => entity.source_full_path,
                'current_stage' => 0,
                'source_version' => entity.bulk_import.source_version_info.to_s,
                'importer' => 'gitlab_migration'
              )
            )
        end

        expect(BulkImports::PipelineWorker)
          .to receive(:perform_async)
            .with(
              next_stage_pipeline_tracker.id,
              next_stage_pipeline_tracker.stage,
              entity.id
            )

        subject
      end
    end
  end
end