2020-06-03 05:08:47 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 14:09:03 -04:00
|
|
|
RSpec.describe BlobViewer::MetricsDashboardYml do
|
2020-06-03 05:08:47 -04:00
|
|
|
include FakeBlobHelpers
|
|
|
|
include RepoHelpers
|
|
|
|
|
|
|
|
let_it_be(:project) { create(:project, :repository) }
|
2021-04-09 05:09:10 -04:00
|
|
|
|
2020-06-03 05:08:47 -04:00
|
|
|
let(:blob) { fake_blob(path: '.gitlab/dashboards/custom-dashboard.yml', data: data) }
|
|
|
|
let(:sha) { sample_commit.id }
|
2020-08-27 11:10:21 -04:00
|
|
|
let(:data) { fixture_file('lib/gitlab/metrics/dashboard/sample_dashboard.yml') }
|
2020-06-03 05:08:47 -04:00
|
|
|
|
|
|
|
subject(:viewer) { described_class.new(blob) }
|
|
|
|
|
2020-08-27 11:10:21 -04:00
|
|
|
context 'with metrics_dashboard_exhaustive_validations feature flag on' do
|
|
|
|
before do
|
|
|
|
stub_feature_flags(metrics_dashboard_exhaustive_validations: true)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the definition is valid' do
|
|
|
|
before do
|
|
|
|
allow(Gitlab::Metrics::Dashboard::Validator).to receive(:errors).and_return([])
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#valid?' do
|
|
|
|
it 'calls prepare! on the viewer' do
|
|
|
|
expect(viewer).to receive(:prepare!)
|
|
|
|
|
|
|
|
viewer.valid?
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'processes dashboard yaml and 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(Gitlab::Metrics::Dashboard::Validator)
|
|
|
|
.to receive(:errors)
|
|
|
|
.with(yml, dashboard_path: '.gitlab/dashboards/custom-dashboard.yml', project: project)
|
|
|
|
.and_call_original
|
|
|
|
expect(viewer.valid?).to be true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#errors' do
|
|
|
|
it 'returns empty array' do
|
|
|
|
expect(viewer.errors).to eq []
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when definition is invalid' do
|
|
|
|
let(:error) { ::Gitlab::Metrics::Dashboard::Validator::Errors::SchemaValidationError.new }
|
|
|
|
let(:data) do
|
|
|
|
<<~YAML
|
|
|
|
dashboard:
|
|
|
|
YAML
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
allow(Gitlab::Metrics::Dashboard::Validator).to receive(:errors).and_return([error])
|
|
|
|
end
|
2020-06-03 05:08:47 -04:00
|
|
|
|
2020-08-27 11:10:21 -04:00
|
|
|
describe '#valid?' do
|
|
|
|
it 'returns false' do
|
|
|
|
expect(viewer.valid?).to be false
|
|
|
|
end
|
|
|
|
end
|
2020-06-03 05:08:47 -04:00
|
|
|
|
2020-08-27 11:10:21 -04:00
|
|
|
describe '#errors' do
|
|
|
|
it 'returns validation errors' do
|
|
|
|
expect(viewer.errors).to eq ["Dashboard failed schema validation"]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-06-03 05:08:47 -04:00
|
|
|
|
2020-08-27 11:10:21 -04:00
|
|
|
context 'when YAML syntax is invalid' do
|
|
|
|
let(:data) do
|
|
|
|
<<~YAML
|
|
|
|
dashboard: 'empty metrics'
|
|
|
|
panel_groups:
|
|
|
|
- group: 'Group Title'
|
|
|
|
YAML
|
2020-06-03 05:08:47 -04:00
|
|
|
end
|
|
|
|
|
2020-08-27 11:10:21 -04:00
|
|
|
describe '#valid?' do
|
|
|
|
it 'returns false' do
|
|
|
|
expect(Gitlab::Metrics::Dashboard::Validator).not_to receive(:errors)
|
|
|
|
expect(viewer.valid?).to be false
|
|
|
|
end
|
|
|
|
end
|
2020-06-16 14:09:01 -04:00
|
|
|
|
2020-08-27 11:10:21 -04:00
|
|
|
describe '#errors' do
|
|
|
|
it 'returns validation errors' do
|
|
|
|
expect(viewer.errors).to all be_kind_of String
|
2020-06-16 14:09:01 -04:00
|
|
|
end
|
2020-06-03 05:08:47 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-08-27 11:10:21 -04:00
|
|
|
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
|
2020-06-03 05:08:47 -04:00
|
|
|
|
2020-08-27 11:10:21 -04:00
|
|
|
it 'is invalid' do
|
|
|
|
expect(Gitlab::Metrics::Dashboard::Validator).not_to receive(:errors)
|
|
|
|
expect(viewer.valid?).to be false
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns validation errors' do
|
|
|
|
expect(viewer.errors).to eq ['The parsed YAML is too big']
|
2020-06-03 05:08:47 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-08-27 11:10:21 -04:00
|
|
|
context 'with metrics_dashboard_exhaustive_validations feature flag off' do
|
|
|
|
before do
|
|
|
|
stub_feature_flags(metrics_dashboard_exhaustive_validations: false)
|
2020-06-03 05:08:47 -04:00
|
|
|
end
|
|
|
|
|
2020-08-27 11:10:21 -04:00
|
|
|
context 'when the definition is valid' do
|
|
|
|
describe '#valid?' do
|
|
|
|
before do
|
|
|
|
allow(PerformanceMonitoring::PrometheusDashboard).to receive(:from_json)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'calls prepare! on the viewer' do
|
|
|
|
expect(viewer).to receive(:prepare!)
|
2020-08-19 14:10:34 -04:00
|
|
|
|
2020-08-27 11:10:21 -04:00
|
|
|
viewer.valid?
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'processes dashboard yaml and 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)
|
|
|
|
.to receive(:from_json)
|
|
|
|
.with(yml)
|
|
|
|
.and_call_original
|
|
|
|
expect(viewer.valid?).to be true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#errors' do
|
|
|
|
it 'returns empty array' do
|
|
|
|
expect(viewer.errors).to eq []
|
|
|
|
end
|
2020-06-03 05:08:47 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-08-27 11:10:21 -04:00
|
|
|
context 'when definition is invalid' do
|
|
|
|
let(:error) { ActiveModel::ValidationError.new(PerformanceMonitoring::PrometheusDashboard.new.tap(&:validate)) }
|
|
|
|
let(:data) do
|
|
|
|
<<~YAML
|
|
|
|
dashboard:
|
|
|
|
YAML
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#valid?' do
|
|
|
|
it 'returns false' do
|
|
|
|
expect(PerformanceMonitoring::PrometheusDashboard)
|
|
|
|
.to receive(:from_json).and_raise(error)
|
2020-08-19 14:10:34 -04:00
|
|
|
|
2020-08-27 11:10:21 -04:00
|
|
|
expect(viewer.valid?).to be false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#errors' do
|
|
|
|
it 'returns validation errors' do
|
|
|
|
allow(PerformanceMonitoring::PrometheusDashboard)
|
|
|
|
.to receive(:from_json).and_raise(error)
|
|
|
|
|
|
|
|
expect(viewer.errors).to eq error.model.errors.messages.map { |messages| messages.join(': ') }
|
|
|
|
end
|
2020-06-03 05:08:47 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-08-27 11:10:21 -04:00
|
|
|
context 'when YAML syntax is invalid' do
|
|
|
|
let(:data) do
|
|
|
|
<<~YAML
|
2020-06-03 05:08:47 -04:00
|
|
|
dashboard: 'empty metrics'
|
|
|
|
panel_groups:
|
|
|
|
- group: 'Group Title'
|
2020-08-27 11:10:21 -04:00
|
|
|
YAML
|
2020-06-03 05:08:47 -04:00
|
|
|
end
|
|
|
|
|
2020-08-27 11:10:21 -04:00
|
|
|
describe '#valid?' do
|
|
|
|
it 'returns false' do
|
|
|
|
expect(PerformanceMonitoring::PrometheusDashboard).not_to receive(:from_json)
|
|
|
|
expect(viewer.valid?).to be false
|
|
|
|
end
|
|
|
|
end
|
2020-08-19 14:10:34 -04:00
|
|
|
|
2020-08-27 11:10:21 -04:00
|
|
|
describe '#errors' do
|
|
|
|
it 'returns validation errors' do
|
|
|
|
expect(viewer.errors).to eq ["YAML syntax: (<unknown>): did not find expected key while parsing a block mapping at line 1 column 1"]
|
|
|
|
end
|
2020-06-03 05:08:47 -04:00
|
|
|
end
|
|
|
|
end
|
2020-06-16 14:09:01 -04:00
|
|
|
|
2020-08-27 11:10:21 -04:00
|
|
|
context 'when YAML loader raises error' do
|
|
|
|
let(:data) do
|
|
|
|
<<~YAML
|
2020-06-16 14:09:01 -04:00
|
|
|
large yaml file
|
2020-08-27 11:10:21 -04:00
|
|
|
YAML
|
|
|
|
end
|
2020-06-16 14:09:01 -04:00
|
|
|
|
2020-08-27 11:10:21 -04:00
|
|
|
before do
|
|
|
|
allow(::Gitlab::Config::Loader::Yaml).to(
|
|
|
|
receive(:new).and_raise(::Gitlab::Config::Loader::Yaml::DataTooLargeError, 'The parsed YAML is too big')
|
|
|
|
)
|
|
|
|
end
|
2020-06-16 14:09:01 -04:00
|
|
|
|
2020-08-27 11:10:21 -04:00
|
|
|
it 'is invalid' do
|
|
|
|
expect(PerformanceMonitoring::PrometheusDashboard).not_to receive(:from_json)
|
|
|
|
expect(viewer.valid?).to be false
|
|
|
|
end
|
2020-08-19 14:10:34 -04:00
|
|
|
|
2020-08-27 11:10:21 -04:00
|
|
|
it 'returns validation errors' do
|
|
|
|
expect(viewer.errors).to eq ["YAML syntax: The parsed YAML is too big"]
|
|
|
|
end
|
2020-06-16 14:09:01 -04:00
|
|
|
end
|
|
|
|
end
|
2020-06-03 05:08:47 -04:00
|
|
|
end
|