debian-mirror-gitlab/spec/services/submit_usage_ping_service_spec.rb

127 lines
3 KiB
Ruby
Raw Normal View History

2019-07-31 22:56:46 +05:30
# frozen_string_literal: true
2017-09-10 17:25:29 +05:30
require 'spec_helper'
2020-07-28 23:09:34 +05:30
RSpec.describe SubmitUsagePingService do
2019-06-05 12:25:43 +05:30
include StubRequests
2020-06-23 00:09:42 +05:30
include UsageDataHelpers
2019-06-05 12:25:43 +05:30
2020-03-13 15:44:24 +05:30
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: {} } }
2017-09-10 17:25:29 +05:30
context 'when usage ping is disabled' do
before do
stub_application_setting(usage_ping_enabled: false)
end
it 'does not run' do
expect(HTTParty).not_to receive(:post)
result = subject.execute
expect(result).to eq false
end
end
2020-03-13 15:44:24 +05:30
shared_examples 'saves DevOps score data from the response' do
it do
expect { subject.execute }
.to change { DevOpsScore::Metric.count }
.by(1)
expect(DevOpsScore::Metric.last.leader_issues).to eq 10.2
expect(DevOpsScore::Metric.last.instance_issues).to eq 3.2
expect(DevOpsScore::Metric.last.percentage_issues).to eq 31.37
end
end
2017-09-10 17:25:29 +05:30
context 'when usage ping is enabled' do
before do
2020-06-23 00:09:42 +05:30
stub_usage_data_connections
2017-09-10 17:25:29 +05:30
stub_application_setting(usage_ping_enabled: true)
end
it 'sends a POST request' do
2020-03-13 15:44:24 +05:30
response = stub_response(without_dev_ops_score_params)
2017-09-10 17:25:29 +05:30
subject.execute
expect(response).to have_been_requested
end
it 'refreshes usage data statistics before submitting' do
2020-03-13 15:44:24 +05:30
stub_response(without_dev_ops_score_params)
2017-09-10 17:25:29 +05:30
expect(Gitlab::UsageData).to receive(:to_json)
.with(force_refresh: true)
.and_call_original
subject.execute
end
2020-03-13 15:44:24 +05:30
context 'when conv_index data is passed' do
before do
stub_response(with_conv_index_params)
end
2017-09-10 17:25:29 +05:30
2020-03-13 15:44:24 +05:30
it_behaves_like 'saves DevOps score data from the response'
2017-09-10 17:25:29 +05:30
end
2020-03-13 15:44:24 +05:30
context 'when DevOps score data is passed' do
before do
stub_response(with_dev_ops_score_params)
end
2017-09-10 17:25:29 +05:30
2020-03-13 15:44:24 +05:30
it_behaves_like 'saves DevOps score data from the response'
end
2017-09-10 17:25:29 +05:30
end
def stub_response(body)
2019-06-05 12:25:43 +05:30
stub_full_request('https://version.gitlab.com/usage_data', method: :post)
2017-09-10 17:25:29 +05:30
.to_return(
headers: { 'Content-Type' => 'application/json' },
body: body.to_json
)
end
end