summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/metrics/dashboard/stages/panel_ids_inserter_spec.rb
blob: 6124f471e3980f537bd788b7cf100e4448995df2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# frozen_string_literal: true

require 'spec_helper'

describe Gitlab::Metrics::Dashboard::Stages::PanelIdsInserter do
  let(:project) { build_stubbed(:project) }

  def fetch_panel_ids(dashboard_hash)
    dashboard_hash[:panel_groups].flat_map { |group| group[:panels].flat_map { |panel| panel[:id] } }
  end

  describe '#transform!' do
    subject(:transform!) { described_class.new(project, dashboard, nil).transform! }

    let(:dashboard) { YAML.safe_load(fixture_file('lib/gitlab/metrics/dashboard/sample_dashboard.yml')).deep_symbolize_keys }

    context 'when dashboard panels are present' do
      it 'assigns unique ids to each panel using PerformanceMonitoring::PrometheusPanel', :aggregate_failures do
        dashboard.fetch(:panel_groups).each do |group|
          group.fetch(:panels).each do |panel|
            panel_double = instance_double(::PerformanceMonitoring::PrometheusPanel)

            expect(::PerformanceMonitoring::PrometheusPanel).to receive(:new).with(panel).and_return(panel_double)
            expect(panel_double).to receive(:id).with(group[:group]).and_return(FFaker::Lorem.unique.characters(125))
          end
        end

        transform!

        expect(fetch_panel_ids(dashboard)).not_to include nil
      end
    end

    context 'when dashboard panels has duplicated ids' do
      it 'no panel has assigned id' do
        panel_double = instance_double(::PerformanceMonitoring::PrometheusPanel)
        allow(::PerformanceMonitoring::PrometheusPanel).to receive(:new).and_return(panel_double)
        allow(panel_double).to receive(:id).and_return('duplicated id')

        transform!

        expect(fetch_panel_ids(dashboard)).to all be_nil
        expect(fetch_panel_ids(dashboard)).not_to include 'duplicated id'
      end
    end

    context 'when there are no panels in the dashboard' do
      it 'raises a processing error' do
        dashboard[:panel_groups][0].delete(:panels)

        expect { transform! }.to(
          raise_error(::Gitlab::Metrics::Dashboard::Errors::DashboardProcessingError)
        )
      end
    end

    context 'when there are no panel_groups in the dashboard' do
      it 'raises a processing error' do
        dashboard.delete(:panel_groups)

        expect { transform! }.to(
          raise_error(::Gitlab::Metrics::Dashboard::Errors::DashboardProcessingError)
        )
      end
    end

    context 'when dashboard panels has unknown schema attributes' do
      before do
        error = ActiveModel::UnknownAttributeError.new(double, 'unknown_panel_attribute')
        allow(::PerformanceMonitoring::PrometheusPanel).to receive(:new).and_raise(error)
      end

      it 'no panel has assigned id' do
        transform!

        expect(fetch_panel_ids(dashboard)).to all be_nil
      end

      it 'logs the failure' do
        expect(Gitlab::ErrorTracking).to receive(:log_exception)

        transform!
      end
    end
  end
end