summaryrefslogtreecommitdiff
path: root/spec/models/project_services/prometheus_service_spec.rb
blob: 1f9d3c07b513d1676e224d8b6bb28f48ba0704c4 (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
require 'spec_helper'

describe PrometheusService, models: true, caching: true 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 service is active' do
      before { subject.active = true }

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

    context 'when service is inactive' do
      before { subject.active = false }

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

  describe '#test' do
    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[:success]).to be_truthy
        expect(req_stub).to have_been_requested
      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_data)
      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) }

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

      it 'returns reactive data' do
        is_expected.to eq(prometheus_data.merge(deployment_time: deployment.created_at.to_i))
      end
    end
  end

  describe '#calculate_reactive_cache' do
    let(:environment) { create(:environment, slug: 'env-slug') }

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

    subject do
      service.calculate_reactive_cache(environment_query.to_s, environment.id)
    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) }
    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
end