Welcome to mirror list, hosted at ThFree Co, Russian Federation.

rate_limit_spec.rb « limit « chain « pipeline « ci « gitlab « lib « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: aa8aec2af4af42483e69ea1f3edd311097ac7f0d (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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
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
144
145
146
147
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
174
175
176
177
178
179
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe ::Gitlab::Ci::Pipeline::Chain::Limit::RateLimit, :freeze_time, :clean_gitlab_redis_rate_limiting do
  let_it_be(:user) { create(:user) }
  let_it_be(:namespace) { create(:namespace) }
  let_it_be(:project, reload: true) { create(:project, namespace: namespace) }

  let(:save_incompleted) { false }
  let(:throttle_message) do
    'Too many pipelines created in the last minute. Try again later.'
  end

  let(:command) do
    Gitlab::Ci::Pipeline::Chain::Command.new(
      project: project,
      current_user: user,
      save_incompleted: save_incompleted
    )
  end

  let(:pipeline) { build(:ci_pipeline, project: project, source: source) }
  let(:source) { 'push' }
  let(:step) { described_class.new(pipeline, command) }

  def perform(count: 2)
    count.times { step.perform! }
  end

  context 'when the limit is exceeded' do
    before do
      allow(Gitlab::ApplicationRateLimiter).to receive(:rate_limits)
        .and_return(pipelines_create: { threshold: 1, interval: 1.minute })

      stub_feature_flags(ci_throttle_pipelines_creation_dry_run: false)
    end

    it 'does not persist the pipeline' do
      perform

      expect(pipeline).not_to be_persisted
      expect(pipeline.errors.added?(:base, throttle_message)).to be_truthy
    end

    it 'breaks the chain' do
      perform

      expect(step.break?).to be_truthy
    end

    it 'creates a log entry' do
      expect(Gitlab::AppJsonLogger).to receive(:info).with(
        a_hash_including(
          class: described_class.name,
          project_id: project.id,
          subscription_plan: project.actual_plan_name,
          commit_sha: command.sha
        )
      )

      perform
    end

    context 'with child pipelines' do
      let(:source) { 'parent_pipeline' }

      it 'does not break the chain' do
        perform

        expect(step.break?).to be_falsey
      end

      it 'does not invalidate the pipeline' do
        perform

        expect(pipeline.errors).to be_empty
      end

      it 'does not log anything' do
        expect(Gitlab::AppJsonLogger).not_to receive(:info)

        perform
      end
    end

    context 'when saving incompleted pipelines' do
      let(:save_incompleted) { true }

      it 'does not persist the pipeline' do
        perform

        expect(pipeline).not_to be_persisted
        expect(pipeline.errors.added?(:base, throttle_message)).to be_truthy
      end

      it 'breaks the chain' do
        perform

        expect(step.break?).to be_truthy
      end
    end

    context 'when ci_throttle_pipelines_creation is disabled' do
      before do
        stub_feature_flags(ci_throttle_pipelines_creation: false)
      end

      it 'does not break the chain' do
        perform

        expect(step.break?).to be_falsey
      end

      it 'does not invalidate the pipeline' do
        perform

        expect(pipeline.errors).to be_empty
      end

      it 'does not log anything' do
        expect(Gitlab::AppJsonLogger).not_to receive(:info)

        perform
      end
    end

    context 'when ci_throttle_pipelines_creation_dry_run is enabled' do
      before do
        stub_feature_flags(ci_throttle_pipelines_creation_dry_run: true)
      end

      it 'does not break the chain' do
        perform

        expect(step.break?).to be_falsey
      end

      it 'does not invalidate the pipeline' do
        perform

        expect(pipeline.errors).to be_empty
      end

      it 'creates a log entry' do
        expect(Gitlab::AppJsonLogger).to receive(:info).with(
          a_hash_including(
            class: described_class.name,
            project_id: project.id,
            subscription_plan: project.actual_plan_name,
            commit_sha: command.sha
          )
        )

        perform
      end
    end
  end

  context 'when the limit is not exceeded' do
    it 'does not break the chain' do
      perform

      expect(step.break?).to be_falsey
    end

    it 'does not invalidate the pipeline' do
      perform

      expect(pipeline.errors).to be_empty
    end

    it 'does not log anything' do
      expect(Gitlab::AppJsonLogger).not_to receive(:info)

      perform
    end
  end
end