2015-09-15 20:23:43 +00:00
|
|
|
require 'spec_helper'
|
2015-09-15 13:00:45 +00:00
|
|
|
|
|
|
|
module Ci
|
2017-07-10 14:24:02 +00:00
|
|
|
describe RegisterJobService do
|
2017-10-05 11:29:21 +00:00
|
|
|
let!(:project) { create :project, shared_runners_enabled: false }
|
|
|
|
let!(:group) { create :group }
|
|
|
|
let!(:pipeline) { create :ci_pipeline, project: project }
|
|
|
|
let!(:pending_job) { create :ci_build, pipeline: pipeline }
|
|
|
|
let!(:shared_runner) { create :ci_runner, is_shared: true }
|
|
|
|
let!(:specific_runner) { create :ci_runner, is_shared: false }
|
2018-05-01 09:11:57 +00:00
|
|
|
let!(:group_runner) { create :ci_runner, groups: [group], runner_type: :group_type }
|
2015-09-15 13:00:45 +00:00
|
|
|
|
|
|
|
before do
|
2015-12-04 11:55:23 +00:00
|
|
|
specific_runner.assign_to(project)
|
2015-08-26 01:42:46 +00:00
|
|
|
end
|
|
|
|
|
2016-07-11 22:12:31 +00:00
|
|
|
describe '#execute' do
|
2015-09-15 13:00:45 +00:00
|
|
|
context 'runner follow tag list' do
|
|
|
|
it "picks build with the same tag" do
|
2017-11-30 22:17:41 +00:00
|
|
|
pending_job.update(tag_list: ["linux"])
|
|
|
|
specific_runner.update(tag_list: ["linux"])
|
2017-09-03 14:58:25 +00:00
|
|
|
expect(execute(specific_runner)).to eq(pending_job)
|
2015-09-15 13:00:45 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "does not pick build with different tag" do
|
2017-11-30 22:17:41 +00:00
|
|
|
pending_job.update(tag_list: ["linux"])
|
|
|
|
specific_runner.update(tag_list: ["win32"])
|
2017-01-20 11:25:53 +00:00
|
|
|
expect(execute(specific_runner)).to be_falsey
|
2015-09-15 13:00:45 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "picks build without tag" do
|
2017-09-03 14:58:25 +00:00
|
|
|
expect(execute(specific_runner)).to eq(pending_job)
|
2015-09-15 13:00:45 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "does not pick build with tag" do
|
2017-11-30 22:17:41 +00:00
|
|
|
pending_job.update(tag_list: ["linux"])
|
2017-01-20 11:25:53 +00:00
|
|
|
expect(execute(specific_runner)).to be_falsey
|
2015-09-15 13:00:45 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "pick build without tag" do
|
2017-11-30 22:17:41 +00:00
|
|
|
specific_runner.update(tag_list: ["win32"])
|
2017-09-03 14:58:25 +00:00
|
|
|
expect(execute(specific_runner)).to eq(pending_job)
|
2015-09-15 13:00:45 +00:00
|
|
|
end
|
2015-08-26 01:42:46 +00:00
|
|
|
end
|
|
|
|
|
2016-06-15 14:48:42 +00:00
|
|
|
context 'deleted projects' do
|
|
|
|
before do
|
|
|
|
project.update(pending_delete: true)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'for shared runners' do
|
|
|
|
before do
|
|
|
|
project.update(shared_runners_enabled: true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not pick a build' do
|
2017-01-20 11:25:53 +00:00
|
|
|
expect(execute(shared_runner)).to be_nil
|
2016-06-15 14:48:42 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'for specific runner' do
|
|
|
|
it 'does not pick a build' do
|
2017-01-20 11:25:53 +00:00
|
|
|
expect(execute(specific_runner)).to be_nil
|
2016-06-15 14:48:42 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-09-15 13:00:45 +00:00
|
|
|
context 'allow shared runners' do
|
|
|
|
before do
|
2015-12-04 11:55:23 +00:00
|
|
|
project.update(shared_runners_enabled: true)
|
2015-09-15 13:00:45 +00:00
|
|
|
end
|
|
|
|
|
2016-06-13 19:52:41 +00:00
|
|
|
context 'for multiple builds' do
|
2017-08-02 19:55:11 +00:00
|
|
|
let!(:project2) { create :project, shared_runners_enabled: true }
|
2016-06-13 19:52:41 +00:00
|
|
|
let!(:pipeline2) { create :ci_pipeline, project: project2 }
|
2017-08-02 19:55:11 +00:00
|
|
|
let!(:project3) { create :project, shared_runners_enabled: true }
|
2016-06-13 19:52:41 +00:00
|
|
|
let!(:pipeline3) { create :ci_pipeline, project: project3 }
|
2017-09-03 14:58:25 +00:00
|
|
|
let!(:build1_project1) { pending_job }
|
2017-12-14 00:13:44 +00:00
|
|
|
let!(:build2_project1) { FactoryBot.create :ci_build, pipeline: pipeline }
|
|
|
|
let!(:build3_project1) { FactoryBot.create :ci_build, pipeline: pipeline }
|
|
|
|
let!(:build1_project2) { FactoryBot.create :ci_build, pipeline: pipeline2 }
|
|
|
|
let!(:build2_project2) { FactoryBot.create :ci_build, pipeline: pipeline2 }
|
|
|
|
let!(:build1_project3) { FactoryBot.create :ci_build, pipeline: pipeline3 }
|
2016-06-13 19:52:41 +00:00
|
|
|
|
|
|
|
it 'prefers projects without builds first' do
|
|
|
|
# it gets for one build from each of the projects
|
2017-01-20 11:25:53 +00:00
|
|
|
expect(execute(shared_runner)).to eq(build1_project1)
|
|
|
|
expect(execute(shared_runner)).to eq(build1_project2)
|
|
|
|
expect(execute(shared_runner)).to eq(build1_project3)
|
2016-06-13 19:52:41 +00:00
|
|
|
|
|
|
|
# then it gets a second build from each of the projects
|
2017-01-20 11:25:53 +00:00
|
|
|
expect(execute(shared_runner)).to eq(build2_project1)
|
|
|
|
expect(execute(shared_runner)).to eq(build2_project2)
|
2016-06-13 19:52:41 +00:00
|
|
|
|
|
|
|
# in the end the third build
|
2017-01-20 11:25:53 +00:00
|
|
|
expect(execute(shared_runner)).to eq(build3_project1)
|
2016-06-13 19:52:41 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'equalises number of running builds' do
|
|
|
|
# after finishing the first build for project 1, get a second build from the same project
|
2017-01-20 11:25:53 +00:00
|
|
|
expect(execute(shared_runner)).to eq(build1_project1)
|
2016-10-26 09:34:40 +00:00
|
|
|
build1_project1.reload.success
|
2017-01-20 11:25:53 +00:00
|
|
|
expect(execute(shared_runner)).to eq(build2_project1)
|
2016-06-13 19:52:41 +00:00
|
|
|
|
2017-01-20 11:25:53 +00:00
|
|
|
expect(execute(shared_runner)).to eq(build1_project2)
|
2016-10-26 09:34:40 +00:00
|
|
|
build1_project2.reload.success
|
2017-01-20 11:25:53 +00:00
|
|
|
expect(execute(shared_runner)).to eq(build2_project2)
|
|
|
|
expect(execute(shared_runner)).to eq(build1_project3)
|
|
|
|
expect(execute(shared_runner)).to eq(build3_project1)
|
2016-06-13 19:52:41 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-09-15 13:00:45 +00:00
|
|
|
context 'shared runner' do
|
2017-01-20 11:25:53 +00:00
|
|
|
let(:build) { execute(shared_runner) }
|
2015-09-15 13:00:45 +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) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'specific runner' do
|
2017-01-20 11:25:53 +00:00
|
|
|
let(:build) { execute(specific_runner) }
|
2015-09-15 13:00:45 +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-08-26 01:42:46 +00:00
|
|
|
end
|
|
|
|
|
2015-09-15 13:00:45 +00:00
|
|
|
context 'disallow shared runners' do
|
2015-11-05 15:22:37 +00:00
|
|
|
before do
|
2015-12-04 11:55:23 +00:00
|
|
|
project.update(shared_runners_enabled: false)
|
2015-11-05 15:22:37 +00:00
|
|
|
end
|
|
|
|
|
2015-09-15 13:00:45 +00:00
|
|
|
context 'shared runner' do
|
2017-01-20 11:25:53 +00:00
|
|
|
let(:build) { execute(shared_runner) }
|
2015-08-26 01:42:46 +00:00
|
|
|
|
2015-09-15 13:00:45 +00:00
|
|
|
it { expect(build).to be_nil }
|
|
|
|
end
|
2015-08-26 01:42:46 +00:00
|
|
|
|
2015-09-15 13:00:45 +00:00
|
|
|
context 'specific runner' do
|
2017-01-20 11:25:53 +00:00
|
|
|
let(:build) { execute(specific_runner) }
|
2015-08-26 01:42:46 +00:00
|
|
|
|
2015-09-15 13:00:45 +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-08-26 01:42:46 +00:00
|
|
|
end
|
2016-08-01 22:31:21 +00:00
|
|
|
|
|
|
|
context 'disallow when builds are disabled' do
|
|
|
|
before do
|
|
|
|
project.update(shared_runners_enabled: true)
|
|
|
|
project.project_feature.update_attribute(:builds_access_level, ProjectFeature::DISABLED)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'and uses shared runner' do
|
2017-01-20 11:25:53 +00:00
|
|
|
let(:build) { execute(shared_runner) }
|
2016-08-01 22:31:21 +00:00
|
|
|
|
|
|
|
it { expect(build).to be_nil }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'and uses specific runner' do
|
2017-01-20 11:25:53 +00:00
|
|
|
let(:build) { execute(specific_runner) }
|
2016-08-01 22:31:21 +00:00
|
|
|
|
|
|
|
it { expect(build).to be_nil }
|
|
|
|
end
|
|
|
|
end
|
2017-01-20 11:25:53 +00:00
|
|
|
|
2017-10-05 11:29:21 +00:00
|
|
|
context 'allow group runners' do
|
|
|
|
before do
|
|
|
|
project.update!(group_runners_enabled: true, group: group)
|
|
|
|
end
|
|
|
|
|
|
|
|
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
|
|
|
|
2017-10-05 11:29:21 +00:00
|
|
|
let!(:build1_project1) { pending_job }
|
|
|
|
let!(:build2_project1) { create :ci_build, pipeline: pipeline }
|
|
|
|
let!(:build3_project1) { create :ci_build, pipeline: pipeline }
|
|
|
|
let!(:build1_project2) { create :ci_build, pipeline: pipeline2 }
|
|
|
|
let!(:build2_project2) { create :ci_build, pipeline: pipeline2 }
|
|
|
|
let!(:build1_project3) { create :ci_build, pipeline: pipeline3 }
|
|
|
|
|
2017-12-19 14:12:21 +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 }
|
|
|
|
let!(:build1_unrelated_project) { create :ci_build, pipeline: unrelated_pipeline }
|
|
|
|
let!(:unrelated_group_runner) { create :ci_runner, groups: [unrelated_group] }
|
|
|
|
|
|
|
|
it 'does not consider builds from other group runners' do
|
2018-04-30 05:39:47 +00:00
|
|
|
expect(described_class.new(group_runner).send(:builds_for_group_runner).count).to eq 6
|
2017-12-19 14:12:21 +00:00
|
|
|
execute(group_runner)
|
|
|
|
|
2018-04-30 05:39:47 +00:00
|
|
|
expect(described_class.new(group_runner).send(:builds_for_group_runner).count).to eq 5
|
2017-12-19 14:12:21 +00:00
|
|
|
execute(group_runner)
|
|
|
|
|
2018-04-30 05:39:47 +00:00
|
|
|
expect(described_class.new(group_runner).send(:builds_for_group_runner).count).to eq 4
|
2017-12-19 14:12:21 +00:00
|
|
|
execute(group_runner)
|
|
|
|
|
2018-04-30 05:39:47 +00:00
|
|
|
expect(described_class.new(group_runner).send(:builds_for_group_runner).count).to eq 3
|
2017-12-19 14:12:21 +00:00
|
|
|
execute(group_runner)
|
|
|
|
|
2018-04-30 05:39:47 +00:00
|
|
|
expect(described_class.new(group_runner).send(:builds_for_group_runner).count).to eq 2
|
2017-12-19 14:12:21 +00:00
|
|
|
execute(group_runner)
|
|
|
|
|
2018-04-30 05:39:47 +00:00
|
|
|
expect(described_class.new(group_runner).send(:builds_for_group_runner).count).to eq 1
|
2017-12-19 14:12:21 +00:00
|
|
|
execute(group_runner)
|
|
|
|
|
2018-04-30 05:39:47 +00:00
|
|
|
expect(described_class.new(group_runner).send(:builds_for_group_runner).count).to eq 0
|
2017-12-19 14:12:21 +00:00
|
|
|
expect(execute(group_runner)).to be_nil
|
2017-10-05 11:29:21 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'group runner' do
|
|
|
|
let(:build) { execute(group_runner) }
|
|
|
|
|
|
|
|
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
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'disallow group runners' do
|
|
|
|
before do
|
|
|
|
project.update(group_runners_enabled: false)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'group runner' do
|
|
|
|
let(:build) { execute(group_runner) }
|
|
|
|
|
|
|
|
it { expect(build).to be_nil }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-02-28 18:23:35 +00:00
|
|
|
context 'when first build is stalled' do
|
|
|
|
before do
|
2017-11-30 22:17:41 +00:00
|
|
|
pending_job.update(lock_version: 0)
|
2017-02-28 18:23:35 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
subject { described_class.new(specific_runner).execute }
|
|
|
|
|
|
|
|
context 'with multiple builds are in queue' do
|
|
|
|
let!(:other_build) { create :ci_build, pipeline: pipeline }
|
|
|
|
|
|
|
|
before do
|
2018-04-30 05:39:47 +00:00
|
|
|
allow_any_instance_of(Ci::RegisterJobService).to receive(:builds_for_project_runner)
|
2017-11-30 22:17:41 +00:00
|
|
|
.and_return(Ci::Build.where(id: [pending_job, other_build]))
|
2017-02-28 18:23:35 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "receives second build from the queue" do
|
|
|
|
expect(subject).to be_valid
|
|
|
|
expect(subject.build).to eq(other_build)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when single build is in queue' do
|
|
|
|
before do
|
2018-04-30 05:39:47 +00:00
|
|
|
allow_any_instance_of(Ci::RegisterJobService).to receive(:builds_for_project_runner)
|
2017-11-30 22:17:41 +00:00
|
|
|
.and_return(Ci::Build.where(id: pending_job))
|
2017-02-28 18:23:35 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "does not receive any valid result" do
|
|
|
|
expect(subject).not_to be_valid
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when there is no build in queue' do
|
|
|
|
before do
|
2018-04-30 05:39:47 +00:00
|
|
|
allow_any_instance_of(Ci::RegisterJobService).to receive(:builds_for_project_runner)
|
2017-11-30 22:17:41 +00:00
|
|
|
.and_return(Ci::Build.none)
|
2017-02-28 18:23:35 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "does not receive builds but result is valid" do
|
|
|
|
expect(subject).to be_valid
|
|
|
|
expect(subject.build).to be_nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-08-31 09:28:29 +00:00
|
|
|
context 'when access_level of runner is not_protected' do
|
2017-09-02 08:53:54 +00:00
|
|
|
let!(:specific_runner) { create(:ci_runner, :specific) }
|
2017-08-31 09:28:29 +00:00
|
|
|
|
2017-08-22 08:01:11 +00:00
|
|
|
context 'when a job is protected' do
|
2017-09-03 14:58:25 +00:00
|
|
|
let!(:pending_job) { create(:ci_build, :protected, pipeline: pipeline) }
|
2017-08-22 08:01:11 +00:00
|
|
|
|
2017-09-03 14:58:25 +00:00
|
|
|
it 'picks the job' do
|
|
|
|
expect(execute(specific_runner)).to eq(pending_job)
|
2017-08-22 08:01:11 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when a job is unprotected' do
|
2017-09-03 14:58:25 +00:00
|
|
|
let!(:pending_job) { create(:ci_build, pipeline: pipeline) }
|
2017-08-22 08:01:11 +00:00
|
|
|
|
2017-09-03 14:58:25 +00:00
|
|
|
it 'picks the job' do
|
|
|
|
expect(execute(specific_runner)).to eq(pending_job)
|
2017-09-02 07:31:14 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when protected attribute of a job is nil' do
|
2017-09-03 14:58:25 +00:00
|
|
|
let!(:pending_job) { create(:ci_build, pipeline: pipeline) }
|
2017-09-02 07:31:14 +00:00
|
|
|
|
|
|
|
before do
|
2017-09-03 14:58:25 +00:00
|
|
|
pending_job.update_attribute(:protected, nil)
|
2017-09-02 07:31:14 +00:00
|
|
|
end
|
|
|
|
|
2017-09-03 14:58:25 +00:00
|
|
|
it 'picks the job' do
|
|
|
|
expect(execute(specific_runner)).to eq(pending_job)
|
2017-08-22 08:01:11 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-08-31 09:28:29 +00:00
|
|
|
context 'when access_level of runner is ref_protected' do
|
2017-08-29 08:31:59 +00:00
|
|
|
let!(:specific_runner) { create(:ci_runner, :ref_protected, :specific) }
|
2017-08-22 08:01:11 +00:00
|
|
|
|
|
|
|
context 'when a job is protected' do
|
2017-09-03 14:58:25 +00:00
|
|
|
let!(:pending_job) { create(:ci_build, :protected, pipeline: pipeline) }
|
2017-08-22 08:01:11 +00:00
|
|
|
|
2017-09-03 14:58:25 +00:00
|
|
|
it 'picks the job' do
|
|
|
|
expect(execute(specific_runner)).to eq(pending_job)
|
2017-08-22 08:01:11 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when a job is unprotected' do
|
2017-09-03 14:58:25 +00:00
|
|
|
let!(:pending_job) { create(:ci_build, pipeline: pipeline) }
|
2017-08-22 08:01:11 +00:00
|
|
|
|
2017-09-03 14:58:25 +00:00
|
|
|
it 'does not pick the job' do
|
2017-08-22 08:01:11 +00:00
|
|
|
expect(execute(specific_runner)).to be_nil
|
|
|
|
end
|
|
|
|
end
|
2017-09-02 07:31:14 +00:00
|
|
|
|
|
|
|
context 'when protected attribute of a job is nil' do
|
2017-09-03 14:58:25 +00:00
|
|
|
let!(:pending_job) { create(:ci_build, pipeline: pipeline) }
|
2017-09-02 07:31:14 +00:00
|
|
|
|
|
|
|
before do
|
2017-09-03 14:58:25 +00:00
|
|
|
pending_job.update_attribute(:protected, nil)
|
2017-09-02 07:31:14 +00:00
|
|
|
end
|
|
|
|
|
2017-09-03 14:58:25 +00:00
|
|
|
it 'does not pick the job' do
|
2017-09-02 07:31:14 +00:00
|
|
|
expect(execute(specific_runner)).to be_nil
|
|
|
|
end
|
|
|
|
end
|
2017-08-22 08:01:11 +00:00
|
|
|
end
|
|
|
|
|
2017-09-03 14:35:37 +00:00
|
|
|
context 'when "dependencies" keyword is specified' do
|
2017-12-07 17:21:16 +00:00
|
|
|
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-06 07:40:56 +00:00
|
|
|
end
|
|
|
|
|
2017-12-07 17:21:16 +00:00
|
|
|
shared_examples 'validation is active' do
|
|
|
|
context 'when depended job has not been completed yet' do
|
2017-12-14 08:28:40 +00:00
|
|
|
let!(:pre_stage_job) { create(:ci_build, :manual, pipeline: pipeline, name: 'test', stage_idx: 0) }
|
2017-12-07 17:21:16 +00:00
|
|
|
|
2017-12-14 08:28:40 +00:00
|
|
|
it { expect(subject).to eq(pending_job) }
|
2017-12-07 17:21:16 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
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_behaves_like 'not pick'
|
|
|
|
end
|
|
|
|
|
|
|
|
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
|
|
|
|
2017-12-07 17:21:16 +00:00
|
|
|
it_behaves_like 'not pick'
|
|
|
|
end
|
2017-12-12 07:17:45 +00:00
|
|
|
|
|
|
|
context 'when job object is staled' do
|
2017-12-14 08:28:40 +00:00
|
|
|
let!(:pre_stage_job) { create(:ci_build, :success, :expired, pipeline: pipeline, name: 'test', stage_idx: 0) }
|
2017-12-12 07:17:45 +00:00
|
|
|
|
|
|
|
before do
|
|
|
|
allow_any_instance_of(Ci::Build).to receive(:drop!)
|
|
|
|
.and_raise(ActiveRecord::StaleObjectError.new(pending_job, :drop!))
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not drop nor pick' do
|
|
|
|
expect(subject).to be_nil
|
|
|
|
end
|
|
|
|
end
|
2017-09-03 14:35:37 +00:00
|
|
|
end
|
|
|
|
|
2017-12-07 17:21:16 +00:00
|
|
|
shared_examples 'validation is not active' do
|
|
|
|
context 'when depended job has not been completed yet' do
|
2017-12-14 08:28:40 +00:00
|
|
|
let!(:pre_stage_job) { create(:ci_build, :manual, pipeline: pipeline, name: 'test', stage_idx: 0) }
|
2017-09-03 14:35:37 +00:00
|
|
|
|
2017-12-07 17:21:16 +00:00
|
|
|
it { expect(subject).to eq(pending_job) }
|
|
|
|
end
|
|
|
|
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-09-03 14:35:37 +00:00
|
|
|
|
2017-12-07 17:21:16 +00:00
|
|
|
it { expect(subject).to eq(pending_job) }
|
2017-09-03 14:35:37 +00:00
|
|
|
end
|
2017-09-05 13:37:28 +00:00
|
|
|
|
2017-12-07 17:21:16 +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-09-05 13:37:28 +00:00
|
|
|
|
2017-12-07 17:21:16 +00:00
|
|
|
before do
|
|
|
|
pre_stage_job.erase
|
2017-09-05 13:37:28 +00:00
|
|
|
end
|
|
|
|
|
2017-12-07 17:21:16 +00:00
|
|
|
it { expect(subject).to eq(pending_job) }
|
|
|
|
end
|
|
|
|
end
|
2017-09-05 13:37:28 +00:00
|
|
|
|
2017-12-07 17:21:16 +00:00
|
|
|
before do
|
|
|
|
stub_feature_flags(ci_disable_validates_dependencies: false)
|
|
|
|
end
|
|
|
|
|
|
|
|
let!(:pre_stage_job) { create(:ci_build, :success, pipeline: pipeline, name: 'test', stage_idx: 0) }
|
|
|
|
let!(:pending_job) { create(:ci_build, :pending, pipeline: pipeline, stage_idx: 1, options: { dependencies: ['test'] } ) }
|
|
|
|
|
|
|
|
subject { execute(specific_runner) }
|
|
|
|
|
|
|
|
context 'when validates for dependencies is enabled' do
|
|
|
|
before do
|
|
|
|
stub_feature_flags(ci_disable_validates_dependencies: false)
|
2017-09-05 13:37:28 +00:00
|
|
|
end
|
2017-12-07 17:21:16 +00:00
|
|
|
|
|
|
|
it_behaves_like 'validation is active'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when validates for dependencies is disabled' do
|
|
|
|
before do
|
|
|
|
stub_feature_flags(ci_disable_validates_dependencies: true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'validation is not active'
|
2017-09-03 14:35:37 +00:00
|
|
|
end
|
|
|
|
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
|
|
|
|
let!(:current_time) { Time.new(2018, 4, 5, 14, 0, 0) }
|
|
|
|
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 defaults for any metrics other than the ones we're testing
|
|
|
|
allow(Gitlab::Metrics).to receive(:counter)
|
|
|
|
.with(any_args)
|
|
|
|
.and_return(Gitlab::Metrics::NullMetric.instance)
|
|
|
|
allow(Gitlab::Metrics).to receive(:histogram)
|
|
|
|
.with(any_args)
|
|
|
|
.and_return(Gitlab::Metrics::NullMetric.instance)
|
|
|
|
|
|
|
|
# Stub tested metrics
|
|
|
|
allow(Gitlab::Metrics).to receive(:counter)
|
|
|
|
.with(:job_register_attempts_total, anything)
|
|
|
|
.and_return(attempt_counter)
|
|
|
|
allow(Gitlab::Metrics).to receive(:histogram)
|
|
|
|
.with(:job_queue_duration_seconds, anything, anything, anything)
|
|
|
|
.and_return(job_queue_duration_seconds)
|
|
|
|
|
|
|
|
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,
|
|
|
|
jobs_running_for_project: expected_jobs_running_for_project_first_job }, 1800)
|
|
|
|
|
|
|
|
execute(runner)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when project already has running jobs' do
|
|
|
|
let!(:build2) { create( :ci_build, :running, pipeline: pipeline, runner: shared_runner) }
|
|
|
|
let!(:build3) { create( :ci_build, :running, pipeline: pipeline, runner: shared_runner) }
|
|
|
|
|
|
|
|
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,
|
|
|
|
jobs_running_for_project: expected_jobs_running_for_project_third_job }, 1800)
|
|
|
|
|
|
|
|
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
|
|
|
|
let(:runner) { shared_runner }
|
|
|
|
let(:expected_shared_runner) { true }
|
|
|
|
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
|
|
|
|
|
|
|
context 'when pending job with queued_at=nil is used' do
|
|
|
|
before do
|
|
|
|
pending_job.update(queued_at: nil)
|
|
|
|
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
|
|
|
|
let(:runner) { specific_runner }
|
|
|
|
let(:expected_shared_runner) { false }
|
|
|
|
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
|
|
|
|
|
|
|
|
def execute(runner)
|
|
|
|
described_class.new(runner).execute.build
|
2015-08-26 01:42:46 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|