BigW Consortium Gitlab

runner_spec.rb 4.09 KB
Newer Older
1 2
require 'spec_helper'

Douwe Maan committed
3
describe Ci::Runner, models: true do
4 5
  describe '#display_name' do
    it 'should return the description if it has a value' do
Valery Sizov committed
6
      runner = FactoryGirl.build(:ci_runner, description: 'Linux/Ruby-1.9.3-p448')
7 8 9 10
      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
11
      runner = FactoryGirl.create(:ci_runner)
12 13 14 15
      expect(runner.display_name).to eq runner.description
    end

    it 'should return the token if the description is an empty string' do
Kamil Trzcinski committed
16
      runner = FactoryGirl.build(:ci_runner, description: '', token: 'token')
17 18 19 20 21
      expect(runner.display_name).to eq runner.token
    end
  end

  describe :assign_to do
22
    let!(:project) { FactoryGirl.create :empty_project }
23
    let!(:shared_runner) { FactoryGirl.create(:ci_runner, :shared) }
24 25 26

    before { shared_runner.assign_to(project) }

Dmitriy Zaporozhets committed
27 28 29
    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 }
30 31
  end

32 33 34 35
  describe :online do
    subject { Ci::Runner.online }

    before do
36 37
      @runner1 = FactoryGirl.create(:ci_runner, :shared, contacted_at: 1.year.ago)
      @runner2 = FactoryGirl.create(:ci_runner, :shared, contacted_at: 1.second.ago)
38 39 40 41 42 43
    end

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

  describe :online? do
44
    let(:runner) { FactoryGirl.create(:ci_runner, :shared) }
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67

    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
68
    let(:runner) { FactoryGirl.create(:ci_runner, :shared, contacted_at: 1.second.ago) }
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96

    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

97 98
  describe "belongs_to_one_project?" do
    it "returns false if there are two projects runner assigned to" do
99
      runner = FactoryGirl.create(:ci_runner)
100 101
      project = FactoryGirl.create(:empty_project)
      project1 = FactoryGirl.create(:empty_project)
102 103
      project.runners << runner
      project1.runners << runner
104

Dmitriy Zaporozhets committed
105
      expect(runner.belongs_to_one_project?).to be_falsey
106 107 108
    end

    it "returns true" do
109
      runner = FactoryGirl.create(:ci_runner)
110
      project = FactoryGirl.create(:empty_project)
111
      project.runners << runner
112

Dmitriy Zaporozhets committed
113
      expect(runner.belongs_to_one_project?).to be_truthy
114 115
    end
  end
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143

  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
144
end