diff options
author | Douglas Barbosa Alexandre <dbalexandre@gmail.com> | 2016-07-19 20:45:31 -0300 |
---|---|---|
committer | Douglas Barbosa Alexandre <dbalexandre@gmail.com> | 2016-07-21 10:31:49 -0300 |
commit | 1bda45178977f613a9f4adc1d00e2694ca3963b5 (patch) | |
tree | 292e743f1cf38cf00c538758f1001aa18dd97db3 | |
parent | a6de806498c405355380be4b80f63d134658b779 (diff) | |
download | gitlab-ce-1bda45178977f613a9f4adc1d00e2694ca3963b5.tar.gz |
Cover the behavior RepositoryArchiveCleanUpService with tests
-rw-r--r-- | spec/services/repository_archive_clean_up_service_spec.rb | 96 |
1 files changed, 89 insertions, 7 deletions
diff --git a/spec/services/repository_archive_clean_up_service_spec.rb b/spec/services/repository_archive_clean_up_service_spec.rb index a9ac21258da..321cc6daf9d 100644 --- a/spec/services/repository_archive_clean_up_service_spec.rb +++ b/spec/services/repository_archive_clean_up_service_spec.rb @@ -2,14 +2,17 @@ require 'spec_helper' describe RepositoryArchiveCleanUpService, services: true do describe '#execute' do - let(:path) { Gitlab.config.gitlab.repository_downloads_path } + 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) @@ -18,13 +21,92 @@ describe RepositoryArchiveCleanUpService, services: true do end context 'when the downloads directory exists' do - it 'removes old archives' do - expect(File).to receive(:directory?).with(path).and_return(true) + before do + FileUtils.mkdir_p(path) + end - expect(service).to receive(:clean_up_old_archives) - expect(service).to receive(:clean_up_empty_directories) + after do + FileUtils.rm_rf(path) + end - service.execute + 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 |