Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/spec
diff options
context:
space:
mode:
Diffstat (limited to 'spec')
-rw-r--r--spec/controllers/projects/environments_controller_spec.rb24
-rw-r--r--spec/fixtures/services/metrics_dashboard_processing_service.yml36
-rw-r--r--spec/services/metrics_dashboard_processing_service_spec.rb56
-rw-r--r--spec/services/metrics_dashboard_service_spec.rb22
4 files changed, 138 insertions, 0 deletions
diff --git a/spec/controllers/projects/environments_controller_spec.rb b/spec/controllers/projects/environments_controller_spec.rb
index 75158f2e8e0..02441385da0 100644
--- a/spec/controllers/projects/environments_controller_spec.rb
+++ b/spec/controllers/projects/environments_controller_spec.rb
@@ -461,6 +461,30 @@ describe Projects::EnvironmentsController do
end
end
+ describe 'metrics_dashboard' do
+ context 'when prometheus endpoint is disabled' do
+ before do
+ stub_feature_flags(environment_metrics_use_prometheus_endpoint: false)
+ end
+
+ it 'responds with status code 403' do
+ get :metrics_dashboard, params: environment_params(format: :json)
+
+ expect(response).to have_gitlab_http_status(:forbidden)
+ end
+ end
+
+ context 'when prometheus endpoint is enabled' do
+ it 'returns a json representation of the environment dashboard' do
+ get :metrics_dashboard, params: environment_params(format: :json)
+
+ expect(response).to have_gitlab_http_status(:ok)
+ expect(json_response).to include('dashboard', 'order', 'panel_groups')
+ expect(json_response['panel_groups']).to all( include('group', 'priority', 'panels') )
+ end
+ end
+ end
+
describe 'GET #search' do
before do
create(:environment, name: 'staging', project: project)
diff --git a/spec/fixtures/services/metrics_dashboard_processing_service.yml b/spec/fixtures/services/metrics_dashboard_processing_service.yml
new file mode 100644
index 00000000000..ebfe06da6db
--- /dev/null
+++ b/spec/fixtures/services/metrics_dashboard_processing_service.yml
@@ -0,0 +1,36 @@
+dashboard: 'Test Dashboard'
+order: 1
+panel_groups:
+- group: Group A
+ priority: 10
+ panels:
+ - title: "Super Chart A1"
+ type: "area-chart"
+ y_label: "y_label"
+ weight: 2
+ metrics:
+ - id: metric_a1
+ query_range: 'query'
+ unit: unit
+ label: Legend Label
+ - title: "Super Chart A2"
+ type: "area-chart"
+ y_label: "y_label"
+ weight: 1
+ metrics:
+ - id: metric_a2
+ query_range: 'query'
+ label: Legend Label
+ unit: unit
+- group: Group B
+ priority: 1
+ panels:
+ - title: "Super Chart B"
+ type: "area-chart"
+ y_label: "y_label"
+ weight: 1
+ metrics:
+ - id: metric_b
+ query_range: 'query'
+ unit: unit
+ label: Legend Label
diff --git a/spec/services/metrics_dashboard_processing_service_spec.rb b/spec/services/metrics_dashboard_processing_service_spec.rb
new file mode 100644
index 00000000000..658ff1dab49
--- /dev/null
+++ b/spec/services/metrics_dashboard_processing_service_spec.rb
@@ -0,0 +1,56 @@
+require 'spec_helper'
+
+describe MetricsDashboardProcessingService do
+ let(:project) { build(:project) }
+ let(:dashboard_yml) { YAML.load_file('spec/fixtures/services/metrics_dashboard_processing_service.yml') }
+
+ describe 'process' do
+ let(:dashboard) { JSON.parse(described_class.new(dashboard_yml, project).process, symbolize_names: true) }
+
+ context 'when dashboard config corresponds to common metrics' do
+ let!(:common_metric) { create(:prometheus_metric, :common, identifier: 'metric_a1') }
+
+ it 'inserts metric ids into the config' do
+ target_metric = all_metrics.find { |metric| metric[:id] == 'metric_a1' }
+
+ expect(target_metric).to include(:metric_id)
+ end
+ end
+
+ context 'when the project has associated metrics' do
+ let!(:project_metric) { create(:prometheus_metric, project: project) }
+
+ it 'includes project-specific metrics' do
+
+ project_metric_details = {
+ query_range: project_metric.query,
+ unit: project_metric.unit,
+ label: project_metric.legend,
+ metric_id: project_metric.id,
+ }
+
+ expect(all_metrics).to include project_metric_details
+ end
+
+ it 'includes project metrics at the end of the config' do
+ expected_metrics_order = ['metric_b', 'metric_a2', 'metric_a1', nil]
+ actual_metrics_order = all_metrics.map { |m| m[:id] }
+
+ expect(actual_metrics_order).to eq expected_metrics_order
+ end
+ end
+
+ it 'orders groups by priority and panels by weight' do
+ expected_metrics_order = ['metric_b', 'metric_a2', 'metric_a1']
+ actual_metrics_order = all_metrics.map { |m| m[:id] }
+
+ expect(actual_metrics_order).to eq expected_metrics_order
+ end
+ end
+
+ def all_metrics
+ dashboard[:panel_groups].map do |group|
+ group[:panels].map { |panel| panel[:metrics] }
+ end.flatten
+ end
+end
diff --git a/spec/services/metrics_dashboard_service_spec.rb b/spec/services/metrics_dashboard_service_spec.rb
new file mode 100644
index 00000000000..9b7994fa34f
--- /dev/null
+++ b/spec/services/metrics_dashboard_service_spec.rb
@@ -0,0 +1,22 @@
+require 'spec_helper'
+
+describe MetricsDashboardService, :use_clean_rails_memory_store_caching do
+ let(:project) { build(:project) }
+
+ describe 'get_dashboard' do
+ it 'returns a json representation of the environment dashboard' do
+ dashboard = described_class.new(project).get_dashboard
+ json = JSON.parse(dashboard, symbolize_names: true)
+
+ expect(json).to include(:dashboard, :order, :panel_groups)
+ expect(json[:panel_groups]).to all( include(:group, :priority, :panels) )
+ end
+
+ it 'caches the dashboard for subsequent calls' do
+ expect(YAML).to receive(:load_file).once.and_call_original
+
+ described_class.new(project).get_dashboard
+ described_class.new(project).get_dashboard
+ end
+ end
+end