summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/import/import_failure_service_spec.rb
blob: 50b32d634ad78e1c9bbff034daef0d3712015308 (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 Gitlab::Import::ImportFailureService do
  let_it_be(:import_type) { 'import_type' }

  let_it_be(:project) do
    create(
      :project,
      :import_started,
      import_type: import_type
    )
  end

  let(:import_state) { project.import_state }
  let(:exception) { StandardError.new('some error') }

  shared_examples 'logs the exception and fails the import' do
    it 'when the failure does not abort the import' do
      expect(Gitlab::ErrorTracking)
        .to receive(:track_exception)
        .with(
          exception,
          project_id: project.id,
          import_type: import_type,
          source: 'SomeImporter'
        )

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

      described_class.track(**arguments)

      expect(project.import_state.reload.status).to eq('failed')

      expect(project.import_failures).not_to be_empty
      expect(project.import_failures.last.exception_class).to eq('StandardError')
      expect(project.import_failures.last.exception_message).to eq('some error')
    end
  end

  shared_examples 'logs the exception and does not fail the import' do
    it 'when the failure does not abort the import' do
      expect(Gitlab::ErrorTracking)
        .to receive(:track_exception)
        .with(
          exception,
          project_id: project.id,
          import_type: import_type,
          source: 'SomeImporter'
        )

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

      described_class.track(**arguments)

      expect(project.import_state.reload.status).to eq('started')

      expect(project.import_failures).not_to be_empty
      expect(project.import_failures.last.exception_class).to eq('StandardError')
      expect(project.import_failures.last.exception_message).to eq('some error')
    end
  end

  context 'when using the project as reference' do
    context 'when it fails the import' do
      let(:arguments) do
        {
          project_id: project.id,
          exception: exception,
          error_source: 'SomeImporter',
          fail_import: true
        }
      end

      it_behaves_like 'logs the exception and fails the import'
    end

    context 'when it does not fail the import' do
      let(:arguments) do
        {
          project_id: project.id,
          exception: exception,
          error_source: 'SomeImporter',
          fail_import: false
        }
      end

      it_behaves_like 'logs the exception and does not fail the import'
    end
  end

  context 'when using the import_state as reference' do
    context 'when it fails the import' do
      let(:arguments) do
        {
          import_state: import_state,
          exception: exception,
          error_source: 'SomeImporter',
          fail_import: true
        }
      end

      it_behaves_like 'logs the exception and fails the import'
    end

    context 'when it does not fail the import' do
      let(:arguments) do
        {
          import_state: import_state,
          exception: exception,
          error_source: 'SomeImporter',
          fail_import: false
        }
      end

      it_behaves_like 'logs the exception and does not fail the import'
    end
  end
end