diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-05-20 17:34:42 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-05-20 17:34:42 +0300 |
commit | 9f46488805e86b1bc341ea1620b866016c2ce5ed (patch) | |
tree | f9748c7e287041e37d6da49e0a29c9511dc34768 /spec/models/performance_monitoring | |
parent | dfc92d081ea0332d69c8aca2f0e745cb48ae5e6d (diff) |
Add latest changes from gitlab-org/gitlab@13-0-stable-ee
Diffstat (limited to 'spec/models/performance_monitoring')
4 files changed, 292 insertions, 0 deletions
diff --git a/spec/models/performance_monitoring/prometheus_dashboard_spec.rb b/spec/models/performance_monitoring/prometheus_dashboard_spec.rb new file mode 100644 index 00000000000..e6fc03a0fb6 --- /dev/null +++ b/spec/models/performance_monitoring/prometheus_dashboard_spec.rb @@ -0,0 +1,102 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe PerformanceMonitoring::PrometheusDashboard do + let(:json_content) do + { + "dashboard" => "Dashboard Title", + "templating" => { + "variables" => { + "variable1" => %w(value1 value2 value3) + } + }, + "panel_groups" => [{ + "group" => "Group Title", + "panels" => [{ + "type" => "area-chart", + "title" => "Chart Title", + "y_label" => "Y-Axis", + "metrics" => [{ + "id" => "metric_of_ages", + "unit" => "count", + "label" => "Metric of Ages", + "query_range" => "http_requests_total" + }] + }] + }] + } + end + + describe '.from_json' do + subject { described_class.from_json(json_content) } + + it 'creates a PrometheusDashboard object' do + expect(subject).to be_a PerformanceMonitoring::PrometheusDashboard + expect(subject.dashboard).to eq(json_content['dashboard']) + expect(subject.panel_groups).to all(be_a PerformanceMonitoring::PrometheusPanelGroup) + end + + describe 'validations' do + context 'when dashboard is missing' do + before do + json_content['dashboard'] = nil + end + + subject { described_class.from_json(json_content) } + + it { expect { subject }.to raise_error(ActiveModel::ValidationError) } + end + + context 'when panel groups are missing' do + before do + json_content['panel_groups'] = [] + end + + subject { described_class.from_json(json_content) } + + it { expect { subject }.to raise_error(ActiveModel::ValidationError) } + end + end + end + + describe '.find_for' do + let(:project) { build_stubbed(:project) } + let(:user) { build_stubbed(:user) } + let(:environment) { build_stubbed(:environment) } + let(:path) { ::Metrics::Dashboard::SystemDashboardService::DASHBOARD_PATH } + + context 'dashboard has been found' do + it 'uses dashboard finder to find and load dashboard data and returns dashboard instance', :aggregate_failures do + expect(Gitlab::Metrics::Dashboard::Finder).to receive(:find).with(project, user, environment: environment, dashboard_path: path).and_return(status: :success, dashboard: json_content) + + dashboard_instance = described_class.find_for(project: project, user: user, path: path, options: { environment: environment }) + + expect(dashboard_instance).to be_instance_of described_class + expect(dashboard_instance.environment).to be environment + expect(dashboard_instance.path).to be path + end + end + + context 'dashboard has NOT been found' do + it 'returns nil' do + allow(Gitlab::Metrics::Dashboard::Finder).to receive(:find).and_return(status: :error) + + dashboard_instance = described_class.find_for(project: project, user: user, path: path, options: { environment: environment }) + + expect(dashboard_instance).to be_nil + end + end + end + + describe '#to_yaml' do + subject { prometheus_dashboard.to_yaml } + + let(:prometheus_dashboard) { described_class.from_json(json_content) } + let(:expected_yaml) do + "---\npanel_groups:\n- panels:\n - metrics:\n - id: metric_of_ages\n unit: count\n label: Metric of Ages\n query: \n query_range: http_requests_total\n type: area-chart\n title: Chart Title\n y_label: Y-Axis\n weight: \n group: Group Title\n priority: \ndashboard: Dashboard Title\n" + end + + it { is_expected.to eq(expected_yaml) } + end +end diff --git a/spec/models/performance_monitoring/prometheus_metric_spec.rb b/spec/models/performance_monitoring/prometheus_metric_spec.rb new file mode 100644 index 00000000000..08288e5d993 --- /dev/null +++ b/spec/models/performance_monitoring/prometheus_metric_spec.rb @@ -0,0 +1,59 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe PerformanceMonitoring::PrometheusMetric do + let(:json_content) do + { + "id" => "metric_of_ages", + "unit" => "count", + "label" => "Metric of Ages", + "query_range" => "http_requests_total" + } + end + + describe '.from_json' do + subject { described_class.from_json(json_content) } + + it 'creates a PrometheusMetric object' do + expect(subject).to be_a PerformanceMonitoring::PrometheusMetric + expect(subject.id).to eq(json_content['id']) + expect(subject.unit).to eq(json_content['unit']) + expect(subject.label).to eq(json_content['label']) + expect(subject.query_range).to eq(json_content['query_range']) + end + + describe 'validations' do + context 'when unit is missing' do + before do + json_content['unit'] = nil + end + + subject { described_class.from_json(json_content) } + + it { expect { subject }.to raise_error(ActiveModel::ValidationError) } + end + + context 'when query and query_range is missing' do + before do + json_content['query_range'] = nil + end + + subject { described_class.from_json(json_content) } + + it { expect { subject }.to raise_error(ActiveModel::ValidationError) } + end + + context 'when query_range is missing but query is available' do + before do + json_content['query_range'] = nil + json_content['query'] = 'http_requests_total' + end + + subject { described_class.from_json(json_content) } + + it { is_expected.to be_valid } + end + end + end +end diff --git a/spec/models/performance_monitoring/prometheus_panel_group_spec.rb b/spec/models/performance_monitoring/prometheus_panel_group_spec.rb new file mode 100644 index 00000000000..2447bb5df94 --- /dev/null +++ b/spec/models/performance_monitoring/prometheus_panel_group_spec.rb @@ -0,0 +1,54 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe PerformanceMonitoring::PrometheusPanelGroup do + let(:json_content) do + { + "group" => "Group Title", + "panels" => [{ + "type" => "area-chart", + "title" => "Chart Title", + "y_label" => "Y-Axis", + "metrics" => [{ + "id" => "metric_of_ages", + "unit" => "count", + "label" => "Metric of Ages", + "query_range" => "http_requests_total" + }] + }] + } + end + + describe '.from_json' do + subject { described_class.from_json(json_content) } + + it 'creates a PrometheusPanelGroup object' do + expect(subject).to be_a PerformanceMonitoring::PrometheusPanelGroup + expect(subject.group).to eq(json_content['group']) + expect(subject.panels).to all(be_a PerformanceMonitoring::PrometheusPanel) + end + + describe 'validations' do + context 'when group is missing' do + before do + json_content['group'] = nil + end + + subject { described_class.from_json(json_content) } + + it { expect { subject }.to raise_error(ActiveModel::ValidationError) } + end + + context 'when panels are missing' do + before do + json_content['panels'] = [] + end + + subject { described_class.from_json(json_content) } + + it { expect { subject }.to raise_error(ActiveModel::ValidationError) } + end + end + end +end diff --git a/spec/models/performance_monitoring/prometheus_panel_spec.rb b/spec/models/performance_monitoring/prometheus_panel_spec.rb new file mode 100644 index 00000000000..f5e04ec91e2 --- /dev/null +++ b/spec/models/performance_monitoring/prometheus_panel_spec.rb @@ -0,0 +1,77 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe PerformanceMonitoring::PrometheusPanel do + let(:json_content) do + { + "max_value" => 1, + "type" => "area-chart", + "title" => "Chart Title", + "y_label" => "Y-Axis", + "weight" => 1, + "metrics" => [{ + "id" => "metric_of_ages", + "unit" => "count", + "label" => "Metric of Ages", + "query_range" => "http_requests_total" + }] + } + end + + describe '#new' do + it 'accepts old schema format' do + expect { described_class.new(json_content) }.not_to raise_error + end + + it 'accepts new schema format' do + expect { described_class.new(json_content.merge("y_axis" => { "precision" => 0 })) }.not_to raise_error + end + end + + describe '.from_json' do + subject { described_class.from_json(json_content) } + + it 'creates a PrometheusPanelGroup object' do + expect(subject).to be_a PerformanceMonitoring::PrometheusPanel + expect(subject.type).to eq(json_content['type']) + expect(subject.title).to eq(json_content['title']) + expect(subject.y_label).to eq(json_content['y_label']) + expect(subject.weight).to eq(json_content['weight']) + expect(subject.metrics).to all(be_a PerformanceMonitoring::PrometheusMetric) + end + + describe 'validations' do + context 'when title is missing' do + before do + json_content['title'] = nil + end + + subject { described_class.from_json(json_content) } + + it { expect { subject }.to raise_error(ActiveModel::ValidationError) } + end + + context 'when metrics are missing' do + before do + json_content['metrics'] = [] + end + + subject { described_class.from_json(json_content) } + + it { expect { subject }.to raise_error(ActiveModel::ValidationError) } + end + end + end + + describe '.id' do + it 'returns hexdigest of group_title, type and title as the panel id' do + group_title = 'Business Group' + panel_type = 'area-chart' + panel_title = 'New feature requests made' + + expect(Digest::SHA2).to receive(:hexdigest).with("#{group_title}#{panel_type}#{panel_title}").and_return('hexdigest') + expect(described_class.new(title: panel_title, type: panel_type).id(group_title)).to eql 'hexdigest' + end + end +end |