notify_service_spec.rb 9.3 KB
Newer Older
1 2 3 4
# frozen_string_literal: true

require 'spec_helper'

5
RSpec.describe Projects::Prometheus::Alerts::NotifyService do
6 7
  include PrometheusHelpers

8 9 10 11 12 13 14 15 16 17 18 19
  let_it_be(:project, reload: true) { create(:project) }

  let(:service) { described_class.new(project, nil, payload) }
  let(:token_input) { 'token' }

  let!(:setting) do
    create(:project_incident_management_setting, project: project, send_email: true, create_issue: true)
  end

  let(:subject) { service.execute(token_input) }

  context 'with valid payload' do
20 21 22 23
    let_it_be(:alert_firing) { create(:prometheus_alert, project: project) }
    let_it_be(:alert_resolved) { create(:prometheus_alert, project: project) }
    let_it_be(:cluster) { create(:cluster, :provided_by_user, projects: [project]) }
    let(:payload_raw) { prometheus_alert_payload(firing: [alert_firing], resolved: [alert_resolved]) }
24 25 26 27
    let(:payload) { ActionController::Parameters.new(payload_raw).permit! }
    let(:payload_alert_firing) { payload_raw['alerts'].first }
    let(:token) { 'token' }

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
    context 'with environment specific clusters' do
      let(:prd_cluster) do
        cluster
      end

      let(:stg_cluster) do
        create(:cluster, :provided_by_user, projects: [project], enabled: true, environment_scope: 'stg/*')
      end

      let(:stg_environment) do
        create(:environment, project: project, name: 'stg/1')
      end

      let(:alert_firing) do
        create(:prometheus_alert, project: project, environment: stg_environment)
      end

      before do
        create(:clusters_applications_prometheus, :installed,
               cluster: prd_cluster, alert_manager_token: token)
        create(:clusters_applications_prometheus, :installed,
               cluster: stg_cluster, alert_manager_token: nil)
      end

      context 'without token' do
        let(:token_input) { nil }

55
        it_behaves_like 'Alert Notification Service sends notification email'
56 57 58
      end

      context 'with token' do
59
        it_behaves_like 'Alert Notification Service sends no notifications', http_status: :unauthorized
60 61 62
      end
    end

63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
    context 'with project specific cluster' do
      using RSpec::Parameterized::TableSyntax

      where(:cluster_enabled, :status, :configured_token, :token_input, :result) do
        true  | :installed | token | token | :success
        true  | :installed | nil   | nil   | :success
        true  | :updated   | token | token | :success
        true  | :updating  | token | token | :failure
        true  | :installed | token | 'x'   | :failure
        true  | :installed | nil   | token | :failure
        true  | :installed | token | nil   | :failure
        true  | nil        | token | token | :failure
        false | :installed | token | token | :failure
      end

      with_them do
        before do
80
          cluster.update!(enabled: cluster_enabled)
81 82 83 84 85 86 87 88 89 90

          if status
            create(:clusters_applications_prometheus, status,
                   cluster: cluster,
                   alert_manager_token: configured_token)
          end
        end

        case result = params[:result]
        when :success
91
          it_behaves_like 'Alert Notification Service sends notification email'
92
        when :failure
93
          it_behaves_like 'Alert Notification Service sends no notifications', http_status: :unauthorized
94 95 96 97 98 99 100 101 102
        else
          raise "invalid result: #{result.inspect}"
        end
      end
    end

    context 'without project specific cluster' do
      let!(:cluster) { create(:cluster, enabled: true) }

103
      it_behaves_like 'Alert Notification Service sends no notifications', http_status: :unauthorized
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
    end

    context 'with manual prometheus installation' do
      using RSpec::Parameterized::TableSyntax

      where(:alerting_setting, :configured_token, :token_input, :result) do
        true  | token | token | :success
        true  | token | 'x'   | :failure
        true  | token | nil   | :failure
        false | nil   | nil   | :success
        false | nil   | token | :failure
      end

      with_them do
        let(:alert_manager_token) { token_input }

        before do
          create(:prometheus_service, project: project)

          if alerting_setting
            create(:project_alerting_setting,
                   project: project,
                   token: configured_token)
          end
        end

        case result = params[:result]
        when :success
132
          it_behaves_like 'Alert Notification Service sends notification email'
133
        when :failure
134
          it_behaves_like 'Alert Notification Service sends no notifications', http_status: :unauthorized
135 136 137 138 139 140
        else
          raise "invalid result: #{result.inspect}"
        end
      end
    end

141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
    context 'with generic alerts integration' do
      using RSpec::Parameterized::TableSyntax

      where(:alerts_service, :token, :result) do
        :active   | :valid    | :success
        :active   | :invalid  | :failure
        :active   | nil       | :failure
        :inactive | :valid    | :failure
        nil       | nil       | :failure
      end

      with_them do
        let(:valid) { project.alerts_service.token }
        let(:invalid) { 'invalid token' }
        let(:token_input) { public_send(token) if token }

        before do
          if alerts_service
            create(:alerts_service, alerts_service, project: project)
          end
        end

        case result = params[:result]
        when :success
165
          it_behaves_like 'Alert Notification Service sends notification email'
166
        when :failure
167
          it_behaves_like 'Alert Notification Service sends no notifications', http_status: :unauthorized
168 169 170 171 172 173
        else
          raise "invalid result: #{result.inspect}"
        end
      end
    end

174 175 176 177 178 179 180 181 182 183 184 185 186
    context 'alert emails' do
      before do
        create(:prometheus_service, project: project)
        create(:project_alerting_setting, project: project, token: token)
      end

      context 'when incident_management_setting does not exist' do
        let!(:setting) { nil }

        it 'does not send notification email', :sidekiq_might_not_need_inline do
          expect_any_instance_of(NotificationService)
            .not_to receive(:async)

187
          expect(subject).to be_success
188 189 190 191
        end
      end

      context 'when incident_management_setting.send_email is true' do
192
        it_behaves_like 'Alert Notification Service sends notification email'
193 194 195 196 197 198 199 200 201 202
      end

      context 'incident_management_setting.send_email is false' do
        let!(:setting) do
          create(:project_incident_management_setting, send_email: false, project: project)
        end

        it 'does not send notification' do
          expect(NotificationService).not_to receive(:new)

203
          expect(subject).to be_success
204 205 206 207
        end
      end
    end

208 209 210 211 212 213 214 215
    context 'process Alert Management alerts' do
      let(:process_service) { instance_double(AlertManagement::ProcessPrometheusAlertService) }

      before do
        create(:prometheus_service, project: project)
        create(:project_alerting_setting, project: project, token: token)
      end

216 217
      context 'with multiple firing alerts and resolving alerts' do
        let(:payload_raw) do
218
          prometheus_alert_payload(firing: [alert_firing, alert_firing], resolved: [alert_resolved])
219 220
        end

221
        it 'processes Prometheus alerts' do
222
          expect(AlertManagement::ProcessPrometheusAlertService)
223 224 225 226 227
            .to receive(:new)
            .with(project, nil, kind_of(Hash))
            .exactly(3).times
            .and_return(process_service)
          expect(process_service).to receive(:execute).exactly(3).times
228 229 230 231 232

          subject
        end
      end
    end
233 234 235
  end

  context 'with invalid payload' do
236
    context 'when payload is not processable' do
237 238
      let(:payload) { {} }

239 240 241 242
      before do
        allow(described_class).to receive(:processable?).with(payload)
          .and_return(false)
      end
243

244
      it_behaves_like 'Alert Notification Service sends no notifications', http_status: :unprocessable_entity
245 246 247 248 249 250 251 252 253 254
    end

    context 'when the payload is too big' do
      let(:payload) { { 'the-payload-is-too-big' => true } }
      let(:deep_size_object) { instance_double(Gitlab::Utils::DeepSize, valid?: false) }

      before do
        allow(Gitlab::Utils::DeepSize).to receive(:new).and_return(deep_size_object)
      end

255
      it_behaves_like 'Alert Notification Service sends no notifications', http_status: :bad_request
256

257 258 259 260 261 262
      it 'does not process Prometheus alerts' do
        expect(AlertManagement::ProcessPrometheusAlertService)
          .not_to receive(:new)

        subject
      end
263 264 265
    end
  end

266 267 268 269 270 271 272 273 274 275 276 277
  describe '.processable?' do
    let(:valid_payload) { prometheus_alert_payload }

    subject { described_class.processable?(payload) }

    context 'with valid payload' do
      let(:payload) { valid_payload }

      it { is_expected.to eq(true) }

      context 'containing unrelated keys' do
        let(:payload) { valid_payload.merge('unrelated' => 'key') }
278

279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
        it { is_expected.to eq(true) }
      end
    end

    context 'with invalid payload' do
      where(:missing_key) do
        described_class::REQUIRED_PAYLOAD_KEYS.to_a
      end

      with_them do
        let(:payload) { valid_payload.except(missing_key) }

        it { is_expected.to eq(false) }
      end
    end

    context 'with unsupported version' do
      let(:payload) { valid_payload.merge('version' => '5') }

      it { is_expected.to eq(false) }
    end
300 301
  end
end