summaryrefslogtreecommitdiff
path: root/spec/uploaders/workers/object_storage/background_move_worker_spec.rb
blob: cc8970d2ba059edfa78ae3c902197ac19d46ad89 (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
require 'spec_helper'

describe ObjectStorage::BackgroundMoveWorker do
  let(:local) { ObjectStorage::Store::LOCAL }
  let(:remote) { ObjectStorage::Store::REMOTE }

  def perform
    described_class.perform_async(uploader_class.name, subject_class, file_field, subject_id)
  end

  context 'for LFS' do
    let!(:lfs_object) { create(:lfs_object, :with_file, file_store: local) }
    let(:uploader_class) { LfsObjectUploader }
    let(:subject_class) { LfsObject }
    let(:file_field) { :file }
    let(:subject_id) { lfs_object.id }

    context 'when object storage is enabled' do
      before do
        stub_lfs_object_storage(background_upload: true)
      end

      it 'uploads object to storage' do
        expect { perform }.to change { lfs_object.reload.file_store }.from(local).to(remote)
      end

      context 'when background upload is disabled' do
        before do
          allow(Gitlab.config.lfs.object_store).to receive(:background_upload) { false }
        end

        it 'is skipped' do
          expect { perform }.not_to change { lfs_object.reload.file_store }
        end
      end
    end

    context 'when object storage is disabled' do
      before do
        stub_lfs_object_storage(enabled: false)
      end

      it "doesn't migrate files" do
        perform

        expect(lfs_object.reload.file_store).to eq(local)
      end
    end
  end

  context 'for job artifacts' do
    let(:artifact) { create(:ci_job_artifact, :archive) }
    let(:uploader_class) { JobArtifactUploader }
    let(:subject_class) { Ci::JobArtifact }
    let(:file_field) { :file }
    let(:subject_id) { artifact.id }

    context 'when local storage is used' do
      let(:store) { local }

      context 'and remote storage is defined' do
        before do
          stub_artifacts_object_storage(background_upload: true)
        end

        it "migrates file to remote storage" do
          perform

          expect(artifact.reload.file_store).to eq(remote)
        end
      end
    end
  end

  context 'for uploads' do
    let!(:project) { create(:project, :with_avatar) }
    let(:uploader_class) { AvatarUploader }
    let(:file_field) { :avatar }

    context 'when local storage is used' do
      let(:store) { local }

      context 'and remote storage is defined' do
        before do
          stub_uploads_object_storage(uploader_class, background_upload: true)
        end

        describe 'supports using the model' do
          let(:subject_class) { project.class }
          let(:subject_id) { project.id }

          it "migrates file to remote storage" do
            perform
            project.reload
            BatchLoader::Executor.clear_current

            expect(project.avatar).not_to be_file_storage
          end
        end

        describe 'supports using the Upload' do
          let(:subject_class) { Upload }
          let(:subject_id) { project.avatar.upload.id }

          it "migrates file to remote storage" do
            perform

            expect(project.reload.avatar).not_to be_file_storage
          end
        end
      end
    end
  end
end