repository_archive_clean_up_service_spec.rb 4.01 KB
Newer Older
1 2
require 'spec_helper'

3
describe RepositoryArchiveCleanUpService do
4
  subject(:service) { described_class.new }
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
  describe '#execute (new archive locations)' do
    let(:sha) { "0" * 40 }

    it 'removes outdated archives and directories in a new-style path' do
      in_directory_with_files("project-999/#{sha}", %w[tar tar.bz2 tar.gz zip], 3.hours) do |dirname, files|
        service.execute

        files.each { |filename| expect(File.exist?(filename)).to be_falsy }
        expect(File.directory?(dirname)).to be_falsy
        expect(File.directory?(File.dirname(dirname))).to be_falsy
      end
    end

    it 'does not remove directories when they contain outdated non-archives' do
      in_directory_with_files("project-999/#{sha}", %w[tar conf rb], 2.hours) do |dirname, files|
        service.execute

        expect(File.directory?(dirname)).to be_truthy
      end
    end

    it 'does not remove in-date archives in a new-style path' do
      in_directory_with_files("project-999/#{sha}", %w[tar tar.bz2 tar.gz zip], 1.hour) do |dirname, files|
        service.execute

        files.each { |filename| expect(File.exist?(filename)).to be_truthy }
      end
    end
  end

  describe '#execute (legacy archive locations)' do
37 38
    context 'when the downloads directory does not exist' do
      it 'does not remove any archives' do
39 40 41
        path = '/invalid/path/'
        stub_repository_downloads_path(path)

42
        allow(File).to receive(:directory?).and_call_original
43
        expect(File).to receive(:directory?).with(path).and_return(false)
44

45 46 47 48 49 50 51 52
        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
53
      shared_examples 'invalid archive files' do |dirname, extensions, mtime|
54
        it 'does not remove files and directory' do
55
          in_directory_with_files(dirname, extensions, mtime) do |dir, files|
56
            service.execute
57

58
            files.each { |file| expect(File.exist?(file)).to eq true }
59
            expect(File.directory?(dir)).to eq true
60
          end
61 62 63
        end
      end

64 65 66
      it 'removes files older than 2 hours that matches valid archive extensions' do
        in_directory_with_files('sample.git', %w[tar tar.bz2 tar.gz zip], 2.hours) do |dir, files|
          service.execute
67

68 69
          files.each { |file| expect(File.exist?(file)).to eq false }
          expect(File.directory?(dir)).to eq false
70
        end
71
      end
72

73 74 75
      context 'with files older than 2 hours that does not matches valid archive extensions' do
        it_behaves_like 'invalid archive files', 'sample.git', %w[conf rb], 2.hours
      end
76

77
      context 'with files older than 2 hours inside invalid directories' do
78
        it_behaves_like 'invalid archive files', 'john/doe/sample.git', %w[conf rb tar tar.gz], 2.hours
79
      end
80

81 82 83
      context 'with files newer than 2 hours that matches valid archive extensions' do
        it_behaves_like 'invalid archive files', 'sample.git', %w[tar tar.bz2 tar.gz zip], 1.hour
      end
84

85 86 87
      context 'with files newer than 2 hours that does not matches valid archive extensions' do
        it_behaves_like 'invalid archive files', 'sample.git', %w[conf rb], 1.hour
      end
88

89 90
      context 'with files newer than 2 hours inside invalid directories' do
        it_behaves_like 'invalid archive files', 'sample.git', %w[conf rb tar tar.gz], 1.hour
91
      end
92
    end
93
  end
94

95 96 97 98 99
  def in_directory_with_files(dirname, extensions, mtime)
    Dir.mktmpdir do |tmpdir|
      stub_repository_downloads_path(tmpdir)
      dir = File.join(tmpdir, dirname)
      files = create_temporary_files(dir, extensions, mtime)
100

101
      yield(dir, files)
102
    end
103
  end
104

105 106 107
  def stub_repository_downloads_path(path)
    allow(Gitlab.config.gitlab).to receive(:repository_downloads_path).and_return(path)
  end
108

109 110 111
  def create_temporary_files(dir, extensions, mtime)
    FileUtils.mkdir_p(dir)
    FileUtils.touch(extensions.map { |ext| File.join(dir, "sample.#{ext}") }, mtime: Time.now - mtime)
112 113
  end
end