diff --git a/app/models/repository.rb b/app/models/repository.rb index 88c729e83..abbeb47de 100644 --- a/app/models/repository.rb +++ b/app/models/repository.rb @@ -45,22 +45,31 @@ class Repository < ActiveRecord::Base end end + # Checks locking of sync def sync_locked? sync_actions :check end + # Uses for locking sync + # Calls from UI def lock_sync sync_actions :lock end + # Uses for unlocking sync + # Calls from UI def unlock_sync sync_actions :unlock end + # Uses for locking publishing + # Calls from API def start_sync sync_actions :lock, '.repo.lock' end + # Uses for unlocking publishing + # Calls from API def stop_sync sync_actions :unlock, '.repo.lock' end @@ -98,9 +107,9 @@ class Repository < ActiveRecord::Base path = "#{platform.path}/repository/#{arch}/#{name}/#{lock_file}" case action when :lock - result ||= system 'touch', path + result ||= FileUtils.touch(path) rescue nil when :unlock - result ||= system 'rm', '-rf', path + result ||= FileUtils.rm_f(path) when :check return true if File.exist?(path) end diff --git a/spec/models/repository_spec.rb b/spec/models/repository_spec.rb index 35ea72a33..2af7684d4 100644 --- a/spec/models/repository_spec.rb +++ b/spec/models/repository_spec.rb @@ -30,6 +30,44 @@ describe Repository do end + context '#sync_locked?, #lock_sync, #unlock_sync, #start_sync, #stop_sync' do + let(:repository) { FactoryGirl.create(:repository) } + let(:path) { "#{repository.platform.path}/repository/SRPMS/#{repository.name}" } + before { FileUtils.mkdir_p path } + + it 'ensures that #sync_locked? returns false if .sync.lock file does not exist' do + repository.sync_locked?.should be_false + end + + it 'ensures that #sync_locked? returns true if .sync.lock file does exist' do + FileUtils.touch "#{path}/.sync.lock" + repository.sync_locked?.should be_true + end + + it 'ensures that #lock_sync creates .sync.lock file' do + repository.lock_sync + File.exist?("#{path}/.sync.lock").should be_true + end + + it 'ensures that #unlock_sync removes .sync.lock file' do + FileUtils.touch "#{path}/.sync.lock" + repository.unlock_sync + File.exist?("#{path}/.sync.lock").should be_false + end + + it 'ensures that #start_sync creates .repo.lock file' do + repository.start_sync + File.exist?("#{path}/.repo.lock").should be_true + end + + it 'ensures that #stop_sync removes .repo.lock file' do + FileUtils.touch "#{path}/.repo.lock" + repository.stop_sync + File.exist?("#{path}/.repo.lock").should be_false + end + + end + context 'when create with same owner that platform' do before do @platform = FactoryGirl.create(:platform)