summaryrefslogtreecommitdiff
path: root/spec/models/project_services/prometheus_service_spec.rb
blob: 6693e5783a56d30f76eb6d4453828293705f83fa (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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
require 'spec_helper'

describe PrometheusService, :use_clean_rails_memory_store_caching do
  include PrometheusHelpers
  include ReactiveCachingHelpers

  let(:project) { create(:prometheus_project) }
  let(:service) { project.prometheus_service }
  let(:environment_query) { Gitlab::Prometheus::Queries::EnvironmentQuery }

  describe "Associations" do
    it { is_expected.to belong_to :project }
  end

  describe 'Validations' do
    context 'when manual_configuration is enabled' do
      before do
        subject.manual_configuration = true
      end

      it { is_expected.to validate_presence_of(:api_url) }
    end

    context 'when manual configuration is disabled' do
      before do
        subject.manual_configuration = false
      end

      it { is_expected.not_to validate_presence_of(:api_url) }
    end
  end

  describe '#test' do
    before do
      service.manual_configuration = true
    end

    let!(:req_stub) { stub_prometheus_request(prometheus_query_url('1'), body: prometheus_value_body('vector')) }

    context 'success' do
      it 'reads the discovery endpoint' do
        expect(service.test[:result]).to eq('Checked API endpoint')
        expect(service.test[:success]).to be_truthy
        expect(req_stub).to have_been_requested.twice
      end
    end

    context 'failure' do
      let!(:req_stub) { stub_prometheus_request(prometheus_query_url('1'), status: 404) }

      it 'fails to read the discovery endpoint' do
        expect(service.test[:success]).to be_falsy
        expect(req_stub).to have_been_requested
      end
    end
  end

  describe '#environment_metrics' do
    let(:environment) { build_stubbed(:environment, slug: 'env-slug') }

    around do |example|
      Timecop.freeze { example.run }
    end

    context 'with valid data' do
      subject { service.environment_metrics(environment) }

      before do
        stub_reactive_cache(service, prometheus_data, environment_query, environment.id)
      end

      it 'returns reactive data' do
        is_expected.to eq(prometheus_metrics_data)
      end
    end
  end

  describe '#matched_metrics' do
    let(:matched_metrics_query) { Gitlab::Prometheus::Queries::MatchedMetricsQuery }
    let(:client) { double(:client, label_values: nil) }

    context 'with valid data' do
      subject { service.matched_metrics }

      before do
        allow(service).to receive(:client).and_return(client)
        synchronous_reactive_cache(service)
      end

      it 'returns reactive data' do
        expect(subject[:success]).to be_truthy
        expect(subject[:data]).to eq([])
      end
    end
  end

  describe '#deployment_metrics' do
    let(:deployment) { build_stubbed(:deployment) }
    let(:deployment_query) { Gitlab::Prometheus::Queries::DeploymentQuery }

    around do |example|
      Timecop.freeze { example.run }
    end

    context 'with valid data' do
      subject { service.deployment_metrics(deployment) }
      let(:fake_deployment_time) { 10 }

      before do
        stub_reactive_cache(service, prometheus_data, deployment_query, deployment.environment.id, deployment.id)
      end

      it 'returns reactive data' do
        expect(deployment).to receive(:created_at).and_return(fake_deployment_time)

        expect(subject).to eq(prometheus_metrics_data.merge(deployment_time: fake_deployment_time))
      end
    end
  end

  describe '#calculate_reactive_cache' do
    let(:environment) { create(:environment, slug: 'env-slug') }
    before do
      service.manual_configuration = true
      service.active = true
    end

    subject do
      service.calculate_reactive_cache(environment_query.name, environment.id)
    end

    around do |example|
      Timecop.freeze { example.run }
    end

    context 'when service is inactive' do
      before do
        service.active = false
      end

      it { is_expected.to be_nil }
    end

    context 'when Prometheus responds with valid data' do
      before do
        stub_all_prometheus_requests(environment.slug)
      end

      it { expect(subject.to_json).to eq(prometheus_data.to_json) }
      it { expect(subject.to_json).to eq(prometheus_data.to_json) }
    end

    [404, 500].each do |status|
      context "when Prometheus responds with #{status}" do
        before do
          stub_all_prometheus_requests(environment.slug, status: status, body: "QUERY FAILED!")
        end

        it { is_expected.to eq(success: false, result: %(#{status} - "QUERY FAILED!")) }
      end
    end
  end

  describe '#client' do
    context 'manual configuration is enabled' do
      let(:api_url) { 'http://some_url' }
      before do
        subject.manual_configuration = true
        subject.api_url = api_url
      end

      it 'returns simple rest client from api_url' do
        expect(subject.client).to be_instance_of(Gitlab::PrometheusClient)
        expect(subject.client.rest_client.url).to eq(api_url)
      end
    end

    context 'manual configuration is disabled' do
      let!(:cluster_for_all) { create(:cluster, environment_scope: '*', projects: [project]) }
      let!(:cluster_for_dev) { create(:cluster, environment_scope: 'dev', projects: [project]) }

      let!(:prometheus_for_dev) { create(:clusters_applications_prometheus, :installed, cluster: cluster_for_dev) }
      let(:proxy_client) { double('proxy_client') }

      before do
        service.manual_configuration = false
      end

      context 'with cluster for all environments with prometheus installed' do
        let!(:prometheus_for_all) { create(:clusters_applications_prometheus, :installed, cluster: cluster_for_all) }

        context 'without environment supplied' do
          it 'returns client handling all environments' do
            expect(service).to receive(:client_from_cluster).with(cluster_for_all).and_return(proxy_client).twice

            expect(service.client).to be_instance_of(Gitlab::PrometheusClient)
            expect(service.client.rest_client).to eq(proxy_client)
          end
        end

        context 'with dev environment supplied' do
          let!(:environment) { create(:environment, project: project, name: 'dev') }

          it 'returns dev cluster client' do
            expect(service).to receive(:client_from_cluster).with(cluster_for_dev).and_return(proxy_client).twice

            expect(service.client(environment.id)).to be_instance_of(Gitlab::PrometheusClient)
            expect(service.client(environment.id).rest_client).to eq(proxy_client)
          end
        end

        context 'with prod environment supplied' do
          let!(:environment) { create(:environment, project: project, name: 'prod') }

          it 'returns dev cluster client' do
            expect(service).to receive(:client_from_cluster).with(cluster_for_all).and_return(proxy_client).twice

            expect(service.client(environment.id)).to be_instance_of(Gitlab::PrometheusClient)
            expect(service.client(environment.id).rest_client).to eq(proxy_client)
          end
        end
      end

      context 'with cluster for all environments without prometheus installed' do
        context 'without environment supplied' do
          it 'raises PrometheusClient::Error because cluster was not found' do
            expect { service.client }.to raise_error(Gitlab::PrometheusClient::Error, /couldn't find cluster with Prometheus installed/)
          end
        end

        context 'with dev environment supplied' do
          let!(:environment) { create(:environment, project: project, name: 'dev') }

          it 'returns dev cluster client' do
            expect(service).to receive(:client_from_cluster).with(cluster_for_dev).and_return(proxy_client).twice

            expect(service.client(environment.id)).to be_instance_of(Gitlab::PrometheusClient)
            expect(service.client(environment.id).rest_client).to eq(proxy_client)
          end
        end

        context 'with prod environment supplied' do
          let!(:environment) { create(:environment, project: project, name: 'prod') }

          it 'raises PrometheusClient::Error because cluster was not found' do
            expect { service.client }.to raise_error(Gitlab::PrometheusClient::Error, /couldn't find cluster with Prometheus installed/)
          end
        end
      end
    end
  end

  describe '#prometheus_installed?' do
    context 'clusters with installed prometheus' do
      let!(:cluster) { create(:cluster, projects: [project]) }
      let!(:prometheus) { create(:clusters_applications_prometheus, :installed, cluster: cluster) }

      it 'returns true' do
        expect(service.prometheus_installed?).to be(true)
      end
    end

    context 'clusters without prometheus installed' do
      let(:cluster) { create(:cluster, projects: [project]) }
      let!(:prometheus) { create(:clusters_applications_prometheus, cluster: cluster) }

      it 'returns false' do
        expect(service.prometheus_installed?).to be(false)
      end
    end

    context 'clusters without prometheus' do
      let(:cluster) { create(:cluster, projects: [project]) }

      it 'returns false' do
        expect(service.prometheus_installed?).to be(false)
      end
    end

    context 'no clusters' do
      it 'returns false' do
        expect(service.prometheus_installed?).to be(false)
      end
    end
  end

  describe '#synchronize_service_state! before_save callback' do
    context 'no clusters with prometheus are installed' do
      context 'when service is inactive' do
        before do
          service.active = false
        end

        it 'activates service when manual_configuration is enabled' do
          expect { service.update!(manual_configuration: true) }.to change { service.active }.from(false).to(true)
        end

        it 'keeps service inactive when manual_configuration is disabled' do
          expect { service.update!(manual_configuration: false) }.not_to change { service.active }.from(false)
        end
      end

      context 'when service is active' do
        before do
          service.active = true
        end

        it 'keeps the service active when manual_configuration is enabled' do
          expect { service.update!(manual_configuration: true) }.not_to change { service.active }.from(true)
        end

        it 'inactivates the service when manual_configuration is disabled' do
          expect { service.update!(manual_configuration: false) }.to change { service.active }.from(true).to(false)
        end
      end
    end

    context 'with prometheus installed in the cluster' do
      before do
        allow(service).to receive(:prometheus_installed?).and_return(true)
      end

      context 'when service is inactive' do
        before do
          service.active = false
        end

        it 'activates service when manual_configuration is enabled' do
          expect { service.update!(manual_configuration: true) }.to change { service.active }.from(false).to(true)
        end

        it 'activates service when manual_configuration is disabled' do
          expect { service.update!(manual_configuration: false) }.to change { service.active }.from(false).to(true)
        end
      end

      context 'when service is active' do
        before do
          service.active = true
        end

        it 'keeps service active when manual_configuration is enabled' do
          expect { service.update!(manual_configuration: true) }.not_to change { service.active }.from(true)
        end

        it 'keeps service active when manual_configuration is disabled' do
          expect { service.update!(manual_configuration: false) }.not_to change { service.active }.from(true)
        end
      end
    end
  end
end