summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/metrics/dashboard/importer_spec.rb
blob: 8b705395a2c257069f8762284868507b4fcb0217 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::Metrics::Dashboard::Importer do
  include MetricsDashboardHelpers

  let_it_be(:dashboard_path) { '.gitlab/dashboards/sample_dashboard.yml' }
  let_it_be(:project) { create(:project) }

  before do
    allow(subject).to receive(:dashboard_hash).and_return(dashboard_hash)
  end

  subject { described_class.new(dashboard_path, project) }

  describe '.execute' do
    context 'valid dashboard hash' do
      let(:dashboard_hash) { load_sample_dashboard }

      it 'imports metrics to database' do
        expect { subject.execute }
          .to change { PrometheusMetric.count }.from(0).to(3)
      end
    end

    context 'invalid dashboard hash' do
      let(:dashboard_hash) { {} }

      it 'returns false' do
        expect(subject.execute).to be(false)
      end
    end
  end

  describe '.execute!' do
    context 'valid dashboard hash' do
      let(:dashboard_hash) { load_sample_dashboard }

      it 'imports metrics to database' do
        expect { subject.execute }
          .to change { PrometheusMetric.count }.from(0).to(3)
      end
    end

    context 'invalid dashboard hash' do
      let(:dashboard_hash) { {} }

      it 'raises error' do
        expect { subject.execute! }.to raise_error(Gitlab::Metrics::Dashboard::Validator::Errors::SchemaValidationError,
          'root is missing required keys: dashboard, panel_groups')
      end
    end
  end
end