File: bulk_import_spec.rb

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (253 lines) | stat: -rw-r--r-- 8,227 bytes parent folder | download
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe BulkImport, type: :model, feature_category: :importers do
  let_it_be(:created_bulk_import) { create(:bulk_import, :created, updated_at: 2.hours.ago) }
  let_it_be(:started_bulk_import) { create(:bulk_import, :started, updated_at: 3.hours.ago) }
  let_it_be(:finished_bulk_import) { create(:bulk_import, :finished, updated_at: 1.hour.ago) }
  let_it_be(:failed_bulk_import) { create(:bulk_import, :failed) }
  let_it_be(:stale_created_bulk_import) { create(:bulk_import, :created, updated_at: 3.days.ago) }
  let_it_be(:stale_started_bulk_import) { create(:bulk_import, :started, updated_at: 2.days.ago) }

  describe 'associations' do
    it { is_expected.to belong_to(:user).required }
    it { is_expected.to have_one(:configuration) }
    it { is_expected.to have_many(:entities) }
  end

  describe 'validations' do
    it { is_expected.to validate_presence_of(:source_type) }
    it { is_expected.to validate_presence_of(:status) }

    it { is_expected.to define_enum_for(:source_type).with_values(%i[gitlab]) }
  end

  describe 'scopes' do
    describe '.stale' do
      subject { described_class.stale }

      it { is_expected.to contain_exactly(stale_created_bulk_import, stale_started_bulk_import) }
    end

    describe '.order_by_updated_at_and_id' do
      subject { described_class.order_by_updated_at_and_id(:desc) }

      it 'sorts by given direction' do
        is_expected.to eq([
          failed_bulk_import,
          finished_bulk_import,
          created_bulk_import,
          started_bulk_import,
          stale_started_bulk_import,
          stale_created_bulk_import
        ])
      end
    end

    describe '.with_configuration' do
      it 'includes configuration association' do
        imports = described_class.with_configuration

        expect(imports.first.association_cached?(:configuration)).to be(true)
      end
    end
  end

  describe '.all_human_statuses' do
    it 'returns all human readable entity statuses' do
      expect(described_class.all_human_statuses)
        .to contain_exactly('created', 'started', 'finished', 'failed', 'timeout', 'canceled')
    end
  end

  describe '.min_gl_version_for_project' do
    it { expect(described_class.min_gl_version_for_project_migration).to be_a(Gitlab::VersionInfo) }
    it { expect(described_class.min_gl_version_for_project_migration.to_s).to eq('14.4.0') }
  end

  describe '#completed?' do
    it { expect(described_class.new(status: -2)).to be_completed }
    it { expect(described_class.new(status: -1)).to be_completed }
    it { expect(described_class.new(status: 0)).not_to be_completed }
    it { expect(described_class.new(status: 1)).not_to be_completed }
    it { expect(described_class.new(status: 2)).to be_completed }
    it { expect(described_class.new(status: 3)).to be_completed }
  end

  describe '#source_version_info' do
    it 'returns source_version as Gitlab::VersionInfo' do
      bulk_import = build(:bulk_import, source_version: '9.13.2')

      expect(bulk_import.source_version_info).to be_a(Gitlab::VersionInfo)
      expect(bulk_import.source_version_info.to_s).to eq(bulk_import.source_version)
    end
  end

  describe '#update_has_failures' do
    let(:import) { create(:bulk_import, :started) }
    let(:entity) { create(:bulk_import_entity, bulk_import: import) }

    context 'when entity has failures' do
      it 'sets has_failures flag to true' do
        expect(import.has_failures).to eq(false)

        entity.update!(has_failures: true)
        import.fail_op!

        expect(import.has_failures).to eq(true)
      end
    end

    context 'when entity does not have failures' do
      it 'sets has_failures flag to false' do
        expect(import.has_failures).to eq(false)

        entity.update!(has_failures: false)
        import.fail_op!

        expect(import.has_failures).to eq(false)
      end
    end
  end

  describe '#supports_batched_export?' do
    context 'when source version is greater than min supported version for batched migrations' do
      it 'returns true' do
        bulk_import = build(:bulk_import, source_version: '16.2.0')

        expect(bulk_import.supports_batched_export?).to eq(true)
      end
    end

    context 'when source version is less than min supported version for batched migrations' do
      it 'returns false' do
        bulk_import = build(:bulk_import, source_version: '15.5.0')

        expect(bulk_import.supports_batched_export?).to eq(false)
      end
    end
  end

  describe 'import canceling' do
    let(:import) { create(:bulk_import, :started) }

    it 'marks import as canceled' do
      expect(import.canceled?).to eq(false)

      import.cancel!

      expect(import.canceled?).to eq(true)
    end

    context 'when import has entities' do
      it 'marks entities as canceled' do
        entity = create(:bulk_import_entity, bulk_import: import)

        expect(entity.canceled?).to eq(false)

        import.cancel!

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

  describe 'completion notification trigger' do
    RSpec::Matchers.define :send_completion_notification do
      def supports_block_expectations?
        true
      end

      match(notify_expectation_failures: true) do |proc|
        expect(Notify).to receive(:bulk_import_complete).with(import.user.id, import.id).and_call_original

        proc.call
        true
      end

      match_when_negated(notify_expectation_failures: true) do |proc|
        expect(Notify).not_to receive(:bulk_import_complete)

        proc.call
        true
      end
    end

    subject(:import) { create(:bulk_import, :started) }

    let(:non_triggering_events) do
      import.status_paths.events - %i[finish cleanup_stale fail_op]
    end

    it { expect { import.finish! }.to send_completion_notification }
    it { expect { import.fail_op! }.to send_completion_notification }
    it { expect { import.cleanup_stale! }.to send_completion_notification }

    it "does not email after non-completing events" do
      non_triggering_events.each do |event|
        expect { import.send(:"#{event}!") }.not_to send_completion_notification
      end
    end
  end

  describe '#destination_group_roots' do
    subject(:import) do
      create(:bulk_import, :started, entities: [
        root_project_entity,
        root_group_entity,
        create(:bulk_import_entity, parent: root_group_entity)
      ])
    end

    let_it_be(:project_namespace) { create(:group) }
    let_it_be(:project) { create(:project, namespace: project_namespace) }
    let_it_be(:root_project_entity) { create(:bulk_import_entity, :project_entity, project: project) }

    let_it_be(:top_level_group) { create(:group) }
    let_it_be(:root_group_entity) { create(:bulk_import_entity, :group_entity, group: top_level_group) }

    it 'returns the topmost group nodes of the import entity tree' do
      expect(import.destination_group_roots).to match_array([project_namespace, top_level_group])
    end
  end

  describe '#source_url' do
    it 'returns migration source url via configuration' do
      import = create(:bulk_import, :with_configuration)

      expect(import.source_url).to eq(import.configuration.url)
    end

    context 'when configuration is missing' do
      it 'returns nil' do
        import = create(:bulk_import)

        expect(import.source_url).to be_nil
      end
    end
  end

  describe '#namespaces_with_unassigned_placeholders' do
    let_it_be(:group) { create(:group) }
    let_it_be(:entity) do
      create(:bulk_import_entity, :group_entity, bulk_import: finished_bulk_import, group: group)
    end

    before do
      create_list(:import_source_user, 5, :completed, namespace: group)
    end

    context 'when all placeholders have been assigned' do
      it { expect(finished_bulk_import.namespaces_with_unassigned_placeholders).to be_empty }
    end

    context 'when some placeholders have not been assigned' do
      before do
        create(:import_source_user, :pending_reassignment, namespace: group)
      end

      it { expect(finished_bulk_import.namespaces_with_unassigned_placeholders).to include(group) }
    end
  end
end