summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/cleanup/project_uploads_spec.rb
blob: cb7d6cbb27d0ecedacdf14328ea373d9be51dd72 (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
# frozen_string_literal: true

require 'spec_helper'

describe Gitlab::Cleanup::ProjectUploads do
  subject { described_class.new(logger: logger) }
  let(:logger) { double(:logger) }

  before do
    allow(logger).to receive(:info).at_least(1).times
    allow(logger).to receive(:debug).at_least(1).times
  end

  describe '#run!' do
    shared_examples_for 'moves the file' do
      shared_examples_for 'a real run' do
        let(:args) { [dry_run: false] }

        it 'moves the file to its proper location' do
          subject.run!(*args)

          expect(File.exist?(path)).to be_falsey
          expect(File.exist?(new_path)).to be_truthy
        end

        it 'logs action as done' do
          expect(logger).to receive(:info).with(_("Looking for orphaned project uploads to clean up..."))
          expect(logger).to receive(:info).with("Did #{action}")

          subject.run!(*args)
        end
      end

      shared_examples_for 'a dry run' do
        it 'does not move the file' do
          subject.run!(*args)

          expect(File.exist?(path)).to be_truthy
          expect(File.exist?(new_path)).to be_falsey
        end

        it 'logs action as able to be done' do
          expect(logger).to receive(:info).with(_("Looking for orphaned project uploads to clean up. Dry run..."))
          expect(logger).to receive(:info).with("Can #{action}")

          subject.run!(*args)
        end
      end

      context 'when dry_run is false' do
        let(:args) { [dry_run: false] }

        it_behaves_like 'a real run'
      end

      context 'when dry_run is nil' do
        let(:args) { [dry_run: nil] }

        it_behaves_like 'a real run'
      end

      context 'when dry_run is true' do
        let(:args) { [dry_run: true] }

        it_behaves_like 'a dry run'
      end

      context 'with dry_run not specified' do
        let(:args) { [] }

        it_behaves_like 'a dry run'
      end
    end

    shared_examples_for 'moves the file to lost and found' do
      let(:action) { "move to lost and found #{path} -> #{new_path}" }

      it_behaves_like 'moves the file'
    end

    shared_examples_for 'fixes the file' do
      let(:action) { "fix #{path} -> #{new_path}" }

      it_behaves_like 'moves the file'
    end

    context 'orphaned project upload file' do
      context 'when an upload record matching the secret and filename is found' do
        context 'when the project is still in legacy storage' do
          let(:orphaned) { create(:upload, :issuable_upload, :with_file, model: create(:project, :legacy_storage)) }
          let(:new_path) { orphaned.absolute_path }
          let(:path) { File.join(FileUploader.root, 'some', 'wrong', 'location', orphaned.path) }

          before do
            FileUtils.mkdir_p(File.dirname(path))
            FileUtils.mv(new_path, path)
          end

          it_behaves_like 'fixes the file'
        end

        context 'when the project was moved to hashed storage' do
          let(:orphaned) { create(:upload, :issuable_upload, :with_file) }
          let(:new_path) { orphaned.absolute_path }
          let(:path) { File.join(FileUploader.root, 'some', 'wrong', 'location', orphaned.path) }

          before do
            FileUtils.mkdir_p(File.dirname(path))
            FileUtils.mv(new_path, path)
          end

          it_behaves_like 'fixes the file'
        end

        context 'when the project is missing (the upload *record* is an orphan)' do
          let(:orphaned) { create(:upload, :issuable_upload, :with_file, model: build(:project, :legacy_storage)) }
          let!(:path) { orphaned.absolute_path }
          let!(:new_path) { File.join(FileUploader.root, '-', 'project-lost-found', orphaned.model.full_path, orphaned.path) }

          before do
            orphaned.model.delete
          end

          it_behaves_like 'moves the file to lost and found'
        end

        # We will probably want to add logic (Reschedule background upload) to
        # cover Case 2 in https://gitlab.com/gitlab-org/gitlab-ce/issues/46535#note_75355104
        context 'when the file should be in object storage' do
          context 'when the file otherwise has the correct local path' do
            let!(:orphaned) { create(:upload, :issuable_upload, :object_storage, model: build(:project, :legacy_storage)) }
            let!(:path) { File.join(FileUploader.root, orphaned.model.full_path, orphaned.path) }

            before do
              stub_uploads_object_storage(FileUploader)

              FileUtils.mkdir_p(File.dirname(path))
              FileUtils.touch(path)
            end

            it 'does not move the file' do
              expect(File.exist?(path)).to be_truthy

              subject.run!(dry_run: false)

              expect(File.exist?(path)).to be_truthy
            end
          end

          # E.g. the upload file was orphaned, and then uploads were migrated to
          # object storage
          context 'when the file has the wrong local path' do
            let!(:orphaned) { create(:upload, :issuable_upload, :object_storage, model: build(:project, :legacy_storage)) }
            let!(:path) { File.join(FileUploader.root, 'wrong', orphaned.path) }
            let!(:new_path) { File.join(FileUploader.root, '-', 'project-lost-found', 'wrong', orphaned.path) }

            before do
              stub_uploads_object_storage(FileUploader)

              FileUtils.mkdir_p(File.dirname(path))
              FileUtils.touch(path)
            end

            it_behaves_like 'moves the file to lost and found'
          end
        end
      end

      context 'when a matching upload record can not be found' do
        context 'when the file path fits the known pattern' do
          let!(:orphaned) { create(:upload, :issuable_upload, :with_file, model: build(:project, :legacy_storage)) }
          let!(:path) { orphaned.absolute_path }
          let!(:new_path) { File.join(FileUploader.root, '-', 'project-lost-found', orphaned.model.full_path, orphaned.path) }

          before do
            orphaned.delete
          end

          it_behaves_like 'moves the file to lost and found'
        end

        context 'when the file path does not fit the known pattern' do
          let!(:invalid_path) { File.join('group', 'file.jpg') }
          let!(:path) { File.join(FileUploader.root, invalid_path) }
          let!(:new_path) { File.join(FileUploader.root, '-', 'project-lost-found', invalid_path) }

          before do
            FileUtils.mkdir_p(File.dirname(path))
            FileUtils.touch(path)
          end

          after do
            File.delete(path) if File.exist?(path)
          end

          it_behaves_like 'moves the file to lost and found'
        end
      end
    end

    context 'non-orphaned project upload file' do
      it 'does not move the file' do
        tracked = create(:upload, :issuable_upload, :with_file, model: build(:project, :legacy_storage))
        tracked_path = tracked.absolute_path

        expect(logger).not_to receive(:info).with(/move|fix/i)
        expect(File.exist?(tracked_path)).to be_truthy

        subject.run!(dry_run: false)

        expect(File.exist?(tracked_path)).to be_truthy
      end
    end

    context 'ignorable cases' do
      # Because we aren't concerned about these, and can save a lot of
      # processing time by ignoring them. If we wish to cleanup hashed storage
      # directories, it should simply require removing this test and modifying
      # the find command.
      context 'when the file is already in hashed storage' do
        let(:project) { create(:project) }

        before do
          expect(logger).not_to receive(:info).with(/move|fix/i)
        end

        it 'does not move even an orphan file' do
          orphaned = create(:upload, :issuable_upload, :with_file, model: project)
          path = orphaned.absolute_path
          orphaned.delete

          expect(File.exist?(path)).to be_truthy

          subject.run!(dry_run: false)

          expect(File.exist?(path)).to be_truthy
        end
      end

      it 'does not move any non-project (FileUploader) uploads' do
        paths = []
        orphaned1 = create(:upload, :personal_snippet_upload, :with_file)
        orphaned2 = create(:upload, :namespace_upload, :with_file)
        orphaned3 = create(:upload, :attachment_upload, :with_file)
        orphaned4 = create(:upload, :favicon_upload, :with_file)
        paths << orphaned1.absolute_path
        paths << orphaned2.absolute_path
        paths << orphaned3.absolute_path
        paths << orphaned4.absolute_path
        Upload.delete_all

        expect(logger).not_to receive(:info).with(/move|fix/i)
        paths.each do |path|
          expect(File.exist?(path)).to be_truthy
        end

        subject.run!(dry_run: false)

        paths.each do |path|
          expect(File.exist?(path)).to be_truthy
        end
      end

      it 'does not move any uploads in tmp (which would interfere with ongoing upload activity)' do
        path = File.join(FileUploader.root, 'tmp', 'foo.jpg')
        FileUtils.mkdir_p(File.dirname(path))
        FileUtils.touch(path)

        expect(logger).not_to receive(:info).with(/move|fix/i)
        expect(File.exist?(path)).to be_truthy

        subject.run!(dry_run: false)

        expect(File.exist?(path)).to be_truthy
      end
    end
  end
end