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
end
def can_be_served?(runner)
return false unless has_tags? || runner.run_untagged?
(tag_list - runner.tag_list).empty?
runner.can_serve?(self)
end
def has_tags?
......
......@@ -91,6 +91,12 @@ module Ci
!shared?
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)
projects == [project]
end
......@@ -111,5 +117,17 @@ module Ci
'can not be empty when runner is not allowed to pick untagged jobs')
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
......@@ -296,16 +296,67 @@ describe Ci::Build, models: true do
it_behaves_like 'tagged build picker'
end
context 'when runner can not pick untagged jobs' do
context 'when runner cannot pick untagged jobs' do
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
end
it_behaves_like 'tagged build picker'
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
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