diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-08-20 18:42:06 +0000 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-08-20 18:42:06 +0000 |
commit | 6e4e1050d9dba2b7b2523fdd1768823ab85feef4 (patch) | |
tree | 78be5963ec075d80116a932011d695dd33910b4e /spec/services/metrics | |
parent | 1ce776de4ae122aba3f349c02c17cebeaa8ecf07 (diff) | |
download | gitlab-ce-6e4e1050d9dba2b7b2523fdd1768823ab85feef4.tar.gz |
Add latest changes from gitlab-org/gitlab@13-3-stable-ee
Diffstat (limited to 'spec/services/metrics')
10 files changed, 177 insertions, 16 deletions
diff --git a/spec/services/metrics/dashboard/clone_dashboard_service_spec.rb b/spec/services/metrics/dashboard/clone_dashboard_service_spec.rb index 4a226fe386c..728b343b801 100644 --- a/spec/services/metrics/dashboard/clone_dashboard_service_spec.rb +++ b/spec/services/metrics/dashboard/clone_dashboard_service_spec.rb @@ -84,14 +84,12 @@ RSpec.describe Metrics::Dashboard::CloneDashboardService, :use_clean_rails_memor it_behaves_like 'valid dashboard cloning process', ::Metrics::Dashboard::SystemDashboardService::DASHBOARD_PATH, [ ::Gitlab::Metrics::Dashboard::Stages::CommonMetricsInserter, - ::Gitlab::Metrics::Dashboard::Stages::CustomMetricsInserter, - ::Gitlab::Metrics::Dashboard::Stages::Sorter + ::Gitlab::Metrics::Dashboard::Stages::CustomMetricsInserter ] it_behaves_like 'valid dashboard cloning process', ::Metrics::Dashboard::ClusterDashboardService::DASHBOARD_PATH, [ - ::Gitlab::Metrics::Dashboard::Stages::CommonMetricsInserter, - ::Gitlab::Metrics::Dashboard::Stages::Sorter + ::Gitlab::Metrics::Dashboard::Stages::CommonMetricsInserter ] it_behaves_like 'valid dashboard cloning process', diff --git a/spec/services/metrics/dashboard/cluster_metrics_embed_service_spec.rb b/spec/services/metrics/dashboard/cluster_metrics_embed_service_spec.rb index e80911d6265..dbb89af45d0 100644 --- a/spec/services/metrics/dashboard/cluster_metrics_embed_service_spec.rb +++ b/spec/services/metrics/dashboard/cluster_metrics_embed_service_spec.rb @@ -52,6 +52,7 @@ RSpec.describe Metrics::Dashboard::ClusterMetricsEmbedService, :use_clean_rails_ } ] end + let(:service_call) { described_class.new(*service_params).get_dashboard } let(:panel_groups) { service_call[:dashboard][:panel_groups] } let(:panel) { panel_groups.first[:panels].first } diff --git a/spec/services/metrics/dashboard/custom_dashboard_service_spec.rb b/spec/services/metrics/dashboard/custom_dashboard_service_spec.rb index d4391ecb6b9..aea9c25d104 100644 --- a/spec/services/metrics/dashboard/custom_dashboard_service_spec.rb +++ b/spec/services/metrics/dashboard/custom_dashboard_service_spec.rb @@ -57,6 +57,16 @@ RSpec.describe Metrics::Dashboard::CustomDashboardService, :use_clean_rails_memo described_class.new(*service_params).get_dashboard end + it 'tracks panel type' do + allow(::Gitlab::Tracking).to receive(:event).and_call_original + + described_class.new(*service_params).get_dashboard + + expect(::Gitlab::Tracking).to have_received(:event) + .with('MetricsDashboard::Chart', 'chart_rendered', { label: 'area-chart' }) + .at_least(:once) + end + context 'and the dashboard is then deleted' do it 'does not return the previously cached dashboard' do described_class.new(*service_params).get_dashboard @@ -104,6 +114,16 @@ RSpec.describe Metrics::Dashboard::CustomDashboardService, :use_clean_rails_memo }] ) end + + it 'caches repo file list' do + expect(Gitlab::Metrics::Dashboard::RepoDashboardFinder).to receive(:list_dashboards) + .with(project) + .once + .and_call_original + + described_class.all_dashboard_paths(project) + described_class.all_dashboard_paths(project) + end end end diff --git a/spec/services/metrics/dashboard/custom_metric_embed_service_spec.rb b/spec/services/metrics/dashboard/custom_metric_embed_service_spec.rb index a5f7c2ab8ab..82321dbc822 100644 --- a/spec/services/metrics/dashboard/custom_metric_embed_service_spec.rb +++ b/spec/services/metrics/dashboard/custom_metric_embed_service_spec.rb @@ -111,7 +111,8 @@ RSpec.describe Metrics::Dashboard::CustomMetricEmbedService do it_behaves_like 'valid embedded dashboard service response' it 'does not cache the unprocessed dashboard' do - expect(Gitlab::Metrics::Dashboard::Cache).not_to receive(:fetch) + # Fail spec if any method of Cache class is called. + stub_const('Gitlab::Metrics::Dashboard::Cache', double) described_class.new(*service_params).get_dashboard end diff --git a/spec/services/metrics/dashboard/dynamic_embed_service_spec.rb b/spec/services/metrics/dashboard/dynamic_embed_service_spec.rb index 72b356be60f..3c533b0c464 100644 --- a/spec/services/metrics/dashboard/dynamic_embed_service_spec.rb +++ b/spec/services/metrics/dashboard/dynamic_embed_service_spec.rb @@ -132,7 +132,7 @@ RSpec.describe Metrics::Dashboard::DynamicEmbedService, :use_clean_rails_memory_ end shared_examples 'uses system dashboard' do - it 'uses the default dashboard' do + it 'uses the overview dashboard' do expect(Gitlab::Metrics::Dashboard::Finder) .to receive(:find_raw) .with(project, dashboard_path: system_dashboard_path) diff --git a/spec/services/metrics/dashboard/gitlab_alert_embed_service_spec.rb b/spec/services/metrics/dashboard/gitlab_alert_embed_service_spec.rb index 29c941826b5..dd9d498e307 100644 --- a/spec/services/metrics/dashboard/gitlab_alert_embed_service_spec.rb +++ b/spec/services/metrics/dashboard/gitlab_alert_embed_service_spec.rb @@ -72,12 +72,18 @@ RSpec.describe Metrics::Dashboard::GitlabAlertEmbedService do it_behaves_like 'valid embedded dashboard service response' it_behaves_like 'raises error for users with insufficient permissions' - it 'uses the metric info corresponding to the alert' do + it 'generates an panel based on the alert' do result = service_call - metrics = result[:dashboard][:panel_groups][0][:panels][0][:metrics] - - expect(metrics.length).to eq 1 - expect(metrics.first[:metric_id]).to eq alert.prometheus_metric_id + panel = result[:dashboard][:panel_groups][0][:panels][0] + metric = panel[:metrics].first + + expect(panel[:metrics].length).to eq 1 + expect(panel).to include( + title: alert.prometheus_metric.title, + y_label: alert.prometheus_metric.y_label, + type: 'area-chart' + ) + expect(metric[:metric_id]).to eq alert.prometheus_metric_id end context 'when the metric does not exist' do @@ -87,7 +93,8 @@ RSpec.describe Metrics::Dashboard::GitlabAlertEmbedService do end it 'does not cache the unprocessed dashboard' do - expect(Gitlab::Metrics::Dashboard::Cache).not_to receive(:fetch) + # Fail spec if any method of Cache class is called. + stub_const('Gitlab::Metrics::Dashboard::Cache', double) described_class.new(*service_params).get_dashboard end diff --git a/spec/services/metrics/dashboard/grafana_metric_embed_service_spec.rb b/spec/services/metrics/dashboard/grafana_metric_embed_service_spec.rb index ee3c55cb642..5263fd40a40 100644 --- a/spec/services/metrics/dashboard/grafana_metric_embed_service_spec.rb +++ b/spec/services/metrics/dashboard/grafana_metric_embed_service_spec.rb @@ -7,7 +7,7 @@ RSpec.describe Metrics::Dashboard::GrafanaMetricEmbedService do include ReactiveCachingHelpers include GrafanaApiHelpers - let_it_be(:project) { build(:project) } + let_it_be(:project) { create(:project) } let_it_be(:user) { create(:user) } let_it_be(:grafana_integration) { create(:grafana_integration, project: project) } @@ -15,7 +15,7 @@ RSpec.describe Metrics::Dashboard::GrafanaMetricEmbedService do valid_grafana_dashboard_link(grafana_integration.grafana_url) end - before do + before_all do project.add_maintainer(user) end @@ -58,6 +58,31 @@ RSpec.describe Metrics::Dashboard::GrafanaMetricEmbedService do expect(subject.current_user).to eq(user) expect(subject.params[:grafana_url]).to eq(grafana_url) end + + context 'with unknown users' do + let(:params) { [project.id, current_user_id, grafana_url] } + + context 'when anonymous' do + where(:current_user_id) do + [nil, ''] + end + + with_them do + it 'sets current_user as nil' do + expect(subject.current_user).to be_nil + end + end + end + + context 'when invalid' do + let(:current_user_id) { non_existing_record_id } + + it 'raise record not found error' do + expect { subject } + .to raise_error(ActiveRecord::RecordNotFound, /Couldn't find User/) + end + end + end end describe '#get_dashboard', :use_clean_rails_memory_store_caching do @@ -145,7 +170,17 @@ RSpec.describe Metrics::Dashboard::GrafanaMetricEmbedService do stub_datasource_request(grafana_integration.grafana_url) end - it_behaves_like 'valid embedded dashboard service response' + context 'when project is private and user is member' do + it_behaves_like 'valid embedded dashboard service response' + end + + context 'when project is public and user is anonymous' do + let(:project) { create(:project, :public) } + let(:user) { nil } + let(:grafana_integration) { create(:grafana_integration, project: project) } + + it_behaves_like 'valid embedded dashboard service response' + end end end diff --git a/spec/services/metrics/dashboard/panel_preview_service_spec.rb b/spec/services/metrics/dashboard/panel_preview_service_spec.rb new file mode 100644 index 00000000000..d58dee3e7a3 --- /dev/null +++ b/spec/services/metrics/dashboard/panel_preview_service_spec.rb @@ -0,0 +1,83 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Metrics::Dashboard::PanelPreviewService do + let_it_be(:project) { create(:project) } + let_it_be(:environment) { create(:environment, project: project) } + let_it_be(:panel_yml) do + <<~YML + --- + title: test panel + YML + end + let_it_be(:dashboard) do + { + panel_groups: [ + { + panels: [{ 'title' => 'test panel' }] + } + ] + } + end + + describe '#execute' do + subject(:service_response) { described_class.new(project, panel_yml, environment).execute } + + context "valid panel's yaml" do + before do + allow_next_instance_of(::Gitlab::Metrics::Dashboard::Processor) do |processor| + allow(processor).to receive(:process).and_return(dashboard) + end + end + + it 'returns success service response' do + expect(service_response.success?).to be_truthy + end + + it 'returns processed panel' do + expect(service_response.payload).to eq('title' => 'test panel') + end + + it 'uses dashboard processor' do + sequence = [ + ::Gitlab::Metrics::Dashboard::Stages::CommonMetricsInserter, + ::Gitlab::Metrics::Dashboard::Stages::MetricEndpointInserter, + ::Gitlab::Metrics::Dashboard::Stages::PanelIdsInserter, + ::Gitlab::Metrics::Dashboard::Stages::AlertsInserter, + ::Gitlab::Metrics::Dashboard::Stages::UrlValidator + ] + processor_params = [project, dashboard, sequence, environment: environment] + + expect_next_instance_of(::Gitlab::Metrics::Dashboard::Processor, *processor_params) do |processor| + expect(processor).to receive(:process).and_return(dashboard) + end + + service_response + end + end + + context "invalid panel's yaml" do + [ + Gitlab::Metrics::Dashboard::Errors::DashboardProcessingError, + Gitlab::Config::Loader::Yaml::NotHashError, + Gitlab::Config::Loader::Yaml::DataTooLargeError, + Gitlab::Config::Loader::FormatError + ].each do |error_class| + before do + allow_next_instance_of(::Gitlab::Metrics::Dashboard::Processor) do |processor| + allow(processor).to receive(:process).and_raise(error_class.new('error')) + end + end + + it 'returns error service response' do + expect(service_response.error?).to be_truthy + end + + it 'returns error message' do + expect(service_response.message).to eq('error') + end + end + end + end +end diff --git a/spec/services/metrics/dashboard/pod_dashboard_service_spec.rb b/spec/services/metrics/dashboard/pod_dashboard_service_spec.rb index ae0e38a04b2..0ea812e93ee 100644 --- a/spec/services/metrics/dashboard/pod_dashboard_service_spec.rb +++ b/spec/services/metrics/dashboard/pod_dashboard_service_spec.rb @@ -54,4 +54,20 @@ RSpec.describe Metrics::Dashboard::PodDashboardService, :use_clean_rails_memory_ let(:dashboard_version) { subject.send(:dashboard_version) } end end + + describe '.all_dashboard_paths' do + it 'returns the dashboard attributes' do + all_dashboards = described_class.all_dashboard_paths(project) + + expect(all_dashboards).to eq( + [{ + path: described_class::DASHBOARD_PATH, + display_name: described_class::DASHBOARD_NAME, + default: false, + system_dashboard: false, + out_of_the_box_dashboard: true + }] + ) + end + end end diff --git a/spec/services/metrics/dashboard/self_monitoring_dashboard_service_spec.rb b/spec/services/metrics/dashboard/self_monitoring_dashboard_service_spec.rb index aea3e41a013..33b7e3c85cd 100644 --- a/spec/services/metrics/dashboard/self_monitoring_dashboard_service_spec.rb +++ b/spec/services/metrics/dashboard/self_monitoring_dashboard_service_spec.rb @@ -50,7 +50,7 @@ RSpec.describe Metrics::Dashboard::SelfMonitoringDashboardService, :use_clean_ra path: described_class::DASHBOARD_PATH, display_name: described_class::DASHBOARD_NAME, default: true, - system_dashboard: false, + system_dashboard: true, out_of_the_box_dashboard: true }] ) |