summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/cleanup/remote_uploads_spec.rb
blob: 1752608f844f6ee23a33a6c757cc70a02fc2f0c0 (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
# frozen_string_literal: true
require 'spec_helper'

RSpec.describe Gitlab::Cleanup::RemoteUploads do
  context 'when object_storage is enabled' do
    let(:connection) { double }
    let(:directory) { double }
    let!(:uploads) do
      [
        create(:upload, path: 'dir/file1', store: ObjectStorage::Store::REMOTE),
        create(:upload, path: 'dir/file2', store: ObjectStorage::Store::LOCAL)
      ]
    end
    let(:remote_files) do
      [
        double(key: 'dir/file1'),
        double(key: 'dir/file2'),
        double(key: 'dir/file3'),
        double(key: 'lost_and_found/dir/file3')
      ]
    end

    before do
      stub_uploads_object_storage(FileUploader)

      expect(::Fog::Storage).to receive(:new).and_return(connection)

      expect(connection).to receive(:directories).and_return(double(new: directory))
      expect(directory).to receive(:files).and_return(remote_files)
    end

    context 'when dry_run is set to false' do
      subject { described_class.new.run!(dry_run: false) }

      it 'moves files that are not in uploads table' do
        expect(remote_files[0]).not_to receive(:copy)
        expect(remote_files[0]).not_to receive(:destroy)
        expect(remote_files[1]).to receive(:copy)
        expect(remote_files[1]).to receive(:destroy)
        expect(remote_files[2]).to receive(:copy)
        expect(remote_files[2]).to receive(:destroy)
        expect(remote_files[3]).not_to receive(:copy)
        expect(remote_files[3]).not_to receive(:destroy)

        subject
      end
    end

    context 'when dry_run is set to true' do
      subject { described_class.new.run!(dry_run: true) }

      it 'does not move filese' do
        expect(remote_files[0]).not_to receive(:copy)
        expect(remote_files[0]).not_to receive(:destroy)
        expect(remote_files[1]).not_to receive(:copy)
        expect(remote_files[1]).not_to receive(:destroy)
        expect(remote_files[2]).not_to receive(:copy)
        expect(remote_files[2]).not_to receive(:destroy)
        expect(remote_files[3]).not_to receive(:copy)
        expect(remote_files[3]).not_to receive(:destroy)

        subject
      end
    end
  end

  context 'when object_storage is not enabled' do
    it 'does not connect to any storage' do
      expect(::Fog::Storage).not_to receive(:new)

      subject
    end
  end
end