summaryrefslogtreecommitdiff
path: root/spec/models/ci/running_build_spec.rb
blob: 629861e35b8d71d15c5d536ee994fe6145c9f508 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Ci::RunningBuild do
  let_it_be(:project) { create(:project) }
  let_it_be(:pipeline) { create(:ci_pipeline, project: project) }

  let(:runner) { create(:ci_runner, :instance_type) }
  let(:build) { create(:ci_build, :running, runner: runner, pipeline: pipeline) }

  describe '.upsert_shared_runner_build!' do
    context 'another pending entry does not exist' do
      it 'creates a new pending entry' do
        result = described_class.upsert_shared_runner_build!(build)

        expect(result.rows.dig(0, 0)).to eq build.id
        expect(build.reload.runtime_metadata).to be_present
      end
    end

    context 'when another queuing entry exists for given build' do
      before do
        create(:ci_running_build, build: build, project: project, runner: runner)
      end

      it 'returns a build id as a result' do
        result = described_class.upsert_shared_runner_build!(build)

        expect(result.rows.dig(0, 0)).to eq build.id
      end
    end

    context 'when build has been picked by a specific runner' do
      let(:runner) { create(:ci_runner, :project) }

      it 'raises an error' do
        expect { described_class.upsert_shared_runner_build!(build) }
          .to raise_error(ArgumentError, 'build has not been picked by a shared runner')
      end
    end

    context 'when build has not been picked by a runner yet' do
      let(:build) { create(:ci_build, pipeline: pipeline) }

      it 'raises an error' do
        expect { described_class.upsert_shared_runner_build!(build) }
          .to raise_error(ArgumentError, 'build has not been picked by a shared runner')
      end
    end
  end
end