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

submit_service_ping_service_spec.rb « service_ping « services « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 2248febda5c86e322a2702a7b16b50fce5c41e5a (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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe ServicePing::SubmitService, feature_category: :service_ping do
  include StubRequests
  include UsageDataHelpers

  let(:usage_data_id) { 31643 }
  let(:score_params) do
    {
      score: {
        leader_issues: 10.2,
        instance_issues: 3.2,
        percentage_issues: 31.37,

        leader_notes: 25.3,
        instance_notes: 23.2,

        leader_milestones: 16.2,
        instance_milestones: 5.5,

        leader_boards: 5.2,
        instance_boards: 3.2,

        leader_merge_requests: 5.2,
        instance_merge_requests: 3.2,

        leader_ci_pipelines: 25.1,
        instance_ci_pipelines: 21.3,

        leader_environments: 3.3,
        instance_environments: 2.2,

        leader_deployments: 41.3,
        instance_deployments: 15.2,

        leader_projects_prometheus_active: 0.31,
        instance_projects_prometheus_active: 0.30,

        leader_service_desk_issues: 15.8,
        instance_service_desk_issues: 15.1,

        usage_data_id: usage_data_id,

        non_existing_column: 'value'
      }
    }
  end

  let(:with_dev_ops_score_params) { { dev_ops_score: score_params[:score] } }
  let(:with_conv_index_params) { { conv_index: score_params[:score] } }
  let(:with_usage_data_id_params) { { conv_index: { usage_data_id: usage_data_id } } }
  let(:service_ping_payload_url) { File.join(described_class::STAGING_BASE_URL, described_class::USAGE_DATA_PATH) }
  let(:service_ping_errors_url) { File.join(described_class::STAGING_BASE_URL, described_class::ERROR_PATH) }
  let(:service_ping_metadata_url) { File.join(described_class::STAGING_BASE_URL, described_class::METADATA_PATH) }
  let!(:usage_data) { { uuid: 'uuid', recorded_at: Time.current } }

  let(:subject) { described_class.new(payload: usage_data) }

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

      subject.execute
    end
  end

  shared_examples 'does not send a blank usage ping payload' do
    it do
      expect(Gitlab::HTTP).not_to receive(:post).with(service_ping_payload_url, any_args)

      expect { subject.execute }.to raise_error(described_class::SubmissionError) do |error|
        expect(error.message).to include('Usage data payload is blank')
      end
    end
  end

  shared_examples 'saves DevOps report data from the response' do
    it do
      expect { subject.execute }
        .to change { DevOpsReport::Metric.count }
        .by(1)

      expect(DevOpsReport::Metric.last.leader_issues).to eq 10.2
      expect(DevOpsReport::Metric.last.instance_issues).to eq 3.2
      expect(DevOpsReport::Metric.last.percentage_issues).to eq 31.37
    end
  end

  context 'when usage ping is disabled' do
    before do
      stub_application_setting(usage_ping_enabled: false)
    end

    it_behaves_like 'does not run'
  end

  context 'when usage ping is disabled from GitLab config file' do
    before do
      stub_config_setting(usage_ping_enabled: false)
    end

    it_behaves_like 'does not run'
  end

  context 'when product_intelligence_enabled is false' do
    before do
      allow(ServicePing::ServicePingSettings).to receive(:product_intelligence_enabled?).and_return(false)
    end

    it_behaves_like 'does not run'
  end

  context 'when product_intelligence_enabled is true' do
    before do
      stub_usage_data_connections
      stub_database_flavor_check

      allow(ServicePing::ServicePingSettings).to receive(:product_intelligence_enabled?).and_return(true)
    end

    it 'submits a service ping payload without errors', :aggregate_failures do
      response = stub_response(body: with_dev_ops_score_params)
      error_response = stub_response(body: nil, url: service_ping_errors_url, status: 201)
      metadata_response = stub_response(body: nil, url: service_ping_metadata_url, status: 201)

      expect(Gitlab::HTTP).to receive(:post).twice.and_call_original

      subject.execute

      expect(response).to have_been_requested
      expect(error_response).not_to have_been_requested
      expect(metadata_response).to have_been_requested
    end
  end

  context 'when usage ping is enabled' do
    before do
      stub_usage_data_connections
      stub_database_flavor_check
      stub_application_setting(usage_ping_enabled: true)
      stub_response(body: nil, url: service_ping_errors_url, status: 201)
      stub_response(body: nil, url: service_ping_metadata_url, status: 201)
    end

    context 'and user requires usage stats consent' do
      before do
        allow(User).to receive(:single_user)
          .and_return(instance_double(User, :user, requires_usage_stats_consent?: true))
      end

      it_behaves_like 'does not run'
    end

    it 'sends a POST request' do
      stub_response(body: nil, url: service_ping_metadata_url, status: 201)
      response = stub_response(body: with_dev_ops_score_params)

      subject.execute

      expect(response).to have_been_requested
    end

    context 'when conv_index data is passed' do
      let(:usage_data) { { uuid: 'uuid', recorded_at: Time.current } }

      before do
        stub_response(body: with_conv_index_params)
      end

      it_behaves_like 'saves DevOps report data from the response'

      it 'saves usage_data_id to version_usage_data_id_value' do
        subject.execute

        raw_usage_data = RawUsageData.find_by(recorded_at: usage_data[:recorded_at])

        expect(raw_usage_data.version_usage_data_id_value).to eq(31643)
      end
    end

    context 'when only usage_data_id is passed in response' do
      let(:usage_data) { { uuid: 'uuid', recorded_at: Time.current } }

      before do
        stub_response(body: with_usage_data_id_params)
      end

      it 'does not save DevOps report data' do
        expect { subject.execute }.not_to change { DevOpsReport::Metric.count }
      end

      it 'saves usage_data_id to version_usage_data_id_value' do
        subject.execute

        raw_usage_data = RawUsageData.find_by(recorded_at: usage_data[:recorded_at])

        expect(raw_usage_data.version_usage_data_id_value).to eq(31643)
      end
    end

    context 'when version app usage_data_id is invalid' do
      let(:usage_data_id) { -1000 }

      before do
        stub_response(body: with_conv_index_params)
      end

      it 'raises an exception' do
        expect { subject.execute }.to raise_error(described_class::SubmissionError) do |error|
          expect(error.message).to include('Invalid usage_data_id in response: -1000')
        end
      end
    end

    context 'when DevOps report data is passed' do
      before do
        stub_response(body: with_dev_ops_score_params)
      end

      it_behaves_like 'saves DevOps report data from the response'
    end

    context 'with saving raw_usage_data' do
      let(:usage_data) { { uuid: 'uuid', recorded_at: Time.current } }

      before do
        stub_response(body: with_dev_ops_score_params)
      end

      it 'creates a raw_usage_data record' do
        expect { subject.execute }.to change(RawUsageData, :count).by(1)
      end

      it 'saves the correct payload' do
        subject.execute

        raw_usage_data = RawUsageData.find_by(recorded_at: usage_data[:recorded_at])

        expect(raw_usage_data.payload.to_json).to eq(usage_data.to_json)
      end
    end

    context 'and usage ping response has unsuccessful status' do
      before do
        stub_response(body: nil, status: 504)
      end

      it 'raises an exception' do
        expect { subject.execute }.to raise_error(described_class::SubmissionError) do |error|
          expect(error.message).to include('Unsuccessful response code: 504')
        end
      end
    end

    context 'and usage data is empty string' do
      let(:usage_data) { {} }

      it_behaves_like 'does not send a blank usage ping payload'
    end

    context 'and usage data is nil' do
      let(:usage_data) { nil }

      it_behaves_like 'does not send a blank usage ping payload'
    end

    context 'if version app response fails' do
      before do
        stub_response(body: with_dev_ops_score_params, status: 404)
      end

      it 'raises SubmissionError' do
        # SubmissionError is raised as a result of 404 in response from HTTP Request
        expect { subject.execute }.to raise_error(described_class::SubmissionError)
      end
    end

    context 'when skip_db_write passed to service' do
      let(:subject) { described_class.new(payload: usage_data, skip_db_write: true) }

      before do
        stub_response(body: with_dev_ops_score_params)
      end

      it 'does not save RawUsageData' do
        expect { subject.execute }
          .not_to change { RawUsageData.count }
      end

      it 'does not call DevOpsReport service' do
        expect(ServicePing::DevopsReport).not_to receive(:new)

        subject.execute
      end
    end
  end

  context 'metadata reporting' do
    before do
      stub_usage_data_connections
      stub_database_flavor_check
      stub_application_setting(usage_ping_enabled: true)
      stub_response(body: with_conv_index_params)
    end

    let(:metric_double) do
      instance_double(Gitlab::Usage::ServicePing::LegacyMetricMetadataDecorator, duration: 123, error: nil)
    end

    let(:metric_double_with_error) do
      instance_double(Gitlab::Usage::ServicePing::LegacyMetricMetadataDecorator, duration: 123, error: 'Error')
    end

    let(:usage_data) do
      {
        uuid: 'uuid',
        metric_a: metric_double,
        metric_group: {
            metric_b: metric_double_with_error
          },
        metric_without_timing: "value",
        recorded_at: Time.current
        }
    end

    let(:metadata_payload) do
      {
        metadata: {
          uuid: 'uuid',
          metrics: [
            { name: 'metric_a', time_elapsed: 123 },
            { name: 'metric_group.metric_b', time_elapsed: 123, error: 'Error' }
          ]
          }
        }
    end

    it 'submits metadata' do
      response = stub_full_request(service_ping_metadata_url, method: :post)
                   .with(body: metadata_payload)

      subject.execute

      expect(response).to have_been_requested
    end
  end

  def stub_response(body:, url: service_ping_payload_url, status: 201)
    stub_full_request(url, method: :post)
      .to_return(
        headers: { 'Content-Type' => 'application/json' },
        body: body.to_json,
        status: status
      )
  end
end