Skip to content
Snippets Groups Projects
Commit 65f67718 authored by Rajendra Kadam's avatar Rajendra Kadam :two: Committed by Vitali Tatarintev
Browse files

Clean metric templates yml related files

Update specs and usages

Updated usages in models
parent c59c9253
No related branches found
No related tags found
1 merge request!121632Clean metric templates yml related files
Showing
with 3 additions and 335 deletions
......@@ -72,7 +72,6 @@ Gitlab/StrongMemoizeAttr:
- 'app/models/alert_management/alert.rb'
- 'app/models/application_setting_implementation.rb'
- 'app/models/blob_viewer/go_mod.rb'
- 'app/models/blob_viewer/metrics_dashboard_yml.rb'
- 'app/models/bulk_imports/export.rb'
- 'app/models/bulk_imports/export_status.rb'
- 'app/models/bulk_imports/file_transfer/base_config.rb'
......
......@@ -33,7 +33,6 @@ Style/PercentLiteralDelimiters:
- 'app/models/blob_viewer/godeps_json.rb'
- 'app/models/blob_viewer/license.rb'
- 'app/models/blob_viewer/markup.rb'
- 'app/models/blob_viewer/metrics_dashboard_yml.rb'
- 'app/models/blob_viewer/notebook.rb'
- 'app/models/blob_viewer/open_api.rb'
- 'app/models/blob_viewer/package_json.rb'
......
......@@ -27,14 +27,9 @@ def all_template_names(project, type)
end
def type_allowed?(type)
case type.to_s
when 'licenses'
true
when 'metrics_dashboard_ymls'
!Feature.enabled?(:remove_monitor_metrics)
else
VENDORED_TEMPLATES.key?(type)
end
return true if type.to_s == 'licenses'
VENDORED_TEMPLATES.key?(type)
end
end
......
......@@ -54,7 +54,6 @@ class Blob < SimpleDelegator
BlobViewer::License,
BlobViewer::Contributing,
BlobViewer::Changelog,
BlobViewer::MetricsDashboardYml,
BlobViewer::CargoToml,
BlobViewer::Cartfile,
......
# frozen_string_literal: true
module BlobViewer
class MetricsDashboardYml < Base
include ServerSide
include Gitlab::Utils::StrongMemoize
include Auxiliary
self.partial_name = 'metrics_dashboard_yml'
self.loading_partial_name = 'metrics_dashboard_yml_loading'
self.file_types = %i(metrics_dashboard)
self.binary = false
def self.can_render?(blob, verify_binary: true)
super && !Feature.enabled?(:remove_monitor_metrics)
end
def valid?
errors.blank?
end
def errors
strong_memoize(:errors) do
prepare!
parse_blob_data
end
end
private
def parse_blob_data
old_metrics_dashboard_validation
end
def old_metrics_dashboard_validation
yaml = ::Gitlab::Config::Loader::Yaml.new(blob.data).load_raw!
::PerformanceMonitoring::PrometheusDashboard.from_json(yaml)
[]
rescue Gitlab::Config::Loader::FormatError => e
["YAML syntax: #{e.message}"]
rescue ActiveModel::ValidationError => e
e.model.errors.messages.map { |messages| messages.join(': ') }
end
end
end
# frozen_string_literal: true
module Gitlab
module Template
class CustomMetricsDashboardYmlTemplate < CustomTemplate
class << self
def extension
'.yml'
end
def base_dir
'metrics-dashboards/'
end
end
end
end
end
......@@ -20,10 +20,6 @@
'LICENSE/bar.txt' => 'CustomLicenseTemplate Bar',
'LICENSE/bad.xyz' => 'CustomLicenseTemplate Bad',
'metrics-dashboards/foo.yml' => 'CustomMetricsDashboardYmlTemplate Foo',
'metrics-dashboards/bar.yml' => 'CustomMetricsDashboardYmlTemplate Bar',
'metrics-dashboards/bad.xyz' => 'CustomMetricsDashboardYmlTemplate Bad',
'Dockerfile/category/baz.txt' => 'CustomDockerfileTemplate category baz',
'gitignore/category/baz.txt' => 'CustomGitignoreTemplate category baz',
'gitlab-ci/category/baz.yml' => 'CustomGitlabCiYmlTemplate category baz',
......@@ -47,7 +43,6 @@
{ class_name: ::Gitlab::Template::CustomGitignoreTemplate, category: 'Custom' },
{ class_name: ::Gitlab::Template::CustomGitlabCiYmlTemplate, category: 'Custom' },
{ class_name: ::Gitlab::Template::CustomLicenseTemplate, category: 'Custom' },
{ class_name: ::Gitlab::Template::CustomMetricsDashboardYmlTemplate, category: 'Custom' },
{ class_name: ::Gitlab::Template::IssueTemplate, category: 'Project Templates' },
{ class_name: ::Gitlab::Template::MergeRequestTemplate, category: 'Project Templates' }
].freeze
......
# frozen_string_literal: true
module Gitlab
module Template
class MetricsDashboardTemplate < BaseTemplate
def description
"# This file is a template, and might need editing before it works on your project."
end
class << self
def extension
'.metrics-dashboard.yml'
end
def categories
{
"General" => ''
}
end
def base_dir
Rails.root.join('lib/gitlab/metrics/templates')
end
def finder(project = nil)
Gitlab::Template::Finders::GlobalTemplateFinder.new(self.base_dir, self.extension, self.categories)
end
end
end
end
end
......@@ -579,66 +579,6 @@ def switch_ref_to(ref_name)
end
end
describe '.gitlab/dashboards/custom-dashboard.yml' do
let(:remove_monitor_metrics) { false }
before do
stub_feature_flags(remove_monitor_metrics: remove_monitor_metrics)
project.add_maintainer(project.creator)
Files::CreateService.new(
project,
project.creator,
start_branch: 'master',
branch_name: 'master',
commit_message: "Add .gitlab/dashboards/custom-dashboard.yml",
file_path: '.gitlab/dashboards/custom-dashboard.yml',
file_content: file_content
).execute
visit_blob('.gitlab/dashboards/custom-dashboard.yml')
end
context 'valid dashboard file' do
let(:file_content) { File.read(Rails.root.join('config/prometheus/common_metrics.yml')) }
it 'displays an auxiliary viewer' do
aggregate_failures do
# shows that dashboard yaml is valid
expect(page).to have_content('Metrics Dashboard YAML definition is valid.')
# shows a learn more link
expect(page).to have_link('Learn more')
end
end
context 'when metrics dashboard feature is unavailable' do
let(:remove_monitor_metrics) { true }
it 'displays the blob without an auxiliary viewer' do
expect(page).to have_content('Environment metrics')
expect(page).not_to have_content('Metrics Dashboard YAML definition', wait: 0)
end
end
end
context 'invalid dashboard file' do
let(:file_content) { "dashboard: 'invalid'" }
it 'displays an auxiliary viewer' do
aggregate_failures do
# shows that dashboard yaml is invalid
expect(page).to have_content('Metrics Dashboard YAML definition is invalid:')
expect(page).to have_content("panel_groups: should be an array of panel_groups objects")
# shows a learn more link
expect(page).to have_link('Learn more')
end
end
end
end
context 'LICENSE' do
before do
visit_blob('LICENSE')
......
......@@ -102,10 +102,6 @@
describe '#build' do
let(:project) { build_stubbed(:project) }
before do
stub_feature_flags(remove_monitor_metrics: false)
end
where(:type, :expected_class) do
:dockerfiles | described_class
:gitignores | described_class
......
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Gitlab::Template::MetricsDashboardTemplate do
subject { described_class }
describe '.all' do
it 'combines the globals and rest' do
all = subject.all.map(&:name)
expect(all).to include('Default')
end
end
describe '#content' do
it 'loads the full file' do
example_dashboard = subject.new(Rails.root.join('lib/gitlab/metrics/templates/Default.metrics-dashboard.yml'))
expect(example_dashboard.name).to eq 'Default'
expect(example_dashboard.content).to start_with('#')
end
end
it_behaves_like 'file template shared examples', 'Default', '.metrics-dashboard.yml'
end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe BlobViewer::MetricsDashboardYml, feature_category: :metrics 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 }
let(:data) { fixture_file('lib/gitlab/metrics/dashboard/sample_dashboard.yml') }
subject(:viewer) { described_class.new(blob) }
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!)
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
end
end
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)
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
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(PerformanceMonitoring::PrometheusDashboard).not_to receive(:from_json)
expect(viewer.valid?).to be false
end
end
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
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
expect(viewer.errors).to eq ["YAML syntax: The parsed YAML is too big"]
end
end
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
end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment