summaryrefslogtreecommitdiff
path: root/spec/services/repository_archive_clean_up_service_spec.rb
blob: 321cc6daf9d1becf466e32cdea41c6c249d48d5c (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
require 'spec_helper'

describe RepositoryArchiveCleanUpService, services: true do
  describe '#execute' do
    let(:path) { File.join(Rails.root, 'tmp/tests/shared/cache/archive') }

    subject(:service) { described_class.new }

    before do
      allow(Gitlab.config.gitlab).to receive(:repository_downloads_path).and_return(path)
    end

    context 'when the downloads directory does not exist' do
      it 'does not remove any archives' do
        expect(File).to receive(:directory?).with(path).and_return(false)
        expect(service).not_to receive(:clean_up_old_archives)
        expect(service).not_to receive(:clean_up_empty_directories)

        service.execute
      end
    end

    context 'when the downloads directory exists' do
      before do
        FileUtils.mkdir_p(path)
      end

      after do
        FileUtils.rm_rf(path)
      end

      context 'when archives older than 2 hours exists' do
        before do
          allow_any_instance_of(File).to receive(:mtime).and_return(2.hours.ago)
        end

        it 'removes old files that matches valid archive extensions' do
          dirname = File.join(path, 'sample.git')
          files = create_temporary_files(dirname, %w[tar tar.bz2 tar.gz zip])

          service.execute

          files.each { |file| expect(File.exist?(file)).to eq false }
          expect(File.directory?(dirname)).to eq false
        end

        it 'keeps old files that does not matches valid archive extensions' do
          dirname = File.join(path, 'sample.git')
          files = create_temporary_files(dirname, %w[conf rb])

          service.execute

          files.each { |file| expect(File.exist?(file)).to eq true }
          expect(File.directory?(dirname)).to eq true
        end

        it 'keeps old files inside invalid directories' do
          dirname = File.join(path, 'john_doe/sample.git')
          files = create_temporary_files(dirname, %w[conf rb tar tar.gz])

          service.execute

          files.each { |file| expect(File.exist?(file)).to eq true }
          expect(File.directory?(dirname)).to eq true
        end
      end

      context 'when archives older than 2 hours does not exist' do
        before do
          allow_any_instance_of(File).to receive(:mtime).and_return(1.hour.ago)
        end

        it 'keeps files that matches valid archive extensions' do
          dirname = File.join(path, 'sample.git')
          files = create_temporary_files(dirname, %w[tar tar.bz2 tar.gz zip])

          service.execute

          files.each { |file| expect(File.exist?(file)).to eq true }
          expect(File.directory?(dirname)).to eq true
        end

        it 'keeps files that does not matches valid archive extensions' do
          dirname = File.join(path, 'sample.git')
          files = create_temporary_files(dirname, %w[conf rb])

          service.execute

          files.each { |file| expect(File.exist?(file)).to eq true }
          expect(File.directory?(dirname)).to eq true
        end

        it 'keeps files inside invalid directories' do
          dirname = File.join(path, 'john_doe/sample.git')
          files = create_temporary_files(dirname, %w[conf rb tar tar.gz])

          service.execute

          files.each { |file| expect(File.exist?(file)).to eq true }
          expect(File.directory?(dirname)).to eq true
        end
      end

      def create_temporary_files(dirname, extensions)
        FileUtils.mkdir_p(dirname)

        extensions.flat_map do |extension|
          FileUtils.touch(File.join(dirname, "sample.#{extension}"))
        end
      end
    end
  end
end