diff options
author | Sarah Yasonik <syasonik@gitlab.com> | 2019-08-07 19:17:35 +0300 |
---|---|---|
committer | Sean McGivern <sean@gitlab.com> | 2019-08-07 19:17:35 +0300 |
commit | bf918b68f643266e91a9308cbc64a8304c647f17 (patch) | |
tree | ddface6092de44fafe2e0929158fc65d92766f47 /spec/lib/gitlab/metrics | |
parent | d8966abd20c860d2f30141f3647f2b81f70b683d (diff) |
Support dashboard params for metrics dashboard
https://gitlab.com/gitlab-org/gitlab-ce/issues/62971
Adds support to EnvironmentsController#metrics_dashboard
for the following params: group, title, y_label
These params are used to uniquely identify a panel on
the metrics dashboard.
Metrics are stored in several places, so this adds
utilities to find a specific panel from the database
or filesystem depending on the metric specified.
Also moves some shared utilities into separate classes,
notably default values and errors.
Diffstat (limited to 'spec/lib/gitlab/metrics')
-rw-r--r-- | spec/lib/gitlab/metrics/dashboard/defaults_spec.rb | 8 | ||||
-rw-r--r-- | spec/lib/gitlab/metrics/dashboard/finder_spec.rb | 76 | ||||
-rw-r--r-- | spec/lib/gitlab/metrics/dashboard/service_selector_spec.rb | 80 |
3 files changed, 161 insertions, 3 deletions
diff --git a/spec/lib/gitlab/metrics/dashboard/defaults_spec.rb b/spec/lib/gitlab/metrics/dashboard/defaults_spec.rb new file mode 100644 index 00000000000..420b246b3f5 --- /dev/null +++ b/spec/lib/gitlab/metrics/dashboard/defaults_spec.rb @@ -0,0 +1,8 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe Gitlab::Metrics::Dashboard::Defaults do + it { is_expected.to be_const_defined(:DEFAULT_PANEL_TYPE) } + it { is_expected.to be_const_defined(:DEFAULT_PANEL_WEIGHT) } +end diff --git a/spec/lib/gitlab/metrics/dashboard/finder_spec.rb b/spec/lib/gitlab/metrics/dashboard/finder_spec.rb index e57c7326320..ce1bb49f5c9 100644 --- a/spec/lib/gitlab/metrics/dashboard/finder_spec.rb +++ b/spec/lib/gitlab/metrics/dashboard/finder_spec.rb @@ -5,10 +5,9 @@ require 'spec_helper' describe Gitlab::Metrics::Dashboard::Finder, :use_clean_rails_memory_store_caching do include MetricsDashboardHelpers - set(:project) { build(:project) } + set(:project) { create(:project) } set(:user) { create(:user) } set(:environment) { create(:environment, project: project) } - let(:system_dashboard_path) { ::Metrics::Dashboard::SystemDashboardService::SYSTEM_DASHBOARD_PATH} before do project.add_maintainer(user) @@ -52,9 +51,80 @@ describe Gitlab::Metrics::Dashboard::Finder, :use_clean_rails_memory_store_cachi end context 'when the dashboard is expected to be embedded' do - let(:service_call) { described_class.find(project, user, environment, dashboard_path: nil, embedded: true) } + let(:service_call) { described_class.find(project, user, environment, **params) } + let(:params) { { embedded: true } } it_behaves_like 'valid embedded dashboard service response' + + context 'when params are incomplete' do + let(:params) { { embedded: true, dashboard_path: system_dashboard_path } } + + 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 + { embedded: true, + dashboard_path: system_dashboard_path, + group: business_metric_title, + title: 'title', + y_label: 'y_label' } + 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 + { embedded: true, + dashboard_path: dashboard_path, + group: 'Group A', + title: 'Super Chart A1', + y_label: 'y_label' } + 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 + let(:dashboard) { YAML.load_file(Rails.root.join('config', 'prometheus', 'common_metrics.yml')) } + 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 + let(:dashboard) { YAML.safe_load(fixture_file('lib/gitlab/metrics/dashboard/sample_dashboard.yml')) } + let(:params) { { dashboard_path: '.gitlab/dashboards/test.yml' } } + let(:project) { project_with_dashboard(params[:dashboard_path]) } + + it { is_expected.to eq dashboard } end end diff --git a/spec/lib/gitlab/metrics/dashboard/service_selector_spec.rb b/spec/lib/gitlab/metrics/dashboard/service_selector_spec.rb new file mode 100644 index 00000000000..095d0a2df78 --- /dev/null +++ b/spec/lib/gitlab/metrics/dashboard/service_selector_spec.rb @@ -0,0 +1,80 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe Gitlab::Metrics::Dashboard::ServiceSelector do + include MetricsDashboardHelpers + + describe '#call' do + let(:arguments) { {} } + + subject { described_class.call(arguments) } + + it { is_expected.to be Metrics::Dashboard::SystemDashboardService } + + context 'when just the dashboard path is provided' do + let(:arguments) { { dashboard_path: '.gitlab/dashboards/test.yml' } } + + it { is_expected.to be Metrics::Dashboard::ProjectDashboardService } + + context 'when the path is for the system dashboard' do + let(:arguments) { { dashboard_path: system_dashboard_path } } + + it { is_expected.to be Metrics::Dashboard::SystemDashboardService } + end + end + + context 'when the embedded flag is provided' do + let(:arguments) { { embedded: true } } + + it { is_expected.to be Metrics::Dashboard::DefaultEmbedService } + + context 'when an incomplete set of dashboard identifiers are provided' do + let(:arguments) { { embedded: true, dashboard_path: '.gitlab/dashboards/test.yml' } } + + it { is_expected.to be Metrics::Dashboard::DefaultEmbedService } + end + + context 'when all the chart identifiers are provided' do + let(:arguments) do + { + embedded: true, + dashboard_path: '.gitlab/dashboards/test.yml', + group: 'Important Metrics', + title: 'Total Requests', + y_label: 'req/sec' + } + end + + it { is_expected.to be Metrics::Dashboard::DynamicEmbedService } + end + + context 'when all chart params expect dashboard_path are provided' do + let(:arguments) do + { + embedded: true, + group: 'Important Metrics', + title: 'Total Requests', + y_label: 'req/sec' + } + end + + it { is_expected.to be Metrics::Dashboard::DynamicEmbedService } + end + + context 'with a system dashboard and "custom" group' do + let(:arguments) do + { + embedded: true, + dashboard_path: system_dashboard_path, + group: business_metric_title, + title: 'Total Requests', + y_label: 'req/sec' + } + end + + it { is_expected.to be Metrics::Dashboard::CustomMetricEmbedService } + end + end + end +end |