diff options
Diffstat (limited to 'spec/support/shared_examples/services/metrics/dashboard_shared_examples.rb')
-rw-r--r-- | spec/support/shared_examples/services/metrics/dashboard_shared_examples.rb | 80 |
1 files changed, 78 insertions, 2 deletions
diff --git a/spec/support/shared_examples/services/metrics/dashboard_shared_examples.rb b/spec/support/shared_examples/services/metrics/dashboard_shared_examples.rb index 5dd1badbefc..c8fabfe30b9 100644 --- a/spec/support/shared_examples/services/metrics/dashboard_shared_examples.rb +++ b/spec/support/shared_examples/services/metrics/dashboard_shared_examples.rb @@ -29,14 +29,43 @@ RSpec.shared_examples 'valid dashboard service response' do end RSpec.shared_examples 'caches the unprocessed dashboard for subsequent calls' do - it do - expect(YAML).to receive(:safe_load).once.and_call_original + specify do + expect_next_instance_of(::Gitlab::Config::Loader::Yaml) do |loader| + expect(loader).to receive(:load_raw!).once.and_call_original + end described_class.new(*service_params).get_dashboard described_class.new(*service_params).get_dashboard end end +# This spec is applicable for predefined/out-of-the-box dashboard services. +RSpec.shared_examples 'refreshes cache when dashboard_version is changed' do + specify do + allow_next_instance_of(described_class) do |service| + allow(service).to receive(:dashboard_version).and_return('1', '2') + end + + expect(File).to receive(:read).twice.and_call_original + + service = described_class.new(*service_params) + + service.get_dashboard + service.get_dashboard + end +end + +# This spec is applicable for predefined/out-of-the-box dashboard services. +# This shared_example requires the following variables to be defined: +# dashboard_path: Relative path to the dashboard, ex: 'config/prometheus/common_metrics.yml' +# dashboard_version: The version string used in the cache_key. +RSpec.shared_examples 'dashboard_version contains SHA256 hash of dashboard file content' do + specify do + dashboard = File.read(Rails.root.join(dashboard_path)) + expect(Digest::SHA256.hexdigest(dashboard)).to eq(dashboard_version) + end +end + RSpec.shared_examples 'valid embedded dashboard service response' do let(:dashboard_schema) { Gitlab::Json.parse(fixture_file('lib/gitlab/metrics/dashboard/schemas/embedded_dashboard.json')) } @@ -128,3 +157,50 @@ RSpec.shared_examples 'updates gitlab_metrics_dashboard_processing_time_ms metri expect(metric.get(labels)).to be > 0 end end + +RSpec.shared_examples '#raw_dashboard raises error if dashboard loading fails' do + context 'when yaml is too large' do + before do + allow_next_instance_of(::Gitlab::Config::Loader::Yaml) do |loader| + allow(loader).to receive(:load_raw!) + .and_raise(Gitlab::Config::Loader::Yaml::DataTooLargeError, 'The parsed YAML is too big') + end + end + + it 'raises error' do + expect { subject.raw_dashboard }.to raise_error( + Gitlab::Metrics::Dashboard::Errors::LayoutError, + 'The parsed YAML is too big' + ) + end + end + + context 'when yaml loader returns error' do + before do + allow_next_instance_of(::Gitlab::Config::Loader::Yaml) do |loader| + allow(loader).to receive(:load_raw!) + .and_raise(Gitlab::Config::Loader::FormatError, 'Invalid configuration format') + end + end + + it 'raises error' do + expect { subject.raw_dashboard }.to raise_error( + Gitlab::Metrics::Dashboard::Errors::LayoutError, + 'Invalid yaml' + ) + end + end + + context 'when yaml is not a hash' do + before do + allow_next_instance_of(::Gitlab::Config::Loader::Yaml) do |loader| + allow(loader).to receive(:load_raw!) + .and_raise(Gitlab::Config::Loader::Yaml::NotHashError, 'Invalid configuration format') + end + end + + it 'returns nil' do + expect(subject.raw_dashboard).to eq({}) + end + end +end |