summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/metrics/dashboard/stages/grafana_formatter_spec.rb
blob: e41004bb57edf8094b7c51bdfbf027b9faf3e502 (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
# 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(:expected_dashboard) { JSON.parse(fixture_file('grafana/expected_grafana_embed.json'), symbolize_names: true) }

    subject(: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
      it { is_expected.to eq expected_dashboard }
    end

    context 'when a panelId is not included in the grafana_url' do
      before do
        params[:grafana_url].gsub('&panelId=8', '')
      end

      it { is_expected.to eq expected_dashboard }

      context 'when there is also no valid panel in the dashboard' do
        before do
          params[:grafana_dashboard][:dashboard][:panels] = []
        end

        it 'raises a processing error' do
          expect { dashboard }.to raise_error(::Gitlab::Metrics::Dashboard::Errors::DashboardProcessingError)
        end
      end
    end

    context 'when an input is invalid' do
      before do
        params[:datasource][:access] = 'not-proxy'
      end

      it 'raises a processing error' do
        expect { dashboard }.to raise_error(::Gitlab::Metrics::Dashboard::Errors::DashboardProcessingError)
      end
    end
  end
end