summaryrefslogtreecommitdiff
path: root/spec/workers/container_registry/migration/enqueuer_worker_spec.rb
blob: ab3bd8f75d4a59d50e72c5df67e7494ceea2550c (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
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe ContainerRegistry::Migration::EnqueuerWorker, :aggregate_failures, :clean_gitlab_redis_shared_state do
  using RSpec::Parameterized::TableSyntax
  include ExclusiveLeaseHelpers

  let_it_be_with_reload(:container_repository) { create(:container_repository, created_at: 2.days.ago) }
  let_it_be(:importing_repository) { create(:container_repository, :importing) }
  let_it_be(:pre_importing_repository) { create(:container_repository, :pre_importing) }

  let(:worker) { described_class.new }

  before do
    stub_container_registry_config(enabled: true)
    stub_application_setting(container_registry_import_created_before: 1.day.ago)
    stub_container_registry_tags(repository: container_repository.path, tags: %w(tag1 tag2 tag3), with_manifest: true)
  end

  describe '#perform' do
    subject { worker.perform }

    shared_examples 'no action' do
      it 'does not queue or change any repositories' do
        expect(worker).not_to receive(:handle_next_migration)
        expect(worker).not_to receive(:handle_aborted_migration)

        subject

        expect(container_repository.reload).to be_default
      end
    end

    context 'migrations are disabled' do
      before do
        allow(ContainerRegistry::Migration).to receive(:enabled?).and_return(false)
      end

      it_behaves_like 'no action' do
        before do
          expect_log_extra_metadata(migration_enabled: false)
        end
      end
    end

    context 'with no repository qualifies' do
      include_examples 'an idempotent worker' do
        before do
          allow(ContainerRepository).to receive(:ready_for_import).and_return(ContainerRepository.none)
        end

        it_behaves_like 'no action'
      end
    end

    context 'when multiple aborted imports are available' do
      let_it_be(:aborted_repository1) { create(:container_repository, :import_aborted) }
      let_it_be(:aborted_repository2) { create(:container_repository, :import_aborted) }

      before do
        container_repository.update!(created_at: 30.seconds.ago)
      end

      context 'with successful registry requests' do
        before do
          allow_worker(on: :next_aborted_repository) do |repository|
            allow(repository).to receive(:migration_import).and_return(:ok)
            allow(repository.gitlab_api_client).to receive(:import_status).and_return('import_failed')
          end
        end

        it 'retries the import for the aborted repository' do
          expect_log_info(
            [
              {
                import_type: 'retry',
                container_repository_id: aborted_repository1.id,
                container_repository_path: aborted_repository1.path,
                container_repository_migration_state: 'importing'
              },
              {
                import_type: 'retry',
                container_repository_id: aborted_repository2.id,
                container_repository_path: aborted_repository2.path,
                container_repository_migration_state: 'importing'
              }
            ]
          )

          expect(worker).to receive(:handle_next_migration).and_call_original

          subject

          expect(aborted_repository1.reload).to be_importing
          expect(aborted_repository2.reload).to be_importing
        end
      end

      context 'when an error occurs' do
        it 'does abort that migration' do
          allow_worker(on: :next_aborted_repository) do |repository|
            allow(repository).to receive(:retry_aborted_migration).and_raise(StandardError)
          end

          expect_log_info(
            [
              {
                import_type: 'retry',
                container_repository_id: aborted_repository1.id,
                container_repository_path: aborted_repository1.path,
                container_repository_migration_state: 'import_aborted'
              }
            ]
          )

          subject

          expect(aborted_repository1.reload).to be_import_aborted
          expect(aborted_repository2.reload).to be_import_aborted
        end
      end
    end

    context 'when multiple qualified repositories are available' do
      let_it_be(:container_repository2) { create(:container_repository, created_at: 2.days.ago) }

      before do
        allow_worker(on: :next_repository) do |repository|
          allow(repository).to receive(:migration_pre_import).and_return(:ok)
        end

        stub_container_registry_tags(
          repository: container_repository2.path,
          tags: %w(tag4 tag5 tag6),
          with_manifest: true
        )
      end

      shared_examples 'starting all the next imports' do
        it 'starts the pre-import for the next qualified repositories' do
          expect_log_info(
            [
              {
                import_type: 'next',
                container_repository_id: container_repository.id,
                container_repository_path: container_repository.path,
                container_repository_migration_state: 'pre_importing'
              },
              {
                import_type: 'next',
                container_repository_id: container_repository2.id,
                container_repository_path: container_repository2.path,
                container_repository_migration_state: 'pre_importing'
              }
            ]
          )

          expect(worker).to receive(:handle_next_migration).exactly(3).times.and_call_original

          expect { subject }.to make_queries_matching(/LIMIT 2/)

          expect(container_repository.reload).to be_pre_importing
          expect(container_repository2.reload).to be_pre_importing
        end
      end

      it_behaves_like 'starting all the next imports'

      context 'when the new pre-import maxes out the capacity' do
        before do
          # set capacity to 10
          stub_feature_flags(
            container_registry_migration_phase2_capacity_25: false,
            container_registry_migration_phase2_capacity_40: false
          )

          # Plus 2 created above gives 9 importing repositories
          create_list(:container_repository, 7, :importing)
        end

        it 'starts the pre-import only for one qualified repository' do
          expect_log_info(
            [
              {
                import_type: 'next',
                container_repository_id: container_repository.id,
                container_repository_path: container_repository.path,
                container_repository_migration_state: 'pre_importing'
              }
            ]
          )

          subject

          expect(container_repository.reload).to be_pre_importing
          expect(container_repository2.reload).to be_default
        end
      end

      context 'max tag count is 0' do
        before do
          stub_application_setting(container_registry_import_max_tags_count: 0)
          # Add 8 tags to the next repository
          stub_container_registry_tags(
            repository: container_repository.path, tags: %w(a b c d e f g h), with_manifest: true
          )
        end

        it_behaves_like 'starting all the next imports'
      end

      context 'when the deadline is hit' do
        it 'does not handle the second qualified repository' do
          expect(worker).to receive(:loop_deadline).and_return(5.seconds.from_now, 2.seconds.ago)
          expect(worker).to receive(:handle_next_migration).once.and_call_original

          subject

          expect(container_repository.reload).to be_pre_importing
          expect(container_repository2.reload).to be_default
        end
      end
    end

    context 'when a mix of aborted imports and qualified repositories are available' do
      let_it_be(:aborted_repository) { create(:container_repository, :import_aborted) }

      before do
        allow_worker(on: :next_aborted_repository) do |repository|
          allow(repository).to receive(:migration_import).and_return(:ok)
          allow(repository.gitlab_api_client).to receive(:import_status).and_return('import_failed')
        end

        allow_worker(on: :next_repository) do |repository|
          allow(repository).to receive(:migration_pre_import).and_return(:ok)
        end
      end

      it 'retries the aborted repository and start the migration on the qualified repository' do
        expect_log_info(
          [
            {
              import_type: 'retry',
              container_repository_id: aborted_repository.id,
              container_repository_path: aborted_repository.path,
              container_repository_migration_state: 'importing'
            },
            {
              import_type: 'next',
              container_repository_id: container_repository.id,
              container_repository_path: container_repository.path,
              container_repository_migration_state: 'pre_importing'
            }
          ]
        )

        subject

        expect(aborted_repository.reload).to be_importing
        expect(container_repository.reload).to be_pre_importing
      end
    end

    context 'above capacity' do
      before do
        create(:container_repository, :importing)
        create(:container_repository, :importing)
        allow(ContainerRegistry::Migration).to receive(:capacity).and_return(1)
      end

      it_behaves_like 'no action' do
        before do
          expect_log_extra_metadata(below_capacity: false, max_capacity_setting: 1)
        end
      end
    end

    context 'too soon before previous completed import step' do
      where(:state, :timestamp) do
        :import_done     | :migration_import_done_at
        :pre_import_done | :migration_pre_import_done_at
        :import_aborted  | :migration_aborted_at
        :import_skipped  | :migration_skipped_at
      end

      with_them do
        before do
          allow(ContainerRegistry::Migration).to receive(:enqueue_waiting_time).and_return(45.minutes)
          create(:container_repository, state, timestamp => 1.minute.ago)
        end

        it_behaves_like 'no action' do
          before do
            expect_log_extra_metadata(waiting_time_passed: false, current_waiting_time_setting: 45.minutes)
          end
        end
      end

      context 'when last completed repository has nil timestamps' do
        before do
          allow(ContainerRegistry::Migration).to receive(:enqueue_waiting_time).and_return(45.minutes)
          create(:container_repository, migration_state: 'import_done')
        end

        it 'continues to try the next import' do
          expect { subject }.to change { container_repository.reload.migration_state }
        end
      end
    end

    context 'over max tag count' do
      before do
        stub_application_setting(container_registry_import_max_tags_count: 2)
      end

      it 'skips the repository' do
        expect_log_info(
          [
            {
              import_type: 'next',
              container_repository_id: container_repository.id,
              container_repository_path: container_repository.path,
              container_repository_migration_state: 'import_skipped',
              container_repository_migration_skipped_reason: 'too_many_tags'
            }
          ]
        )

        expect(worker).to receive(:handle_next_migration).twice.and_call_original
        # skipping the migration will re_enqueue the job
        expect(described_class).to receive(:enqueue_a_job)

        subject

        expect(container_repository.reload).to be_import_skipped
        expect(container_repository.migration_skipped_reason).to eq('too_many_tags')
        expect(container_repository.migration_skipped_at).not_to be_nil
      end
    end

    context 'when an error occurs' do
      before do
        allow(ContainerRegistry::Migration).to receive(:max_tags_count).and_raise(StandardError)
      end

      it 'aborts the import' do
        expect_log_info(
          [
            {
              import_type: 'next',
              container_repository_id: container_repository.id,
              container_repository_path: container_repository.path,
              container_repository_migration_state: 'import_aborted'
            }
          ]
        )

        expect(Gitlab::ErrorTracking).to receive(:log_exception).with(
          instance_of(StandardError),
          next_repository_id: container_repository.id
        )

        # aborting the migration will re_enqueue the job
        expect(described_class).to receive(:enqueue_a_job)

        subject

        expect(container_repository.reload).to be_import_aborted
      end
    end

    context 'with the exclusive lease taken' do
      let(:lease_key) { worker.send(:lease_key) }

      before do
        stub_exclusive_lease_taken(lease_key, timeout: 30.minutes)
      end

      it 'does not perform' do
        expect(worker).not_to receive(:handle_aborted_migration)
        expect(worker).not_to receive(:handle_next_migration)

        subject
      end
    end

    def expect_log_extra_metadata(metadata)
      metadata.each do |key, value|
        expect(worker).to receive(:log_extra_metadata_on_done).with(key, value)
      end
    end

    def expect_log_info(expected_multiple_arguments)
      expected_multiple_arguments.each do |extras|
        expect(worker.logger).to receive(:info).with(worker.structured_payload(extras))
      end
    end

    def allow_worker(on:)
      method_repository = worker.method(on)
      allow(worker).to receive(on) do
        repository = method_repository.call

        yield repository if repository

        repository
      end
    end
  end

  describe 'worker attributes' do
    it 'has deduplication set' do
      expect(described_class.get_deduplicate_strategy).to eq(:until_executing)
      expect(described_class.get_deduplication_options).to include(ttl: 30.minutes)
    end
  end
end