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
|
|
|
|
2020-06-03 14:08:28 -04:00
|
|
|
RSpec.describe Projects::Prometheus::MetricsController do
|
2020-04-07 08:09:34 -04:00
|
|
|
let_it_be(:user) { create(:user) }
|
|
|
|
let_it_be(:project) { create(:prometheus_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
|
|
|
|
2020-02-04 22:08:49 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:no_content)
|
2018-05-23 11:28:14 -04:00
|
|
|
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
|
|
|
|
2020-02-04 22:08:49 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2018-05-23 11:28:14 -04:00
|
|
|
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
|
|
|
|
2020-02-04 22:08:49 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:not_found)
|
2018-05-23 11:28:14 -04:00
|
|
|
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
|
|
|
|
2020-02-04 22:08:49 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:not_found)
|
2018-05-23 11:28:14 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when prometheus_adapter is disabled' do
|
2020-04-07 08:09:34 -04:00
|
|
|
let(:project) { create(:project) }
|
|
|
|
|
2018-05-23 11:28:14 -04:00
|
|
|
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
|
|
|
|
2020-02-04 22:08:49 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:not_found)
|
2017-06-06 08:40:03 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-04-07 08:09:34 -04:00
|
|
|
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.')
|
|
|
|
|
|
|
|
expect(response).to redirect_to(edit_project_service_path(project, PrometheusService))
|
|
|
|
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
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
expect(response).to redirect_to(edit_project_service_path(project, PrometheusService))
|
|
|
|
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-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
|