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

migrate_rake_spec.rb « uploads « gitlab « tasks « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3a368a5011b66c893477a5f4796fbde5f198909e (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
# frozen_string_literal: true

require 'rake_helper'

RSpec.describe 'gitlab:uploads:migrate and migrate_to_local rake tasks', :sidekiq_inline, :silence_stdout do
  before do
    stub_env('MIGRATION_BATCH_SIZE', 3.to_s)
    stub_uploads_object_storage(AvatarUploader)
    stub_uploads_object_storage(FileUploader)
    Rake.application.rake_require 'tasks/gitlab/uploads/migrate'

    create_list(:project, 2, :with_avatar)
    create_list(:group, 2, :with_avatar)
    create_list(:project, 2) do |model|
      FileUploader.new(model).store!(fixture_file_upload('spec/fixtures/doc_sample.txt'))
    end
  end

  let(:total_uploads_count) { 6 }

  it 'migrates all uploads to object storage in batches' do
    expect(ObjectStorage::MigrateUploadsWorker)
      .to receive(:perform_async).twice.and_call_original

    run_rake_task('gitlab:uploads:migrate:all')

    expect(Upload.with_files_stored_locally.count).to eq(0)
    expect(Upload.with_files_stored_remotely.count).to eq(total_uploads_count)
  end

  it 'migrates all uploads to local storage in batches' do
    run_rake_task('gitlab:uploads:migrate')
    expect(Upload.with_files_stored_remotely.count).to eq(total_uploads_count)

    expect(ObjectStorage::MigrateUploadsWorker)
      .to receive(:perform_async).twice.and_call_original

    run_rake_task('gitlab:uploads:migrate_to_local:all')

    expect(Upload.with_files_stored_remotely.count).to eq(0)
    expect(Upload.with_files_stored_locally.count).to eq(total_uploads_count)
  end

  shared_examples 'migrate task with filters' do
    it 'migrates matching uploads to object storage' do
      run_rake_task('gitlab:uploads:migrate', task_arguments)

      migrated_count = matching_uploads.with_files_stored_remotely.count

      expect(migrated_count).to eq(matching_uploads.count)
      expect(Upload.with_files_stored_locally.count).to eq(total_uploads_count - migrated_count)
    end

    it 'migrates matching uploads to local storage' do
      run_rake_task('gitlab:uploads:migrate')
      expect(Upload.with_files_stored_remotely.count).to eq(total_uploads_count)

      run_rake_task('gitlab:uploads:migrate_to_local', task_arguments)

      migrated_count = matching_uploads.with_files_stored_locally.count

      expect(migrated_count).to eq(matching_uploads.count)
      expect(Upload.with_files_stored_remotely.count).to eq(total_uploads_count - migrated_count)
    end
  end

  context 'when uploader_class is given' do
    let(:task_arguments) { ['FileUploader'] }
    let(:matching_uploads) { Upload.where(uploader: 'FileUploader') }

    it_behaves_like 'migrate task with filters'
  end

  context 'when model_class is given' do
    let(:task_arguments) { [nil, 'Project'] }
    let(:matching_uploads) { Upload.where(model_type: 'Project') }

    it_behaves_like 'migrate task with filters'
  end

  context 'when mounted_as is given' do
    let(:task_arguments) { [nil, nil, :avatar] }
    let(:matching_uploads) { Upload.where(mount_point: :avatar) }

    it_behaves_like 'migrate task with filters'
  end

  context 'when multiple filters are given' do
    let(:task_arguments) { %w[AvatarUploader Project] }
    let(:matching_uploads) { Upload.where(uploader: 'AvatarUploader', model_type: 'Project') }

    it_behaves_like 'migrate task with filters'
  end
end