Commit 494e98a5 authored by Jan Provaznik's avatar Jan Provaznik

Merge branch 'rp-use-gitlab-yaml-loader-blob-viewer' into 'master'

Use the Gitlab yaml loader in metrics dashboard blob viewer

See merge request gitlab-org/gitlab!34199
parents 0b67a824 b44e5c36
...@@ -25,9 +25,11 @@ module BlobViewer ...@@ -25,9 +25,11 @@ module BlobViewer
private private
def parse_blob_data def parse_blob_data
::PerformanceMonitoring::PrometheusDashboard.from_json(YAML.safe_load(blob.data)) yaml = ::Gitlab::Config::Loader::Yaml.new(blob.data).load_raw!
::PerformanceMonitoring::PrometheusDashboard.from_json(yaml)
nil nil
rescue Psych::SyntaxError => error rescue Gitlab::Config::Loader::FormatError => error
wrap_yml_syntax_error(error) wrap_yml_syntax_error(error)
rescue ActiveModel::ValidationError => invalid rescue ActiveModel::ValidationError => invalid
invalid.model.errors invalid.model.errors
......
---
title: Display error for YAML files that are too large
merge_request: 34199
author:
type: changed
...@@ -361,6 +361,13 @@ When **Metrics Dashboard YAML definition is invalid** at least one of the follow ...@@ -361,6 +361,13 @@ When **Metrics Dashboard YAML definition is invalid** at least one of the follow
1. `query: can't be blank` [learn more](#metrics-metrics-properties) 1. `query: can't be blank` [learn more](#metrics-metrics-properties)
1. `query_range: can't be blank` [learn more](#metrics-metrics-properties) 1. `query_range: can't be blank` [learn more](#metrics-metrics-properties)
1. `unit: can't be blank` [learn more](#metrics-metrics-properties) 1. `unit: can't be blank` [learn more](#metrics-metrics-properties)
1. `YAML syntax: The parsed YAML is too big`
This is displayed when the YAML file is larger than 1 MB.
1. `YAML syntax: Invalid configuration format`
This is displayed when the YAML file is empty or does not contain valid YAML.
Metrics Dashboard YAML definition validation information is also available as a [GraphQL API field](../../../api/graphql/reference/index.md#metricsdashboard) Metrics Dashboard YAML definition validation information is also available as a [GraphQL API field](../../../api/graphql/reference/index.md#metricsdashboard)
......
...@@ -24,9 +24,16 @@ describe BlobViewer::MetricsDashboardYml do ...@@ -24,9 +24,16 @@ describe BlobViewer::MetricsDashboardYml do
viewer.valid? viewer.valid?
end end
it 'returns true' do it 'returns true', :aggregate_failures do
yml = ::Gitlab::Config::Loader::Yaml.new(data).load_raw!
expect_next_instance_of(::Gitlab::Config::Loader::Yaml, data) do |loader|
expect(loader).to receive(:load_raw!).and_call_original
end
expect(PerformanceMonitoring::PrometheusDashboard) expect(PerformanceMonitoring::PrometheusDashboard)
.to receive(:from_json).with(YAML.safe_load(data)) .to receive(:from_json)
.with(yml)
.and_call_original
expect(viewer.valid?).to be_truthy expect(viewer.valid?).to be_truthy
end end
end end
...@@ -92,4 +99,29 @@ describe BlobViewer::MetricsDashboardYml do ...@@ -92,4 +99,29 @@ describe BlobViewer::MetricsDashboardYml do
end end
end end
end end
context 'when YAML loader raises error' do
let(:data) do
<<~YAML
large yaml file
YAML
end
before do
allow(::Gitlab::Config::Loader::Yaml).to receive(:new)
.and_raise(::Gitlab::Config::Loader::Yaml::DataTooLargeError, 'The parsed YAML is too big')
end
it 'is invalid' do
expect(PerformanceMonitoring::PrometheusDashboard).not_to receive(:from_json)
expect(viewer.valid?).to be(false)
end
it 'returns validation errors' do
yaml_wrapped_errors = { 'YAML syntax': ["The parsed YAML is too big"] }
expect(viewer.errors).to be_kind_of(ActiveModel::Errors)
expect(viewer.errors.messages).to eq(yaml_wrapped_errors)
end
end
end end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment