debian-mirror-gitlab/spec/models/blob_viewer/metrics_dashboard_yml_spec.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

137 lines
3.6 KiB
Ruby
Raw Normal View History

2020-06-23 00:09:42 +05:30
# frozen_string_literal: true
require 'spec_helper'
2023-07-09 08:55:56 +05:30
RSpec.describe BlobViewer::MetricsDashboardYml, feature_category: :metrics do
2020-06-23 00:09:42 +05:30
include FakeBlobHelpers
include RepoHelpers
let_it_be(:project) { create(:project, :repository) }
2021-04-29 21:17:54 +05:30
2020-06-23 00:09:42 +05:30
let(:blob) { fake_blob(path: '.gitlab/dashboards/custom-dashboard.yml', data: data) }
let(:sha) { sample_commit.id }
2020-11-24 15:15:51 +05:30
let(:data) { fixture_file('lib/gitlab/metrics/dashboard/sample_dashboard.yml') }
2020-06-23 00:09:42 +05:30
subject(:viewer) { described_class.new(blob) }
2023-03-04 22:38:38 +05:30
context 'when the definition is valid' do
describe '#valid?' do
2020-11-24 15:15:51 +05:30
before do
2023-03-04 22:38:38 +05:30
allow(PerformanceMonitoring::PrometheusDashboard).to receive(:from_json)
2020-11-24 15:15:51 +05:30
end
2023-03-04 22:38:38 +05:30
it 'calls prepare! on the viewer' do
expect(viewer).to receive(:prepare!)
2020-11-24 15:15:51 +05:30
2023-03-04 22:38:38 +05:30
viewer.valid?
2020-11-24 15:15:51 +05:30
end
2020-06-23 00:09:42 +05:30
2023-03-04 22:38:38 +05:30
it 'processes dashboard yaml and returns true', :aggregate_failures do
yml = ::Gitlab::Config::Loader::Yaml.new(data).load_raw!
2020-06-23 00:09:42 +05:30
2023-03-04 22:38:38 +05:30
expect_next_instance_of(::Gitlab::Config::Loader::Yaml, data) do |loader|
expect(loader).to receive(:load_raw!).and_call_original
2020-11-24 15:15:51 +05:30
end
2023-03-04 22:38:38 +05:30
expect(PerformanceMonitoring::PrometheusDashboard)
.to receive(:from_json)
.with(yml)
.and_call_original
expect(viewer.valid?).to be true
2020-11-24 15:15:51 +05:30
end
end
2020-06-23 00:09:42 +05:30
2023-03-04 22:38:38 +05:30
describe '#errors' do
it 'returns empty array' do
expect(viewer.errors).to eq []
2020-06-23 00:09:42 +05:30
end
end
end
2023-03-04 22:38:38 +05:30
context 'when definition is invalid' do
let(:error) { ActiveModel::ValidationError.new(PerformanceMonitoring::PrometheusDashboard.new.tap(&:validate)) }
let(:data) do
<<~YAML
dashboard:
YAML
2020-06-23 00:09:42 +05:30
end
2023-03-04 22:38:38 +05:30
describe '#valid?' do
it 'returns false' do
expect(PerformanceMonitoring::PrometheusDashboard)
.to receive(:from_json).and_raise(error)
2020-06-23 00:09:42 +05:30
2023-03-04 22:38:38 +05:30
expect(viewer.valid?).to be false
2020-06-23 00:09:42 +05:30
end
end
2023-03-04 22:38:38 +05:30
describe '#errors' do
it 'returns validation errors' do
allow(PerformanceMonitoring::PrometheusDashboard)
.to receive(:from_json).and_raise(error)
2020-06-23 00:09:42 +05:30
2023-03-04 22:38:38 +05:30
expect(viewer.errors).to eq error.model.errors.messages.map { |messages| messages.join(': ') }
2020-06-23 00:09:42 +05:30
end
end
2023-03-04 22:38:38 +05:30
end
2020-06-23 00:09:42 +05:30
2023-03-04 22:38:38 +05:30
context 'when YAML syntax is invalid' do
let(:data) do
<<~YAML
2020-06-23 00:09:42 +05:30
dashboard: 'empty metrics'
panel_groups:
- group: 'Group Title'
2023-03-04 22:38:38 +05:30
YAML
end
2020-06-23 00:09:42 +05:30
2023-03-04 22:38:38 +05:30
describe '#valid?' do
it 'returns false' do
expect(PerformanceMonitoring::PrometheusDashboard).not_to receive(:from_json)
expect(viewer.valid?).to be false
2020-11-24 15:15:51 +05:30
end
2023-03-04 22:38:38 +05:30
end
2020-06-23 00:09:42 +05:30
2023-03-04 22:38:38 +05:30
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"]
2020-06-23 00:09:42 +05:30
end
end
2023-03-04 22:38:38 +05:30
end
2020-06-23 00:09:42 +05:30
2023-03-04 22:38:38 +05:30
context 'when YAML loader raises error' do
let(:data) do
<<~YAML
2020-06-23 00:09:42 +05:30
large yaml file
2023-03-04 22:38:38 +05:30
YAML
end
2020-06-23 00:09:42 +05:30
2023-03-04 22:38:38 +05:30
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-23 00:09:42 +05:30
2023-03-04 22:38:38 +05:30
it 'is invalid' do
expect(PerformanceMonitoring::PrometheusDashboard).not_to receive(:from_json)
expect(viewer.valid?).to be false
end
2020-06-23 00:09:42 +05:30
2023-03-04 22:38:38 +05:30
it 'returns validation errors' do
expect(viewer.errors).to eq ["YAML syntax: The parsed YAML is too big"]
2020-06-23 00:09:42 +05:30
end
end
2023-07-09 08:55:56 +05:30
describe '.can_render?' do
subject { described_class.can_render?(blob) }
it { is_expected.to be false }
context 'when metrics dashboard feature is available' do
before do
stub_feature_flags(remove_monitor_metrics: false)
end
it { is_expected.to be true }
end
end
2020-06-23 00:09:42 +05:30
end