debian-mirror-gitlab/spec/controllers/projects/prometheus/metrics_controller_spec.rb

100 lines
2.9 KiB
Ruby
Raw Normal View History

2019-07-31 22:56:46 +05:30
# frozen_string_literal: true
2018-03-27 19:54:05 +05:30
require 'spec_helper'
describe Projects::Prometheus::MetricsController do
let(:user) { create(:user) }
let(:project) { create(:project) }
let(:prometheus_adapter) { double('prometheus_adapter', can_query?: true) }
before do
2018-11-18 11:00:15 +05:30
project.add_maintainer(user)
2018-03-27 19:54:05 +05:30
sign_in(user)
end
describe 'GET #active_common' do
2018-10-15 14:42:47 +05:30
context 'when prometheus_adapter can query' do
before do
allow(controller).to receive(:prometheus_adapter).and_return(prometheus_adapter)
end
2018-03-27 19:54:05 +05:30
2018-10-15 14:42:47 +05:30
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
2018-03-27 19:54:05 +05:30
2018-10-15 14:42:47 +05:30
it 'returns no content response' do
2019-02-15 15:39:39 +05:30
get :active_common, params: project_params(format: :json)
2018-03-27 19:54:05 +05:30
2020-03-09 13:42:32 +05:30
expect(response).to have_gitlab_http_status(:no_content)
2018-10-15 14:42:47 +05:30
end
2018-03-27 19:54:05 +05:30
end
2018-10-15 14:42:47 +05:30
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
2018-03-27 19:54:05 +05:30
2018-10-15 14:42:47 +05:30
it 'returns no content response' do
2019-02-15 15:39:39 +05:30
get :active_common, params: project_params(format: :json)
2018-10-15 14:42:47 +05:30
2020-03-09 13:42:32 +05:30
expect(response).to have_gitlab_http_status(:ok)
2018-10-15 14:42:47 +05:30
expect(json_response).to eq(sample_response.deep_stringify_keys)
end
2018-03-27 19:54:05 +05:30
end
2018-10-15 14:42:47 +05:30
context 'when requesting non json response' do
it 'returns not found response' do
2019-02-15 15:39:39 +05:30
get :active_common, params: project_params
2018-03-27 19:54:05 +05:30
2020-03-09 13:42:32 +05:30
expect(response).to have_gitlab_http_status(:not_found)
2018-10-15 14:42:47 +05:30
end
2018-03-27 19:54:05 +05:30
end
end
2018-10-15 14:42:47 +05:30
end
2018-03-27 19:54:05 +05:30
2018-10-15 14:42:47 +05:30
context 'when prometheus_adapter cannot query' do
it 'renders 404' do
prometheus_adapter = double('prometheus_adapter', can_query?: false)
2018-03-27 19:54:05 +05:30
2018-10-15 14:42:47 +05:30
allow(controller).to receive(:prometheus_adapter).and_return(prometheus_adapter)
allow(prometheus_adapter).to receive(:query).with(:matched_metrics).and_return({})
2019-02-15 15:39:39 +05:30
get :active_common, params: project_params(format: :json)
2018-10-15 14:42:47 +05:30
2020-03-09 13:42:32 +05:30
expect(response).to have_gitlab_http_status(:not_found)
2018-10-15 14:42:47 +05:30
end
end
context 'when prometheus_adapter is disabled' do
it 'renders 404' do
2019-02-15 15:39:39 +05:30
get :active_common, params: project_params(format: :json)
2018-10-15 14:42:47 +05:30
2020-03-09 13:42:32 +05:30
expect(response).to have_gitlab_http_status(:not_found)
2018-03-27 19:54:05 +05:30
end
end
end
describe '#prometheus_adapter' do
before do
allow(controller).to receive(:project).and_return(project)
end
it 'calls prometheus adapter service' do
2020-03-09 13:42:32 +05:30
expect_next_instance_of(::Gitlab::Prometheus::Adapter) do |instance|
2019-12-26 22:10:19 +05:30
expect(instance).to receive(:prometheus_adapter)
end
2018-03-27 19:54:05 +05:30
subject.__send__(:prometheus_adapter)
end
end
def project_params(opts = {})
opts.reverse_merge(namespace_id: project.namespace, project_id: project)
end
end