# frozen_string_literal: true require 'spec_helper' RSpec.describe BlobViewer::MetricsDashboardYml do include FakeBlobHelpers include RepoHelpers let_it_be(:project) { create(:project, :repository) } let(:blob) { fake_blob(path: '.gitlab/dashboards/custom-dashboard.yml', data: data) } let(:sha) { sample_commit.id } subject(:viewer) { described_class.new(blob) } context 'when the definition is valid' do let(:data) { fixture_file('lib/gitlab/metrics/dashboard/sample_dashboard.yml')} describe '#valid?' do it 'calls prepare! on the viewer' do allow(Gitlab::Metrics::Dashboard::Validator).to receive(:errors) expect(viewer).to receive(:prepare!) viewer.valid? end 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(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_truthy end end describe '#errors' do it 'returns empty array' do allow(Gitlab::Metrics::Dashboard::Validator).to receive(:errors).and_return([]) 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 describe '#valid?' do it 'returns false' do expect(viewer.valid?).to be false end end describe '#errors' do it 'returns validation errors' do expect(viewer.errors).to eq [error] end end end context 'when YAML syntax is invalid' do let(:data) do <<~YAML dashboard: 'empty metrics' panel_groups: - group: 'Group Title' YAML end describe '#valid?' do it 'returns false' do expect(Gitlab::Metrics::Dashboard::Validator).not_to receive(:errors) expect(viewer.valid?).to be false end end describe '#errors' do it 'returns validation errors' do expect(viewer.errors).to all be_kind_of Gitlab::Config::Loader::FormatError 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(Gitlab::Metrics::Dashboard::Validator).not_to receive(:errors) expect(viewer.valid?).to be false end it 'returns validation errors' do expect(viewer.errors).to all be_kind_of Gitlab::Config::Loader::FormatError end end end