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

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

218 lines
6.7 KiB
Ruby
Raw Permalink 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'
2020-06-23 00:09:42 +05:30
RSpec.describe Projects::Prometheus::MetricsController do
2020-04-22 19:07:51 +05:30
let_it_be(:user) { create(:user) }
2022-11-25 23:54:43 +05:30
let_it_be(:project) { create(:project, :with_prometheus_integration) }
2018-03-27 19:54:05 +05:30
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-13 15:44:24 +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-13 15:44:24 +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-13 15:44:24 +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-13 15:44:24 +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
2020-04-22 19:07:51 +05:30
let(:project) { create(:project) }
2018-10-15 14:42:47 +05:30
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-13 15:44:24 +05:30
expect(response).to have_gitlab_http_status(:not_found)
2018-03-27 19:54:05 +05:30
end
end
end
2020-04-22 19:07:51 +05:30
describe 'POST #validate_query' do
before do
allow(controller).to receive(:prometheus_adapter).and_return(prometheus_adapter)
allow(prometheus_adapter).to receive(:query).with(:validate, query) { validation_result }
end
let(:query) { 'avg(metric)' }
context 'validation information is ready' do
let(:validation_result) { { valid: true } }
it 'validation data is returned' do
post :validate_query, params: project_params(format: :json, query: query)
expect(json_response).to eq('valid' => true)
end
end
context 'validation information is not ready' do
let(:validation_result) { nil }
it 'validation data is returned' do
post :validate_query, params: project_params(format: :json, query: query)
expect(response).to have_gitlab_http_status(:accepted)
end
end
end
describe 'GET #index' do
context 'with custom metric present' do
let!(:prometheus_metric) { create(:prometheus_metric, project: project) }
it 'returns a list of metrics' do
get :index, params: project_params(format: :json)
expect(response).to have_gitlab_http_status(:ok)
expect(response).to match_response_schema('prometheus/metrics')
end
end
context 'without custom metrics ' do
it 'returns an empty json' do
get :index, params: project_params(format: :json)
expect(response).to have_gitlab_http_status(:ok)
expect(json_response).to eq({})
end
end
end
describe 'POST #create' do
context 'metric is valid' do
let(:valid_metric) { { prometheus_metric: { title: 'title', query: 'query', group: 'business', y_label: 'label', unit: 'u', legend: 'legend' } } }
it 'shows a success flash message' do
post :create, params: project_params(valid_metric)
expect(flash[:notice]).to include('Metric was successfully added.')
2022-07-23 23:45:48 +05:30
expect(response).to redirect_to(edit_project_settings_integration_path(project, ::Integrations::Prometheus))
2020-04-22 19:07:51 +05:30
end
end
context 'metric is invalid' do
let(:invalid_metric) { { prometheus_metric: { title: 'title' } } }
it 'renders new metric page' do
post :create, params: project_params(invalid_metric)
expect(response).to have_gitlab_http_status(:ok)
expect(response).to render_template('new')
end
end
end
2022-03-02 08:16:31 +05:30
describe 'PUT #update' do
context 'metric is updated' do
let_it_be(:metric) { create(:prometheus_metric, project: project) }
let(:metric_params) { { prometheus_metric: { title: 'new_title' }, id: metric.id } }
it 'shows a success flash message' do
put :update, params: project_params(metric_params)
expect(metric.reload.title).to eq('new_title')
expect(flash[:notice]).to include('Metric was successfully updated.')
2022-07-23 23:45:48 +05:30
expect(response).to redirect_to(edit_project_settings_integration_path(project, ::Integrations::Prometheus))
2022-03-02 08:16:31 +05:30
end
end
end
2020-04-22 19:07:51 +05:30
describe 'DELETE #destroy' do
context 'format html' do
let!(:metric) { create(:prometheus_metric, project: project) }
it 'destroys the metric' do
delete :destroy, params: project_params(id: metric.id)
2022-07-23 23:45:48 +05:30
expect(response).to redirect_to(edit_project_settings_integration_path(project, ::Integrations::Prometheus))
2020-04-22 19:07:51 +05:30
expect(PrometheusMetric.find_by(id: metric.id)).to be_nil
end
end
context 'format json' do
let!(:metric) { create(:prometheus_metric, project: project) }
it 'destroys the metric' do
delete :destroy, params: project_params(id: metric.id, format: :json)
expect(response).to have_gitlab_http_status(:ok)
expect(PrometheusMetric.find_by(id: metric.id)).to be_nil
end
end
end
2018-03-27 19:54:05 +05:30
describe '#prometheus_adapter' do
before do
allow(controller).to receive(:project).and_return(project)
end
it 'calls prometheus adapter service' do
2020-03-13 15:44:24 +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