summaryrefslogtreecommitdiff
path: root/spec/services/grafana/proxy_service_spec.rb
blob: 99120de3593d228b1e1b239bb603b8c9f308c33d (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Grafana::ProxyService do
  include ReactiveCachingHelpers

  let_it_be(:project) { create(:project) }
  let_it_be(:grafana_integration) { create(:grafana_integration, project: project) }

  let(:proxy_path) { 'api/v1/query_range' }
  let(:datasource_id) { '1' }
  let(:query_params) do
    {
      'query' => 'rate(relevant_metric)',
      'start' => '1570441248',
      'end' => '1570444848',
      'step' => '900'
    }
  end

  let(:cache_params) { [project.id, datasource_id, proxy_path, query_params] }

  let(:service) do
    described_class.new(project, datasource_id, proxy_path, query_params)
  end

  shared_examples_for 'initializes an instance' do
    it 'initializes an instance of ProxyService class' do
      expect(subject).to be_an_instance_of(described_class)
      expect(subject.project).to eq(project)
      expect(subject.datasource_id).to eq('1')
      expect(subject.proxy_path).to eq('api/v1/query_range')
      expect(subject.query_params).to eq(query_params)
    end
  end

  describe '.from_cache' do
    subject { described_class.from_cache(*cache_params) }

    it_behaves_like 'initializes an instance'
  end

  describe '#initialize' do
    subject { service }

    it_behaves_like 'initializes an instance'
  end

  describe '#execute' do
    subject(:result) { service.execute }

    shared_examples 'missing proxy support' do
      it 'returns API not supported error' do
        expect(result).to eq(
          status: :error,
          message: 'Proxy support for this API is not available currently'
        )
      end
    end

    context 'with unsupported proxy path' do
      where(:proxy_path) do
        %w[
          /api/vl/query_range
          api/vl/query_range/
          api/vl/labels
          api/v2/query_range
          ../../../org/users
        ]
      end

      with_them do
        include_examples 'missing proxy support'
      end
    end

    context 'with unsupported datasource_id' do
      where(:datasource_id) do
        ['', '-1', '1str', 'str1', '../../1', '1/../..', "1\n1"]
      end

      with_them do
        include_examples 'missing proxy support'
      end
    end

    context 'when grafana integration is not configured' do
      before do
        allow(project).to receive(:grafana_integration).and_return(nil)
      end

      include_examples 'missing proxy support'
    end

    context 'with caching', :use_clean_rails_memory_store_caching do
      context 'when value not present in cache' do
        it 'returns nil' do
          expect(ExternalServiceReactiveCachingWorker)
            .to receive(:perform_async)
            .with(service.class, service.id, *cache_params)

          expect(result).to eq(nil)
        end
      end

      context 'when value present in cache' do
        let(:return_value) { { 'http_status' => 200, 'body' => 'body' } }

        before do
          stub_reactive_cache(service, return_value, cache_params)
        end

        it 'returns cached value' do
          expect(ReactiveCachingWorker)
            .not_to receive(:perform_async)
            .with(service.class, service.id, *cache_params)

          expect(result[:http_status]).to eq(return_value[:http_status])
          expect(result[:body]).to eq(return_value[:body])
        end
      end
    end

    context 'call prometheus api' do
      let(:client) { service.send(:client) }

      before do
        synchronous_reactive_cache(service)
      end

      context 'connection to grafana datasource succeeds' do
        let(:response) { instance_double(Gitlab::HTTP::Response) }
        let(:status_code) { 400 }
        let(:body) { 'body' }

        before do
          allow(client).to receive(:proxy_datasource).and_return(response)

          allow(response).to receive(:code).and_return(status_code)
          allow(response).to receive(:body).and_return(body)
        end

        it 'returns the http status code and body from prometheus' do
          expect(result).to eq(
            http_status: status_code,
            body: body,
            status: :success
          )
        end
      end

      context 'connection to grafana datasource fails' do
        before do
          allow(client).to receive(:proxy_datasource)
            .and_raise(Grafana::Client::Error, 'Network connection error')
        end

        it 'returns error' do
          expect(result).to eq(
            status: :error,
            message: 'Network connection error',
            http_status: :service_unavailable
          )
        end
      end
    end
  end
end