summaryrefslogtreecommitdiff
path: root/spec/services/ci/create_pipeline_service/limit_active_jobs_spec.rb
blob: 003d109a27c6711863b9372c4ffe1a4a1ec00618 (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
53
# frozen_string_literal: true
require 'spec_helper'

RSpec.describe Ci::CreatePipelineService, :yaml_processor_feature_flag_corectness do
  let_it_be(:project) { create(:project, :repository) }
  let_it_be(:user) { project.first_owner }
  let_it_be(:existing_pipeline) { create(:ci_pipeline, project: project) }

  let(:service) { described_class.new(project, user, ref: 'refs/heads/master') }

  subject(:pipeline) { service.execute(:push).payload }

  before do
    create_list(:ci_build, 8, pipeline: existing_pipeline)
    create_list(:ci_bridge, 1, pipeline: existing_pipeline)

    stub_ci_pipeline_yaml_file(<<~YAML)
    job1:
      script: echo
    job3:
      trigger:
        project: org/my-project
    job4:
      script: echo
      only: [tags]
    YAML
  end

  context 'when project has exceeded the active jobs limit' do
    before do
      project.namespace.actual_limits.update!(ci_active_jobs: 10)
    end

    it 'fails the pipeline before populating it' do
      expect(pipeline).to be_failed
      expect(pipeline).to be_job_activity_limit_exceeded

      expect(pipeline.errors.full_messages)
        .to include("Project exceeded the allowed number of jobs in active pipelines. Retry later.")
      expect(pipeline.statuses).to be_empty
    end
  end

  context 'when project has not exceeded the active jobs limit' do
    before do
      project.namespace.actual_limits.update!(ci_active_jobs: 20)
    end

    it 'creates the pipeline successfully' do
      expect(pipeline).to be_created
    end
  end
end