2019-10-12 21:52:04 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
RSpec.describe Metrics::Dashboard::CustomMetricEmbedService do
|
2019-10-12 21:52:04 +05:30
|
|
|
include MetricsDashboardHelpers
|
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
let_it_be(:project, reload: true) { build(:project) }
|
|
|
|
let_it_be(:user) { create(:user) }
|
|
|
|
let_it_be(:environment) { create(:environment, project: project) }
|
2019-10-12 21:52:04 +05:30
|
|
|
|
|
|
|
before do
|
|
|
|
project.add_maintainer(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:dashboard_path) { system_dashboard_path }
|
|
|
|
let(:group) { business_metric_title }
|
|
|
|
let(:title) { 'title' }
|
|
|
|
let(:y_label) { 'y_label' }
|
|
|
|
|
|
|
|
describe '.valid_params?' do
|
|
|
|
let(:valid_params) do
|
|
|
|
{
|
|
|
|
embedded: true,
|
|
|
|
dashboard_path: dashboard_path,
|
|
|
|
group: group,
|
|
|
|
title: title,
|
|
|
|
y_label: y_label
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
subject { described_class.valid_params?(params) }
|
|
|
|
|
|
|
|
let(:params) { valid_params }
|
|
|
|
|
|
|
|
it { is_expected.to be_truthy }
|
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
context 'missing embedded' do
|
2019-10-12 21:52:04 +05:30
|
|
|
let(:params) { valid_params.except(:embedded) }
|
|
|
|
|
|
|
|
it { is_expected.to be_falsey }
|
|
|
|
end
|
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
context 'not embedded' do
|
|
|
|
let(:params) { valid_params.merge(embedded: 'false') }
|
|
|
|
|
|
|
|
it { is_expected.to be_falsey }
|
|
|
|
end
|
|
|
|
|
2019-10-12 21:52:04 +05:30
|
|
|
context 'non-system dashboard' do
|
|
|
|
let(:dashboard_path) { '.gitlab/dashboards/test.yml' }
|
|
|
|
|
|
|
|
it { is_expected.to be_falsey }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'undefined dashboard' do
|
|
|
|
let(:params) { valid_params.except(:dashboard_path) }
|
|
|
|
|
|
|
|
it { is_expected.to be_truthy }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'non-custom metric group' do
|
|
|
|
let(:group) { 'Different Group' }
|
|
|
|
|
|
|
|
it { is_expected.to be_falsey }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'missing group' do
|
|
|
|
let(:group) { nil }
|
|
|
|
|
|
|
|
it { is_expected.to be_falsey }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'missing title' do
|
|
|
|
let(:title) { nil }
|
|
|
|
|
|
|
|
it { is_expected.to be_falsey }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'undefined y-axis label' do
|
|
|
|
let(:params) { valid_params.except(:y_label) }
|
|
|
|
|
|
|
|
it { is_expected.to be_falsey }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#get_dashboard' do
|
|
|
|
let(:service_params) do
|
|
|
|
[
|
|
|
|
project,
|
|
|
|
user,
|
|
|
|
{
|
|
|
|
embedded: true,
|
|
|
|
environment: environment,
|
|
|
|
dashboard_path: dashboard_path,
|
|
|
|
group: group,
|
|
|
|
title: title,
|
|
|
|
y_label: y_label
|
|
|
|
}
|
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:service_call) { described_class.new(*service_params).get_dashboard }
|
|
|
|
|
|
|
|
it_behaves_like 'misconfigured dashboard service response', :not_found
|
|
|
|
it_behaves_like 'raises error for users with insufficient permissions'
|
|
|
|
|
|
|
|
context 'the custom metric exists' do
|
|
|
|
let!(:metric) { create(:prometheus_metric, project: project) }
|
|
|
|
|
|
|
|
it_behaves_like 'valid embedded dashboard service response'
|
|
|
|
|
|
|
|
it 'does not cache the unprocessed dashboard' do
|
2020-10-24 23:57:45 +05:30
|
|
|
# Fail spec if any method of Cache class is called.
|
|
|
|
stub_const('Gitlab::Metrics::Dashboard::Cache', double)
|
2019-10-12 21:52:04 +05:30
|
|
|
|
|
|
|
described_class.new(*service_params).get_dashboard
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'multiple metrics meet criteria' do
|
|
|
|
let!(:metric_2) { create(:prometheus_metric, project: project, query: 'avg(metric_2)') }
|
|
|
|
|
|
|
|
it_behaves_like 'valid embedded dashboard service response'
|
|
|
|
|
2020-04-22 19:07:51 +05:30
|
|
|
it 'includes both metrics in a single panel' do
|
2019-10-12 21:52:04 +05:30
|
|
|
result = service_call
|
|
|
|
|
2020-04-22 19:07:51 +05:30
|
|
|
panel_groups = result[:dashboard][:panel_groups]
|
|
|
|
panels = panel_groups[0][:panels]
|
|
|
|
metrics = panels[0][:metrics]
|
|
|
|
queries = metrics.map { |metric| metric[:query_range] }
|
|
|
|
|
|
|
|
expect(panel_groups.length).to eq(1)
|
|
|
|
expect(panels.length).to eq(1)
|
|
|
|
expect(metrics.length).to eq(2)
|
|
|
|
expect(queries).to include('avg(metric_2)', 'avg(metric)')
|
2019-10-12 21:52:04 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the metric exists in another project' do
|
|
|
|
let!(:metric) { create(:prometheus_metric, project: create(:project)) }
|
|
|
|
|
|
|
|
it_behaves_like 'misconfigured dashboard service response', :not_found
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|