summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/metrics/dashboard/stages/grafana_formatter_spec.rb
blob: 5c2ec6dae6b079174c5ca6dceb61c87da7f1dd33 (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
# frozen_string_literal: true

require 'spec_helper'

describe Gitlab::Metrics::Dashboard::Stages::GrafanaFormatter do
  include GrafanaApiHelpers

  let_it_be(:namespace) { create(:namespace, name: 'foo') }
  let_it_be(:project) { create(:project, namespace: namespace, name: 'bar') }

  describe '#transform!' do
    let(:grafana_dashboard) { JSON.parse(fixture_file('grafana/simplified_dashboard_response.json'), symbolize_names: true) }
    let(:datasource) { JSON.parse(fixture_file('grafana/datasource_response.json'), symbolize_names: true) }

    let(:dashboard) { described_class.new(project, {}, params).transform! }

    let(:params) do
      {
        grafana_dashboard: grafana_dashboard,
        datasource: datasource,
        grafana_url: valid_grafana_dashboard_link('https://grafana.example.com')
      }
    end

    context 'when the query and resources are configured correctly' do
      let(:expected_dashboard) { JSON.parse(fixture_file('grafana/expected_grafana_embed.json'), symbolize_names: true) }

      it 'generates a gitlab-yml formatted dashboard' do
        expect(dashboard).to eq(expected_dashboard)
      end
    end

    context 'when the inputs are invalid' do
      shared_examples_for 'processing error' do
        it 'raises a processing error' do
          expect { dashboard }
            .to raise_error(Gitlab::Metrics::Dashboard::Stages::InputFormatValidator::DashboardProcessingError)
        end
      end

      context 'when the datasource is not proxyable' do
        before do
          params[:datasource][:access] = 'not-proxy'
        end

        it_behaves_like 'processing error'
      end

      context 'when query param "panelId" is not specified' do
        before do
          params[:grafana_url].gsub!('panelId=8', '')
        end

        it_behaves_like 'processing error'
      end

      context 'when query param "from" is not specified' do
        before do
          params[:grafana_url].gsub!('from=1570397739557', '')
        end

        it_behaves_like 'processing error'
      end

      context 'when query param "to" is not specified' do
        before do
          params[:grafana_url].gsub!('to=1570484139557', '')
        end

        it_behaves_like 'processing error'
      end

      context 'when the panel is not a graph' do
        before do
          params[:grafana_dashboard][:dashboard][:panels][0][:type] = 'singlestat'
        end

        it_behaves_like 'processing error'
      end

      context 'when the panel is not a line graph' do
        before do
          params[:grafana_dashboard][:dashboard][:panels][0][:lines] = false
        end

        it_behaves_like 'processing error'
      end

      context 'when the query dashboard includes undefined variables' do
        before do
          params[:grafana_url].gsub!('&var-instance=localhost:9121', '')
        end

        it_behaves_like 'processing error'
      end

      context 'when the expression contains unsupported global variables' do
        before do
          params[:grafana_dashboard][:dashboard][:panels][0][:targets][0][:expr] = 'sum(important_metric[$__interval_ms])'
        end

        it_behaves_like 'processing error'
      end
    end
  end
end