runner_spec.rb 4.9 KB
Newer Older
D
Douwe Maan 已提交
1 2
require 'spec_helper'

D
Douwe Maan 已提交
3
describe Ci::Runner, models: true do
G
Grzegorz Bizon 已提交
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
  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

D
Douwe Maan 已提交
22 23
  describe '#display_name' do
    it 'should return the description if it has a value' do
V
Valery Sizov 已提交
24
      runner = FactoryGirl.build(:ci_runner, description: 'Linux/Ruby-1.9.3-p448')
D
Douwe Maan 已提交
25 26 27 28
      expect(runner.display_name).to eq 'Linux/Ruby-1.9.3-p448'
    end

    it 'should return the token if it does not have a description' do
29
      runner = FactoryGirl.create(:ci_runner)
D
Douwe Maan 已提交
30 31 32 33
      expect(runner.display_name).to eq runner.description
    end

    it 'should return the token if the description is an empty string' do
K
Kamil Trzcinski 已提交
34
      runner = FactoryGirl.build(:ci_runner, description: '', token: 'token')
D
Douwe Maan 已提交
35 36 37 38 39
      expect(runner.display_name).to eq runner.token
    end
  end

  describe :assign_to do
K
Kamil Trzcinski 已提交
40
    let!(:project) { FactoryGirl.create :empty_project }
41
    let!(:shared_runner) { FactoryGirl.create(:ci_runner, :shared) }
D
Douwe Maan 已提交
42 43 44

    before { shared_runner.assign_to(project) }

D
Dmitriy Zaporozhets 已提交
45 46 47
    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 }
D
Douwe Maan 已提交
48 49
  end

50 51 52 53
  describe :online do
    subject { Ci::Runner.online }

    before do
54 55
      @runner1 = FactoryGirl.create(:ci_runner, :shared, contacted_at: 1.year.ago)
      @runner2 = FactoryGirl.create(:ci_runner, :shared, contacted_at: 1.second.ago)
56 57 58 59 60 61
    end

    it { is_expected.to eq([@runner2])}
  end

  describe :online? do
62
    let(:runner) { FactoryGirl.create(:ci_runner, :shared) }
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85

    subject { runner.online? }

    context 'never contacted' do
      before { runner.contacted_at = nil }

      it { is_expected.to be_falsey }
    end

    context 'contacted long time ago time' do
      before { runner.contacted_at = 1.year.ago }

      it { is_expected.to be_falsey }
    end

    context 'contacted 1s ago' do
      before { runner.contacted_at = 1.second.ago }

      it { is_expected.to be_truthy }
    end
  end

  describe :status do
86
    let(:runner) { FactoryGirl.create(:ci_runner, :shared, contacted_at: 1.second.ago) }
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114

    subject { runner.status }

    context 'never connected' do
      before { runner.contacted_at = nil }

      it { is_expected.to eq(:not_connected) }
    end

    context 'contacted 1s ago' do
      before { runner.contacted_at = 1.second.ago }

      it { is_expected.to eq(:online) }
    end

    context 'contacted long time ago' do
      before { runner.contacted_at = 1.year.ago }

      it { is_expected.to eq(:offline) }
    end

    context 'inactive' do
      before { runner.active = false }

      it { is_expected.to eq(:paused) }
    end
  end

D
Douwe Maan 已提交
115 116
  describe "belongs_to_one_project?" do
    it "returns false if there are two projects runner assigned to" do
117
      runner = FactoryGirl.create(:ci_runner)
K
Kamil Trzcinski 已提交
118 119
      project = FactoryGirl.create(:empty_project)
      project1 = FactoryGirl.create(:empty_project)
120 121
      project.runners << runner
      project1.runners << runner
D
Douwe Maan 已提交
122

D
Dmitriy Zaporozhets 已提交
123
      expect(runner.belongs_to_one_project?).to be_falsey
D
Douwe Maan 已提交
124 125 126
    end

    it "returns true" do
127
      runner = FactoryGirl.create(:ci_runner)
K
Kamil Trzcinski 已提交
128
      project = FactoryGirl.create(:empty_project)
129
      project.runners << runner
D
Douwe Maan 已提交
130

D
Dmitriy Zaporozhets 已提交
131
      expect(runner.belongs_to_one_project?).to be_truthy
D
Douwe Maan 已提交
132 133
    end
  end
134

G
Grzegorz Bizon 已提交
135 136 137 138 139 140 141 142 143 144 145 146 147
  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: []) }
      it { is_expected.to_not have_tags }
    end
  end

  describe '.search' do
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
    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
D
Douwe Maan 已提交
174
end