2019-04-11 08:17:24 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-01-20 08:32:32 -05:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 02:09:01 -04:00
|
|
|
RSpec.describe Ci::UpdateBuildQueueService do
|
2017-03-27 17:14:01 -04:00
|
|
|
let(:project) { create(:project, :repository) }
|
2017-01-20 08:32:32 -05:00
|
|
|
let(:pipeline) { create(:ci_pipeline, project: project) }
|
2021-06-11 11:09:58 -04:00
|
|
|
let(:build) { create(:ci_build, pipeline: pipeline) }
|
2017-01-20 08:32:32 -05:00
|
|
|
|
2021-06-11 11:09:58 -04:00
|
|
|
describe 'pending builds queue push / pop' do
|
|
|
|
describe '#push' do
|
|
|
|
let(:transition) { double('transition') }
|
2017-01-20 08:32:32 -05:00
|
|
|
|
2021-06-11 11:09:58 -04:00
|
|
|
before do
|
|
|
|
allow(transition).to receive(:to).and_return('pending')
|
|
|
|
allow(transition).to receive(:within_transaction).and_yield
|
|
|
|
end
|
2017-01-20 08:32:32 -05:00
|
|
|
|
2021-06-11 11:09:58 -04:00
|
|
|
context 'when pending build can be created' do
|
|
|
|
it 'creates a new pending build in transaction' do
|
|
|
|
queued = subject.push(build, transition)
|
2021-05-19 11:10:40 -04:00
|
|
|
|
2021-06-11 11:09:58 -04:00
|
|
|
expect(queued).to eq build.id
|
|
|
|
end
|
2019-08-22 07:08:46 -04:00
|
|
|
|
2021-06-11 11:09:58 -04:00
|
|
|
it 'increments queue push metric' do
|
|
|
|
metrics = spy('metrics')
|
2021-03-02 19:10:50 -05:00
|
|
|
|
2021-06-11 11:09:58 -04:00
|
|
|
described_class.new(metrics).push(build, transition)
|
2021-03-02 19:10:50 -05:00
|
|
|
|
2021-06-11 11:09:58 -04:00
|
|
|
expect(metrics)
|
|
|
|
.to have_received(:increment_queue_operation)
|
|
|
|
.with(:build_queue_push)
|
|
|
|
end
|
2021-03-02 19:10:50 -05:00
|
|
|
end
|
|
|
|
|
2021-06-11 11:09:58 -04:00
|
|
|
context 'when invalid transition is detected' do
|
|
|
|
it 'raises an error' do
|
|
|
|
allow(transition).to receive(:to).and_return('created')
|
|
|
|
|
|
|
|
expect { subject.push(build, transition) }
|
|
|
|
.to raise_error(described_class::InvalidQueueTransition)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when duplicate entry exists' do
|
|
|
|
before do
|
2021-06-21 11:07:30 -04:00
|
|
|
create(:ci_pending_build, build: build, project: build.project)
|
2021-06-11 11:09:58 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'does nothing and returns build id' do
|
|
|
|
queued = subject.push(build, transition)
|
2021-03-02 19:10:50 -05:00
|
|
|
|
2021-06-11 11:09:58 -04:00
|
|
|
expect(queued).to eq build.id
|
|
|
|
end
|
2021-03-02 19:10:50 -05:00
|
|
|
end
|
2017-01-20 08:32:32 -05:00
|
|
|
end
|
|
|
|
|
2021-06-11 11:09:58 -04:00
|
|
|
describe '#pop' do
|
|
|
|
let(:transition) { double('transition') }
|
|
|
|
|
2017-06-14 14:18:56 -04:00
|
|
|
before do
|
2021-06-11 11:09:58 -04:00
|
|
|
allow(transition).to receive(:from).and_return('pending')
|
|
|
|
allow(transition).to receive(:within_transaction).and_yield
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when pending build exists' do
|
|
|
|
before do
|
2021-06-21 11:07:30 -04:00
|
|
|
create(:ci_pending_build, build: build, project: build.project)
|
2021-06-11 11:09:58 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'removes pending build in a transaction' do
|
|
|
|
dequeued = subject.pop(build, transition)
|
|
|
|
|
|
|
|
expect(dequeued).to eq build.id
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'increments queue pop metric' do
|
|
|
|
metrics = spy('metrics')
|
|
|
|
|
|
|
|
described_class.new(metrics).pop(build, transition)
|
|
|
|
|
|
|
|
expect(metrics)
|
|
|
|
.to have_received(:increment_queue_operation)
|
|
|
|
.with(:build_queue_pop)
|
|
|
|
end
|
2017-06-14 14:18:56 -04:00
|
|
|
end
|
2017-01-20 08:32:32 -05:00
|
|
|
|
2021-06-11 11:09:58 -04:00
|
|
|
context 'when pending build does not exist' do
|
|
|
|
it 'does nothing if there is no pending build to remove' do
|
|
|
|
dequeued = subject.pop(build, transition)
|
2021-05-19 11:10:40 -04:00
|
|
|
|
2021-06-11 11:09:58 -04:00
|
|
|
expect(dequeued).to be_nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when invalid transition is detected' do
|
|
|
|
it 'raises an error' do
|
|
|
|
allow(transition).to receive(:from).and_return('created')
|
|
|
|
|
|
|
|
expect { subject.pop(build, transition) }
|
|
|
|
.to raise_error(described_class::InvalidQueueTransition)
|
|
|
|
end
|
2021-05-19 11:10:40 -04:00
|
|
|
end
|
2017-10-04 11:37:05 -04:00
|
|
|
end
|
2022-04-06 11:08:23 -04:00
|
|
|
|
|
|
|
describe '#remove!' do
|
|
|
|
context 'when pending build exists' do
|
|
|
|
before do
|
|
|
|
create(:ci_pending_build, build: build, project: build.project)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'removes pending build in a transaction' do
|
|
|
|
dequeued = subject.remove!(build)
|
|
|
|
|
|
|
|
expect(dequeued).to eq build.id
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when pending build does not exist' do
|
|
|
|
it 'does nothing if there is no pending build to remove' do
|
|
|
|
dequeued = subject.remove!(build)
|
|
|
|
|
|
|
|
expect(dequeued).to be_nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-08-22 07:08:46 -04:00
|
|
|
end
|
2017-10-04 11:37:05 -04:00
|
|
|
|
2021-06-11 11:09:58 -04:00
|
|
|
describe 'shared runner builds tracking' do
|
|
|
|
let(:runner) { create(:ci_runner, :instance_type) }
|
|
|
|
let(:build) { create(:ci_build, runner: runner, pipeline: pipeline) }
|
2021-05-19 11:10:40 -04:00
|
|
|
|
2021-06-11 11:09:58 -04:00
|
|
|
describe '#track' do
|
|
|
|
let(:transition) { double('transition') }
|
2021-05-19 11:10:40 -04:00
|
|
|
|
2017-10-04 11:37:05 -04:00
|
|
|
before do
|
2021-06-11 11:09:58 -04:00
|
|
|
allow(transition).to receive(:to).and_return('running')
|
|
|
|
allow(transition).to receive(:within_transaction).and_yield
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when a shared runner build can be tracked' do
|
|
|
|
it 'creates a new shared runner build tracking entry' do
|
|
|
|
build_id = subject.track(build, transition)
|
|
|
|
|
|
|
|
expect(build_id).to eq build.id
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'increments new shared runner build metric' do
|
|
|
|
metrics = spy('metrics')
|
|
|
|
|
|
|
|
described_class.new(metrics).track(build, transition)
|
|
|
|
|
|
|
|
expect(metrics)
|
|
|
|
.to have_received(:increment_queue_operation)
|
|
|
|
.with(:shared_runner_build_new)
|
|
|
|
end
|
2017-10-04 11:37:05 -04:00
|
|
|
end
|
|
|
|
|
2021-06-11 11:09:58 -04:00
|
|
|
context 'when invalid transition is detected' do
|
|
|
|
it 'raises an error' do
|
|
|
|
allow(transition).to receive(:to).and_return('pending')
|
2021-05-19 11:10:40 -04:00
|
|
|
|
2021-06-11 11:09:58 -04:00
|
|
|
expect { subject.track(build, transition) }
|
|
|
|
.to raise_error(described_class::InvalidQueueTransition)
|
|
|
|
end
|
2021-05-19 11:10:40 -04:00
|
|
|
end
|
|
|
|
|
2021-06-11 11:09:58 -04:00
|
|
|
context 'when duplicate entry exists' do
|
|
|
|
before do
|
2021-06-21 11:07:30 -04:00
|
|
|
create(:ci_running_build, build: build, project: project, runner: runner)
|
2021-06-11 11:09:58 -04:00
|
|
|
end
|
2021-05-19 11:10:40 -04:00
|
|
|
|
2021-06-11 11:09:58 -04:00
|
|
|
it 'does nothing and returns build id' do
|
|
|
|
build_id = subject.track(build, transition)
|
2021-05-19 11:10:40 -04:00
|
|
|
|
2021-06-11 11:09:58 -04:00
|
|
|
expect(build_id).to eq build.id
|
|
|
|
end
|
2021-05-19 11:10:40 -04:00
|
|
|
end
|
2017-10-04 11:37:05 -04:00
|
|
|
end
|
|
|
|
|
2021-06-11 11:09:58 -04:00
|
|
|
describe '#untrack' do
|
|
|
|
let(:transition) { double('transition') }
|
2017-10-04 11:37:05 -04:00
|
|
|
|
2021-06-11 11:09:58 -04:00
|
|
|
before do
|
|
|
|
allow(transition).to receive(:from).and_return('running')
|
|
|
|
allow(transition).to receive(:within_transaction).and_yield
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when shared runner build tracking entry exists' do
|
|
|
|
before do
|
2021-06-21 11:07:30 -04:00
|
|
|
create(:ci_running_build, build: build, project: project, runner: runner)
|
2021-06-11 11:09:58 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'removes shared runner build' do
|
|
|
|
build_id = subject.untrack(build, transition)
|
|
|
|
|
|
|
|
expect(build_id).to eq build.id
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'increments shared runner build done metric' do
|
|
|
|
metrics = spy('metrics')
|
|
|
|
|
|
|
|
described_class.new(metrics).untrack(build, transition)
|
|
|
|
|
|
|
|
expect(metrics)
|
|
|
|
.to have_received(:increment_queue_operation)
|
|
|
|
.with(:shared_runner_build_done)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when tracking entry does not exist' do
|
|
|
|
it 'does nothing if there is no tracking entry to remove' do
|
|
|
|
build_id = subject.untrack(build, transition)
|
|
|
|
|
|
|
|
expect(build_id).to be_nil
|
|
|
|
end
|
2021-05-19 11:10:40 -04:00
|
|
|
end
|
2019-08-22 07:08:46 -04:00
|
|
|
|
2021-06-11 11:09:58 -04:00
|
|
|
context 'when invalid transition is detected' do
|
|
|
|
it 'raises an error' do
|
|
|
|
allow(transition).to receive(:from).and_return('pending')
|
2019-08-22 07:08:46 -04:00
|
|
|
|
2021-06-11 11:09:58 -04:00
|
|
|
expect { subject.untrack(build, transition) }
|
|
|
|
.to raise_error(described_class::InvalidQueueTransition)
|
|
|
|
end
|
2021-05-19 11:10:40 -04:00
|
|
|
end
|
2017-10-04 11:37:05 -04:00
|
|
|
end
|
2019-08-22 07:08:46 -04:00
|
|
|
end
|
2017-10-04 11:37:05 -04:00
|
|
|
|
2021-05-19 11:10:40 -04:00
|
|
|
describe '#tick' do
|
|
|
|
shared_examples 'refreshes runner' do
|
|
|
|
it 'ticks runner queue value' do
|
|
|
|
expect { subject.tick(build) }.to change { runner.ensure_runner_queue_value }
|
|
|
|
end
|
|
|
|
end
|
2019-08-22 07:08:46 -04:00
|
|
|
|
2021-05-19 11:10:40 -04:00
|
|
|
shared_examples 'does not refresh runner' do
|
|
|
|
it 'ticks runner queue value' do
|
|
|
|
expect { subject.tick(build) }.not_to change { runner.ensure_runner_queue_value }
|
2017-10-04 11:37:05 -04:00
|
|
|
end
|
2021-05-19 11:10:40 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples 'matching build' do
|
|
|
|
context 'when there is a online runner that can pick build' do
|
|
|
|
before do
|
|
|
|
runner.update!(contacted_at: 30.minutes.ago)
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'refreshes runner'
|
|
|
|
|
|
|
|
it 'avoids running redundant queries' do
|
|
|
|
expect(Ci::Runner).not_to receive(:owned_or_instance_wide)
|
|
|
|
|
|
|
|
subject.tick(build)
|
|
|
|
end
|
|
|
|
end
|
2017-10-04 11:37:05 -04:00
|
|
|
end
|
2019-08-22 07:08:46 -04:00
|
|
|
|
2021-05-19 11:10:40 -04:00
|
|
|
shared_examples 'mismatching tags' do
|
|
|
|
context 'when there is no runner that can pick build due to tag mismatch' do
|
|
|
|
before do
|
|
|
|
build.tag_list = [:docker]
|
|
|
|
end
|
2019-08-22 07:08:46 -04:00
|
|
|
|
2021-05-19 11:10:40 -04:00
|
|
|
it_behaves_like 'does not refresh runner'
|
|
|
|
end
|
|
|
|
end
|
2017-10-04 11:37:05 -04:00
|
|
|
|
2021-05-19 11:10:40 -04:00
|
|
|
shared_examples 'recent runner queue' do
|
|
|
|
context 'when there is runner with expired cache' do
|
|
|
|
before do
|
|
|
|
runner.update!(contacted_at: Ci::Runner.recent_queue_deadline)
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'does not refresh runner'
|
2017-10-04 11:37:05 -04:00
|
|
|
end
|
2021-05-19 11:10:40 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when updating specific runners' do
|
|
|
|
let(:runner) { create(:ci_runner, :project, projects: [project]) }
|
|
|
|
|
|
|
|
it_behaves_like 'matching build'
|
|
|
|
it_behaves_like 'mismatching tags'
|
|
|
|
it_behaves_like 'recent runner queue'
|
2017-10-04 11:37:05 -04:00
|
|
|
|
2021-05-19 11:10:40 -04:00
|
|
|
context 'when the runner is assigned to another project' do
|
|
|
|
let(:another_project) { create(:project) }
|
|
|
|
let(:runner) { create(:ci_runner, :project, projects: [another_project]) }
|
|
|
|
|
|
|
|
it_behaves_like 'does not refresh runner'
|
|
|
|
end
|
2017-01-20 08:32:32 -05:00
|
|
|
end
|
2021-03-02 19:10:50 -05:00
|
|
|
|
2021-05-19 11:10:40 -04:00
|
|
|
context 'when updating shared runners' do
|
|
|
|
let(:runner) { create(:ci_runner, :instance) }
|
2021-03-02 19:10:50 -05:00
|
|
|
|
2021-05-19 11:10:40 -04:00
|
|
|
it_behaves_like 'matching build'
|
|
|
|
it_behaves_like 'mismatching tags'
|
|
|
|
it_behaves_like 'recent runner queue'
|
|
|
|
|
|
|
|
context 'when there is no runner that can pick build due to being disabled on project' do
|
|
|
|
before do
|
|
|
|
build.project.shared_runners_enabled = false
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'does not refresh runner'
|
2021-03-02 19:10:50 -05:00
|
|
|
end
|
2021-05-19 11:10:40 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when updating group runners' do
|
|
|
|
let(:group) { create(:group) }
|
|
|
|
let(:project) { create(:project, group: group) }
|
|
|
|
let(:runner) { create(:ci_runner, :group, groups: [group]) }
|
2021-03-02 19:10:50 -05:00
|
|
|
|
2021-05-19 11:10:40 -04:00
|
|
|
it_behaves_like 'matching build'
|
|
|
|
it_behaves_like 'mismatching tags'
|
|
|
|
it_behaves_like 'recent runner queue'
|
2021-03-02 19:10:50 -05:00
|
|
|
|
2021-05-19 11:10:40 -04:00
|
|
|
context 'when there is no runner that can pick build due to being disabled on project' do
|
|
|
|
before do
|
|
|
|
build.project.group_runners_enabled = false
|
|
|
|
end
|
2021-03-02 19:10:50 -05:00
|
|
|
|
2021-05-19 11:10:40 -04:00
|
|
|
it_behaves_like 'does not refresh runner'
|
2021-03-02 19:10:50 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-05-19 11:10:40 -04:00
|
|
|
context 'avoids N+1 queries', :request_store do
|
|
|
|
let!(:build) { create(:ci_build, pipeline: pipeline, tag_list: %w[a b]) }
|
|
|
|
let!(:project_runner) { create(:ci_runner, :project, :online, projects: [project], tag_list: %w[a b c]) }
|
|
|
|
|
2022-01-20 10:11:58 -05:00
|
|
|
it 'does execute the same amount of queries regardless of number of runners' do
|
|
|
|
control_count = ActiveRecord::QueryRecorder.new { subject.tick(build) }.count
|
2021-03-02 19:10:50 -05:00
|
|
|
|
2022-01-20 10:11:58 -05:00
|
|
|
create_list(:ci_runner, 10, :project, :online, projects: [project], tag_list: %w[b c d])
|
2021-05-19 11:10:40 -04:00
|
|
|
|
2022-01-20 10:11:58 -05:00
|
|
|
expect { subject.tick(build) }.not_to exceed_all_query_limit(control_count)
|
2021-03-02 19:10:50 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-01-20 08:32:32 -05:00
|
|
|
end
|