2015-08-25 21:42:46 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2015-12-09 04:50:51 -05:00
|
|
|
describe Ci::Runner, models: true do
|
2016-05-06 03:04:09 -04:00
|
|
|
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-08-25 21:42:46 -04:00
|
|
|
describe '#display_name' do
|
2016-06-16 11:33:53 -04:00
|
|
|
it 'returns the description if it has a value' do
|
2015-09-15 09:42:02 -04:00
|
|
|
runner = FactoryGirl.build(:ci_runner, description: 'Linux/Ruby-1.9.3-p448')
|
2015-08-25 21:42:46 -04:00
|
|
|
expect(runner.display_name).to eq 'Linux/Ruby-1.9.3-p448'
|
|
|
|
end
|
|
|
|
|
2016-06-16 11:33:53 -04:00
|
|
|
it 'returns the token if it does not have a description' do
|
2015-09-10 09:47:15 -04:00
|
|
|
runner = FactoryGirl.create(:ci_runner)
|
2015-08-25 21:42:46 -04:00
|
|
|
expect(runner.display_name).to eq runner.description
|
|
|
|
end
|
|
|
|
|
2016-06-16 11:33:53 -04:00
|
|
|
it 'returns the token if the description is an empty string' do
|
2015-10-14 08:21:49 -04:00
|
|
|
runner = FactoryGirl.build(:ci_runner, description: '', token: 'token')
|
2015-08-25 21:42:46 -04:00
|
|
|
expect(runner.display_name).to eq runner.token
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-06-08 02:46:41 -04:00
|
|
|
describe '#assign_to' do
|
2015-12-04 06:55:23 -05:00
|
|
|
let!(:project) { FactoryGirl.create :empty_project }
|
2016-02-17 16:56:33 -05:00
|
|
|
let!(:shared_runner) { FactoryGirl.create(:ci_runner, :shared) }
|
2015-08-25 21:42:46 -04:00
|
|
|
|
2016-06-02 11:28:14 -04:00
|
|
|
before do
|
|
|
|
shared_runner.assign_to(project)
|
|
|
|
end
|
2015-08-25 21:42:46 -04:00
|
|
|
|
2015-09-10 09:52:52 -04:00
|
|
|
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 }
|
2015-08-25 21:42:46 -04:00
|
|
|
end
|
|
|
|
|
2016-06-08 02:46:41 -04:00
|
|
|
describe '.online' do
|
2015-10-12 15:12:31 -04:00
|
|
|
subject { Ci::Runner.online }
|
|
|
|
|
|
|
|
before do
|
2016-02-17 16:56:33 -05:00
|
|
|
@runner1 = FactoryGirl.create(:ci_runner, :shared, contacted_at: 1.year.ago)
|
|
|
|
@runner2 = FactoryGirl.create(:ci_runner, :shared, contacted_at: 1.second.ago)
|
2015-10-12 15:12:31 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it { is_expected.to eq([@runner2])}
|
|
|
|
end
|
|
|
|
|
2016-06-08 02:46:41 -04:00
|
|
|
describe '#online?' do
|
2016-02-17 16:56:33 -05:00
|
|
|
let(:runner) { FactoryGirl.create(:ci_runner, :shared) }
|
2015-10-12 15:12:31 -04:00
|
|
|
|
|
|
|
subject { runner.online? }
|
|
|
|
|
|
|
|
context 'never contacted' do
|
2016-06-02 11:28:14 -04:00
|
|
|
before do
|
|
|
|
runner.contacted_at = nil
|
|
|
|
end
|
2015-10-12 15:12:31 -04:00
|
|
|
|
|
|
|
it { is_expected.to be_falsey }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'contacted long time ago time' do
|
2016-06-02 11:28:14 -04:00
|
|
|
before do
|
|
|
|
runner.contacted_at = 1.year.ago
|
|
|
|
end
|
2015-10-12 15:12:31 -04:00
|
|
|
|
|
|
|
it { is_expected.to be_falsey }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'contacted 1s ago' do
|
2016-06-02 11:28:14 -04:00
|
|
|
before do
|
|
|
|
runner.contacted_at = 1.second.ago
|
|
|
|
end
|
2015-10-12 15:12:31 -04:00
|
|
|
|
|
|
|
it { is_expected.to be_truthy }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-06-08 03:19:49 -04:00
|
|
|
describe '#can_pick?' do
|
2017-01-26 17:44:58 -05:00
|
|
|
let(:pipeline) { create(:ci_pipeline) }
|
2016-06-15 05:34:44 -04:00
|
|
|
let(:build) { create(:ci_build, pipeline: pipeline) }
|
2016-06-08 03:19:49 -04:00
|
|
|
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']
|
2016-06-16 11:26:49 -04:00
|
|
|
|
2016-06-08 03:19:49 -04:00
|
|
|
expect(runner.can_pick?(build)).to be_falsey
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when runner has tags' do
|
|
|
|
before do
|
2017-02-22 12:46:57 -05:00
|
|
|
runner.tag_list = %w(bb cc)
|
2016-06-08 03:19:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples 'tagged build picker' do
|
|
|
|
it 'can handle build with matching tags' do
|
|
|
|
build.tag_list = ['bb']
|
2016-06-16 11:26:49 -04:00
|
|
|
|
2016-06-08 03:19:49 -04:00
|
|
|
expect(runner.can_pick?(build)).to be_truthy
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'cannot handle build without matching tags' do
|
|
|
|
build.tag_list = ['aa']
|
2016-06-16 11:26:49 -04:00
|
|
|
|
2016-06-08 03:19:49 -04:00
|
|
|
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
|
|
|
|
|
2016-06-08 04:07:08 -04:00
|
|
|
shared_examples 'locked build picker' do
|
2016-06-08 03:19:49 -04:00
|
|
|
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
|
2017-02-22 12:46:57 -05:00
|
|
|
runner.tag_list = %w(bb cc)
|
2016-06-08 03:19:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'cannot handle it for builds without matching tags' do
|
|
|
|
build.tag_list = ['aa']
|
2016-06-16 11:26:49 -04:00
|
|
|
|
2016-06-08 03:19:49 -04:00
|
|
|
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
|
|
|
|
|
2016-06-08 04:07:08 -04:00
|
|
|
it_behaves_like 'locked build picker'
|
|
|
|
|
|
|
|
context 'when having runner tags' do
|
|
|
|
before do
|
2017-02-22 12:46:57 -05:00
|
|
|
runner.tag_list = %w(bb cc)
|
2016-06-08 04:07:08 -04:00
|
|
|
build.tag_list = ['bb']
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'can handle it for matching tags' do
|
|
|
|
expect(runner.can_pick?(build)).to be_truthy
|
|
|
|
end
|
|
|
|
end
|
2016-06-08 03:19:49 -04:00
|
|
|
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
|
|
|
|
|
2016-06-08 04:07:08 -04:00
|
|
|
it_behaves_like 'locked build picker'
|
|
|
|
|
|
|
|
context 'when having runner tags' do
|
|
|
|
before do
|
2017-02-22 12:46:57 -05:00
|
|
|
runner.tag_list = %w(bb cc)
|
2016-06-08 04:07:08 -04:00
|
|
|
build.tag_list = ['bb']
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'cannot handle it for matching tags' do
|
|
|
|
expect(runner.can_pick?(build)).to be_falsey
|
|
|
|
end
|
|
|
|
end
|
2016-06-08 03:19:49 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-06-08 02:46:41 -04:00
|
|
|
describe '#status' do
|
2016-02-17 16:56:33 -05:00
|
|
|
let(:runner) { FactoryGirl.create(:ci_runner, :shared, contacted_at: 1.second.ago) }
|
2015-10-12 15:12:31 -04:00
|
|
|
|
|
|
|
subject { runner.status }
|
|
|
|
|
|
|
|
context 'never connected' do
|
2016-06-02 11:28:14 -04:00
|
|
|
before do
|
|
|
|
runner.contacted_at = nil
|
|
|
|
end
|
2015-10-12 15:12:31 -04:00
|
|
|
|
|
|
|
it { is_expected.to eq(:not_connected) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'contacted 1s ago' do
|
2016-06-02 11:28:14 -04:00
|
|
|
before do
|
|
|
|
runner.contacted_at = 1.second.ago
|
|
|
|
end
|
2015-10-12 15:12:31 -04:00
|
|
|
|
|
|
|
it { is_expected.to eq(:online) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'contacted long time ago' do
|
2016-06-02 11:28:14 -04:00
|
|
|
before do
|
|
|
|
runner.contacted_at = 1.year.ago
|
|
|
|
end
|
2015-10-12 15:12:31 -04:00
|
|
|
|
|
|
|
it { is_expected.to eq(:offline) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'inactive' do
|
2016-06-02 11:28:14 -04:00
|
|
|
before do
|
|
|
|
runner.active = false
|
|
|
|
end
|
2015-10-12 15:12:31 -04:00
|
|
|
|
|
|
|
it { is_expected.to eq(:paused) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-01-04 04:46:56 -05:00
|
|
|
describe '#tick_runner_queue' do
|
|
|
|
let(:runner) { create(:ci_runner) }
|
|
|
|
|
|
|
|
it 'returns a new last_update value' do
|
|
|
|
expect(runner.tick_runner_queue).not_to be_empty
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#ensure_runner_queue_value' do
|
|
|
|
let(:runner) { create(:ci_runner) }
|
|
|
|
|
|
|
|
it 'sets a new last_update value when it is called the first time' do
|
|
|
|
last_update = runner.ensure_runner_queue_value
|
|
|
|
|
2017-01-19 17:31:03 -05:00
|
|
|
expect_value_in_redis.to eq(last_update)
|
2017-01-04 04:46:56 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not change if it is not expired and called again' do
|
|
|
|
last_update = runner.ensure_runner_queue_value
|
|
|
|
|
|
|
|
expect(runner.ensure_runner_queue_value).to eq(last_update)
|
2017-01-19 17:31:03 -05:00
|
|
|
expect_value_in_redis.to eq(last_update)
|
2017-01-04 04:46:56 -05:00
|
|
|
end
|
|
|
|
|
2017-01-19 17:31:03 -05:00
|
|
|
context 'updates runner queue after changing editable value' do
|
|
|
|
let!(:last_update) { runner.ensure_runner_queue_value }
|
|
|
|
|
|
|
|
before do
|
2017-01-20 08:57:01 -05:00
|
|
|
Ci::UpdateRunnerService.new(runner).update(description: 'new runner')
|
2017-01-19 17:31:03 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'sets a new last_update value' do
|
|
|
|
expect_value_in_redis.not_to eq(last_update)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'does not update runner value after save' do
|
|
|
|
let!(:last_update) { runner.ensure_runner_queue_value }
|
|
|
|
|
|
|
|
before do
|
|
|
|
runner.touch
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'has an old last_update value' do
|
|
|
|
expect_value_in_redis.to eq(last_update)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def expect_value_in_redis
|
2017-01-04 04:46:56 -05:00
|
|
|
Gitlab::Redis.with do |redis|
|
|
|
|
runner_queue_key = runner.send(:runner_queue_key)
|
2017-01-19 17:31:03 -05:00
|
|
|
expect(redis.get(runner_queue_key))
|
2017-01-04 04:46:56 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-01-20 13:38:58 -05:00
|
|
|
describe '#destroy' do
|
|
|
|
let(:runner) { create(:ci_runner) }
|
|
|
|
|
|
|
|
context 'when there is a tick in the queue' do
|
|
|
|
let!(:queue_key) { runner.send(:runner_queue_key) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
runner.tick_runner_queue
|
|
|
|
runner.destroy
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'cleans up the queue' do
|
|
|
|
Gitlab::Redis.with do |redis|
|
|
|
|
expect(redis.get(queue_key)).to be_nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-06-20 04:52:05 -04:00
|
|
|
describe '.assignable_for' do
|
2016-06-02 06:41:26 -04:00
|
|
|
let(:runner) { create(:ci_runner) }
|
2017-01-26 17:44:58 -05:00
|
|
|
let(:project) { create(:empty_project) }
|
|
|
|
let(:another_project) { create(:empty_project) }
|
2016-06-02 06:41:26 -04:00
|
|
|
|
2016-06-02 11:28:14 -04:00
|
|
|
before do
|
|
|
|
project.runners << runner
|
|
|
|
end
|
2016-06-02 06:41:26 -04:00
|
|
|
|
|
|
|
context 'with shared runners' do
|
2016-06-02 11:28:14 -04:00
|
|
|
before do
|
|
|
|
runner.update(is_shared: true)
|
|
|
|
end
|
2016-06-02 06:41:26 -04:00
|
|
|
|
2016-06-16 11:33:53 -04:00
|
|
|
context 'does not give owned runner' do
|
2016-06-20 04:52:05 -04:00
|
|
|
subject { Ci::Runner.assignable_for(project) }
|
2016-06-02 06:41:26 -04:00
|
|
|
|
|
|
|
it { is_expected.to be_empty }
|
|
|
|
end
|
|
|
|
|
2016-06-16 11:33:53 -04:00
|
|
|
context 'does not give shared runner' do
|
2016-06-20 04:52:05 -04:00
|
|
|
subject { Ci::Runner.assignable_for(another_project) }
|
2016-06-02 06:41:26 -04:00
|
|
|
|
|
|
|
it { is_expected.to be_empty }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with unlocked runner' do
|
2016-06-16 11:33:53 -04:00
|
|
|
context 'does not give owned runner' do
|
2016-06-20 04:52:05 -04:00
|
|
|
subject { Ci::Runner.assignable_for(project) }
|
2016-06-02 06:41:26 -04:00
|
|
|
|
|
|
|
it { is_expected.to be_empty }
|
|
|
|
end
|
|
|
|
|
2016-06-16 11:33:53 -04:00
|
|
|
context 'does give a specific runner' do
|
2016-06-20 04:52:05 -04:00
|
|
|
subject { Ci::Runner.assignable_for(another_project) }
|
2016-06-02 06:41:26 -04:00
|
|
|
|
|
|
|
it { is_expected.to contain_exactly(runner) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with locked runner' do
|
2016-06-02 11:28:14 -04:00
|
|
|
before do
|
|
|
|
runner.update(locked: true)
|
|
|
|
end
|
2016-06-02 06:41:26 -04:00
|
|
|
|
2016-06-16 11:33:53 -04:00
|
|
|
context 'does not give owned runner' do
|
2016-06-20 04:52:05 -04:00
|
|
|
subject { Ci::Runner.assignable_for(project) }
|
2016-06-02 06:41:26 -04:00
|
|
|
|
|
|
|
it { is_expected.to be_empty }
|
|
|
|
end
|
|
|
|
|
2016-06-16 11:33:53 -04:00
|
|
|
context 'does not give a locked runner' do
|
2016-06-20 04:52:05 -04:00
|
|
|
subject { Ci::Runner.assignable_for(another_project) }
|
2016-06-02 06:41:26 -04:00
|
|
|
|
|
|
|
it { is_expected.to be_empty }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-08-25 21:42:46 -04:00
|
|
|
describe "belongs_to_one_project?" do
|
|
|
|
it "returns false if there are two projects runner assigned to" do
|
2016-02-17 16:56:33 -05:00
|
|
|
runner = FactoryGirl.create(:ci_runner)
|
2015-12-04 06:55:23 -05:00
|
|
|
project = FactoryGirl.create(:empty_project)
|
|
|
|
project1 = FactoryGirl.create(:empty_project)
|
2015-12-10 11:44:06 -05:00
|
|
|
project.runners << runner
|
|
|
|
project1.runners << runner
|
2015-08-25 21:42:46 -04:00
|
|
|
|
2015-09-10 09:52:52 -04:00
|
|
|
expect(runner.belongs_to_one_project?).to be_falsey
|
2015-08-25 21:42:46 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns true" do
|
2016-02-17 16:56:33 -05:00
|
|
|
runner = FactoryGirl.create(:ci_runner)
|
2015-12-04 06:55:23 -05:00
|
|
|
project = FactoryGirl.create(:empty_project)
|
2015-12-10 11:44:06 -05:00
|
|
|
project.runners << runner
|
2015-08-25 21:42:46 -04:00
|
|
|
|
2015-09-10 09:52:52 -04:00
|
|
|
expect(runner.belongs_to_one_project?).to be_truthy
|
2015-08-25 21:42:46 -04:00
|
|
|
end
|
|
|
|
end
|
2016-03-01 10:41:18 -05:00
|
|
|
|
2016-05-06 03:04:09 -04:00
|
|
|
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-05-23 19:37:59 -04:00
|
|
|
it { is_expected.not_to have_tags }
|
2016-05-06 03:04:09 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.search' do
|
2016-03-01 10:41:18 -05:00
|
|
|
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-08-25 21:42:46 -04:00
|
|
|
end
|