2015-09-15 16:23:43 -04:00
|
|
|
require 'spec_helper'
|
2015-09-15 09:00:45 -04:00
|
|
|
|
|
|
|
module Ci
|
2017-07-10 10:24:02 -04:00
|
|
|
describe RegisterJobService do
|
2017-08-02 15:55:11 -04:00
|
|
|
let!(:project) { FactoryGirl.create :project, shared_runners_enabled: false }
|
2016-06-03 10:22:26 -04:00
|
|
|
let!(:pipeline) { FactoryGirl.create :ci_pipeline, project: project }
|
|
|
|
let!(:pending_build) { FactoryGirl.create :ci_build, pipeline: pipeline }
|
2015-09-15 09:00:45 -04:00
|
|
|
let!(:shared_runner) { FactoryGirl.create(:ci_runner, is_shared: true) }
|
|
|
|
let!(:specific_runner) { FactoryGirl.create(:ci_runner, is_shared: false) }
|
|
|
|
|
|
|
|
before do
|
2015-12-04 06:55:23 -05:00
|
|
|
specific_runner.assign_to(project)
|
2015-08-25 21:42:46 -04:00
|
|
|
end
|
|
|
|
|
2016-07-11 18:12:31 -04:00
|
|
|
describe '#execute' do
|
2015-09-15 09:00:45 -04:00
|
|
|
context 'runner follow tag list' do
|
|
|
|
it "picks build with the same tag" do
|
|
|
|
pending_build.tag_list = ["linux"]
|
|
|
|
pending_build.save
|
|
|
|
specific_runner.tag_list = ["linux"]
|
2017-01-20 06:25:53 -05:00
|
|
|
expect(execute(specific_runner)).to eq(pending_build)
|
2015-09-15 09:00:45 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "does not pick build with different tag" do
|
|
|
|
pending_build.tag_list = ["linux"]
|
|
|
|
pending_build.save
|
|
|
|
specific_runner.tag_list = ["win32"]
|
2017-01-20 06:25:53 -05:00
|
|
|
expect(execute(specific_runner)).to be_falsey
|
2015-09-15 09:00:45 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "picks build without tag" do
|
2017-01-20 06:25:53 -05:00
|
|
|
expect(execute(specific_runner)).to eq(pending_build)
|
2015-09-15 09:00:45 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "does not pick build with tag" do
|
|
|
|
pending_build.tag_list = ["linux"]
|
|
|
|
pending_build.save
|
2017-01-20 06:25:53 -05:00
|
|
|
expect(execute(specific_runner)).to be_falsey
|
2015-09-15 09:00:45 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "pick build without tag" do
|
|
|
|
specific_runner.tag_list = ["win32"]
|
2017-01-20 06:25:53 -05:00
|
|
|
expect(execute(specific_runner)).to eq(pending_build)
|
2015-09-15 09:00:45 -04:00
|
|
|
end
|
2015-08-25 21:42:46 -04:00
|
|
|
end
|
|
|
|
|
2016-06-15 10:48:42 -04: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 06:25:53 -05:00
|
|
|
expect(execute(shared_runner)).to be_nil
|
2016-06-15 10:48:42 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'for specific runner' do
|
|
|
|
it 'does not pick a build' do
|
2017-01-20 06:25:53 -05:00
|
|
|
expect(execute(specific_runner)).to be_nil
|
2016-06-15 10:48:42 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-09-15 09:00:45 -04:00
|
|
|
context 'allow shared runners' do
|
|
|
|
before do
|
2015-12-04 06:55:23 -05:00
|
|
|
project.update(shared_runners_enabled: true)
|
2015-09-15 09:00:45 -04:00
|
|
|
end
|
|
|
|
|
2016-06-13 15:52:41 -04:00
|
|
|
context 'for multiple builds' do
|
2017-08-02 15:55:11 -04:00
|
|
|
let!(:project2) { create :project, shared_runners_enabled: true }
|
2016-06-13 15:52:41 -04:00
|
|
|
let!(:pipeline2) { create :ci_pipeline, project: project2 }
|
2017-08-02 15:55:11 -04:00
|
|
|
let!(:project3) { create :project, shared_runners_enabled: true }
|
2016-06-13 15:52:41 -04:00
|
|
|
let!(:pipeline3) { create :ci_pipeline, project: project3 }
|
|
|
|
let!(:build1_project1) { pending_build }
|
|
|
|
let!(:build2_project1) { FactoryGirl.create :ci_build, pipeline: pipeline }
|
|
|
|
let!(:build3_project1) { FactoryGirl.create :ci_build, pipeline: pipeline }
|
|
|
|
let!(:build1_project2) { FactoryGirl.create :ci_build, pipeline: pipeline2 }
|
|
|
|
let!(:build2_project2) { FactoryGirl.create :ci_build, pipeline: pipeline2 }
|
|
|
|
let!(:build1_project3) { FactoryGirl.create :ci_build, pipeline: pipeline3 }
|
|
|
|
|
|
|
|
it 'prefers projects without builds first' do
|
|
|
|
# it gets for one build from each of the projects
|
2017-01-20 06:25:53 -05: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 15:52:41 -04:00
|
|
|
|
|
|
|
# then it gets a second build from each of the projects
|
2017-01-20 06:25:53 -05:00
|
|
|
expect(execute(shared_runner)).to eq(build2_project1)
|
|
|
|
expect(execute(shared_runner)).to eq(build2_project2)
|
2016-06-13 15:52:41 -04:00
|
|
|
|
|
|
|
# in the end the third build
|
2017-01-20 06:25:53 -05:00
|
|
|
expect(execute(shared_runner)).to eq(build3_project1)
|
2016-06-13 15:52:41 -04: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 06:25:53 -05:00
|
|
|
expect(execute(shared_runner)).to eq(build1_project1)
|
2016-10-26 05:34:40 -04:00
|
|
|
build1_project1.reload.success
|
2017-01-20 06:25:53 -05:00
|
|
|
expect(execute(shared_runner)).to eq(build2_project1)
|
2016-06-13 15:52:41 -04:00
|
|
|
|
2017-01-20 06:25:53 -05:00
|
|
|
expect(execute(shared_runner)).to eq(build1_project2)
|
2016-10-26 05:34:40 -04:00
|
|
|
build1_project2.reload.success
|
2017-01-20 06:25:53 -05: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 15:52:41 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-09-15 09:00:45 -04:00
|
|
|
context 'shared runner' do
|
2017-01-20 06:25:53 -05:00
|
|
|
let(:build) { execute(shared_runner) }
|
2015-09-15 09:00:45 -04: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 06:25:53 -05:00
|
|
|
let(:build) { execute(specific_runner) }
|
2015-09-15 09:00:45 -04: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-25 21:42:46 -04:00
|
|
|
end
|
|
|
|
|
2015-09-15 09:00:45 -04:00
|
|
|
context 'disallow shared runners' do
|
2015-11-05 10:22:37 -05:00
|
|
|
before do
|
2015-12-04 06:55:23 -05:00
|
|
|
project.update(shared_runners_enabled: false)
|
2015-11-05 10:22:37 -05:00
|
|
|
end
|
|
|
|
|
2015-09-15 09:00:45 -04:00
|
|
|
context 'shared runner' do
|
2017-01-20 06:25:53 -05:00
|
|
|
let(:build) { execute(shared_runner) }
|
2015-08-25 21:42:46 -04:00
|
|
|
|
2015-09-15 09:00:45 -04:00
|
|
|
it { expect(build).to be_nil }
|
|
|
|
end
|
2015-08-25 21:42:46 -04:00
|
|
|
|
2015-09-15 09:00:45 -04:00
|
|
|
context 'specific runner' do
|
2017-01-20 06:25:53 -05:00
|
|
|
let(:build) { execute(specific_runner) }
|
2015-08-25 21:42:46 -04:00
|
|
|
|
2015-09-15 09:00:45 -04: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-25 21:42:46 -04:00
|
|
|
end
|
2016-08-01 18:31:21 -04: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 06:25:53 -05:00
|
|
|
let(:build) { execute(shared_runner) }
|
2016-08-01 18:31:21 -04:00
|
|
|
|
|
|
|
it { expect(build).to be_nil }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'and uses specific runner' do
|
2017-01-20 06:25:53 -05:00
|
|
|
let(:build) { execute(specific_runner) }
|
2016-08-01 18:31:21 -04:00
|
|
|
|
|
|
|
it { expect(build).to be_nil }
|
|
|
|
end
|
|
|
|
end
|
2017-01-20 06:25:53 -05:00
|
|
|
|
2017-02-28 13:23:35 -05:00
|
|
|
context 'when first build is stalled' do
|
|
|
|
before do
|
|
|
|
pending_build.lock_version = 10
|
|
|
|
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
|
2017-03-06 05:56:44 -05:00
|
|
|
allow_any_instance_of(Ci::RegisterJobService).to receive(:builds_for_specific_runner)
|
2017-02-28 13:23:35 -05:00
|
|
|
.and_return([pending_build, other_build])
|
|
|
|
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
|
2017-03-06 05:56:44 -05:00
|
|
|
allow_any_instance_of(Ci::RegisterJobService).to receive(:builds_for_specific_runner)
|
2017-02-28 13:23:35 -05:00
|
|
|
.and_return([pending_build])
|
|
|
|
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
|
2017-03-06 05:56:44 -05:00
|
|
|
allow_any_instance_of(Ci::RegisterJobService).to receive(:builds_for_specific_runner)
|
2017-02-28 13:23:35 -05:00
|
|
|
.and_return([])
|
|
|
|
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-01-20 06:25:53 -05:00
|
|
|
def execute(runner)
|
2017-01-25 08:23:47 -05:00
|
|
|
described_class.new(runner).execute.build
|
2017-01-20 06:25:53 -05:00
|
|
|
end
|
2015-08-25 21:42:46 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|