summaryrefslogtreecommitdiff
path: root/spec/finders/metrics/dashboards/annotations_finder_spec.rb
blob: 7c5932dde1e2e3e7e4a8f2b956474888de2bfc5a (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Metrics::Dashboards::AnnotationsFinder do
  describe '#execute' do
    subject(:annotations) { described_class.new(dashboard: dashboard, params: params).execute }

    let_it_be(:current_user) { create(:user) }

    let(:path) { 'config/prometheus/common_metrics.yml' }
    let(:params) { {} }
    let(:environment) { create(:environment) }
    let(:dashboard) { PerformanceMonitoring::PrometheusDashboard.new(path: path, environment: environment) }

    context 'there are no annotations records' do
      it 'returns empty array' do
        expect(annotations).to be_empty
      end
    end

    context 'with annotation records' do
      let!(:nine_minutes_old_annotation) { create(:metrics_dashboard_annotation, environment: environment, starting_at: 9.minutes.ago, dashboard_path: path) }
      let!(:fifteen_minutes_old_annotation) { create(:metrics_dashboard_annotation, environment: environment, starting_at: 15.minutes.ago, dashboard_path: path) }
      let!(:just_created_annotation) { create(:metrics_dashboard_annotation, environment: environment, dashboard_path: path) }
      let!(:annotation_for_different_env) { create(:metrics_dashboard_annotation, dashboard_path: path) }
      let!(:annotation_for_different_dashboard) { create(:metrics_dashboard_annotation, dashboard_path: '.gitlab/dashboards/test.yml') }

      it 'loads annotations' do
        expect(annotations).to match_array [fifteen_minutes_old_annotation, nine_minutes_old_annotation, just_created_annotation]
      end

      context 'when the from filter is present' do
        let(:params) do
          {
            from: 14.minutes.ago
          }
        end

        it 'loads only younger annotations' do
          expect(annotations).to match_array [nine_minutes_old_annotation, just_created_annotation]
        end
      end

      context 'when the to filter is present' do
        let(:params) do
          {
            to: 5.minutes.ago
          }
        end

        it 'loads only older annotations' do
          expect(annotations).to match_array [fifteen_minutes_old_annotation, nine_minutes_old_annotation]
        end
      end

      context 'when from and to filters are present' do
        context 'and to is bigger than from' do
          let(:params) do
            {
              from: 14.minutes.ago,
              to: 5.minutes.ago
            }
          end

          it 'loads only annotations assigned to this interval' do
            expect(annotations).to match_array [nine_minutes_old_annotation]
          end
        end

        context 'and from is bigger than to' do
          let(:params) do
            {
              to: 14.minutes.ago,
              from: 5.minutes.ago
            }
          end

          it 'ignores to parameter and returns annotations starting at from filter' do
            expect(annotations).to match_array [just_created_annotation]
          end
        end

        context 'when from or to filters are empty strings' do
          let(:params) do
            {
              from: '',
              to: ''
            }
          end

          it 'ignores this parameters' do
            expect(annotations).to match_array [fifteen_minutes_old_annotation, nine_minutes_old_annotation, just_created_annotation]
          end
        end
      end

      context 'dashboard environment is missing' do
        let(:dashboard) { PerformanceMonitoring::PrometheusDashboard.new(path: path, environment: nil) }

        it 'returns empty relation', :aggregate_failures do
          expect(annotations).to be_kind_of ::ActiveRecord::Relation
          expect(annotations).to be_empty
        end
      end
    end
  end
end