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

client_spec.rb « grafana « lib « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c233d0b84457c6cb1840f269ac3e95d017039941 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Grafana::Client do
  let(:grafana_url) { 'https://grafanatest.com/-/grafana-project' }
  let(:token) { 'test-token' }

  subject(:client) { described_class.new(api_url: grafana_url, token: token) }

  shared_examples 'calls grafana api' do
    let!(:grafana_api_request) { stub_grafana_request(grafana_api_url) }

    it 'calls grafana api' do
      subject

      expect(grafana_api_request).to have_been_requested
    end
  end

  shared_examples 'no redirects' do
    let(:redirect_to) { 'https://redirected.example.com' }
    let(:other_url) { 'https://grafana.example.org' }

    let!(:redirected_req_stub) { stub_grafana_request(other_url) }

    let!(:redirect_req_stub) do
      stub_grafana_request(
        grafana_api_url,
        status: 302,
        headers: { location: redirect_to }
      )
    end

    it 'does not follow redirects' do
      expect { subject }.to raise_exception(
        Grafana::Client::Error,
        'Grafana response status code: 302, Message: {}'
      )

      expect(redirect_req_stub).to have_been_requested
      expect(redirected_req_stub).not_to have_been_requested
    end
  end

  shared_examples 'handles exceptions' do
    exceptions = {
      Gitlab::HTTP::Error => 'Error when connecting to Grafana',
      Net::OpenTimeout => 'Connection to Grafana timed out',
      SocketError => 'Received SocketError when trying to connect to Grafana',
      OpenSSL::SSL::SSLError => 'Grafana returned invalid SSL data',
      Errno::ECONNREFUSED => 'Connection refused',
      StandardError => 'Grafana request failed due to StandardError'
    }

    exceptions.each do |exception, message|
      context "#{exception}" do
        before do
          stub_request(:get, grafana_api_url).to_raise(exception)
        end

        it do
          expect { subject }
            .to raise_exception(Grafana::Client::Error, message)
        end
      end
    end
  end

  describe '#get_dashboard' do
    let(:grafana_api_url) { 'https://grafanatest.com/-/grafana-project/api/dashboards/uid/FndfgnX' }

    subject do
      client.get_dashboard(uid: 'FndfgnX')
    end

    it_behaves_like 'calls grafana api'
    it_behaves_like 'no redirects'
    it_behaves_like 'handles exceptions'
  end

  describe '#get_datasource' do
    let(:grafana_api_url) { 'https://grafanatest.com/-/grafana-project/api/datasources/name/Test%20Name' }

    subject do
      client.get_datasource(name: 'Test Name')
    end

    it_behaves_like 'calls grafana api'
    it_behaves_like 'no redirects'
    it_behaves_like 'handles exceptions'
  end

  describe '#proxy_datasource' do
    let(:grafana_api_url) do
      'https://grafanatest.com/-/grafana-project/' \
        'api/datasources/proxy/' \
        '1/api/v1/query_range' \
        '?query=rate(relevant_metric)' \
        '&start=1570441248&end=1570444848&step=900'
    end

    subject do
      client.proxy_datasource(
        datasource_id: '1',
        proxy_path: 'api/v1/query_range',
        query: {
          query: 'rate(relevant_metric)',
          start: 1570441248,
          end: 1570444848,
          step: 900
        }
      )
    end

    it_behaves_like 'calls grafana api'
    it_behaves_like 'no redirects'
    it_behaves_like 'handles exceptions'
  end

  private

  def stub_grafana_request(url, body: {}, status: 200, headers: {})
    stub_request(:get, url)
      .to_return(
        status: status,
        headers: { 'Content-Type' => 'application/json' }.merge(headers),
        body: body.to_json
      )
  end
end