diff options
Diffstat (limited to 'spec/models/performance_monitoring')
4 files changed, 187 insertions, 15 deletions
diff --git a/spec/models/performance_monitoring/prometheus_dashboard_spec.rb b/spec/models/performance_monitoring/prometheus_dashboard_spec.rb index e6fc03a0fb6..ef7298c3d8c 100644 --- a/spec/models/performance_monitoring/prometheus_dashboard_spec.rb +++ b/spec/models/performance_monitoring/prometheus_dashboard_spec.rb @@ -38,24 +38,141 @@ describe PerformanceMonitoring::PrometheusDashboard do end describe 'validations' do - context 'when dashboard is missing' do - before do - json_content['dashboard'] = nil + shared_examples 'validation failed' do |errors_messages| + it 'raises error with corresponding messages', :aggregate_failures do + expect { subject }.to raise_error do |error| + expect(error).to be_kind_of(ActiveModel::ValidationError) + expect(error.model.errors.messages).to eq(errors_messages) + end end + end + + context 'dashboard content is missing' do + let(:json_content) { nil } + + it_behaves_like 'validation failed', panel_groups: ["can't be blank"], dashboard: ["can't be blank"] + end + + context 'dashboard content is NOT a hash' do + let(:json_content) { YAML.safe_load("'test'") } + + it_behaves_like 'validation failed', panel_groups: ["can't be blank"], dashboard: ["can't be blank"] + end + + context 'content is an array' do + let(:json_content) { [{ "dashboard" => "Dashboard Title" }] } + + it_behaves_like 'validation failed', panel_groups: ["can't be blank"], dashboard: ["can't be blank"] + end - subject { described_class.from_json(json_content) } + context 'dashboard definition is missing panels_groups and dashboard keys' do + let(:json_content) do + { + "dashboard" => nil + } + end + + it_behaves_like 'validation failed', panel_groups: ["can't be blank"], dashboard: ["can't be blank"] + end + + context 'group definition is missing panels and group keys' do + let(:json_content) do + { + "dashboard" => "Dashboard Title", + "templating" => { + "variables" => { + "variable1" => %w(value1 value2 value3) + } + }, + "panel_groups" => [{ "group" => nil }] + } + end + + it_behaves_like 'validation failed', panels: ["can't be blank"], group: ["can't be blank"] + end + + context 'panel definition is missing metrics and title keys' do + let(:json_content) do + { + "dashboard" => "Dashboard Title", + "templating" => { + "variables" => { + "variable1" => %w(value1 value2 value3) + } + }, + "panel_groups" => [{ + "group" => "Group Title", + "panels" => [{ + "type" => "area-chart", + "y_label" => "Y-Axis" + }] + }] + } + end - it { expect { subject }.to raise_error(ActiveModel::ValidationError) } + it_behaves_like 'validation failed', metrics: ["can't be blank"], title: ["can't be blank"] end - context 'when panel groups are missing' do - before do - json_content['panel_groups'] = [] + context 'metrics definition is missing unit, query and query_range keys' 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", + "label" => "Metric of Ages", + "query_range" => nil + }] + }] + }] + } end - subject { described_class.from_json(json_content) } + it_behaves_like 'validation failed', unit: ["can't be blank"], query_range: ["can't be blank"], query: ["can't be blank"] + end + + # for each parent entry validation first is done to its children, + # whole execution is stopped on first encountered error + # which is the one that is reported + context 'multiple offences on different levels' do + let(:json_content) do + { + "dashboard" => nil, + "panel_groups" => [{ + "group" => nil, + "panels" => [{ + "type" => "area-chart", + "title" => nil, + "y_label" => "Y-Axis", + "metrics" => [{ + "id" => "metric_of_ages", + "label" => "Metric of Ages", + "query_range" => 'query' + }, { + "id" => "metric_of_ages", + "unit" => "count", + "label" => "Metric of Ages", + "query_range" => nil + }] + }] + }, { + "group" => 'group', + "panels" => nil + }] + } + end - it { expect { subject }.to raise_error(ActiveModel::ValidationError) } + it_behaves_like 'validation failed', unit: ["can't be blank"] end end end @@ -73,20 +190,51 @@ describe PerformanceMonitoring::PrometheusDashboard do 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 + expect(dashboard_instance.environment).to eq environment + expect(dashboard_instance.path).to eq 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) + allow(Gitlab::Metrics::Dashboard::Finder).to receive(:find).and_return(http_status: :not_found) dashboard_instance = described_class.find_for(project: project, user: user, path: path, options: { environment: environment }) expect(dashboard_instance).to be_nil end end + + context 'dashboard has invalid schema', :aggregate_failures do + it 'still returns dashboard object' do + expect(Gitlab::Metrics::Dashboard::Finder).to receive(:find).and_return(http_status: :unprocessable_entity) + + 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 eq environment + expect(dashboard_instance.path).to eq path + end + end + end + + describe '#schema_validation_warnings' do + context 'when schema is valid' do + it 'returns nil' do + expect(described_class).to receive(:from_json) + expect(described_class.new.schema_validation_warnings).to be_nil + end + end + + context 'when schema is invalid' do + it 'returns array with errors messages' do + instance = described_class.new + instance.errors.add(:test, 'test error') + + expect(described_class).to receive(:from_json).and_raise(ActiveModel::ValidationError.new(instance)) + expect(described_class.new.schema_validation_warnings).to eq ['test: test error'] + end + end end describe '#to_yaml' do diff --git a/spec/models/performance_monitoring/prometheus_metric_spec.rb b/spec/models/performance_monitoring/prometheus_metric_spec.rb index 08288e5d993..83f687aa90e 100644 --- a/spec/models/performance_monitoring/prometheus_metric_spec.rb +++ b/spec/models/performance_monitoring/prometheus_metric_spec.rb @@ -24,6 +24,14 @@ describe PerformanceMonitoring::PrometheusMetric do end describe 'validations' do + context 'json_content is not a hash' do + let(:json_content) { nil } + + subject { described_class.from_json(json_content) } + + it { expect { subject }.to raise_error(ActiveModel::ValidationError) } + end + context 'when unit is missing' do before do json_content['unit'] = nil diff --git a/spec/models/performance_monitoring/prometheus_panel_group_spec.rb b/spec/models/performance_monitoring/prometheus_panel_group_spec.rb index 2447bb5df94..ecf7e13a9a3 100644 --- a/spec/models/performance_monitoring/prometheus_panel_group_spec.rb +++ b/spec/models/performance_monitoring/prometheus_panel_group_spec.rb @@ -30,9 +30,17 @@ describe PerformanceMonitoring::PrometheusPanelGroup do end describe 'validations' do + context 'json_content is not a hash' do + let(:json_content) { nil } + + subject { described_class.from_json(json_content) } + + it { expect { subject }.to raise_error(ActiveModel::ValidationError) } + end + context 'when group is missing' do before do - json_content['group'] = nil + json_content.delete('group') end subject { described_class.from_json(json_content) } diff --git a/spec/models/performance_monitoring/prometheus_panel_spec.rb b/spec/models/performance_monitoring/prometheus_panel_spec.rb index f5e04ec91e2..127b9e8183a 100644 --- a/spec/models/performance_monitoring/prometheus_panel_spec.rb +++ b/spec/models/performance_monitoring/prometheus_panel_spec.rb @@ -42,6 +42,14 @@ describe PerformanceMonitoring::PrometheusPanel do end describe 'validations' do + context 'json_content is not a hash' do + let(:json_content) { nil } + + subject { described_class.from_json(json_content) } + + it { expect { subject }.to raise_error(ActiveModel::ValidationError) } + end + context 'when title is missing' do before do json_content['title'] = nil @@ -54,7 +62,7 @@ describe PerformanceMonitoring::PrometheusPanel do context 'when metrics are missing' do before do - json_content['metrics'] = [] + json_content.delete('metrics') end subject { described_class.from_json(json_content) } |