2015-09-25 12:07:36 +05:30
|
|
|
require 'spec_helper'
|
|
|
|
|
2015-12-23 02:04:40 +05:30
|
|
|
describe Ci::Runner, models: true do
|
2016-06-02 11:05:42 +05:30
|
|
|
describe 'validation' do
|
|
|
|
context 'when runner is not allowed to pick untagged jobs' do
|
|
|
|
context 'when runner does not have tags' do
|
|
|
|
it 'is not valid' do
|
|
|
|
runner = build(:ci_runner, tag_list: [], run_untagged: false)
|
|
|
|
expect(runner).to be_invalid
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when runner has tags' do
|
|
|
|
it 'is valid' do
|
|
|
|
runner = build(:ci_runner, tag_list: ['tag'], run_untagged: false)
|
|
|
|
expect(runner).to be_valid
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-09-25 12:07:36 +05:30
|
|
|
describe '#display_name' do
|
2016-06-22 15:30:34 +05:30
|
|
|
it 'returns the description if it has a value' do
|
2015-09-25 12:07:36 +05:30
|
|
|
runner = FactoryGirl.build(:ci_runner, description: 'Linux/Ruby-1.9.3-p448')
|
|
|
|
expect(runner.display_name).to eq 'Linux/Ruby-1.9.3-p448'
|
|
|
|
end
|
|
|
|
|
2016-06-22 15:30:34 +05:30
|
|
|
it 'returns the token if it does not have a description' do
|
2015-09-25 12:07:36 +05:30
|
|
|
runner = FactoryGirl.create(:ci_runner)
|
|
|
|
expect(runner.display_name).to eq runner.description
|
|
|
|
end
|
|
|
|
|
2016-06-22 15:30:34 +05:30
|
|
|
it 'returns the token if the description is an empty string' do
|
2015-10-24 18:46:33 +05:30
|
|
|
runner = FactoryGirl.build(:ci_runner, description: '', token: 'token')
|
2015-09-25 12:07:36 +05:30
|
|
|
expect(runner.display_name).to eq runner.token
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-06-22 15:30:34 +05:30
|
|
|
describe '#assign_to' do
|
2015-12-23 02:04:40 +05:30
|
|
|
let!(:project) { FactoryGirl.create :empty_project }
|
2016-04-02 18:10:28 +05:30
|
|
|
let!(:shared_runner) { FactoryGirl.create(:ci_runner, :shared) }
|
2015-09-25 12:07:36 +05:30
|
|
|
|
2016-06-22 15:30:34 +05:30
|
|
|
before do
|
|
|
|
shared_runner.assign_to(project)
|
|
|
|
end
|
2015-09-25 12:07:36 +05:30
|
|
|
|
|
|
|
it { expect(shared_runner).to be_specific }
|
|
|
|
it { expect(shared_runner.projects).to eq([project]) }
|
|
|
|
it { expect(shared_runner.only_for?(project)).to be_truthy }
|
|
|
|
end
|
|
|
|
|
2016-06-22 15:30:34 +05:30
|
|
|
describe '.online' do
|
2015-10-24 18:46:33 +05:30
|
|
|
subject { Ci::Runner.online }
|
|
|
|
|
|
|
|
before do
|
2016-04-02 18:10:28 +05:30
|
|
|
@runner1 = FactoryGirl.create(:ci_runner, :shared, contacted_at: 1.year.ago)
|
|
|
|
@runner2 = FactoryGirl.create(:ci_runner, :shared, contacted_at: 1.second.ago)
|
2015-10-24 18:46:33 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it { is_expected.to eq([@runner2])}
|
|
|
|
end
|
|
|
|
|
2016-06-22 15:30:34 +05:30
|
|
|
describe '#online?' do
|
2016-04-02 18:10:28 +05:30
|
|
|
let(:runner) { FactoryGirl.create(:ci_runner, :shared) }
|
2015-10-24 18:46:33 +05:30
|
|
|
|
|
|
|
subject { runner.online? }
|
|
|
|
|
|
|
|
context 'never contacted' do
|
2016-06-22 15:30:34 +05:30
|
|
|
before do
|
|
|
|
runner.contacted_at = nil
|
|
|
|
end
|
2015-10-24 18:46:33 +05:30
|
|
|
|
|
|
|
it { is_expected.to be_falsey }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'contacted long time ago time' do
|
2016-06-22 15:30:34 +05:30
|
|
|
before do
|
|
|
|
runner.contacted_at = 1.year.ago
|
|
|
|
end
|
2015-10-24 18:46:33 +05:30
|
|
|
|
|
|
|
it { is_expected.to be_falsey }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'contacted 1s ago' do
|
2016-06-22 15:30:34 +05:30
|
|
|
before do
|
|
|
|
runner.contacted_at = 1.second.ago
|
|
|
|
end
|
2015-10-24 18:46:33 +05:30
|
|
|
|
|
|
|
it { is_expected.to be_truthy }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-06-22 15:30:34 +05:30
|
|
|
describe '#can_pick?' do
|
|
|
|
let(:project) { create(:project) }
|
|
|
|
let(:pipeline) { create(:ci_pipeline, project: project) }
|
|
|
|
let(:build) { create(:ci_build, pipeline: pipeline) }
|
|
|
|
let(:runner) { create(:ci_runner) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
build.project.runners << runner
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when runner does not have tags' do
|
|
|
|
it 'can handle builds without tags' do
|
|
|
|
expect(runner.can_pick?(build)).to be_truthy
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'cannot handle build with tags' do
|
|
|
|
build.tag_list = ['aa']
|
|
|
|
|
|
|
|
expect(runner.can_pick?(build)).to be_falsey
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when runner has tags' do
|
|
|
|
before do
|
|
|
|
runner.tag_list = ['bb', 'cc']
|
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples 'tagged build picker' do
|
|
|
|
it 'can handle build with matching tags' do
|
|
|
|
build.tag_list = ['bb']
|
|
|
|
|
|
|
|
expect(runner.can_pick?(build)).to be_truthy
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'cannot handle build without matching tags' do
|
|
|
|
build.tag_list = ['aa']
|
|
|
|
|
|
|
|
expect(runner.can_pick?(build)).to be_falsey
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when runner can pick untagged jobs' do
|
|
|
|
it 'can handle builds without tags' do
|
|
|
|
expect(runner.can_pick?(build)).to be_truthy
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'tagged build picker'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when runner cannot pick untagged jobs' do
|
|
|
|
before do
|
|
|
|
runner.run_untagged = false
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'cannot handle builds without tags' do
|
|
|
|
expect(runner.can_pick?(build)).to be_falsey
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'tagged build picker'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when runner is locked' do
|
|
|
|
before do
|
|
|
|
runner.locked = true
|
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples 'locked build picker' do
|
|
|
|
context 'when runner cannot pick untagged jobs' do
|
|
|
|
before do
|
|
|
|
runner.run_untagged = false
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'cannot handle builds without tags' do
|
|
|
|
expect(runner.can_pick?(build)).to be_falsey
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when having runner tags' do
|
|
|
|
before do
|
|
|
|
runner.tag_list = ['bb', 'cc']
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'cannot handle it for builds without matching tags' do
|
|
|
|
build.tag_list = ['aa']
|
|
|
|
|
|
|
|
expect(runner.can_pick?(build)).to be_falsey
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when serving the same project' do
|
|
|
|
it 'can handle it' do
|
|
|
|
expect(runner.can_pick?(build)).to be_truthy
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'locked build picker'
|
|
|
|
|
|
|
|
context 'when having runner tags' do
|
|
|
|
before do
|
|
|
|
runner.tag_list = ['bb', 'cc']
|
|
|
|
build.tag_list = ['bb']
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'can handle it for matching tags' do
|
|
|
|
expect(runner.can_pick?(build)).to be_truthy
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'serving a different project' do
|
|
|
|
before do
|
|
|
|
runner.runner_projects.destroy_all
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'cannot handle it' do
|
|
|
|
expect(runner.can_pick?(build)).to be_falsey
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'locked build picker'
|
|
|
|
|
|
|
|
context 'when having runner tags' do
|
|
|
|
before do
|
|
|
|
runner.tag_list = ['bb', 'cc']
|
|
|
|
build.tag_list = ['bb']
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'cannot handle it for matching tags' do
|
|
|
|
expect(runner.can_pick?(build)).to be_falsey
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#status' do
|
2016-04-02 18:10:28 +05:30
|
|
|
let(:runner) { FactoryGirl.create(:ci_runner, :shared, contacted_at: 1.second.ago) }
|
2015-10-24 18:46:33 +05:30
|
|
|
|
|
|
|
subject { runner.status }
|
|
|
|
|
|
|
|
context 'never connected' do
|
2016-06-22 15:30:34 +05:30
|
|
|
before do
|
|
|
|
runner.contacted_at = nil
|
|
|
|
end
|
2015-10-24 18:46:33 +05:30
|
|
|
|
|
|
|
it { is_expected.to eq(:not_connected) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'contacted 1s ago' do
|
2016-06-22 15:30:34 +05:30
|
|
|
before do
|
|
|
|
runner.contacted_at = 1.second.ago
|
|
|
|
end
|
2015-10-24 18:46:33 +05:30
|
|
|
|
|
|
|
it { is_expected.to eq(:online) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'contacted long time ago' do
|
2016-06-22 15:30:34 +05:30
|
|
|
before do
|
|
|
|
runner.contacted_at = 1.year.ago
|
|
|
|
end
|
2015-10-24 18:46:33 +05:30
|
|
|
|
|
|
|
it { is_expected.to eq(:offline) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'inactive' do
|
2016-06-22 15:30:34 +05:30
|
|
|
before do
|
|
|
|
runner.active = false
|
|
|
|
end
|
2015-10-24 18:46:33 +05:30
|
|
|
|
|
|
|
it { is_expected.to eq(:paused) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-06-22 15:30:34 +05:30
|
|
|
describe '.assignable_for' do
|
|
|
|
let(:runner) { create(:ci_runner) }
|
|
|
|
let(:project) { create(:project) }
|
|
|
|
let(:another_project) { create(:project) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
project.runners << runner
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with shared runners' do
|
|
|
|
before do
|
|
|
|
runner.update(is_shared: true)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'does not give owned runner' do
|
|
|
|
subject { Ci::Runner.assignable_for(project) }
|
|
|
|
|
|
|
|
it { is_expected.to be_empty }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'does not give shared runner' do
|
|
|
|
subject { Ci::Runner.assignable_for(another_project) }
|
|
|
|
|
|
|
|
it { is_expected.to be_empty }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with unlocked runner' do
|
|
|
|
context 'does not give owned runner' do
|
|
|
|
subject { Ci::Runner.assignable_for(project) }
|
|
|
|
|
|
|
|
it { is_expected.to be_empty }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'does give a specific runner' do
|
|
|
|
subject { Ci::Runner.assignable_for(another_project) }
|
|
|
|
|
|
|
|
it { is_expected.to contain_exactly(runner) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with locked runner' do
|
|
|
|
before do
|
|
|
|
runner.update(locked: true)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'does not give owned runner' do
|
|
|
|
subject { Ci::Runner.assignable_for(project) }
|
|
|
|
|
|
|
|
it { is_expected.to be_empty }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'does not give a locked runner' do
|
|
|
|
subject { Ci::Runner.assignable_for(another_project) }
|
|
|
|
|
|
|
|
it { is_expected.to be_empty }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-09-25 12:07:36 +05:30
|
|
|
describe "belongs_to_one_project?" do
|
|
|
|
it "returns false if there are two projects runner assigned to" do
|
2016-04-02 18:10:28 +05:30
|
|
|
runner = FactoryGirl.create(:ci_runner)
|
2015-12-23 02:04:40 +05:30
|
|
|
project = FactoryGirl.create(:empty_project)
|
|
|
|
project1 = FactoryGirl.create(:empty_project)
|
2015-09-25 12:07:36 +05:30
|
|
|
project.runners << runner
|
|
|
|
project1.runners << runner
|
|
|
|
|
|
|
|
expect(runner.belongs_to_one_project?).to be_falsey
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns true" do
|
2016-04-02 18:10:28 +05:30
|
|
|
runner = FactoryGirl.create(:ci_runner)
|
2015-12-23 02:04:40 +05:30
|
|
|
project = FactoryGirl.create(:empty_project)
|
2015-09-25 12:07:36 +05:30
|
|
|
project.runners << runner
|
|
|
|
|
|
|
|
expect(runner.belongs_to_one_project?).to be_truthy
|
|
|
|
end
|
|
|
|
end
|
2016-06-02 11:05:42 +05:30
|
|
|
|
|
|
|
describe '#has_tags?' do
|
|
|
|
context 'when runner has tags' do
|
|
|
|
subject { create(:ci_runner, tag_list: ['tag']) }
|
|
|
|
it { is_expected.to have_tags }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when runner does not have tags' do
|
|
|
|
subject { create(:ci_runner, tag_list: []) }
|
2016-06-16 23:09:34 +05:30
|
|
|
it { is_expected.not_to have_tags }
|
2016-06-02 11:05:42 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.search' do
|
|
|
|
let(:runner) { create(:ci_runner, token: '123abc') }
|
|
|
|
|
|
|
|
it 'returns runners with a matching token' do
|
|
|
|
expect(described_class.search(runner.token)).to eq([runner])
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns runners with a partially matching token' do
|
|
|
|
expect(described_class.search(runner.token[0..2])).to eq([runner])
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns runners with a matching token regardless of the casing' do
|
|
|
|
expect(described_class.search(runner.token.upcase)).to eq([runner])
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns runners with a matching description' do
|
|
|
|
expect(described_class.search(runner.description)).to eq([runner])
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns runners with a partially matching description' do
|
|
|
|
expect(described_class.search(runner.description[0..2])).to eq([runner])
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns runners with a matching description regardless of the casing' do
|
|
|
|
expect(described_class.search(runner.description.upcase)).to eq([runner])
|
|
|
|
end
|
|
|
|
end
|
2015-09-25 12:07:36 +05:30
|
|
|
end
|