Render 404 when prometheus adapter is disabled in Prometheus metrics controller

This commit is contained in:
Tiago Botelho 2018-05-23 15:28:14 +00:00 committed by Rémy Coutable
parent a2dbca4a27
commit 58313de430
3 changed files with 60 additions and 32 deletions

View file

@ -25,7 +25,7 @@ module Projects
end
def require_prometheus_metrics!
render_404 unless prometheus_adapter.can_query?
render_404 unless prometheus_adapter&.can_query?
end
end
end

View file

@ -0,0 +1,5 @@
---
title: Render 404 when prometheus adapter is disabled in Prometheus metrics controller
merge_request: 19110
author:
type: fixed

View file

@ -12,44 +12,67 @@ describe Projects::Prometheus::MetricsController do
end
describe 'GET #active_common' do
before do
allow(controller).to receive(:prometheus_adapter).and_return(prometheus_adapter)
context 'when prometheus_adapter can query' do
before do
allow(controller).to receive(:prometheus_adapter).and_return(prometheus_adapter)
end
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
it 'returns no content response' do
get :active_common, project_params(format: :json)
expect(response).to have_gitlab_http_status(204)
end
end
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
it 'returns no content response' do
get :active_common, project_params(format: :json)
expect(response).to have_gitlab_http_status(200)
expect(json_response).to eq(sample_response.deep_stringify_keys)
end
end
context 'when requesting non json response' do
it 'returns not found response' do
get :active_common, project_params
expect(response).to have_gitlab_http_status(404)
end
end
end
end
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
context 'when prometheus_adapter cannot query' do
it 'renders 404' do
prometheus_adapter = double('prometheus_adapter', can_query?: false)
it 'returns no content response' do
get :active_common, project_params(format: :json)
allow(controller).to receive(:prometheus_adapter).and_return(prometheus_adapter)
allow(prometheus_adapter).to receive(:query).with(:matched_metrics).and_return({})
expect(response).to have_gitlab_http_status(204)
end
get :active_common, project_params(format: :json)
expect(response).to have_gitlab_http_status(404)
end
end
context 'when data is available' do
let(:sample_response) { { some_data: 1 } }
context 'when prometheus_adapter is disabled' do
it 'renders 404' do
get :active_common, project_params(format: :json)
before do
allow(prometheus_adapter).to receive(:query).with(:matched_metrics).and_return(sample_response)
end
it 'returns no content response' do
get :active_common, project_params(format: :json)
expect(response).to have_gitlab_http_status(200)
expect(json_response).to eq(sample_response.deep_stringify_keys)
end
end
context 'when requesting non json response' do
it 'returns not found response' do
get :active_common, project_params
expect(response).to have_gitlab_http_status(404)
end
expect(response).to have_gitlab_http_status(404)
end
end
end