Commit 4f7f3258 authored by Lin Jen-Shin's avatar Lin Jen-Shin

Implement the logic for locking runner

parent 86076012
...@@ -291,9 +291,7 @@ module Ci ...@@ -291,9 +291,7 @@ module Ci
end end
def can_be_served?(runner) def can_be_served?(runner)
return false unless has_tags? || runner.run_untagged? runner.can_serve?(self)
(tag_list - runner.tag_list).empty?
end end
def has_tags? def has_tags?
......
...@@ -91,6 +91,12 @@ module Ci ...@@ -91,6 +91,12 @@ module Ci
!shared? !shared?
end end
def can_serve?(build)
not_locked_or_locked_to?(build.project) &&
run_untagged_or_has_tags?(build) &&
accepting_tags?(build.tag_list)
end
def only_for?(project) def only_for?(project)
projects == [project] projects == [project]
end end
...@@ -111,5 +117,17 @@ module Ci ...@@ -111,5 +117,17 @@ module Ci
'can not be empty when runner is not allowed to pick untagged jobs') 'can not be empty when runner is not allowed to pick untagged jobs')
end end
end end
def not_locked_or_locked_to?(project)
!locked? || projects.exists?(id: project.id)
end
def run_untagged_or_has_tags?(build)
run_untagged? || build.has_tags?
end
def accepting_tags?(target_tags)
(target_tags - tag_list).empty?
end
end end
end end
...@@ -296,16 +296,67 @@ describe Ci::Build, models: true do ...@@ -296,16 +296,67 @@ describe Ci::Build, models: true do
it_behaves_like 'tagged build picker' it_behaves_like 'tagged build picker'
end end
context 'when runner can not pick untagged jobs' do context 'when runner cannot pick untagged jobs' do
before { runner.run_untagged = false } before { runner.run_untagged = false }
it 'can not handle builds without tags' do it 'cannot handle builds without tags' do
expect(build.can_be_served?(runner)).to be_falsey expect(build.can_be_served?(runner)).to be_falsey
end end
it_behaves_like 'tagged build picker' it_behaves_like 'tagged build picker'
end end
end end
context 'when runner is locked' do
before { runner.locked = true }
shared_examples 'locked build picker' do |serve_matching_tags|
context 'when runner cannot pick untagged jobs' do
before { runner.run_untagged = false }
it 'cannot handle builds without tags' do
expect(build.can_be_served?(runner)).to be_falsey
end
end
context 'when having runner tags' do
before { runner.tag_list = ['bb', 'cc'] }
it "#{serve_matching_tags} handle it for matching tags" do
build.tag_list = ['bb']
expected = if serve_matching_tags
be_truthy
else
be_falsey
end
expect(build.can_be_served?(runner)).to expected
end
it 'cannot handle it for builds without matching tags' do
build.tag_list = ['aa']
expect(build.can_be_served?(runner)).to be_falsey
end
end
end
context 'when serving the same project' do
it 'can handle it' do
expect(build.can_be_served?(runner)).to be_truthy
end
it_behaves_like 'locked build picker', true
end
context 'serving a different project' do
before { runner.runner_projects.destroy_all }
it 'cannot handle it' do
expect(build.can_be_served?(runner)).to be_falsey
end
it_behaves_like 'locked build picker', false
end
end
end end
describe '#has_tags?' do describe '#has_tags?' do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment