2019-04-11 08:17:24 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-05-24 06:25:44 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 11:08:50 -04:00
|
|
|
RSpec.describe SubmitUsagePingService do
|
2019-04-21 06:03:26 -04:00
|
|
|
include StubRequests
|
2020-06-03 08:08:21 -04:00
|
|
|
include UsageDataHelpers
|
2019-04-21 06:03:26 -04:00
|
|
|
|
2020-01-22 07:08:40 -05:00
|
|
|
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,
|
|
|
|
|
|
|
|
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(:without_dev_ops_score_params) { { dev_ops_score: {} } }
|
|
|
|
|
2020-08-04 23:10:58 -04:00
|
|
|
shared_examples 'does not run' do
|
|
|
|
it do
|
|
|
|
expect(Gitlab::HTTP).not_to receive(:post)
|
2020-08-17 17:09:56 -04:00
|
|
|
expect(Gitlab::UsageData).not_to receive(:data)
|
2017-05-24 06:25:44 -04:00
|
|
|
|
2020-08-04 23:10:58 -04:00
|
|
|
subject.execute
|
|
|
|
end
|
|
|
|
end
|
2017-05-24 06:25:44 -04:00
|
|
|
|
2020-08-04 23:10:58 -04:00
|
|
|
shared_examples 'does not send a blank usage ping payload' do
|
|
|
|
it do
|
|
|
|
expect(Gitlab::HTTP).not_to receive(:post)
|
2017-05-24 06:25:44 -04:00
|
|
|
|
2020-08-04 23:10:58 -04:00
|
|
|
expect { subject.execute }.to raise_error(described_class::SubmissionError) do |error|
|
|
|
|
expect(error.message).to include('Usage data is blank')
|
|
|
|
end
|
2017-05-24 06:25:44 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-08-31 11:10:41 -04:00
|
|
|
shared_examples 'saves DevOps report data from the response' do
|
2020-01-22 07:08:40 -05:00
|
|
|
it do
|
|
|
|
expect { subject.execute }
|
2020-09-10 14:08:54 -04:00
|
|
|
.to change { DevOpsReport::Metric.count }
|
2020-01-22 07:08:40 -05:00
|
|
|
.by(1)
|
|
|
|
|
2020-09-10 14:08:54 -04:00
|
|
|
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
|
2020-01-22 07:08:40 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-08-04 23:10:58 -04:00
|
|
|
context 'when usage ping is disabled' do
|
|
|
|
before do
|
|
|
|
stub_application_setting(usage_ping_enabled: false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'does not run'
|
|
|
|
end
|
|
|
|
|
2017-05-24 06:25:44 -04:00
|
|
|
context 'when usage ping is enabled' do
|
|
|
|
before do
|
2020-06-03 08:08:21 -04:00
|
|
|
stub_usage_data_connections
|
2017-05-24 06:25:44 -04:00
|
|
|
stub_application_setting(usage_ping_enabled: true)
|
|
|
|
end
|
|
|
|
|
2020-08-04 23:10:58 -04:00
|
|
|
context 'and user requires usage stats consent' do
|
|
|
|
before do
|
|
|
|
allow(User).to receive(:single_user).and_return(double(:user, requires_usage_stats_consent?: true))
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'does not run'
|
|
|
|
end
|
|
|
|
|
2017-05-24 06:25:44 -04:00
|
|
|
it 'sends a POST request' do
|
2020-08-04 23:10:58 -04:00
|
|
|
response = stub_response(body: without_dev_ops_score_params)
|
2017-05-24 06:25:44 -04:00
|
|
|
|
|
|
|
subject.execute
|
|
|
|
|
|
|
|
expect(response).to have_been_requested
|
|
|
|
end
|
|
|
|
|
2020-08-04 23:10:58 -04:00
|
|
|
it 'forces a refresh of usage data statistics before submitting' do
|
|
|
|
stub_response(body: without_dev_ops_score_params)
|
2017-05-24 06:25:44 -04:00
|
|
|
|
2020-08-17 17:09:56 -04:00
|
|
|
expect(Gitlab::UsageData).to receive(:data).with(force_refresh: true).and_call_original
|
2017-05-24 06:25:44 -04:00
|
|
|
|
|
|
|
subject.execute
|
|
|
|
end
|
|
|
|
|
2020-01-22 07:08:40 -05:00
|
|
|
context 'when conv_index data is passed' do
|
|
|
|
before do
|
2020-08-04 23:10:58 -04:00
|
|
|
stub_response(body: with_conv_index_params)
|
2020-01-22 07:08:40 -05:00
|
|
|
end
|
2017-05-24 06:25:44 -04:00
|
|
|
|
2020-08-31 11:10:41 -04:00
|
|
|
it_behaves_like 'saves DevOps report data from the response'
|
2017-05-24 06:25:44 -04:00
|
|
|
end
|
|
|
|
|
2020-08-31 11:10:41 -04:00
|
|
|
context 'when DevOps report data is passed' do
|
2020-01-22 07:08:40 -05:00
|
|
|
before do
|
2020-08-04 23:10:58 -04:00
|
|
|
stub_response(body: with_dev_ops_score_params)
|
2020-01-22 07:08:40 -05:00
|
|
|
end
|
2017-05-24 06:25:44 -04:00
|
|
|
|
2020-08-31 11:10:41 -04:00
|
|
|
it_behaves_like 'saves DevOps report data from the response'
|
2020-01-22 07:08:40 -05:00
|
|
|
end
|
2020-08-04 23:10:58 -04:00
|
|
|
|
2020-12-15 04:10:00 -05:00
|
|
|
context 'with saving raw_usage_data' do
|
2020-08-17 17:09:56 -04:00
|
|
|
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
|
|
|
|
recorded_at = Time.current
|
|
|
|
usage_data = { uuid: 'uuid', recorded_at: recorded_at }
|
|
|
|
|
|
|
|
expect(Gitlab::UsageData).to receive(:data).with(force_refresh: true).and_return(usage_data)
|
|
|
|
|
|
|
|
subject.execute
|
|
|
|
|
|
|
|
raw_usage_data = RawUsageData.find_by(recorded_at: recorded_at)
|
|
|
|
|
|
|
|
expect(raw_usage_data.recorded_at).to be_like_time(recorded_at)
|
|
|
|
expect(raw_usage_data.payload.to_json).to eq(usage_data.to_json)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-08-04 23:10:58 -04:00
|
|
|
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
|
|
|
|
before do
|
2020-08-17 17:09:56 -04:00
|
|
|
allow(Gitlab::UsageData).to receive(:data).and_return({})
|
2020-08-04 23:10:58 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'does not send a blank usage ping payload'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'and usage data is nil' do
|
|
|
|
before do
|
2020-08-17 17:09:56 -04:00
|
|
|
allow(Gitlab::UsageData).to receive(:data).and_return(nil)
|
2020-08-04 23:10:58 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'does not send a blank usage ping payload'
|
|
|
|
end
|
2017-05-24 06:25:44 -04:00
|
|
|
end
|
|
|
|
|
2020-08-04 23:10:58 -04:00
|
|
|
def stub_response(body:, status: 201)
|
2020-08-11 05:10:03 -04:00
|
|
|
stub_full_request(SubmitUsagePingService::STAGING_URL, method: :post)
|
2017-06-21 09:48:12 -04:00
|
|
|
.to_return(
|
2017-05-24 06:25:44 -04:00
|
|
|
headers: { 'Content-Type' => 'application/json' },
|
2020-08-04 23:10:58 -04:00
|
|
|
body: body.to_json,
|
|
|
|
status: status
|
2017-05-24 06:25:44 -04:00
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|