2019-07-31 22:56:46 +05:30
# frozen_string_literal: true
require 'spec_helper'
2020-07-28 23:09:34 +05:30
RSpec . describe Gitlab :: Metrics :: Dashboard :: Finder , :use_clean_rails_memory_store_caching do
2019-07-31 22:56:46 +05:30
include MetricsDashboardHelpers
2020-04-08 14:13:33 +05:30
let_it_be ( :project ) { create ( :project ) }
let_it_be ( :user ) { create ( :user ) }
let_it_be ( :environment ) { create ( :environment , project : project ) }
2019-07-31 22:56:46 +05:30
2019-09-30 21:07:59 +05:30
before do
project . add_maintainer ( user )
end
2019-07-31 22:56:46 +05:30
describe '.find' do
let ( :dashboard_path ) { '.gitlab/dashboards/test.yml' }
2019-12-04 20:38:33 +05:30
let ( :service_call ) { described_class . find ( project , user , environment : environment , dashboard_path : dashboard_path ) }
2019-07-31 22:56:46 +05:30
it_behaves_like 'misconfigured dashboard service response' , :not_found
context 'when the dashboard exists' do
let ( :project ) { project_with_dashboard ( dashboard_path ) }
it_behaves_like 'valid dashboard service response'
end
context 'when the dashboard is configured incorrectly' do
let ( :project ) { project_with_dashboard ( dashboard_path , { } ) }
it_behaves_like 'misconfigured dashboard service response' , :unprocessable_entity
end
2019-09-04 21:01:54 +05:30
context 'when the dashboard contains a metric without a query' do
let ( :dashboard ) { { 'panel_groups' = > [ { 'panels' = > [ { 'metrics' = > [ { 'id' = > 'mock' } ] } ] } ] } }
let ( :project ) { project_with_dashboard ( dashboard_path , dashboard . to_yaml ) }
it_behaves_like 'misconfigured dashboard service response' , :unprocessable_entity
end
2019-07-31 22:56:46 +05:30
context 'when the system dashboard is specified' do
let ( :dashboard_path ) { system_dashboard_path }
it_behaves_like 'valid dashboard service response'
end
2023-01-13 00:05:48 +05:30
context 'when the self-monitoring dashboard is specified' do
2020-03-13 15:44:24 +05:30
let ( :dashboard_path ) { self_monitoring_dashboard_path }
it_behaves_like 'valid dashboard service response'
end
2019-07-31 22:56:46 +05:30
context 'when no dashboard is specified' do
2019-12-04 20:38:33 +05:30
let ( :service_call ) { described_class . find ( project , user , environment : environment ) }
2019-07-31 22:56:46 +05:30
it_behaves_like 'valid dashboard service response'
end
2019-09-30 21:07:59 +05:30
context 'when the dashboard is expected to be embedded' do
2019-12-04 20:38:33 +05:30
let ( :service_call ) { described_class . find ( project , user , ** params ) }
let ( :params ) { { environment : environment , embedded : true } }
2019-09-30 21:07:59 +05:30
it_behaves_like 'valid embedded dashboard service response'
2019-10-12 21:52:04 +05:30
context 'when params are incomplete' do
2019-12-04 20:38:33 +05:30
let ( :params ) { { environment : environment , embedded : true , dashboard_path : system_dashboard_path } }
2019-10-12 21:52:04 +05:30
it_behaves_like 'valid embedded dashboard service response'
end
context 'when the panel is specified' do
context 'as a custom metric' do
let ( :params ) do
2019-12-04 20:38:33 +05:30
{
environment : environment ,
embedded : true ,
2019-10-12 21:52:04 +05:30
dashboard_path : system_dashboard_path ,
group : business_metric_title ,
title : 'title' ,
2019-12-04 20:38:33 +05:30
y_label : 'y_label'
}
2019-10-12 21:52:04 +05:30
end
it_behaves_like 'misconfigured dashboard service response' , :not_found
context 'when the metric exists' do
before do
create ( :prometheus_metric , project : project )
end
it_behaves_like 'valid embedded dashboard service response'
end
end
context 'as a project-defined panel' do
let ( :dashboard_path ) { '.gitlab/dashboard/test.yml' }
let ( :params ) do
2019-12-04 20:38:33 +05:30
{
environment : environment ,
embedded : true ,
2019-10-12 21:52:04 +05:30
dashboard_path : dashboard_path ,
group : 'Group A' ,
title : 'Super Chart A1' ,
2019-12-04 20:38:33 +05:30
y_label : 'y_label'
}
2019-10-12 21:52:04 +05:30
end
it_behaves_like 'misconfigured dashboard service response' , :not_found
context 'when the metric exists' do
let ( :project ) { project_with_dashboard ( dashboard_path ) }
it_behaves_like 'valid embedded dashboard service response'
end
end
end
end
end
describe '.find_raw' do
2020-07-28 23:09:34 +05:30
let ( :dashboard ) { load_dashboard_yaml ( File . read ( Rails . root . join ( 'config' , 'prometheus' , 'common_metrics.yml' ) ) ) }
2019-10-12 21:52:04 +05:30
let ( :params ) { { } }
subject { described_class . find_raw ( project , ** params ) }
it { is_expected . to eq dashboard }
context 'when the system dashboard is specified' do
let ( :params ) { { dashboard_path : system_dashboard_path } }
it { is_expected . to eq dashboard }
end
context 'when an existing project dashboard is specified' do
2020-07-28 23:09:34 +05:30
let ( :dashboard ) { load_sample_dashboard }
2019-10-12 21:52:04 +05:30
let ( :params ) { { dashboard_path : '.gitlab/dashboards/test.yml' } }
let ( :project ) { project_with_dashboard ( params [ :dashboard_path ] ) }
it { is_expected . to eq dashboard }
2019-09-30 21:07:59 +05:30
end
2019-07-31 22:56:46 +05:30
end
describe '.find_all_paths' do
let ( :all_dashboard_paths ) { described_class . find_all_paths ( project ) }
2020-10-24 23:57:45 +05:30
let ( :system_dashboard ) { { path : system_dashboard_path , display_name : 'Overview' , default : true , system_dashboard : true , out_of_the_box_dashboard : true } }
let ( :k8s_pod_health_dashboard ) { { path : pod_dashboard_path , display_name : 'K8s pod health' , default : false , system_dashboard : false , out_of_the_box_dashboard : true } }
2019-07-31 22:56:46 +05:30
2020-10-24 23:57:45 +05:30
it 'includes OOTB dashboards by default' do
expect ( all_dashboard_paths ) . to eq ( [ k8s_pod_health_dashboard , system_dashboard ] )
2019-07-31 22:56:46 +05:30
end
context 'when the project contains dashboards' do
2020-10-24 23:57:45 +05:30
let ( :dashboard_content ) { fixture_file ( 'lib/gitlab/metrics/dashboard/sample_dashboard.yml' ) }
let ( :project ) { project_with_dashboards ( dashboards ) }
2019-07-31 22:56:46 +05:30
2020-10-24 23:57:45 +05:30
let ( :dashboards ) do
{
'.gitlab/dashboards/metrics.yml' = > dashboard_content ,
'.gitlab/dashboards/better_metrics.yml' = > dashboard_content
}
end
2019-07-31 22:56:46 +05:30
2020-10-24 23:57:45 +05:30
it 'includes OOTB and project dashboards' do
project_dashboard1 = {
path : '.gitlab/dashboards/metrics.yml' ,
display_name : 'metrics.yml' ,
default : false ,
system_dashboard : false ,
out_of_the_box_dashboard : false
}
project_dashboard2 = {
path : '.gitlab/dashboards/better_metrics.yml' ,
display_name : 'better_metrics.yml' ,
default : false ,
system_dashboard : false ,
out_of_the_box_dashboard : false
}
expect ( all_dashboard_paths ) . to eq ( [ project_dashboard2 , k8s_pod_health_dashboard , project_dashboard1 , system_dashboard ] )
2019-07-31 22:56:46 +05:30
end
end
2020-03-13 15:44:24 +05:30
2023-01-13 00:05:48 +05:30
context 'when the project is self-monitoring' do
2020-03-13 15:44:24 +05:30
let ( :self_monitoring_dashboard ) do
{
path : self_monitoring_dashboard_path ,
2020-10-24 23:57:45 +05:30
display_name : 'Overview' ,
2020-03-13 15:44:24 +05:30
default : true ,
2020-10-24 23:57:45 +05:30
system_dashboard : true ,
2020-07-28 23:09:34 +05:30
out_of_the_box_dashboard : true
2020-03-13 15:44:24 +05:30
}
end
2020-10-24 23:57:45 +05:30
2020-03-13 15:44:24 +05:30
let ( :dashboard_path ) { '.gitlab/dashboards/test.yml' }
let ( :project ) { project_with_dashboard ( dashboard_path ) }
before do
stub_application_setting ( self_monitoring_project_id : project . id )
end
2023-01-13 00:05:48 +05:30
it 'includes self-monitoring and project dashboards' do
2020-03-13 15:44:24 +05:30
project_dashboard = {
path : dashboard_path ,
display_name : 'test.yml' ,
default : false ,
2020-07-28 23:09:34 +05:30
system_dashboard : false ,
out_of_the_box_dashboard : false
2020-03-13 15:44:24 +05:30
}
2020-10-24 23:57:45 +05:30
expect ( all_dashboard_paths ) . to eq ( [ self_monitoring_dashboard , project_dashboard ] )
2020-03-13 15:44:24 +05:30
end
end
2019-07-31 22:56:46 +05:30
end
end