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

import_failure_service_spec.rb « import « gitlab « lib « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 362d809bb566e4e5a403c201c1185fb8e23b840a (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
187
188
189
190
191
192
193
194
195
196
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::Import::ImportFailureService, :aggregate_failures, feature_category: :importers do
  let_it_be(:import_type) { 'import_type' }
  let_it_be(:project) { create(:project, :import_started, import_type: import_type) }

  let(:exception) { StandardError.new('some error') }
  let(:import_state) { nil }
  let(:fail_import) { false }
  let(:metrics) { false }
  let(:external_identifiers) { { foo: 'bar' } }
  let(:project_id) { project.id }

  let(:arguments) do
    {
      project_id: project_id,
      error_source: 'SomeImporter',
      exception: exception,
      fail_import: fail_import,
      metrics: metrics,
      import_state: import_state,
      external_identifiers: external_identifiers
    }
  end

  describe '.track' do
    let(:instance) { double(:failure_service) }

    context 'with all arguments provided' do
      let(:arguments) do
        {
          exception: exception,
          import_state: '_import_state_',
          project_id: '_project_id_',
          error_source: '_error_source_',
          fail_import: '_fail_import_',
          metrics: '_metrics_',
          external_identifiers: { id: 1 }
        }
      end

      it 'invokes a new instance and executes' do
        expect(described_class).to receive(:new).with(**arguments).and_return(instance)
        expect(instance).to receive(:execute)

        described_class.track(**arguments)
      end
    end

    context 'with only necessary arguments utilizing defaults' do
      it 'invokes a new instance and executes' do
        expect(described_class).to receive(:new).with(a_hash_including(exception: exception)).and_return(instance)
        expect(instance).to receive(:execute)

        described_class.track(exception: exception)
      end
    end
  end

  describe '#execute' do
    subject(:service) { described_class.new(**arguments) }

    shared_examples 'logs the exception and fails the import' do
      specify do
        expect(Gitlab::ErrorTracking)
          .to receive(:track_exception)
          .with(
            exception,
            {
              project_id: project.id,
              import_type: import_type,
              source: 'SomeImporter',
              external_identifiers: external_identifiers
            }
          )

        expect(Gitlab::Import::Logger)
          .to receive(:error)
          .with(
            {
              message: 'importer failed',
              'exception.message': 'some error',
              project_id: project.id,
              import_type: import_type,
              source: 'SomeImporter',
              external_identifiers: external_identifiers
            }
          )

        service.execute
        project.reload

        expect(project.import_state.status).to eq('failed')
        expect(project.import_failures).to contain_exactly(
          have_attributes(
            retry_count: 0,
            exception_class: 'StandardError',
            exception_message: 'some error',
            external_identifiers: external_identifiers.with_indifferent_access,
            correlation_id_value: Labkit::Correlation::CorrelationId.current_or_new_id,
            source: 'SomeImporter'
          )
        )
      end
    end

    shared_examples 'logs the exception and does not fail the import' do
      specify do
        expect(Gitlab::ErrorTracking)
          .to receive(:track_exception)
          .with(
            exception,
            {
              project_id: project.id,
              import_type: import_type,
              source: 'SomeImporter',
              external_identifiers: external_identifiers
            }
          )

        expect(Gitlab::Import::Logger)
          .to receive(:error)
          .with(
            {
              message: 'importer failed',
              'exception.message': 'some error',
              project_id: project.id,
              import_type: import_type,
              source: 'SomeImporter',
              external_identifiers: external_identifiers
            }
          )

        service.execute
        project.reload

        expect(project.import_state.reload.status).to eq('started')
        expect(project.import_failures).to contain_exactly(
          have_attributes(
            retry_count: nil,
            exception_class: 'StandardError',
            exception_message: 'some error',
            external_identifiers: external_identifiers.with_indifferent_access,
            correlation_id_value: Labkit::Correlation::CorrelationId.current_or_new_id,
            source: 'SomeImporter'
          )
        )
      end
    end

    context 'when tracking metrics' do
      let(:metrics) { true }

      it 'tracks the failed import' do
        metrics_double = double(:metrics)

        expect(Gitlab::Import::Metrics)
          .to receive(:new)
          .with("#{project.import_type}_importer", project)
          .and_return(metrics_double)
        expect(metrics_double).to receive(:track_failed_import)

        service.execute
      end
    end

    context 'when using the project as reference' do
      context 'when it fails the import' do
        let(:fail_import) { true }

        it_behaves_like 'logs the exception and fails the import'
      end

      context 'when it does not fail the import' do
        it_behaves_like 'logs the exception and does not fail the import'
      end
    end

    context 'when using the import_state as reference' do
      let(:project_id) { nil }
      let(:import_state) { project.import_state }

      context 'when it fails the import' do
        let(:fail_import) { true }

        it_behaves_like 'logs the exception and fails the import'
      end

      context 'when it does not fail the import' do
        it_behaves_like 'logs the exception and does not fail the import'
      end
    end
  end
end