summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/metrics/dashboard/importers/prometheus_metrics_spec.rb
blob: ff8f5797f9da18c7bd4edc3e411f17057e877872 (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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::Metrics::Dashboard::Importers::PrometheusMetrics do
  include MetricsDashboardHelpers

  describe '#execute' do
    let(:project) { create(:project) }
    let(:dashboard_path) { 'path/to/dashboard.yml' }
    let(:prometheus_adapter) { double('adapter', clear_prometheus_reactive_cache!: nil) }

    subject { described_class.new(dashboard_hash, project: project, dashboard_path: dashboard_path) }

    before do
      allow_next_instance_of(::Clusters::Applications::ScheduleUpdateService) do |update_service|
        allow(update_service).to receive(:execute)
      end
    end

    context 'valid dashboard' do
      let(:dashboard_hash) { load_sample_dashboard }

      context 'with all new metrics' do
        it 'creates PrometheusMetrics' do
          expect { subject.execute }.to change { PrometheusMetric.count }.by(3)
        end
      end

      context 'with existing metrics' do
        let(:existing_metric_attributes) do
          {
            project:        project,
            identifier:     'metric_b',
            title:          'overwrite',
            y_label:        'overwrite',
            query:          'overwrite',
            unit:           'overwrite',
            legend:         'overwrite',
            dashboard_path: dashboard_path
          }
        end

        let!(:existing_metric) do
          create(:prometheus_metric, existing_metric_attributes)
        end

        let!(:existing_alert) do
          alert = create(:prometheus_alert, project: project, prometheus_metric: existing_metric)
          existing_metric.prometheus_alerts << alert

          alert
        end

        it 'updates existing PrometheusMetrics' do
          subject.execute

          expect(existing_metric.reload.attributes.with_indifferent_access).to include({
            title:   'Super Chart B',
            y_label: 'y_label',
            query:   'query',
            unit:    'unit',
            legend:  'Legend Label'
          })
        end

        it 'creates new PrometheusMetrics' do
          expect { subject.execute }.to change { PrometheusMetric.count }.by(2)
        end

        it 'updates affected environments' do
          expect(::Clusters::Applications::ScheduleUpdateService).to receive(:new).with(
            existing_alert.environment.cluster_prometheus_adapter,
            project
          ).and_return(double('ScheduleUpdateService', execute: true))

          subject.execute
        end

        context 'with stale metrics' do
          let!(:stale_metric) do
            create(:prometheus_metric,
              project: project,
              identifier: 'stale_metric',
              dashboard_path: dashboard_path,
              group: 3
            )
          end

          let!(:stale_alert) do
            alert = create(:prometheus_alert, project: project, prometheus_metric: stale_metric)
            stale_metric.prometheus_alerts << alert

            alert
          end

          it 'updates existing PrometheusMetrics' do
            subject.execute

            expect(existing_metric.reload.attributes.with_indifferent_access).to include({
              title:   'Super Chart B',
              y_label: 'y_label',
              query:   'query',
              unit:    'unit',
              legend:  'Legend Label'
            })
          end

          it 'deletes stale metrics' do
            subject.execute

            expect { stale_metric.reload }.to raise_error(ActiveRecord::RecordNotFound)
          end

          it 'deletes stale alert' do
            subject.execute

            expect { stale_alert.reload }.to raise_error(ActiveRecord::RecordNotFound)
          end

          it 'updates affected environments' do
            expect(::Clusters::Applications::ScheduleUpdateService).to receive(:new).with(
              existing_alert.environment.cluster_prometheus_adapter,
              project
            ).and_return(double('ScheduleUpdateService', execute: true))

            subject.execute
          end
        end
      end
    end

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

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