2019-04-15 06:17:05 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-02-23 12:58:40 -05:00
|
|
|
require 'spec_helper'
|
2017-06-06 08:40:03 -04:00
|
|
|
|
2018-02-23 12:58:40 -05:00
|
|
|
describe Projects::Prometheus::MetricsController do
|
2017-06-06 08:40:03 -04:00
|
|
|
let(:user) { create(:user) }
|
2018-02-23 12:58:40 -05:00
|
|
|
let(:project) { create(:project) }
|
2017-06-06 08:40:03 -04:00
|
|
|
|
2018-02-23 15:33:33 -05:00
|
|
|
let(:prometheus_adapter) { double('prometheus_adapter', can_query?: true) }
|
2017-06-06 08:40:03 -04:00
|
|
|
|
|
|
|
before do
|
2018-07-11 10:36:08 -04:00
|
|
|
project.add_maintainer(user)
|
2017-06-06 08:40:03 -04:00
|
|
|
sign_in(user)
|
|
|
|
end
|
|
|
|
|
2018-02-23 12:58:40 -05:00
|
|
|
describe 'GET #active_common' do
|
2018-05-23 11:28:14 -04:00
|
|
|
context 'when prometheus_adapter can query' do
|
|
|
|
before do
|
|
|
|
allow(controller).to receive(:prometheus_adapter).and_return(prometheus_adapter)
|
|
|
|
end
|
2018-03-05 15:02:26 -05:00
|
|
|
|
2018-05-23 11:28:14 -04:00
|
|
|
context 'when prometheus metrics are enabled' do
|
|
|
|
context 'when data is not present' do
|
|
|
|
before do
|
|
|
|
allow(prometheus_adapter).to receive(:query).with(:matched_metrics).and_return({})
|
|
|
|
end
|
2017-06-06 20:36:59 -04:00
|
|
|
|
2018-05-23 11:28:14 -04:00
|
|
|
it 'returns no content response' do
|
2018-12-17 17:52:17 -05:00
|
|
|
get :active_common, params: project_params(format: :json)
|
2017-06-06 08:40:03 -04:00
|
|
|
|
2018-05-23 11:28:14 -04:00
|
|
|
expect(response).to have_gitlab_http_status(204)
|
|
|
|
end
|
2017-06-06 08:40:03 -04:00
|
|
|
end
|
|
|
|
|
2018-05-23 11:28:14 -04:00
|
|
|
context 'when data is available' do
|
|
|
|
let(:sample_response) { { some_data: 1 } }
|
|
|
|
|
|
|
|
before do
|
|
|
|
allow(prometheus_adapter).to receive(:query).with(:matched_metrics).and_return(sample_response)
|
|
|
|
end
|
2017-06-06 08:40:03 -04:00
|
|
|
|
2018-05-23 11:28:14 -04:00
|
|
|
it 'returns no content response' do
|
2018-12-17 17:52:17 -05:00
|
|
|
get :active_common, params: project_params(format: :json)
|
2018-05-23 11:28:14 -04:00
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(200)
|
|
|
|
expect(json_response).to eq(sample_response.deep_stringify_keys)
|
|
|
|
end
|
2017-06-06 08:40:03 -04:00
|
|
|
end
|
|
|
|
|
2018-05-23 11:28:14 -04:00
|
|
|
context 'when requesting non json response' do
|
|
|
|
it 'returns not found response' do
|
2018-12-17 17:52:17 -05:00
|
|
|
get :active_common, params: project_params
|
2017-06-06 08:40:03 -04:00
|
|
|
|
2018-05-23 11:28:14 -04:00
|
|
|
expect(response).to have_gitlab_http_status(404)
|
|
|
|
end
|
2017-06-06 08:40:03 -04:00
|
|
|
end
|
|
|
|
end
|
2018-05-23 11:28:14 -04:00
|
|
|
end
|
2017-06-06 08:40:03 -04:00
|
|
|
|
2018-05-23 11:28:14 -04:00
|
|
|
context 'when prometheus_adapter cannot query' do
|
|
|
|
it 'renders 404' do
|
|
|
|
prometheus_adapter = double('prometheus_adapter', can_query?: false)
|
2017-06-06 08:40:03 -04:00
|
|
|
|
2018-05-23 11:28:14 -04:00
|
|
|
allow(controller).to receive(:prometheus_adapter).and_return(prometheus_adapter)
|
|
|
|
allow(prometheus_adapter).to receive(:query).with(:matched_metrics).and_return({})
|
|
|
|
|
2018-12-17 17:52:17 -05:00
|
|
|
get :active_common, params: project_params(format: :json)
|
2018-05-23 11:28:14 -04:00
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(404)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when prometheus_adapter is disabled' do
|
|
|
|
it 'renders 404' do
|
2018-12-17 17:52:17 -05:00
|
|
|
get :active_common, params: project_params(format: :json)
|
2018-05-23 11:28:14 -04:00
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(404)
|
2017-06-06 08:40:03 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-03-05 15:02:26 -05:00
|
|
|
describe '#prometheus_adapter' do
|
|
|
|
before do
|
|
|
|
allow(controller).to receive(:project).and_return(project)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'calls prometheus adapter service' do
|
2020-01-08 04:07:53 -05:00
|
|
|
expect_next_instance_of(::Gitlab::Prometheus::Adapter) do |instance|
|
2019-11-13 22:06:25 -05:00
|
|
|
expect(instance).to receive(:prometheus_adapter)
|
|
|
|
end
|
2018-03-05 15:02:26 -05:00
|
|
|
|
|
|
|
subject.__send__(:prometheus_adapter)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-06-06 08:40:03 -04:00
|
|
|
def project_params(opts = {})
|
|
|
|
opts.reverse_merge(namespace_id: project.namespace, project_id: project)
|
|
|
|
end
|
2017-06-06 10:07:33 -04:00
|
|
|
end
|