2020-04-22 19:07:51 +05:30
# frozen_string_literal: true
require 'spec_helper'
2023-03-04 22:38:38 +05:30
RSpec . describe 'Getting Metrics Dashboard' , feature_category : :metrics do
2020-04-22 19:07:51 +05:30
include GraphqlHelpers
let_it_be ( :current_user ) { create ( :user ) }
2021-09-30 23:02:18 +05:30
2020-04-22 19:07:51 +05:30
let ( :project ) { create ( :project ) }
2020-11-24 15:15:51 +05:30
let ( :environment ) { create ( :environment , project : project ) }
2020-04-22 19:07:51 +05:30
let ( :query ) do
2020-06-23 00:09:42 +05:30
graphql_query_for (
'project' , { 'fullPath' = > project . full_path } ,
query_graphql_field (
:environments , { 'name' = > environment . name } ,
query_graphql_field (
:nodes , nil ,
query_graphql_field (
:metricsDashboard , { 'path' = > path } ,
all_graphql_fields_for ( 'MetricsDashboard' . classify )
)
)
)
2020-04-22 19:07:51 +05:30
)
end
2023-03-04 22:38:38 +05:30
context 'for anonymous user' do
2020-04-22 19:07:51 +05:30
before do
2023-03-04 22:38:38 +05:30
post_graphql ( query , current_user : current_user )
2020-04-22 19:07:51 +05:30
end
2023-03-04 22:38:38 +05:30
context 'requested dashboard is available' do
let ( :path ) { 'config/prometheus/common_metrics.yml' }
2020-04-22 19:07:51 +05:30
2023-03-04 22:38:38 +05:30
it_behaves_like 'a working graphql query'
2020-04-22 19:07:51 +05:30
2023-03-04 22:38:38 +05:30
it 'returns nil' do
dashboard = graphql_data . dig ( 'project' , 'environments' , 'nodes' )
2020-04-22 19:07:51 +05:30
2023-03-04 22:38:38 +05:30
expect ( dashboard ) . to be_nil
2020-04-22 19:07:51 +05:30
end
end
end
2023-03-04 22:38:38 +05:30
context 'for user with developer access' do
2020-04-22 19:07:51 +05:30
before do
2023-03-04 22:38:38 +05:30
project . add_developer ( current_user )
post_graphql ( query , current_user : current_user )
2020-04-22 19:07:51 +05:30
end
2023-03-04 22:38:38 +05:30
context 'requested dashboard is available' do
let ( :path ) { 'config/prometheus/common_metrics.yml' }
2020-04-22 19:07:51 +05:30
2023-03-04 22:38:38 +05:30
it_behaves_like 'a working graphql query'
2020-04-22 19:07:51 +05:30
2023-03-04 22:38:38 +05:30
it 'returns metrics dashboard' do
dashboard = graphql_data . dig ( 'project' , 'environments' , 'nodes' , 0 , 'metricsDashboard' )
2020-04-22 19:07:51 +05:30
2023-03-04 22:38:38 +05:30
expect ( dashboard ) . to eql ( " path " = > path , " schemaValidationWarnings " = > nil )
2020-06-23 00:09:42 +05:30
end
2023-03-04 22:38:38 +05:30
context 'invalid dashboard' do
let ( :path ) { '.gitlab/dashboards/metrics.yml' }
let ( :project ) { create ( :project , :repository , :custom_repo , namespace : current_user . namespace , files : { path = > " --- \n dashboard: 'test' " } ) }
2020-06-23 00:09:42 +05:30
it 'returns metrics dashboard' do
dashboard = graphql_data . dig ( 'project' , 'environments' , 'nodes' , 0 , 'metricsDashboard' )
2023-03-04 22:38:38 +05:30
expect ( dashboard ) . to eql ( " path " = > path , " schemaValidationWarnings " = > [ " panel_groups: should be an array of panel_groups objects " ] )
2020-11-24 15:15:51 +05:30
end
2023-03-04 22:38:38 +05:30
end
2020-11-24 15:15:51 +05:30
2023-03-04 22:38:38 +05:30
context 'empty dashboard' do
let ( :path ) { '.gitlab/dashboards/metrics.yml' }
let ( :project ) { create ( :project , :repository , :custom_repo , namespace : current_user . namespace , files : { path = > " " } ) }
2020-11-24 15:15:51 +05:30
2023-03-04 22:38:38 +05:30
it 'returns metrics dashboard' do
dashboard = graphql_data . dig ( 'project' , 'environments' , 'nodes' , 0 , 'metricsDashboard' )
2020-11-24 15:15:51 +05:30
2023-03-04 22:38:38 +05:30
expect ( dashboard ) . to eql ( " path " = > path , " schemaValidationWarnings " = > [ " dashboard: can't be blank " , " panel_groups: should be an array of panel_groups objects " ] )
2020-06-23 00:09:42 +05:30
end
2020-04-22 19:07:51 +05:30
end
2023-03-04 22:38:38 +05:30
end
2020-04-22 19:07:51 +05:30
2023-03-04 22:38:38 +05:30
context 'requested dashboard can not be found' do
let ( :path ) { 'config/prometheus/i_am_not_here.yml' }
2020-04-22 19:07:51 +05:30
2023-03-04 22:38:38 +05:30
it_behaves_like 'a working graphql query'
2020-04-22 19:07:51 +05:30
2023-03-04 22:38:38 +05:30
it 'returns nil' do
dashboard = graphql_data . dig ( 'project' , 'environments' , 'nodes' , 0 , 'metricsDashboard' )
2020-04-22 19:07:51 +05:30
2023-03-04 22:38:38 +05:30
expect ( dashboard ) . to be_nil
2020-04-22 19:07:51 +05:30
end
end
end
end