summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/database/reindexing/grafana_notifier_spec.rb
blob: e76718fe48a9964a9b86645d45748c252a57b220 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::Database::Reindexing::GrafanaNotifier do
  include Database::DatabaseHelpers

  let(:api_key) { "foo" }
  let(:api_url) { "http://bar"}
  let(:additional_tag) { "some-tag" }

  let(:action) { create(:reindex_action) }

  before do
    swapout_view_for_table(:postgres_indexes)
  end

  let(:headers) do
    {
      'Content-Type': 'application/json',
      'Authorization': "Bearer #{api_key}"
    }
  end

  let(:response) { double('response', success?: true) }

  def expect_api_call(payload)
    expect(Gitlab::HTTP).to receive(:post).with("#{api_url}/api/annotations", body: payload.to_json, headers: headers, allow_local_requests: true).and_return(response)
  end

  shared_examples_for 'interacting with Grafana annotations API' do
    it 'POSTs a JSON payload' do
      expect_api_call(payload)

      expect(subject).to be_truthy
    end

    context 'on error' do
      it 'does not raise the error and returns false' do
        allow(Gitlab::HTTP).to receive(:post).and_raise('something went wrong')

        expect(subject).to be_falsey
      end

      context 'when request was not successful' do
        it 'returns false' do
          expect_api_call(payload)
          allow(response).to receive(:success?).and_return(false)

          expect(subject).to be_falsey
        end
      end
    end

    context 'without api_key' do
      let(:api_key) { '' }

      it 'does not post anything' do
        expect(Gitlab::HTTP).not_to receive(:post)

        expect(subject).to be_falsey
      end
    end

    context 'without api_url' do
      let(:api_url) { '' }

      it 'does not post anything' do
        expect(Gitlab::HTTP).not_to receive(:post)

        expect(subject).to be_falsey
      end
    end
  end

  describe '#notify_start' do
    context 'additional tag is nil' do
      subject { described_class.new(api_key, api_url, nil).notify_start(action) }

      let(:payload) do
        {
          time: (action.action_start.utc.to_f * 1000).to_i,
          tags: ['reindex', action.index.tablename, action.index.name],
          text: "Started reindexing of #{action.index.name} on #{action.index.tablename}"
        }
      end

      it_behaves_like 'interacting with Grafana annotations API'
    end

    context 'additional tag is not nil' do
      subject { described_class.new(api_key, api_url, additional_tag).notify_start(action) }

      let(:payload) do
        {
          time: (action.action_start.utc.to_f * 1000).to_i,
          tags: ['reindex', additional_tag, action.index.tablename, action.index.name],
          text: "Started reindexing of #{action.index.name} on #{action.index.tablename}"
        }
      end

      it_behaves_like 'interacting with Grafana annotations API'
    end
  end

  describe '#notify_end' do
    context 'additional tag is nil' do
      subject { described_class.new(api_key, api_url, nil).notify_end(action) }

      let(:payload) do
        {
          time: (action.action_start.utc.to_f * 1000).to_i,
          tags: ['reindex', action.index.tablename, action.index.name],
          text: "Finished reindexing of #{action.index.name} on #{action.index.tablename} (#{action.state})",
          timeEnd: (action.action_end.utc.to_f * 1000).to_i,
          isRegion: true
        }
      end

      it_behaves_like 'interacting with Grafana annotations API'
    end

    context 'additional tag is not nil' do
      subject { described_class.new(api_key, api_url, additional_tag).notify_end(action) }

      let(:payload) do
        {
          time: (action.action_start.utc.to_f * 1000).to_i,
          tags: ['reindex', additional_tag, action.index.tablename, action.index.name],
          text: "Finished reindexing of #{action.index.name} on #{action.index.tablename} (#{action.state})",
          timeEnd: (action.action_end.utc.to_f * 1000).to_i,
          isRegion: true
        }
      end

      it_behaves_like 'interacting with Grafana annotations API'
    end
  end
end