Welcome to mirror list, hosted at ThFree Co, Russian Federation.

alerts_finder_spec.rb « prometheus « projects « finders « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: bb59e77cca8c276a4474ce815b2dde635dcadb17 (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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# frozen_string_literal: true

require 'spec_helper'

describe Projects::Prometheus::AlertsFinder do
  let(:finder) { described_class.new(params) }
  let(:params) { {} }

  describe 'with params' do
    let_it_be(:project) { create(:project) }
    let_it_be(:other_project) { create(:project) }
    let_it_be(:other_env) { create(:environment, project: other_project) }
    let_it_be(:production) { create(:environment, project: project) }
    let_it_be(:staging) { create(:environment, project: project) }
    let_it_be(:alert) { create_alert(project, production) }
    let_it_be(:alert2) { create_alert(project, production) }
    let_it_be(:stg_alert) { create_alert(project, staging) }
    let_it_be(:other_alert) { create_alert(other_project, other_env) }

    describe '#execute' do
      subject { finder.execute }

      context 'with project' do
        before do
          params[:project] = project
        end

        it { is_expected.to eq([alert, alert2, stg_alert]) }

        context 'with matching metric' do
          before do
            params[:metric] = alert.prometheus_metric
          end

          it { is_expected.to eq([alert]) }
        end

        context 'with matching metric id' do
          before do
            params[:metric] = alert.prometheus_metric_id
          end

          it { is_expected.to eq([alert]) }
        end

        context 'with project non-specific metric' do
          before do
            params[:metric] = other_alert.prometheus_metric
          end

          it { is_expected.to be_empty }
        end
      end

      context 'with environment' do
        before do
          params[:environment] = production
        end

        it { is_expected.to eq([alert, alert2]) }

        context 'with matching metric' do
          before do
            params[:metric] = alert.prometheus_metric
          end

          it { is_expected.to eq([alert]) }
        end

        context 'with environment non-specific metric' do
          before do
            params[:metric] = stg_alert.prometheus_metric
          end

          it { is_expected.to be_empty }
        end
      end

      context 'with matching project and environment' do
        before do
          params[:project] = project
          params[:environment] = production
        end

        it { is_expected.to eq([alert, alert2]) }

        context 'with matching metric' do
          before do
            params[:metric] = alert.prometheus_metric
          end

          it { is_expected.to eq([alert]) }
        end

        context 'with environment non-specific metric' do
          before do
            params[:metric] = stg_alert.prometheus_metric
          end

          it { is_expected.to be_empty }
        end

        context 'with matching id' do
          before do
            params[:id] = alert.id
          end

          it { is_expected.to eq([alert]) }
        end

        context 'with a nil id' do
          before do
            params[:id] = nil
          end

          it { is_expected.to eq([alert, alert2]) }
        end
      end

      context 'with non-matching project-environment pair' do
        before do
          params[:project] = project
          params[:environment] = other_env
        end

        it { is_expected.to be_empty }
      end

      context 'with id' do
        before do
          params[:id] = alert.id
        end

        it { is_expected.to eq([alert]) }
      end

      context 'with multiple ids' do
        before do
          params[:id] = [alert.id, other_alert.id]
        end

        it { is_expected.to eq([alert, other_alert]) }
      end

      context 'with non-matching id' do
        before do
          params[:id] = -5
        end

        it { is_expected.to be_empty }
      end
    end

    private

    def create_alert(project, environment)
      create(:prometheus_alert, project: project, environment: environment)
    end
  end

  describe 'without params' do
    subject { finder }

    it 'raises an error' do
      expect { subject }
        .to raise_error(ArgumentError, 'Please provide one or more of the following params: :project, :environment, :id')
    end
  end
end