Skip to content

Commit 4f7f325

Browse files
committed
Implement the logic for locking runner
1 parent 8607601 commit 4f7f325

File tree

3 files changed

+72
-5
lines changed

3 files changed

+72
-5
lines changed

app/models/ci/build.rb

+1-3
Original file line numberDiff line numberDiff line change
@@ -291,9 +291,7 @@ def valid_token?(token)
291291
end
292292

293293
def can_be_served?(runner)
294-
return false unless has_tags? || runner.run_untagged?
295-
296-
(tag_list - runner.tag_list).empty?
294+
runner.can_serve?(self)
297295
end
298296

299297
def has_tags?

app/models/ci/runner.rb

+18
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,12 @@ def specific?
9191
!shared?
9292
end
9393

94+
def can_serve?(build)
95+
not_locked_or_locked_to?(build.project) &&
96+
run_untagged_or_has_tags?(build) &&
97+
accepting_tags?(build.tag_list)
98+
end
99+
94100
def only_for?(project)
95101
projects == [project]
96102
end
@@ -111,5 +117,17 @@ def tag_constraints
111117
'can not be empty when runner is not allowed to pick untagged jobs')
112118
end
113119
end
120+
121+
def not_locked_or_locked_to?(project)
122+
!locked? || projects.exists?(id: project.id)
123+
end
124+
125+
def run_untagged_or_has_tags?(build)
126+
run_untagged? || build.has_tags?
127+
end
128+
129+
def accepting_tags?(target_tags)
130+
(target_tags - tag_list).empty?
131+
end
114132
end
115133
end

spec/models/build_spec.rb

+53-2
Original file line numberDiff line numberDiff line change
@@ -296,16 +296,67 @@
296296
it_behaves_like 'tagged build picker'
297297
end
298298

299-
context 'when runner can not pick untagged jobs' do
299+
context 'when runner cannot pick untagged jobs' do
300300
before { runner.run_untagged = false }
301301

302-
it 'can not handle builds without tags' do
302+
it 'cannot handle builds without tags' do
303303
expect(build.can_be_served?(runner)).to be_falsey
304304
end
305305

306306
it_behaves_like 'tagged build picker'
307307
end
308308
end
309+
310+
context 'when runner is locked' do
311+
before { runner.locked = true }
312+
313+
shared_examples 'locked build picker' do |serve_matching_tags|
314+
context 'when runner cannot pick untagged jobs' do
315+
before { runner.run_untagged = false }
316+
317+
it 'cannot handle builds without tags' do
318+
expect(build.can_be_served?(runner)).to be_falsey
319+
end
320+
end
321+
322+
context 'when having runner tags' do
323+
before { runner.tag_list = ['bb', 'cc'] }
324+
325+
it "#{serve_matching_tags} handle it for matching tags" do
326+
build.tag_list = ['bb']
327+
expected = if serve_matching_tags
328+
be_truthy
329+
else
330+
be_falsey
331+
end
332+
expect(build.can_be_served?(runner)).to expected
333+
end
334+
335+
it 'cannot handle it for builds without matching tags' do
336+
build.tag_list = ['aa']
337+
expect(build.can_be_served?(runner)).to be_falsey
338+
end
339+
end
340+
end
341+
342+
context 'when serving the same project' do
343+
it 'can handle it' do
344+
expect(build.can_be_served?(runner)).to be_truthy
345+
end
346+
347+
it_behaves_like 'locked build picker', true
348+
end
349+
350+
context 'serving a different project' do
351+
before { runner.runner_projects.destroy_all }
352+
353+
it 'cannot handle it' do
354+
expect(build.can_be_served?(runner)).to be_falsey
355+
end
356+
357+
it_behaves_like 'locked build picker', false
358+
end
359+
end
309360
end
310361

311362
describe '#has_tags?' do

0 commit comments

Comments
 (0)