2019-04-11 12:17:24 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-09-15 20:23:43 +00:00
|
|
|
require 'spec_helper'
|
2015-09-15 13:00:45 +00:00
|
|
|
|
|
|
|
module Ci
|
2020-06-24 06:09:01 +00:00
|
|
|
RSpec.describe RegisterJobService do
|
2020-01-10 09:07:49 +00:00
|
|
|
let_it_be(:group) { create(:group) }
|
2021-08-12 15:09:58 +00:00
|
|
|
let_it_be_with_reload(:project) { create(:project, group: group, shared_runners_enabled: false, group_runners_enabled: false) }
|
|
|
|
let_it_be_with_reload(:pipeline) { create(:ci_pipeline, project: project) }
|
2021-04-21 09:09:15 +00:00
|
|
|
|
2018-05-23 11:55:59 +00:00
|
|
|
let!(:shared_runner) { create(:ci_runner, :instance) }
|
|
|
|
let!(:specific_runner) { create(:ci_runner, :project, projects: [project]) }
|
2018-05-11 10:03:40 +00:00
|
|
|
let!(:group_runner) { create(:ci_runner, :group, groups: [group]) }
|
2021-06-08 09:09:56 +00:00
|
|
|
let!(:pending_job) { create(:ci_build, :pending, :queued, pipeline: pipeline) }
|
2015-09-15 13:00:45 +00:00
|
|
|
|
2016-07-11 22:12:31 +00:00
|
|
|
describe '#execute' do
|
2021-08-05 18:10:13 +00:00
|
|
|
context 'checks database loadbalancing stickiness', :db_load_balancing do
|
2021-06-04 12:10:17 +00:00
|
|
|
subject { described_class.new(shared_runner).execute }
|
|
|
|
|
|
|
|
before do
|
|
|
|
project.update!(shared_runners_enabled: false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'result is valid if replica did caught-up' do
|
|
|
|
expect(Gitlab::Database::LoadBalancing::Sticking).to receive(:all_caught_up?)
|
|
|
|
.with(:runner, shared_runner.id) { true }
|
|
|
|
|
|
|
|
expect(subject).to be_valid
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'result is invalid if replica did not caught-up' do
|
|
|
|
expect(Gitlab::Database::LoadBalancing::Sticking).to receive(:all_caught_up?)
|
|
|
|
.with(:runner, shared_runner.id) { false }
|
|
|
|
|
|
|
|
expect(subject).not_to be_valid
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-03-02 15:10:57 +00:00
|
|
|
shared_examples 'handles runner assignment' do
|
|
|
|
context 'runner follow tag list' do
|
|
|
|
it "picks build with the same tag" do
|
|
|
|
pending_job.update!(tag_list: ["linux"])
|
|
|
|
specific_runner.update!(tag_list: ["linux"])
|
|
|
|
expect(execute(specific_runner)).to eq(pending_job)
|
|
|
|
end
|
2015-09-15 13:00:45 +00:00
|
|
|
|
2021-03-02 15:10:57 +00:00
|
|
|
it "does not pick build with different tag" do
|
|
|
|
pending_job.update!(tag_list: ["linux"])
|
|
|
|
specific_runner.update!(tag_list: ["win32"])
|
|
|
|
expect(execute(specific_runner)).to be_falsey
|
|
|
|
end
|
2015-09-15 13:00:45 +00:00
|
|
|
|
2021-03-02 15:10:57 +00:00
|
|
|
it "picks build without tag" do
|
|
|
|
expect(execute(specific_runner)).to eq(pending_job)
|
|
|
|
end
|
2015-09-15 13:00:45 +00:00
|
|
|
|
2021-03-02 15:10:57 +00:00
|
|
|
it "does not pick build with tag" do
|
|
|
|
pending_job.update!(tag_list: ["linux"])
|
|
|
|
expect(execute(specific_runner)).to be_falsey
|
|
|
|
end
|
2015-08-26 01:42:46 +00:00
|
|
|
|
2021-03-02 15:10:57 +00:00
|
|
|
it "pick build without tag" do
|
|
|
|
specific_runner.update!(tag_list: ["win32"])
|
|
|
|
expect(execute(specific_runner)).to eq(pending_job)
|
|
|
|
end
|
2016-06-15 14:48:42 +00:00
|
|
|
end
|
|
|
|
|
2021-03-02 15:10:57 +00:00
|
|
|
context 'deleted projects' do
|
2016-06-15 14:48:42 +00:00
|
|
|
before do
|
2021-03-02 15:10:57 +00:00
|
|
|
project.update!(pending_delete: true)
|
2016-06-15 14:48:42 +00:00
|
|
|
end
|
|
|
|
|
2021-03-02 15:10:57 +00:00
|
|
|
context 'for shared runners' do
|
|
|
|
before do
|
|
|
|
project.update!(shared_runners_enabled: true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not pick a build' do
|
|
|
|
expect(execute(shared_runner)).to be_nil
|
|
|
|
end
|
2016-06-15 14:48:42 +00:00
|
|
|
end
|
|
|
|
|
2021-03-02 15:10:57 +00:00
|
|
|
context 'for specific runner' do
|
|
|
|
it 'does not pick a build' do
|
|
|
|
expect(execute(specific_runner)).to be_nil
|
|
|
|
end
|
2016-06-15 14:48:42 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-03-02 15:10:57 +00:00
|
|
|
context 'allow shared runners' do
|
|
|
|
before do
|
|
|
|
project.update!(shared_runners_enabled: true)
|
2021-08-10 12:11:00 +00:00
|
|
|
pipeline.reload
|
|
|
|
pending_job.reload
|
|
|
|
pending_job.create_queuing_entry!
|
2021-03-02 15:10:57 +00:00
|
|
|
end
|
2015-09-15 13:00:45 +00:00
|
|
|
|
2021-03-02 15:10:57 +00:00
|
|
|
context 'for multiple builds' do
|
|
|
|
let!(:project2) { create :project, shared_runners_enabled: true }
|
|
|
|
let!(:pipeline2) { create :ci_pipeline, project: project2 }
|
|
|
|
let!(:project3) { create :project, shared_runners_enabled: true }
|
|
|
|
let!(:pipeline3) { create :ci_pipeline, project: project3 }
|
|
|
|
let!(:build1_project1) { pending_job }
|
2021-06-08 09:09:56 +00:00
|
|
|
let!(:build2_project1) { create(:ci_build, :pending, :queued, pipeline: pipeline) }
|
|
|
|
let!(:build3_project1) { create(:ci_build, :pending, :queued, pipeline: pipeline) }
|
|
|
|
let!(:build1_project2) { create(:ci_build, :pending, :queued, pipeline: pipeline2) }
|
|
|
|
let!(:build2_project2) { create(:ci_build, :pending, :queued, pipeline: pipeline2) }
|
|
|
|
let!(:build1_project3) { create(:ci_build, :pending, :queued, pipeline: pipeline3) }
|
2021-03-02 15:10:57 +00:00
|
|
|
|
2021-05-06 15:10:17 +00:00
|
|
|
context 'when using fair scheduling' do
|
|
|
|
context 'when all builds are pending' do
|
|
|
|
it 'prefers projects without builds first' do
|
|
|
|
# it gets for one build from each of the projects
|
|
|
|
expect(execute(shared_runner)).to eq(build1_project1)
|
|
|
|
expect(execute(shared_runner)).to eq(build1_project2)
|
|
|
|
expect(execute(shared_runner)).to eq(build1_project3)
|
|
|
|
|
|
|
|
# then it gets a second build from each of the projects
|
|
|
|
expect(execute(shared_runner)).to eq(build2_project1)
|
|
|
|
expect(execute(shared_runner)).to eq(build2_project2)
|
|
|
|
|
|
|
|
# in the end the third build
|
|
|
|
expect(execute(shared_runner)).to eq(build3_project1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when some builds transition to success' do
|
|
|
|
it 'equalises number of running builds' do
|
|
|
|
# after finishing the first build for project 1, get a second build from the same project
|
|
|
|
expect(execute(shared_runner)).to eq(build1_project1)
|
|
|
|
build1_project1.reload.success
|
|
|
|
expect(execute(shared_runner)).to eq(build2_project1)
|
|
|
|
|
|
|
|
expect(execute(shared_runner)).to eq(build1_project2)
|
|
|
|
build1_project2.reload.success
|
|
|
|
expect(execute(shared_runner)).to eq(build2_project2)
|
|
|
|
expect(execute(shared_runner)).to eq(build1_project3)
|
|
|
|
expect(execute(shared_runner)).to eq(build3_project1)
|
|
|
|
end
|
|
|
|
end
|
2021-03-02 15:10:57 +00:00
|
|
|
end
|
|
|
|
|
2021-05-06 15:10:17 +00:00
|
|
|
context 'when using DEFCON mode that disables fair scheduling' do
|
|
|
|
before do
|
2021-07-01 15:07:40 +00:00
|
|
|
stub_feature_flags(ci_queueing_disaster_recovery_disable_fair_scheduling: true)
|
2021-05-06 15:10:17 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when all builds are pending' do
|
|
|
|
it 'returns builds in order of creation (FIFO)' do
|
|
|
|
# it gets for one build from each of the projects
|
|
|
|
expect(execute(shared_runner)).to eq(build1_project1)
|
|
|
|
expect(execute(shared_runner)).to eq(build2_project1)
|
|
|
|
expect(execute(shared_runner)).to eq(build3_project1)
|
|
|
|
expect(execute(shared_runner)).to eq(build1_project2)
|
|
|
|
expect(execute(shared_runner)).to eq(build2_project2)
|
|
|
|
expect(execute(shared_runner)).to eq(build1_project3)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when some builds transition to success' do
|
|
|
|
it 'returns builds in order of creation (FIFO)' do
|
|
|
|
expect(execute(shared_runner)).to eq(build1_project1)
|
|
|
|
build1_project1.reload.success
|
|
|
|
expect(execute(shared_runner)).to eq(build2_project1)
|
|
|
|
|
|
|
|
expect(execute(shared_runner)).to eq(build3_project1)
|
|
|
|
build2_project1.reload.success
|
|
|
|
expect(execute(shared_runner)).to eq(build1_project2)
|
|
|
|
expect(execute(shared_runner)).to eq(build2_project2)
|
|
|
|
expect(execute(shared_runner)).to eq(build1_project3)
|
|
|
|
end
|
|
|
|
end
|
2021-03-02 15:10:57 +00:00
|
|
|
end
|
2016-06-13 19:52:41 +00:00
|
|
|
end
|
|
|
|
|
2021-03-02 15:10:57 +00:00
|
|
|
context 'shared runner' do
|
|
|
|
let(:response) { described_class.new(shared_runner).execute }
|
|
|
|
let(:build) { response.build }
|
2015-09-15 13:00:45 +00:00
|
|
|
|
2021-03-02 15:10:57 +00:00
|
|
|
it { expect(build).to be_kind_of(Build) }
|
|
|
|
it { expect(build).to be_valid }
|
|
|
|
it { expect(build).to be_running }
|
|
|
|
it { expect(build.runner).to eq(shared_runner) }
|
|
|
|
it { expect(Gitlab::Json.parse(response.build_json)['id']).to eq(build.id) }
|
|
|
|
end
|
2015-09-15 13:00:45 +00:00
|
|
|
|
2021-03-02 15:10:57 +00:00
|
|
|
context 'specific runner' do
|
|
|
|
let(:build) { execute(specific_runner) }
|
2015-09-15 13:00:45 +00:00
|
|
|
|
2021-03-02 15:10:57 +00:00
|
|
|
it { expect(build).to be_kind_of(Build) }
|
|
|
|
it { expect(build).to be_valid }
|
|
|
|
it { expect(build).to be_running }
|
|
|
|
it { expect(build.runner).to eq(specific_runner) }
|
|
|
|
end
|
2015-09-15 13:00:45 +00:00
|
|
|
end
|
2015-08-26 01:42:46 +00:00
|
|
|
|
2021-03-02 15:10:57 +00:00
|
|
|
context 'disallow shared runners' do
|
|
|
|
before do
|
|
|
|
project.update!(shared_runners_enabled: false)
|
|
|
|
end
|
2015-11-05 15:22:37 +00:00
|
|
|
|
2021-03-02 15:10:57 +00:00
|
|
|
context 'shared runner' do
|
|
|
|
let(:build) { execute(shared_runner) }
|
2015-08-26 01:42:46 +00:00
|
|
|
|
2021-03-02 15:10:57 +00:00
|
|
|
it { expect(build).to be_nil }
|
|
|
|
end
|
2015-08-26 01:42:46 +00:00
|
|
|
|
2021-03-02 15:10:57 +00:00
|
|
|
context 'specific runner' do
|
|
|
|
let(:build) { execute(specific_runner) }
|
2015-08-26 01:42:46 +00:00
|
|
|
|
2021-03-02 15:10:57 +00:00
|
|
|
it { expect(build).to be_kind_of(Build) }
|
|
|
|
it { expect(build).to be_valid }
|
|
|
|
it { expect(build).to be_running }
|
|
|
|
it { expect(build.runner).to eq(specific_runner) }
|
|
|
|
end
|
2015-09-15 13:00:45 +00:00
|
|
|
end
|
2016-08-01 22:31:21 +00:00
|
|
|
|
2021-03-02 15:10:57 +00:00
|
|
|
context 'disallow when builds are disabled' do
|
|
|
|
before do
|
|
|
|
project.update!(shared_runners_enabled: true, group_runners_enabled: true)
|
|
|
|
project.project_feature.update_attribute(:builds_access_level, ProjectFeature::DISABLED)
|
|
|
|
end
|
2016-08-01 22:31:21 +00:00
|
|
|
|
2021-03-02 15:10:57 +00:00
|
|
|
context 'and uses shared runner' do
|
|
|
|
let(:build) { execute(shared_runner) }
|
2016-08-01 22:31:21 +00:00
|
|
|
|
2021-03-02 15:10:57 +00:00
|
|
|
it { expect(build).to be_nil }
|
|
|
|
end
|
2016-08-01 22:31:21 +00:00
|
|
|
|
2021-03-02 15:10:57 +00:00
|
|
|
context 'and uses group runner' do
|
|
|
|
let(:build) { execute(group_runner) }
|
2018-05-03 12:37:01 +00:00
|
|
|
|
2021-03-02 15:10:57 +00:00
|
|
|
it { expect(build).to be_nil }
|
|
|
|
end
|
2018-05-03 12:37:01 +00:00
|
|
|
|
2021-03-02 15:10:57 +00:00
|
|
|
context 'and uses project runner' do
|
|
|
|
let(:build) { execute(specific_runner) }
|
2016-08-01 22:31:21 +00:00
|
|
|
|
2021-03-02 15:10:57 +00:00
|
|
|
it { expect(build).to be_nil }
|
|
|
|
end
|
2016-08-01 22:31:21 +00:00
|
|
|
end
|
2017-01-20 11:25:53 +00:00
|
|
|
|
2021-03-02 15:10:57 +00:00
|
|
|
context 'allow group runners' do
|
|
|
|
before do
|
|
|
|
project.update!(group_runners_enabled: true)
|
|
|
|
end
|
2017-10-05 11:29:21 +00:00
|
|
|
|
2021-03-02 15:10:57 +00:00
|
|
|
context 'for multiple builds' do
|
|
|
|
let!(:project2) { create(:project, group_runners_enabled: true, group: group) }
|
|
|
|
let!(:pipeline2) { create(:ci_pipeline, project: project2) }
|
|
|
|
let!(:project3) { create(:project, group_runners_enabled: true, group: group) }
|
|
|
|
let!(:pipeline3) { create(:ci_pipeline, project: project3) }
|
2017-12-19 14:12:21 +00:00
|
|
|
|
2021-03-02 15:10:57 +00:00
|
|
|
let!(:build1_project1) { pending_job }
|
2021-06-08 09:09:56 +00:00
|
|
|
let!(:build2_project1) { create(:ci_build, :queued, pipeline: pipeline) }
|
|
|
|
let!(:build3_project1) { create(:ci_build, :queued, pipeline: pipeline) }
|
|
|
|
let!(:build1_project2) { create(:ci_build, :queued, pipeline: pipeline2) }
|
|
|
|
let!(:build2_project2) { create(:ci_build, :queued, pipeline: pipeline2) }
|
|
|
|
let!(:build1_project3) { create(:ci_build, :queued, pipeline: pipeline3) }
|
2017-10-05 11:29:21 +00:00
|
|
|
|
2021-03-02 15:10:57 +00:00
|
|
|
# these shouldn't influence the scheduling
|
|
|
|
let!(:unrelated_group) { create(:group) }
|
|
|
|
let!(:unrelated_project) { create(:project, group_runners_enabled: true, group: unrelated_group) }
|
|
|
|
let!(:unrelated_pipeline) { create(:ci_pipeline, project: unrelated_project) }
|
2021-06-08 09:09:56 +00:00
|
|
|
let!(:build1_unrelated_project) { create(:ci_build, :pending, :queued, pipeline: unrelated_pipeline) }
|
2021-03-02 15:10:57 +00:00
|
|
|
let!(:unrelated_group_runner) { create(:ci_runner, :group, groups: [unrelated_group]) }
|
2017-12-19 14:12:21 +00:00
|
|
|
|
2021-03-02 15:10:57 +00:00
|
|
|
it 'does not consider builds from other group runners' do
|
2021-06-22 12:08:05 +00:00
|
|
|
queue = ::Ci::Queue::BuildQueueService.new(group_runner)
|
|
|
|
|
|
|
|
expect(queue.builds_for_group_runner.size).to eq 6
|
2021-03-02 15:10:57 +00:00
|
|
|
execute(group_runner)
|
2017-12-19 14:12:21 +00:00
|
|
|
|
2021-06-22 12:08:05 +00:00
|
|
|
expect(queue.builds_for_group_runner.size).to eq 5
|
2021-03-02 15:10:57 +00:00
|
|
|
execute(group_runner)
|
2017-12-19 14:12:21 +00:00
|
|
|
|
2021-06-22 12:08:05 +00:00
|
|
|
expect(queue.builds_for_group_runner.size).to eq 4
|
2021-03-02 15:10:57 +00:00
|
|
|
execute(group_runner)
|
2017-12-19 14:12:21 +00:00
|
|
|
|
2021-06-22 12:08:05 +00:00
|
|
|
expect(queue.builds_for_group_runner.size).to eq 3
|
2021-03-02 15:10:57 +00:00
|
|
|
execute(group_runner)
|
2017-12-19 14:12:21 +00:00
|
|
|
|
2021-06-22 12:08:05 +00:00
|
|
|
expect(queue.builds_for_group_runner.size).to eq 2
|
2021-03-02 15:10:57 +00:00
|
|
|
execute(group_runner)
|
2017-12-19 14:12:21 +00:00
|
|
|
|
2021-06-22 12:08:05 +00:00
|
|
|
expect(queue.builds_for_group_runner.size).to eq 1
|
2021-03-02 15:10:57 +00:00
|
|
|
execute(group_runner)
|
2017-12-19 14:12:21 +00:00
|
|
|
|
2021-06-22 12:08:05 +00:00
|
|
|
expect(queue.builds_for_group_runner.size).to eq 0
|
2021-03-02 15:10:57 +00:00
|
|
|
expect(execute(group_runner)).to be_nil
|
|
|
|
end
|
2017-10-05 11:29:21 +00:00
|
|
|
end
|
|
|
|
|
2021-03-02 15:10:57 +00:00
|
|
|
context 'group runner' do
|
|
|
|
let(:build) { execute(group_runner) }
|
2017-10-05 11:29:21 +00:00
|
|
|
|
2021-03-02 15:10:57 +00:00
|
|
|
it { expect(build).to be_kind_of(Build) }
|
|
|
|
it { expect(build).to be_valid }
|
|
|
|
it { expect(build).to be_running }
|
|
|
|
it { expect(build.runner).to eq(group_runner) }
|
|
|
|
end
|
2017-10-05 11:29:21 +00:00
|
|
|
end
|
|
|
|
|
2021-03-02 15:10:57 +00:00
|
|
|
context 'disallow group runners' do
|
|
|
|
before do
|
|
|
|
project.update!(group_runners_enabled: false)
|
|
|
|
end
|
2017-10-05 11:29:21 +00:00
|
|
|
|
2021-03-02 15:10:57 +00:00
|
|
|
context 'group runner' do
|
|
|
|
let(:build) { execute(group_runner) }
|
2017-10-05 11:29:21 +00:00
|
|
|
|
2021-03-02 15:10:57 +00:00
|
|
|
it { expect(build).to be_nil }
|
|
|
|
end
|
2017-10-05 11:29:21 +00:00
|
|
|
end
|
|
|
|
|
2021-03-02 15:10:57 +00:00
|
|
|
context 'when first build is stalled' do
|
|
|
|
before do
|
|
|
|
allow_any_instance_of(Ci::RegisterJobService).to receive(:assign_runner!).and_call_original
|
|
|
|
allow_any_instance_of(Ci::RegisterJobService).to receive(:assign_runner!)
|
|
|
|
.with(pending_job, anything).and_raise(ActiveRecord::StaleObjectError)
|
|
|
|
end
|
2017-02-28 18:23:35 +00:00
|
|
|
|
2021-03-02 15:10:57 +00:00
|
|
|
subject { described_class.new(specific_runner).execute }
|
2017-02-28 18:23:35 +00:00
|
|
|
|
2021-03-02 15:10:57 +00:00
|
|
|
context 'with multiple builds are in queue' do
|
2021-06-08 09:09:56 +00:00
|
|
|
let!(:other_build) { create(:ci_build, :pending, :queued, pipeline: pipeline) }
|
2017-02-28 18:23:35 +00:00
|
|
|
|
2021-03-02 15:10:57 +00:00
|
|
|
before do
|
2021-06-22 12:08:05 +00:00
|
|
|
allow_any_instance_of(::Ci::Queue::BuildQueueService)
|
|
|
|
.to receive(:execute)
|
|
|
|
.and_return(Ci::Build.where(id: [pending_job, other_build]).pluck(:id))
|
2021-03-02 15:10:57 +00:00
|
|
|
end
|
2017-02-28 18:23:35 +00:00
|
|
|
|
2021-03-02 15:10:57 +00:00
|
|
|
it "receives second build from the queue" do
|
|
|
|
expect(subject).to be_valid
|
|
|
|
expect(subject.build).to eq(other_build)
|
|
|
|
end
|
2017-02-28 18:23:35 +00:00
|
|
|
end
|
|
|
|
|
2021-03-02 15:10:57 +00:00
|
|
|
context 'when single build is in queue' do
|
|
|
|
before do
|
2021-06-22 12:08:05 +00:00
|
|
|
allow_any_instance_of(::Ci::Queue::BuildQueueService)
|
|
|
|
.to receive(:execute)
|
|
|
|
.and_return(Ci::Build.where(id: pending_job).pluck(:id))
|
2021-03-02 15:10:57 +00:00
|
|
|
end
|
2017-02-28 18:23:35 +00:00
|
|
|
|
2021-03-02 15:10:57 +00:00
|
|
|
it "does not receive any valid result" do
|
|
|
|
expect(subject).not_to be_valid
|
|
|
|
end
|
2017-02-28 18:23:35 +00:00
|
|
|
end
|
|
|
|
|
2021-03-02 15:10:57 +00:00
|
|
|
context 'when there is no build in queue' do
|
|
|
|
before do
|
2021-06-22 12:08:05 +00:00
|
|
|
allow_any_instance_of(::Ci::Queue::BuildQueueService)
|
|
|
|
.to receive(:execute)
|
|
|
|
.and_return([])
|
2021-03-02 15:10:57 +00:00
|
|
|
end
|
2017-02-28 18:23:35 +00:00
|
|
|
|
2021-03-02 15:10:57 +00:00
|
|
|
it "does not receive builds but result is valid" do
|
|
|
|
expect(subject).to be_valid
|
|
|
|
expect(subject.build).to be_nil
|
|
|
|
end
|
2017-02-28 18:23:35 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-03-02 15:10:57 +00:00
|
|
|
context 'when access_level of runner is not_protected' do
|
|
|
|
let!(:specific_runner) { create(:ci_runner, :project, projects: [project]) }
|
2017-08-31 09:28:29 +00:00
|
|
|
|
2021-03-02 15:10:57 +00:00
|
|
|
context 'when a job is protected' do
|
2021-06-08 09:09:56 +00:00
|
|
|
let!(:pending_job) { create(:ci_build, :pending, :queued, :protected, pipeline: pipeline) }
|
2017-08-22 08:01:11 +00:00
|
|
|
|
2021-03-02 15:10:57 +00:00
|
|
|
it 'picks the job' do
|
|
|
|
expect(execute(specific_runner)).to eq(pending_job)
|
|
|
|
end
|
2017-08-22 08:01:11 +00:00
|
|
|
end
|
|
|
|
|
2021-03-02 15:10:57 +00:00
|
|
|
context 'when a job is unprotected' do
|
2021-06-08 09:09:56 +00:00
|
|
|
let!(:pending_job) { create(:ci_build, :pending, :queued, pipeline: pipeline) }
|
2017-08-22 08:01:11 +00:00
|
|
|
|
2021-03-02 15:10:57 +00:00
|
|
|
it 'picks the job' do
|
|
|
|
expect(execute(specific_runner)).to eq(pending_job)
|
|
|
|
end
|
2017-09-02 07:31:14 +00:00
|
|
|
end
|
|
|
|
|
2021-03-02 15:10:57 +00:00
|
|
|
context 'when protected attribute of a job is nil' do
|
2021-06-08 09:09:56 +00:00
|
|
|
let!(:pending_job) { create(:ci_build, :pending, :queued, pipeline: pipeline) }
|
2017-09-02 07:31:14 +00:00
|
|
|
|
2021-03-02 15:10:57 +00:00
|
|
|
before do
|
|
|
|
pending_job.update_attribute(:protected, nil)
|
|
|
|
end
|
2017-09-02 07:31:14 +00:00
|
|
|
|
2021-03-02 15:10:57 +00:00
|
|
|
it 'picks the job' do
|
|
|
|
expect(execute(specific_runner)).to eq(pending_job)
|
|
|
|
end
|
2017-08-22 08:01:11 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-03-02 15:10:57 +00:00
|
|
|
context 'when access_level of runner is ref_protected' do
|
|
|
|
let!(:specific_runner) { create(:ci_runner, :project, :ref_protected, projects: [project]) }
|
2017-08-22 08:01:11 +00:00
|
|
|
|
2021-03-02 15:10:57 +00:00
|
|
|
context 'when a job is protected' do
|
2021-06-08 09:09:56 +00:00
|
|
|
let!(:pending_job) { create(:ci_build, :pending, :queued, :protected, pipeline: pipeline) }
|
2017-08-22 08:01:11 +00:00
|
|
|
|
2021-03-02 15:10:57 +00:00
|
|
|
it 'picks the job' do
|
|
|
|
expect(execute(specific_runner)).to eq(pending_job)
|
|
|
|
end
|
2017-08-22 08:01:11 +00:00
|
|
|
end
|
|
|
|
|
2021-03-02 15:10:57 +00:00
|
|
|
context 'when a job is unprotected' do
|
2021-06-08 09:09:56 +00:00
|
|
|
let!(:pending_job) { create(:ci_build, :pending, :queued, pipeline: pipeline) }
|
2017-08-22 08:01:11 +00:00
|
|
|
|
2021-03-02 15:10:57 +00:00
|
|
|
it 'does not pick the job' do
|
|
|
|
expect(execute(specific_runner)).to be_nil
|
|
|
|
end
|
2017-08-22 08:01:11 +00:00
|
|
|
end
|
2017-09-02 07:31:14 +00:00
|
|
|
|
2021-03-02 15:10:57 +00:00
|
|
|
context 'when protected attribute of a job is nil' do
|
2021-06-08 09:09:56 +00:00
|
|
|
let!(:pending_job) { create(:ci_build, :pending, :queued, pipeline: pipeline) }
|
2017-09-02 07:31:14 +00:00
|
|
|
|
2021-03-02 15:10:57 +00:00
|
|
|
before do
|
|
|
|
pending_job.update_attribute(:protected, nil)
|
|
|
|
end
|
2017-09-02 07:31:14 +00:00
|
|
|
|
2021-03-02 15:10:57 +00:00
|
|
|
it 'does not pick the job' do
|
|
|
|
expect(execute(specific_runner)).to be_nil
|
|
|
|
end
|
2017-09-02 07:31:14 +00:00
|
|
|
end
|
|
|
|
end
|
2017-08-22 08:01:11 +00:00
|
|
|
|
2021-03-02 15:10:57 +00:00
|
|
|
context 'runner feature set is verified' do
|
|
|
|
let(:options) { { artifacts: { reports: { junit: "junit.xml" } } } }
|
2021-06-08 09:09:56 +00:00
|
|
|
let!(:pending_job) { create(:ci_build, :pending, :queued, pipeline: pipeline, options: options) }
|
2018-08-01 10:23:43 +00:00
|
|
|
|
2021-03-02 15:10:57 +00:00
|
|
|
subject { execute(specific_runner, params) }
|
2018-08-01 10:23:43 +00:00
|
|
|
|
2021-03-02 15:10:57 +00:00
|
|
|
context 'when feature is missing by runner' do
|
|
|
|
let(:params) { {} }
|
2018-08-01 12:03:12 +00:00
|
|
|
|
2021-03-02 15:10:57 +00:00
|
|
|
it 'does not pick the build and drops the build' do
|
|
|
|
expect(subject).to be_nil
|
|
|
|
expect(pending_job.reload).to be_failed
|
|
|
|
expect(pending_job).to be_runner_unsupported
|
|
|
|
end
|
2018-08-01 10:23:43 +00:00
|
|
|
end
|
|
|
|
|
2021-03-02 15:10:57 +00:00
|
|
|
context 'when feature is supported by runner' do
|
|
|
|
let(:params) do
|
|
|
|
{ info: { features: { upload_multiple_artifacts: true } } }
|
|
|
|
end
|
2018-08-01 10:23:43 +00:00
|
|
|
|
2021-03-02 15:10:57 +00:00
|
|
|
it 'does pick job' do
|
|
|
|
expect(subject).not_to be_nil
|
|
|
|
end
|
2018-08-01 10:23:43 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-03-02 15:10:57 +00:00
|
|
|
context 'when "dependencies" keyword is specified' do
|
|
|
|
shared_examples 'not pick' do
|
|
|
|
it 'does not pick the build and drops the build' do
|
|
|
|
expect(subject).to be_nil
|
|
|
|
expect(pending_job.reload).to be_failed
|
|
|
|
expect(pending_job).to be_missing_dependency_failure
|
|
|
|
end
|
2017-12-07 17:21:16 +00:00
|
|
|
end
|
2017-12-06 07:40:56 +00:00
|
|
|
|
2021-03-02 15:10:57 +00:00
|
|
|
shared_examples 'validation is active' do
|
|
|
|
context 'when depended job has not been completed yet' do
|
2021-06-08 09:09:56 +00:00
|
|
|
let!(:pre_stage_job) { create(:ci_build, :pending, :queued, :manual, pipeline: pipeline, name: 'test', stage_idx: 0) }
|
2017-12-07 17:21:16 +00:00
|
|
|
|
2021-08-12 15:09:58 +00:00
|
|
|
it { is_expected.to eq(pending_job) }
|
2021-03-02 15:10:57 +00:00
|
|
|
end
|
2017-12-07 17:21:16 +00:00
|
|
|
|
2021-03-02 15:10:57 +00:00
|
|
|
context 'when artifacts of depended job has been expired' do
|
|
|
|
let!(:pre_stage_job) { create(:ci_build, :success, :expired, pipeline: pipeline, name: 'test', stage_idx: 0) }
|
2017-12-07 17:21:16 +00:00
|
|
|
|
2021-08-12 15:09:58 +00:00
|
|
|
context 'when the pipeline is locked' do
|
|
|
|
before do
|
|
|
|
pipeline.artifacts_locked!
|
|
|
|
end
|
|
|
|
|
|
|
|
it { is_expected.to eq(pending_job) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the pipeline is unlocked' do
|
|
|
|
before do
|
|
|
|
pipeline.unlocked!
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'not pick'
|
|
|
|
end
|
2021-03-02 15:10:57 +00:00
|
|
|
end
|
2017-12-07 17:21:16 +00:00
|
|
|
|
2021-03-02 15:10:57 +00:00
|
|
|
context 'when artifacts of depended job has been erased' do
|
|
|
|
let!(:pre_stage_job) { create(:ci_build, :success, pipeline: pipeline, name: 'test', stage_idx: 0, erased_at: 1.minute.ago) }
|
2017-12-07 17:21:16 +00:00
|
|
|
|
2021-03-02 15:10:57 +00:00
|
|
|
before do
|
|
|
|
pre_stage_job.erase
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'not pick'
|
2017-12-07 17:21:16 +00:00
|
|
|
end
|
2017-09-03 14:35:37 +00:00
|
|
|
|
2021-03-02 15:10:57 +00:00
|
|
|
context 'when job object is staled' do
|
|
|
|
let!(:pre_stage_job) { create(:ci_build, :success, :expired, pipeline: pipeline, name: 'test', stage_idx: 0) }
|
|
|
|
|
|
|
|
before do
|
2021-08-12 15:09:58 +00:00
|
|
|
pipeline.unlocked!
|
|
|
|
|
|
|
|
allow_next_instance_of(Ci::Build) do |build|
|
|
|
|
expect(build).to receive(:drop!)
|
|
|
|
.and_raise(ActiveRecord::StaleObjectError.new(pending_job, :drop!))
|
|
|
|
end
|
2021-03-02 15:10:57 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not drop nor pick' do
|
|
|
|
expect(subject).to be_nil
|
|
|
|
end
|
|
|
|
end
|
2017-12-07 17:21:16 +00:00
|
|
|
end
|
2017-12-12 07:17:45 +00:00
|
|
|
|
2021-03-02 15:10:57 +00:00
|
|
|
shared_examples 'validation is not active' do
|
|
|
|
context 'when depended job has not been completed yet' do
|
2021-06-08 09:09:56 +00:00
|
|
|
let!(:pre_stage_job) { create(:ci_build, :pending, :queued, :manual, pipeline: pipeline, name: 'test', stage_idx: 0) }
|
2017-12-12 07:17:45 +00:00
|
|
|
|
2021-03-02 15:10:57 +00:00
|
|
|
it { expect(subject).to eq(pending_job) }
|
2017-12-12 07:17:45 +00:00
|
|
|
end
|
|
|
|
|
2021-03-02 15:10:57 +00:00
|
|
|
context 'when artifacts of depended job has been expired' do
|
|
|
|
let!(:pre_stage_job) { create(:ci_build, :success, :expired, pipeline: pipeline, name: 'test', stage_idx: 0) }
|
|
|
|
|
|
|
|
it { expect(subject).to eq(pending_job) }
|
2017-12-12 07:17:45 +00:00
|
|
|
end
|
2017-09-03 14:35:37 +00:00
|
|
|
|
2021-03-02 15:10:57 +00:00
|
|
|
context 'when artifacts of depended job has been erased' do
|
|
|
|
let!(:pre_stage_job) { create(:ci_build, :success, pipeline: pipeline, name: 'test', stage_idx: 0, erased_at: 1.minute.ago) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
pre_stage_job.erase
|
|
|
|
end
|
2017-09-03 14:35:37 +00:00
|
|
|
|
2021-03-02 15:10:57 +00:00
|
|
|
it { expect(subject).to eq(pending_job) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
let!(:pre_stage_job) { create(:ci_build, :success, pipeline: pipeline, name: 'test', stage_idx: 0) }
|
2017-09-03 14:35:37 +00:00
|
|
|
|
2021-03-02 15:10:57 +00:00
|
|
|
let!(:pending_job) do
|
2021-06-08 09:09:56 +00:00
|
|
|
create(:ci_build, :pending, :queued,
|
2021-03-02 15:10:57 +00:00
|
|
|
pipeline: pipeline, stage_idx: 1,
|
|
|
|
options: { script: ["bash"], dependencies: ['test'] })
|
2017-09-03 14:35:37 +00:00
|
|
|
end
|
2017-09-05 13:37:28 +00:00
|
|
|
|
2021-03-02 15:10:57 +00:00
|
|
|
subject { execute(specific_runner) }
|
2017-09-05 13:37:28 +00:00
|
|
|
|
2021-05-07 15:10:39 +00:00
|
|
|
it_behaves_like 'validation is active'
|
2017-12-07 17:21:16 +00:00
|
|
|
end
|
|
|
|
|
2021-03-02 15:10:57 +00:00
|
|
|
context 'when build is degenerated' do
|
2021-06-08 09:09:56 +00:00
|
|
|
let!(:pending_job) { create(:ci_build, :pending, :queued, :degenerated, pipeline: pipeline) }
|
2021-03-02 15:10:57 +00:00
|
|
|
|
|
|
|
subject { execute(specific_runner, {}) }
|
2018-09-02 14:35:15 +00:00
|
|
|
|
2021-03-02 15:10:57 +00:00
|
|
|
it 'does not pick the build and drops the build' do
|
|
|
|
expect(subject).to be_nil
|
|
|
|
|
|
|
|
pending_job.reload
|
|
|
|
expect(pending_job).to be_failed
|
|
|
|
expect(pending_job).to be_archived_failure
|
|
|
|
end
|
2018-09-02 14:35:15 +00:00
|
|
|
end
|
2017-12-07 17:21:16 +00:00
|
|
|
|
2021-03-02 15:10:57 +00:00
|
|
|
context 'when build has data integrity problem' do
|
|
|
|
let!(:pending_job) do
|
2021-06-08 09:09:56 +00:00
|
|
|
create(:ci_build, :pending, :queued, pipeline: pipeline)
|
2021-03-02 15:10:57 +00:00
|
|
|
end
|
2017-12-07 17:21:16 +00:00
|
|
|
|
|
|
|
before do
|
2021-03-02 15:10:57 +00:00
|
|
|
pending_job.update_columns(options: "string")
|
2017-09-05 13:37:28 +00:00
|
|
|
end
|
2017-12-07 17:21:16 +00:00
|
|
|
|
2021-03-02 15:10:57 +00:00
|
|
|
subject { execute(specific_runner, {}) }
|
2021-02-09 18:09:59 +00:00
|
|
|
|
2021-03-02 15:10:57 +00:00
|
|
|
it 'does drop the build and logs both failures' do
|
|
|
|
expect(Gitlab::ErrorTracking).to receive(:track_exception)
|
|
|
|
.with(anything, a_hash_including(build_id: pending_job.id))
|
|
|
|
.twice
|
|
|
|
.and_call_original
|
2021-02-09 18:09:59 +00:00
|
|
|
|
2021-03-02 15:10:57 +00:00
|
|
|
expect(subject).to be_nil
|
2021-02-09 18:09:59 +00:00
|
|
|
|
2021-03-02 15:10:57 +00:00
|
|
|
pending_job.reload
|
|
|
|
expect(pending_job).to be_failed
|
|
|
|
expect(pending_job).to be_data_integrity_failure
|
2021-02-09 18:09:59 +00:00
|
|
|
end
|
2017-12-07 17:21:16 +00:00
|
|
|
end
|
|
|
|
|
2021-03-02 15:10:57 +00:00
|
|
|
context 'when build fails to be run!' do
|
|
|
|
let!(:pending_job) do
|
2021-06-08 09:09:56 +00:00
|
|
|
create(:ci_build, :pending, :queued, pipeline: pipeline)
|
2021-03-02 15:10:57 +00:00
|
|
|
end
|
|
|
|
|
2017-12-07 17:21:16 +00:00
|
|
|
before do
|
2021-03-02 15:10:57 +00:00
|
|
|
expect_any_instance_of(Ci::Build).to receive(:run!)
|
|
|
|
.and_raise(RuntimeError, 'scheduler error')
|
2017-12-07 17:21:16 +00:00
|
|
|
end
|
|
|
|
|
2021-03-02 15:10:57 +00:00
|
|
|
subject { execute(specific_runner, {}) }
|
|
|
|
|
|
|
|
it 'does drop the build and logs failure' do
|
|
|
|
expect(Gitlab::ErrorTracking).to receive(:track_exception)
|
|
|
|
.with(anything, a_hash_including(build_id: pending_job.id))
|
|
|
|
.once
|
|
|
|
.and_call_original
|
|
|
|
|
|
|
|
expect(subject).to be_nil
|
|
|
|
|
|
|
|
pending_job.reload
|
|
|
|
expect(pending_job).to be_failed
|
|
|
|
expect(pending_job).to be_scheduler_failure
|
|
|
|
end
|
2017-09-03 14:35:37 +00:00
|
|
|
end
|
2018-10-23 10:58:41 +00:00
|
|
|
|
2021-03-02 15:10:57 +00:00
|
|
|
context 'when an exception is raised during a persistent ref creation' do
|
|
|
|
before do
|
|
|
|
allow_any_instance_of(Ci::PersistentRef).to receive(:exist?) { false }
|
|
|
|
allow_any_instance_of(Ci::PersistentRef).to receive(:create_ref) { raise ArgumentError }
|
|
|
|
end
|
2018-10-23 10:58:41 +00:00
|
|
|
|
2021-03-02 15:10:57 +00:00
|
|
|
subject { execute(specific_runner, {}) }
|
2018-10-23 10:58:41 +00:00
|
|
|
|
2021-03-02 15:10:57 +00:00
|
|
|
it 'picks the build' do
|
|
|
|
expect(subject).to eq(pending_job)
|
2018-10-23 10:58:41 +00:00
|
|
|
|
2021-03-02 15:10:57 +00:00
|
|
|
pending_job.reload
|
|
|
|
expect(pending_job).to be_running
|
|
|
|
end
|
2018-10-23 10:58:41 +00:00
|
|
|
end
|
2019-09-27 18:06:20 +00:00
|
|
|
|
2021-03-02 15:10:57 +00:00
|
|
|
context 'when only some builds can be matched by runner' do
|
|
|
|
let!(:specific_runner) { create(:ci_runner, :project, projects: [project], tag_list: %w[matching]) }
|
2021-06-08 09:09:56 +00:00
|
|
|
let!(:pending_job) { create(:ci_build, :pending, :queued, pipeline: pipeline, tag_list: %w[matching]) }
|
2019-10-30 21:07:58 +00:00
|
|
|
|
2021-03-02 15:10:57 +00:00
|
|
|
before do
|
|
|
|
# create additional matching and non-matching jobs
|
2021-06-08 09:09:56 +00:00
|
|
|
create_list(:ci_build, 2, :pending, :queued, pipeline: pipeline, tag_list: %w[matching])
|
|
|
|
create(:ci_build, :pending, :queued, pipeline: pipeline, tag_list: %w[non-matching])
|
2021-03-02 15:10:57 +00:00
|
|
|
end
|
|
|
|
|
2021-03-30 18:10:47 +00:00
|
|
|
it 'observes queue size of only matching jobs' do
|
2021-03-02 15:10:57 +00:00
|
|
|
# pending_job + 2 x matching ones
|
2021-03-23 15:09:28 +00:00
|
|
|
expect(Gitlab::Ci::Queue::Metrics.queue_size_total).to receive(:observe)
|
|
|
|
.with({ runner_type: specific_runner.runner_type }, 3)
|
2021-03-02 15:10:57 +00:00
|
|
|
|
|
|
|
expect(execute(specific_runner)).to eq(pending_job)
|
|
|
|
end
|
2021-03-30 18:10:47 +00:00
|
|
|
|
|
|
|
it 'observes queue processing time by the runner type' do
|
|
|
|
expect(Gitlab::Ci::Queue::Metrics.queue_iteration_duration_seconds)
|
|
|
|
.to receive(:observe)
|
|
|
|
.with({ runner_type: specific_runner.runner_type }, anything)
|
|
|
|
|
|
|
|
expect(Gitlab::Ci::Queue::Metrics.queue_retrieval_duration_seconds)
|
|
|
|
.to receive(:observe)
|
|
|
|
.with({ runner_type: specific_runner.runner_type }, anything)
|
|
|
|
|
|
|
|
expect(execute(specific_runner)).to eq(pending_job)
|
|
|
|
end
|
2019-10-30 21:07:58 +00:00
|
|
|
end
|
|
|
|
|
2021-03-02 15:10:57 +00:00
|
|
|
context 'when ci_register_job_temporary_lock is enabled' do
|
|
|
|
before do
|
|
|
|
stub_feature_flags(ci_register_job_temporary_lock: true)
|
2019-10-30 21:07:58 +00:00
|
|
|
|
2021-03-02 15:10:57 +00:00
|
|
|
allow(Gitlab::Ci::Queue::Metrics.queue_operations_total).to receive(:increment)
|
|
|
|
end
|
2019-10-30 21:07:58 +00:00
|
|
|
|
2021-03-02 15:10:57 +00:00
|
|
|
context 'when a build is temporarily locked' do
|
|
|
|
let(:service) { described_class.new(specific_runner) }
|
2019-10-30 21:07:58 +00:00
|
|
|
|
2021-03-02 15:10:57 +00:00
|
|
|
before do
|
|
|
|
service.send(:acquire_temporary_lock, pending_job.id)
|
|
|
|
end
|
2019-10-30 21:07:58 +00:00
|
|
|
|
2021-03-02 15:10:57 +00:00
|
|
|
it 'skips this build and marks queue as invalid' do
|
|
|
|
expect(Gitlab::Ci::Queue::Metrics.queue_operations_total).to receive(:increment)
|
|
|
|
.with(operation: :queue_iteration)
|
|
|
|
expect(Gitlab::Ci::Queue::Metrics.queue_operations_total).to receive(:increment)
|
|
|
|
.with(operation: :build_temporary_locked)
|
2019-10-30 21:07:58 +00:00
|
|
|
|
2021-03-02 15:10:57 +00:00
|
|
|
expect(service.execute).not_to be_valid
|
|
|
|
end
|
2019-10-30 21:07:58 +00:00
|
|
|
|
2021-03-02 15:10:57 +00:00
|
|
|
context 'when there is another build in queue' do
|
2021-06-08 09:09:56 +00:00
|
|
|
let!(:next_pending_job) { create(:ci_build, :pending, :queued, pipeline: pipeline) }
|
2019-10-30 21:07:58 +00:00
|
|
|
|
2021-03-02 15:10:57 +00:00
|
|
|
it 'skips this build and picks another build' do
|
|
|
|
expect(Gitlab::Ci::Queue::Metrics.queue_operations_total).to receive(:increment)
|
|
|
|
.with(operation: :queue_iteration).twice
|
|
|
|
expect(Gitlab::Ci::Queue::Metrics.queue_operations_total).to receive(:increment)
|
|
|
|
.with(operation: :build_temporary_locked)
|
2019-10-30 21:07:58 +00:00
|
|
|
|
2021-03-02 15:10:57 +00:00
|
|
|
result = service.execute
|
2019-10-30 21:07:58 +00:00
|
|
|
|
2021-03-02 15:10:57 +00:00
|
|
|
expect(result.build).to eq(next_pending_job)
|
|
|
|
expect(result).to be_valid
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-10-30 21:07:58 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-06-16 09:10:10 +00:00
|
|
|
context 'when a long queue is created' do
|
2021-03-02 15:10:57 +00:00
|
|
|
it 'picks builds one-by-one' do
|
|
|
|
expect(Ci::Build).to receive(:find).with(pending_job.id).and_call_original
|
2019-09-27 18:06:20 +00:00
|
|
|
|
2021-03-02 15:10:57 +00:00
|
|
|
expect(execute(specific_runner)).to eq(pending_job)
|
|
|
|
end
|
|
|
|
|
|
|
|
include_examples 'handles runner assignment'
|
|
|
|
end
|
2019-09-27 18:06:20 +00:00
|
|
|
|
2021-06-22 12:08:05 +00:00
|
|
|
context 'when using pending builds table' do
|
2021-06-08 09:09:56 +00:00
|
|
|
before do
|
2021-06-22 12:08:05 +00:00
|
|
|
stub_feature_flags(ci_pending_builds_queue_source: true)
|
2021-06-08 09:09:56 +00:00
|
|
|
end
|
|
|
|
|
2021-08-10 12:11:00 +00:00
|
|
|
context 'with ci_queueing_denormalize_shared_runners_information enabled' do
|
|
|
|
before do
|
|
|
|
stub_feature_flags(ci_queueing_denormalize_shared_runners_information: true)
|
|
|
|
end
|
|
|
|
|
|
|
|
include_examples 'handles runner assignment'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with ci_queueing_denormalize_shared_runners_information disabled' do
|
|
|
|
before do
|
|
|
|
stub_feature_flags(ci_queueing_denormalize_shared_runners_information: false)
|
|
|
|
end
|
2021-08-20 12:09:31 +00:00
|
|
|
|
|
|
|
include_examples 'handles runner assignment'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with ci_queueing_denormalize_tags_information enabled' do
|
|
|
|
before do
|
|
|
|
stub_feature_flags(ci_queueing_denormalize_tags_information: true)
|
|
|
|
end
|
|
|
|
|
|
|
|
include_examples 'handles runner assignment'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with ci_queueing_denormalize_tags_information disabled' do
|
|
|
|
before do
|
|
|
|
stub_feature_flags(ci_queueing_denormalize_tags_information: false)
|
|
|
|
end
|
2021-08-10 12:11:00 +00:00
|
|
|
|
|
|
|
include_examples 'handles runner assignment'
|
|
|
|
end
|
2021-06-08 09:09:56 +00:00
|
|
|
end
|
|
|
|
|
2021-06-22 12:08:05 +00:00
|
|
|
context 'when not using pending builds table' do
|
2021-06-08 09:09:56 +00:00
|
|
|
before do
|
2021-06-22 12:08:05 +00:00
|
|
|
stub_feature_flags(ci_pending_builds_queue_source: false)
|
2021-06-08 09:09:56 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
include_examples 'handles runner assignment'
|
|
|
|
end
|
2018-03-13 18:33:46 +00:00
|
|
|
end
|
2017-09-03 14:35:37 +00:00
|
|
|
|
2018-03-13 18:33:46 +00:00
|
|
|
describe '#register_success' do
|
2020-05-19 06:08:03 +00:00
|
|
|
let!(:current_time) { Time.zone.local(2018, 4, 5, 14, 0, 0) }
|
2018-03-13 18:33:46 +00:00
|
|
|
let!(:attempt_counter) { double('Gitlab::Metrics::NullMetric') }
|
|
|
|
let!(:job_queue_duration_seconds) { double('Gitlab::Metrics::NullMetric') }
|
|
|
|
|
|
|
|
before do
|
|
|
|
allow(Time).to receive(:now).and_return(current_time)
|
|
|
|
# Stub tested metrics
|
2021-02-23 18:10:40 +00:00
|
|
|
allow(Gitlab::Ci::Queue::Metrics)
|
|
|
|
.to receive(:attempt_counter)
|
|
|
|
.and_return(attempt_counter)
|
|
|
|
|
|
|
|
allow(Gitlab::Ci::Queue::Metrics)
|
|
|
|
.to receive(:job_queue_duration_seconds)
|
|
|
|
.and_return(job_queue_duration_seconds)
|
2018-03-13 18:33:46 +00:00
|
|
|
|
2020-09-07 18:08:32 +00:00
|
|
|
project.update!(shared_runners_enabled: true)
|
|
|
|
pending_job.update!(created_at: current_time - 3600, queued_at: current_time - 1800)
|
2017-01-20 11:25:53 +00:00
|
|
|
end
|
2018-03-13 18:33:46 +00:00
|
|
|
|
2018-04-10 13:13:09 +00:00
|
|
|
shared_examples 'attempt counter collector' do
|
2018-03-13 18:33:46 +00:00
|
|
|
it 'increments attempt counter' do
|
|
|
|
allow(job_queue_duration_seconds).to receive(:observe)
|
|
|
|
expect(attempt_counter).to receive(:increment)
|
|
|
|
|
|
|
|
execute(runner)
|
|
|
|
end
|
2018-04-10 13:13:09 +00:00
|
|
|
end
|
2018-03-13 18:33:46 +00:00
|
|
|
|
2018-04-10 13:13:09 +00:00
|
|
|
shared_examples 'jobs queueing time histogram collector' do
|
2018-03-13 18:33:46 +00:00
|
|
|
it 'counts job queuing time histogram with expected labels' do
|
|
|
|
allow(attempt_counter).to receive(:increment)
|
|
|
|
expect(job_queue_duration_seconds).to receive(:observe)
|
|
|
|
.with({ shared_runner: expected_shared_runner,
|
2020-02-24 12:09:00 +00:00
|
|
|
jobs_running_for_project: expected_jobs_running_for_project_first_job,
|
|
|
|
shard: expected_shard }, 1800)
|
2018-03-13 18:33:46 +00:00
|
|
|
|
|
|
|
execute(runner)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when project already has running jobs' do
|
2021-06-08 09:09:56 +00:00
|
|
|
let!(:build2) { create(:ci_build, :running, pipeline: pipeline, runner: shared_runner) }
|
|
|
|
let!(:build3) { create(:ci_build, :running, pipeline: pipeline, runner: shared_runner) }
|
2018-03-13 18:33:46 +00:00
|
|
|
|
|
|
|
it 'counts job queuing time histogram with expected labels' do
|
|
|
|
allow(attempt_counter).to receive(:increment)
|
|
|
|
expect(job_queue_duration_seconds).to receive(:observe)
|
|
|
|
.with({ shared_runner: expected_shared_runner,
|
2020-02-24 12:09:00 +00:00
|
|
|
jobs_running_for_project: expected_jobs_running_for_project_third_job,
|
|
|
|
shard: expected_shard }, 1800)
|
2018-03-13 18:33:46 +00:00
|
|
|
|
|
|
|
execute(runner)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-04-10 13:13:09 +00:00
|
|
|
shared_examples 'metrics collector' do
|
|
|
|
it_behaves_like 'attempt counter collector'
|
|
|
|
it_behaves_like 'jobs queueing time histogram collector'
|
|
|
|
end
|
|
|
|
|
2018-03-13 18:33:46 +00:00
|
|
|
context 'when shared runner is used' do
|
2021-08-10 12:11:00 +00:00
|
|
|
before do
|
|
|
|
pending_job.reload
|
|
|
|
pending_job.create_queuing_entry!
|
|
|
|
end
|
|
|
|
|
2020-02-24 12:09:00 +00:00
|
|
|
let(:runner) { create(:ci_runner, :instance, tag_list: %w(tag1 tag2)) }
|
2018-03-13 18:33:46 +00:00
|
|
|
let(:expected_shared_runner) { true }
|
2021-02-23 18:10:40 +00:00
|
|
|
let(:expected_shard) { ::Gitlab::Ci::Queue::Metrics::DEFAULT_METRICS_SHARD }
|
2018-03-13 18:33:46 +00:00
|
|
|
let(:expected_jobs_running_for_project_first_job) { 0 }
|
|
|
|
let(:expected_jobs_running_for_project_third_job) { 2 }
|
|
|
|
|
|
|
|
it_behaves_like 'metrics collector'
|
2018-04-10 13:13:09 +00:00
|
|
|
|
2020-02-24 12:09:00 +00:00
|
|
|
context 'when metrics_shard tag is defined' do
|
|
|
|
let(:runner) { create(:ci_runner, :instance, tag_list: %w(tag1 metrics_shard::shard_tag tag2)) }
|
|
|
|
let(:expected_shard) { 'shard_tag' }
|
|
|
|
|
|
|
|
it_behaves_like 'metrics collector'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when multiple metrics_shard tag is defined' do
|
|
|
|
let(:runner) { create(:ci_runner, :instance, tag_list: %w(tag1 metrics_shard::shard_tag metrics_shard::shard_tag_2 tag2)) }
|
|
|
|
let(:expected_shard) { 'shard_tag' }
|
|
|
|
|
|
|
|
it_behaves_like 'metrics collector'
|
|
|
|
end
|
|
|
|
|
2018-04-10 13:13:09 +00:00
|
|
|
context 'when pending job with queued_at=nil is used' do
|
|
|
|
before do
|
2020-09-07 18:08:32 +00:00
|
|
|
pending_job.update!(queued_at: nil)
|
2018-04-10 13:13:09 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'attempt counter collector'
|
|
|
|
|
|
|
|
it "doesn't count job queuing time histogram" do
|
|
|
|
allow(attempt_counter).to receive(:increment)
|
|
|
|
expect(job_queue_duration_seconds).not_to receive(:observe)
|
|
|
|
|
|
|
|
execute(runner)
|
|
|
|
end
|
|
|
|
end
|
2018-03-13 18:33:46 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when specific runner is used' do
|
2020-02-24 12:09:00 +00:00
|
|
|
let(:runner) { create(:ci_runner, :project, projects: [project], tag_list: %w(tag1 metrics_shard::shard_tag tag2)) }
|
2018-03-13 18:33:46 +00:00
|
|
|
let(:expected_shared_runner) { false }
|
2021-02-23 18:10:40 +00:00
|
|
|
let(:expected_shard) { ::Gitlab::Ci::Queue::Metrics::DEFAULT_METRICS_SHARD }
|
2018-03-13 18:33:46 +00:00
|
|
|
let(:expected_jobs_running_for_project_first_job) { '+Inf' }
|
|
|
|
let(:expected_jobs_running_for_project_third_job) { '+Inf' }
|
|
|
|
|
|
|
|
it_behaves_like 'metrics collector'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-07-05 13:55:10 +00:00
|
|
|
context 'when runner_session params are' do
|
|
|
|
it 'present sets runner session configuration in the build' do
|
|
|
|
runner_session_params = { session: { 'url' => 'https://example.com' } }
|
|
|
|
|
|
|
|
expect(execute(specific_runner, runner_session_params).runner_session.attributes)
|
|
|
|
.to include(runner_session_params[:session])
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'not present it does not configure the runner session' do
|
|
|
|
expect(execute(specific_runner).runner_session).to be_nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-03-02 18:11:20 +00:00
|
|
|
context 'when max queue depth is reached' do
|
2021-06-08 09:09:56 +00:00
|
|
|
let!(:pending_job) { create(:ci_build, :pending, :queued, :degenerated, pipeline: pipeline) }
|
|
|
|
let!(:pending_job_2) { create(:ci_build, :pending, :queued, :degenerated, pipeline: pipeline) }
|
|
|
|
let!(:pending_job_3) { create(:ci_build, :pending, :queued, pipeline: pipeline) }
|
2021-03-02 18:11:20 +00:00
|
|
|
|
|
|
|
before do
|
|
|
|
stub_const("#{described_class}::MAX_QUEUE_DEPTH", 2)
|
|
|
|
end
|
|
|
|
|
2021-05-26 18:10:52 +00:00
|
|
|
it 'returns 409 conflict' do
|
|
|
|
expect(Ci::Build.pending.unstarted.count).to eq 3
|
2021-03-02 18:11:20 +00:00
|
|
|
|
2021-05-26 18:10:52 +00:00
|
|
|
result = described_class.new(specific_runner).execute
|
2021-03-02 18:11:20 +00:00
|
|
|
|
2021-05-26 18:10:52 +00:00
|
|
|
expect(result).not_to be_valid
|
|
|
|
expect(result.build).to be_nil
|
2021-03-02 18:11:20 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-07-05 13:55:10 +00:00
|
|
|
def execute(runner, params = {})
|
|
|
|
described_class.new(runner).execute(params).build
|
2015-08-26 01:42:46 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|