summaryrefslogtreecommitdiff
path: root/spec/services/metrics/dashboard/grafana_metric_embed_service_spec.rb
blob: f200c636aacf67c4443c854686da6e48fb2b77d1 (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
170
171
172
173
174
175
176
177
# frozen_string_literal: true

require 'spec_helper'

describe Metrics::Dashboard::GrafanaMetricEmbedService do
  include MetricsDashboardHelpers
  include ReactiveCachingHelpers
  include GrafanaApiHelpers

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

  let(:grafana_url) do
    valid_grafana_dashboard_link(grafana_integration.grafana_url)
  end

  before do
    project.add_maintainer(user)
  end

  describe '.valid_params?' do
    let(:valid_params) { { embedded: true, grafana_url: grafana_url } }

    subject { described_class.valid_params?(params) }

    let(:params) { valid_params }

    it { is_expected.to be_truthy }

    context 'not embedded' do
      let(:params) { valid_params.except(:embedded) }

      it { is_expected.to be_falsey }
    end

    context 'undefined grafana_url' do
      let(:params) { valid_params.except(:grafana_url) }

      it { is_expected.to be_falsey }
    end
  end

  describe '.from_cache' do
    let(:params) { [project.id, user.id, grafana_url] }

    subject { described_class.from_cache(*params) }

    it 'initializes an instance of GrafanaMetricEmbedService' do
      expect(subject).to be_an_instance_of(described_class)
      expect(subject.project).to eq(project)
      expect(subject.current_user).to eq(user)
      expect(subject.params[:grafana_url]).to eq(grafana_url)
    end
  end

  describe '#get_dashboard', :use_clean_rails_memory_store_caching do
    let(:service_params) do
      [
        project,
        user,
        {
          embedded: true,
          grafana_url: grafana_url
        }
      ]
    end

    let(:service) { described_class.new(*service_params) }
    let(:service_call) { service.get_dashboard }

    context 'without caching' do
      before do
        synchronous_reactive_cache(service)
      end

      it_behaves_like 'raises error for users with insufficient permissions'

      context 'without a grafana integration' do
        before do
          allow(project).to receive(:grafana_integration).and_return(nil)
        end

        it_behaves_like 'misconfigured dashboard service response', :bad_request
      end

      context 'when grafana cannot be reached' do
        before do
          allow(grafana_integration.client).to receive(:get_dashboard).and_raise(::Grafana::Client::Error)
        end

        it_behaves_like 'misconfigured dashboard service response', :service_unavailable
      end

      context 'when panelId is missing' do
        let(:grafana_url) do
          grafana_integration.grafana_url +
            '/d/XDaNK6amz/gitlab-omnibus-redis' \
            '?from=1570397739557&to=1570484139557'
        end

        before do
          stub_dashboard_request(grafana_integration.grafana_url)
        end

        it_behaves_like 'misconfigured dashboard service response', :unprocessable_entity
      end

      context 'when uid is missing' do
        let(:grafana_url) { grafana_integration.grafana_url + '/d/' }

        before do
          stub_dashboard_request(grafana_integration.grafana_url)
        end

        it_behaves_like 'misconfigured dashboard service response', :unprocessable_entity
      end

      context 'when the dashboard response contains misconfigured json' do
        before do
          stub_dashboard_request(grafana_integration.grafana_url, body: '')
        end

        it_behaves_like 'misconfigured dashboard service response', :unprocessable_entity
      end

      context 'when the datasource response contains misconfigured json' do
        before do
          stub_dashboard_request(grafana_integration.grafana_url)
          stub_datasource_request(grafana_integration.grafana_url, body: '')
        end

        it_behaves_like 'misconfigured dashboard service response', :unprocessable_entity
      end

      context 'when the embed was created successfully' do
        before do
          stub_dashboard_request(grafana_integration.grafana_url)
          stub_datasource_request(grafana_integration.grafana_url)
        end

        it_behaves_like 'valid embedded dashboard service response'
      end
    end

    context 'with caching', :use_clean_rails_memory_store_caching do
      let(:cache_params) { [project.id, user.id, grafana_url] }

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

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

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

        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(service_call[:http_status]).to eq(return_value[:http_status])
          expect(service_call[:dashboard]).to eq(return_value[:dashboard])
        end
      end
    end
  end
end