summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/database/reindexing/grafana_notifier_spec.rb
blob: e67c97cbf9c749db099680c028a49eb7640b4183 (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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# 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, connection: ApplicationRecord.connection)
  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 'when Grafana is configured using application settings' do
      subject { described_class.new.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

      before do
        stub_application_setting(database_grafana_api_key: api_key)
        stub_application_setting(database_grafana_api_url: api_url)
        stub_application_setting(database_grafana_tag: additional_tag)
      end

      it_behaves_like 'interacting with Grafana annotations API'
    end

    context 'when there is no additional tag' do
      subject { described_class.new(api_key: api_key, api_url: api_url, additional_tag: '').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 provided' do
      subject do
        described_class.new(api_key: api_key, api_url: api_url, additional_tag: additional_tag).notify_start(action)
      end

      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 'when Grafana is configured using application settings' do
      subject { described_class.new.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

      before do
        stub_application_setting(database_grafana_api_key: api_key)
        stub_application_setting(database_grafana_api_url: api_url)
        stub_application_setting(database_grafana_tag: additional_tag)
      end

      it_behaves_like 'interacting with Grafana annotations API'
    end

    context 'when there is no additional tag' do
      subject { described_class.new(api_key: api_key, api_url: api_url, additional_tag: '').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 provided' do
      subject do
        described_class.new(api_key: api_key, api_url: api_url, additional_tag: additional_tag).notify_end(action)
      end

      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