summaryrefslogtreecommitdiff
path: root/spec/tasks/gitlab/cleanup_rake_spec.rb
blob: ba08ece1b4b3d55141fa6e9ceed5b8006592f674 (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
require 'rake_helper'

describe 'gitlab:cleanup rake tasks' do
  before do
    Rake.application.rake_require 'tasks/gitlab/cleanup'
  end

  describe 'cleanup namespaces and repos' do
    let(:storages) do
      {
        'default' => Gitlab::GitalyClient::StorageSettings.new(@default_storage_hash.merge('path' => 'tmp/tests/default_storage'))
      }
    end

    before(:all) do
      @default_storage_hash = Gitlab.config.repositories.storages.default.to_h
    end

    before do
      FileUtils.mkdir(Settings.absolute('tmp/tests/default_storage'))
      allow(Gitlab.config.repositories).to receive(:storages).and_return(storages)
    end

    after do
      FileUtils.rm_rf(Settings.absolute('tmp/tests/default_storage'))
    end

    describe 'cleanup:repos' do
      before do
        FileUtils.mkdir_p(Settings.absolute('tmp/tests/default_storage/broken/project.git'))
        FileUtils.mkdir_p(Settings.absolute('tmp/tests/default_storage/@hashed/12/34/5678.git'))
      end

      it 'moves it to an orphaned path' do
        run_rake_task('gitlab:cleanup:repos')
        repo_list = Dir['tmp/tests/default_storage/broken/*']

        expect(repo_list.first).to include('+orphaned+')
      end

      it 'ignores @hashed repos' do
        run_rake_task('gitlab:cleanup:repos')

        expect(Dir.exist?(Settings.absolute('tmp/tests/default_storage/@hashed/12/34/5678.git'))).to be_truthy
      end
    end

    describe 'cleanup:dirs' do
      it 'removes missing namespaces' do
        FileUtils.mkdir_p(Settings.absolute("tmp/tests/default_storage/namespace_1/project.git"))
        FileUtils.mkdir_p(Settings.absolute("tmp/tests/default_storage/namespace_2/project.git"))
        allow(Namespace).to receive(:pluck).and_return('namespace_1')

        stub_env('REMOVE', 'true')
        run_rake_task('gitlab:cleanup:dirs')

        expect(Dir.exist?(Settings.absolute('tmp/tests/default_storage/namespace_1'))).to be_truthy
        expect(Dir.exist?(Settings.absolute('tmp/tests/default_storage/namespace_2'))).to be_falsey
      end

      it 'ignores @hashed directory' do
        FileUtils.mkdir_p(Settings.absolute('tmp/tests/default_storage/@hashed/12/34/5678.git'))

        run_rake_task('gitlab:cleanup:dirs')

        expect(Dir.exist?(Settings.absolute('tmp/tests/default_storage/@hashed/12/34/5678.git'))).to be_truthy
      end
    end
  end

  describe 'cleanup:project_uploads' do
    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: build(:project, :legacy_storage)) }
          let!(:correct_path) { orphaned.absolute_path }
          let!(:other_project) { create(:project, :legacy_storage) }
          let!(:orphaned_path) { correct_path.sub(/#{orphaned.model.full_path}/, other_project.full_path) }

          before do
            FileUtils.mkdir_p(File.dirname(orphaned_path))
            FileUtils.mv(correct_path, orphaned_path)
          end

          it 'moves the file to its proper location' do
            expect(Rails.logger).to receive(:info).twice
            expect(Rails.logger).to receive(:info).with("Did fix #{orphaned_path} -> #{correct_path}")

            expect(File.exist?(orphaned_path)).to be_truthy
            expect(File.exist?(correct_path)).to be_falsey

            stub_env('DRY_RUN', 'false')
            run_rake_task('gitlab:cleanup:project_uploads')

            expect(File.exist?(orphaned_path)).to be_falsey
            expect(File.exist?(correct_path)).to be_truthy
          end

          it 'a dry run does not move the file' do
            expect(Rails.logger).to receive(:info).twice
            expect(Rails.logger).to receive(:info).with("Can fix #{orphaned_path} -> #{correct_path}")
            expect(Rails.logger).to receive(:info)

            expect(File.exist?(orphaned_path)).to be_truthy
            expect(File.exist?(correct_path)).to be_falsey

            run_rake_task('gitlab:cleanup:project_uploads')

            expect(File.exist?(orphaned_path)).to be_truthy
            expect(File.exist?(correct_path)).to be_falsey
          end

          context 'when the project record is missing (Upload#absolute_path raises error)' do
            let!(:lost_and_found_path) { File.join(FileUploader.root, '-', 'project-lost-found', other_project.full_path, orphaned.path) }

            before do
              orphaned.model.delete
            end

            it 'moves the file to lost and found' do
              expect(Rails.logger).to receive(:info).twice
              expect(Rails.logger).to receive(:info).with("Did move to lost and found #{orphaned_path} -> #{lost_and_found_path}")

              expect(File.exist?(orphaned_path)).to be_truthy
              expect(File.exist?(lost_and_found_path)).to be_falsey

              stub_env('DRY_RUN', 'false')
              run_rake_task('gitlab:cleanup:project_uploads')

              expect(File.exist?(orphaned_path)).to be_falsey
              expect(File.exist?(lost_and_found_path)).to be_truthy
            end

            it 'a dry run does not move the file' do
              expect(Rails.logger).to receive(:info).twice
              expect(Rails.logger).to receive(:info).with("Can move to lost and found #{orphaned_path} -> #{lost_and_found_path}")
              expect(Rails.logger).to receive(:info)

              expect(File.exist?(orphaned_path)).to be_truthy
              expect(File.exist?(lost_and_found_path)).to be_falsey

              run_rake_task('gitlab:cleanup:project_uploads')

              expect(File.exist?(orphaned_path)).to be_truthy
              expect(File.exist?(lost_and_found_path)).to be_falsey
            end
          end
        end

        context 'when the project was moved to hashed storage' do
          let!(:orphaned) { create(:upload, :issuable_upload, :with_file) }
          let!(:correct_path) { orphaned.absolute_path }
          let!(:orphaned_path) { File.join(FileUploader.root, 'foo', 'bar', orphaned.path) }

          before do
            FileUtils.mkdir_p(File.dirname(orphaned_path))
            FileUtils.mv(correct_path, orphaned_path)
          end

          it 'moves the file to its proper location' do
            expect(Rails.logger).to receive(:info).twice
            expect(Rails.logger).to receive(:info).with("Did fix #{orphaned_path} -> #{correct_path}")

            expect(File.exist?(orphaned_path)).to be_truthy
            expect(File.exist?(correct_path)).to be_falsey

            stub_env('DRY_RUN', 'false')
            run_rake_task('gitlab:cleanup:project_uploads')

            expect(File.exist?(orphaned_path)).to be_falsey
            expect(File.exist?(correct_path)).to be_truthy
          end

          it 'a dry run does not move the file' do
            expect(Rails.logger).to receive(:info).twice
            expect(Rails.logger).to receive(:info).with("Can fix #{orphaned_path} -> #{correct_path}")
            expect(Rails.logger).to receive(:info)

            expect(File.exist?(orphaned_path)).to be_truthy
            expect(File.exist?(correct_path)).to be_falsey

            run_rake_task('gitlab:cleanup:project_uploads')

            expect(File.exist?(orphaned_path)).to be_truthy
            expect(File.exist?(correct_path)).to be_falsey
          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!(:orphaned_path) { orphaned.absolute_path }
          let!(:lost_and_found_path) { File.join(FileUploader.root, '-', 'project-lost-found', orphaned.model.full_path, orphaned.path) }

          before do
            orphaned.delete
          end

          it 'moves the file to lost and found' do
            expect(Rails.logger).to receive(:info).twice
            expect(Rails.logger).to receive(:info).with("Did move to lost and found #{orphaned_path} -> #{lost_and_found_path}")

            expect(File.exist?(orphaned_path)).to be_truthy
            expect(File.exist?(lost_and_found_path)).to be_falsey

            stub_env('DRY_RUN', 'false')
            run_rake_task('gitlab:cleanup:project_uploads')

            expect(File.exist?(orphaned_path)).to be_falsey
            expect(File.exist?(lost_and_found_path)).to be_truthy
          end

          it 'a dry run does not move the file' do
            expect(Rails.logger).to receive(:info).twice
            expect(Rails.logger).to receive(:info).with("Can move to lost and found #{orphaned_path} -> #{lost_and_found_path}")
            expect(Rails.logger).to receive(:info)

            expect(File.exist?(orphaned_path)).to be_truthy
            expect(File.exist?(lost_and_found_path)).to be_falsey

            run_rake_task('gitlab:cleanup:project_uploads')

            expect(File.exist?(orphaned_path)).to be_truthy
            expect(File.exist?(lost_and_found_path)).to be_falsey
          end
        end

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

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

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

          it 'moves the file to lost and found' do
            expect(Rails.logger).to receive(:info).twice
            expect(Rails.logger).to receive(:info).with("Did move to lost and found #{orphaned_path} -> #{lost_and_found_path}")

            expect(File.exist?(orphaned_path)).to be_truthy
            expect(File.exist?(lost_and_found_path)).to be_falsey

            stub_env('DRY_RUN', 'false')
            run_rake_task('gitlab:cleanup:project_uploads')

            expect(File.exist?(orphaned_path)).to be_falsey
            expect(File.exist?(lost_and_found_path)).to be_truthy
          end

          it 'a dry run does not move the file' do
            expect(Rails.logger).to receive(:info).twice
            expect(Rails.logger).to receive(:info).with("Can move to lost and found #{orphaned_path} -> #{lost_and_found_path}")
            expect(Rails.logger).to receive(:info)

            expect(File.exist?(orphaned_path)).to be_truthy
            expect(File.exist?(lost_and_found_path)).to be_falsey

            run_rake_task('gitlab:cleanup:project_uploads')

            expect(File.exist?(orphaned_path)).to be_truthy
            expect(File.exist?(lost_and_found_path)).to be_falsey
          end
        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(Rails.logger).not_to receive(:info).with(/move|fix/i)
        expect(File.exist?(tracked_path)).to be_truthy

        stub_env('DRY_RUN', 'false')
        run_rake_task('gitlab:cleanup:project_uploads')

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

    context 'ignorable cases' do
      shared_examples_for 'does not move anything' do
        it 'does not move even an orphan file' do
          orphaned = create(:upload, :issuable_upload, :with_file, model: project)
          orphaned_path = orphaned.absolute_path
          orphaned.delete

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

          run_rake_task('gitlab:cleanup:project_uploads')

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

      # 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
          stub_env('DRY_RUN', 'false')
          expect(Rails.logger).not_to receive(:info).with(/move|fix/i)
        end

        it_behaves_like 'does not move anything'
      end

      context 'when DRY_RUN env var is unset' do
        let(:project) { create(:project, :legacy_storage) }

        it_behaves_like 'does not move anything'
      end

      context 'when DRY_RUN env var is true' do
        let(:project) { create(:project, :legacy_storage) }

        before do
          stub_env('DRY_RUN', 'true')
        end

        it_behaves_like 'does not move anything'
      end

      context 'when DRY_RUN env var is foo' do
        let(:project) { create(:project, :legacy_storage) }

        before do
          stub_env('DRY_RUN', 'foo')
        end

        it_behaves_like 'does not move anything'
      end

      it 'does not move any non-project (FileUploader) uploads' do
        stub_env('DRY_RUN', 'false')

        paths = []
        orphaned1 = create(:upload, :personal_snippet_upload, :with_file)
        orphaned2 = create(:upload, :namespace_upload, :with_file)
        orphaned3 = create(:upload, :attachment_upload, :with_file)
        paths << orphaned1.absolute_path
        paths << orphaned2.absolute_path
        paths << orphaned3.absolute_path
        Upload.delete_all

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

        run_rake_task('gitlab:cleanup:project_uploads')

        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
        stub_env('DRY_RUN', 'false')

        path = File.join(FileUploader.root, 'tmp', 'foo.jpg')
        FileUtils.mkdir_p(File.dirname(path))
        FileUtils.touch(path)

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

        run_rake_task('gitlab:cleanup:project_uploads')

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