summaryrefslogtreecommitdiff
path: root/spec/controllers/projects/grafana_api_controller_spec.rb
blob: 9bc4a83030e16d4b91c4a0793206f60f221a38c7 (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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Projects::GrafanaApiController, feature_category: :metrics do
  let_it_be(:project) { create(:project, :public) }
  let_it_be(:reporter) { create(:user) }
  let_it_be(:guest) { create(:user) }
  let(:anonymous) { nil }
  let(:user) { reporter }

  before_all do
    project.add_reporter(reporter)
    project.add_guest(guest)
  end

  before do
    stub_feature_flags(remove_monitor_metrics: false)
    sign_in(user) if user
  end

  describe 'GET #proxy' do
    let(:proxy_service) { instance_double(Grafana::ProxyService) }
    let(:params) do
      {
        namespace_id: project.namespace.full_path,
        project_id: project.path,
        proxy_path: 'api/v1/query_range',
        datasource_id: '1',
        query: 'rate(relevant_metric)',
        start_time: '1570441248',
        end_time: '1570444848',
        step: '900'
      }
    end

    before do
      allow(Grafana::ProxyService).to receive(:new).and_return(proxy_service)
      allow(proxy_service).to receive(:execute).and_return(service_result)
    end

    shared_examples_for 'error response' do |http_status|
      it "returns #{http_status}" do
        get :proxy, params: params

        expect(response).to have_gitlab_http_status(http_status)
        expect(json_response['status']).to eq('error')
        expect(json_response['message']).to eq('error message')
      end
    end

    shared_examples_for 'accessible' do
      let(:service_result) { nil }

      it 'returns non erroneous response' do
        get :proxy, params: params

        # We don't care about the specific code as long it's not an error.
        expect(response).to have_gitlab_http_status(:no_content)
      end
    end

    shared_examples_for 'not accessible' do
      let(:service_result) { nil }

      it 'returns 404 Not found' do
        get :proxy, params: params

        expect(response).to have_gitlab_http_status(:not_found)
        expect(Grafana::ProxyService).not_to have_received(:new)
      end
    end

    shared_examples_for 'login required' do
      let(:service_result) { nil }

      it 'redirects to login page' do
        get :proxy, params: params

        expect(response).to redirect_to(new_user_session_path)
        expect(Grafana::ProxyService).not_to have_received(:new)
      end
    end

    context 'with a successful result' do
      let(:service_result) { { status: :success, body: '{}' } }

      it 'returns a grafana datasource response' do
        get :proxy, params: params

        expect(Grafana::ProxyService).to have_received(:new).with(
          project, '1', 'api/v1/query_range',
          {
            'query' => params[:query],
            'start' => params[:start_time],
            'end' => params[:end_time],
            'step' => params[:step]
          }
        )

        expect(response).to have_gitlab_http_status(:ok)
        expect(json_response).to eq({})
      end
    end

    context 'when the request is still unavailable' do
      let(:service_result) { nil }

      it 'returns 204 no content' do
        get :proxy, params: params

        expect(response).to have_gitlab_http_status(:no_content)
        expect(json_response['status']).to eq('processing')
        expect(json_response['message']).to eq('Not ready yet. Try again later.')
      end
    end

    context 'when an error has occurred' do
      context 'with an error accessing grafana' do
        let(:service_result) do
          {
            http_status: :service_unavailable,
            status: :error,
            message: 'error message'
          }
        end

        it_behaves_like 'error response', :service_unavailable
      end

      context 'with a processing error' do
        let(:service_result) do
          {
            status: :error,
            message: 'error message'
          }
        end

        it_behaves_like 'error response', :bad_request
      end
    end

    context 'as guest' do
      let(:user) { guest }

      it_behaves_like 'not accessible'
    end

    context 'as anonymous' do
      let(:user) { anonymous }

      it_behaves_like 'not accessible'
    end

    context 'on a private project' do
      let_it_be(:project) { create(:project, :private) }

      before_all do
        project.add_guest(guest)
      end

      context 'as anonymous' do
        let(:user) { anonymous }

        it_behaves_like 'login required'
      end

      context 'as guest' do
        let(:user) { guest }

        it_behaves_like 'accessible'
      end
    end

    context 'when metrics dashboard feature is unavailable' do
      before do
        stub_feature_flags(remove_monitor_metrics: true)
      end

      it_behaves_like 'not accessible'
    end
  end

  describe 'GET #metrics_dashboard' do
    let(:service_result) { { status: :success, dashboard: '{}' } }
    let(:params) do
      {
        format: :json,
        embedded: true,
        grafana_url: 'https://grafana.example.com',
        namespace_id: project.namespace.full_path,
        project_id: project.path
      }
    end

    before do
      allow(Gitlab::Metrics::Dashboard::Finder)
      .to receive(:find)
      .and_return(service_result)
    end

    context 'when the result is still processing' do
      let(:service_result) { nil }

      it 'returns 204 no content' do
        get :metrics_dashboard, params: params

        expect(response).to have_gitlab_http_status(:no_content)
      end
    end

    context 'when the result was successful' do
      it 'returns the dashboard response' do
        get :metrics_dashboard, params: params

        expect(response).to have_gitlab_http_status(:ok)
        expect(json_response).to include({
          'dashboard' => '{}',
          'status' => 'success'
        })
        expect(json_response).to include('metrics_data')
      end
    end

    context 'when an error has occurred' do
      shared_examples_for 'error response' do |http_status|
        it "returns #{http_status}" do
          get :metrics_dashboard, params: params

          expect(response).to have_gitlab_http_status(http_status)
          expect(json_response['status']).to eq('error')
          expect(json_response['message']).to eq('error message')
        end
      end

      context 'with an error accessing grafana' do
        let(:service_result) do
          {
            http_status: :service_unavailable,
            status: :error,
            message: 'error message'
          }
        end

        it_behaves_like 'error response', :service_unavailable
      end

      context 'with a processing error' do
        let(:service_result) { { status: :error, message: 'error message' } }

        it_behaves_like 'error response', :bad_request
      end

      context 'when metrics dashboard feature is unavailable' do
        before do
          stub_feature_flags(remove_monitor_metrics: true)
        end

        it 'returns 404 Not found' do
          get :metrics_dashboard, params: params

          expect(response).to have_gitlab_http_status(:not_found)
          expect(response.body).to be_empty
        end
      end
    end
  end
end