Commit 0bcc4399 authored by Doug Stull's avatar Doug Stull

Add tests and condition for license scan

- we want to capture the case of license failing for fallback
parent 14f9744f
...@@ -6,12 +6,25 @@ module EE ...@@ -6,12 +6,25 @@ module EE
extend ActiveSupport::Concern extend ActiveSupport::Concern
SECURE_PRODUCT_TYPES = { SECURE_PRODUCT_TYPES = {
container_scanning: :container_scanning_jobs, container_scanning: {
dast: :dast_jobs, name: :container_scanning_jobs
dependency_scanning: :dependency_scanning_jobs, },
license_management: :license_management_jobs, dast: {
license_scanning: :license_scanning_jobs, name: :dast_jobs
sast: :sast_jobs },
dependency_scanning: {
name: :dependency_scanning_jobs
},
license_management: {
name: :license_management_jobs
},
license_scanning: {
name: :license_scanning_jobs,
fallback: 0
},
sast: {
name: :sast_jobs
}
}.freeze }.freeze
class_methods do class_methods do
...@@ -108,7 +121,7 @@ module EE ...@@ -108,7 +121,7 @@ module EE
results["license_management"] += license_scan_count results["license_management"] += license_scan_count
end end
results.each_with_object({}) { |(key, value), response| response[SECURE_PRODUCT_TYPES[key.to_sym]] = value } results.each_with_object({}) { |(key, value), response| response[SECURE_PRODUCT_TYPES[key.to_sym][:name]] = value }
end end
# rubocop: enable CodeReuse/ActiveRecord # rubocop: enable CodeReuse/ActiveRecord
...@@ -320,8 +333,8 @@ module EE ...@@ -320,8 +333,8 @@ module EE
user_preferences_group_overview_security_dashboard: count(::User.active.group_view_security_dashboard.where(time_period)) user_preferences_group_overview_security_dashboard: count(::User.active.group_view_security_dashboard.where(time_period))
} }
SECURE_PRODUCT_TYPES.each do |secure_type, type_with_name| SECURE_PRODUCT_TYPES.each do |secure_type, attribs|
results["#{prefix}#{type_with_name}".to_sym] = distinct_count(::Ci::Build.where(name: secure_type).where(time_period), :user_id) results["#{prefix}#{attribs[:name]}".to_sym] = distinct_count(::Ci::Build.where(name: secure_type).where(time_period), :user_id, fallback: attribs.fetch(:fallback, -1))
end end
# handle license rename https://gitlab.com/gitlab-org/gitlab/issues/8911 # handle license rename https://gitlab.com/gitlab-org/gitlab/issues/8911
......
...@@ -344,16 +344,48 @@ describe Gitlab::UsageData do ...@@ -344,16 +344,48 @@ describe Gitlab::UsageData do
end end
context 'for secure' do context 'for secure' do
let_it_be(:user) { create(:user, group_view: :security_dashboard) }
let_it_be(:ci_build_container_scanning) { create(:ci_build, name: 'container_scanning', user: user) }
let_it_be(:ci_build_dast) { create(:ci_build, name: 'dast', user: user) }
let_it_be(:ci_build_dependency_scanning) { create(:ci_build, name: 'dependency_scanning', user: user) }
let_it_be(:ci_build_license_management) { create(:ci_build, name: 'license_management', user: user) }
let_it_be(:ci_build_sast) { create(:ci_build, name: 'sast', user: user) }
it 'includes accurate usage_activity_by_stage data' do it 'includes accurate usage_activity_by_stage data' do
create(:user, group_view: :security_dashboard) expect(described_class.uncached_data[:usage_activity_by_stage_monthly][:secure]).to eq(
user_preferences_group_overview_security_dashboard: 1,
user_container_scanning_jobs: 1,
user_dast_jobs: 1,
user_dependency_scanning_jobs: 1,
user_license_management_jobs: 1,
user_sast_jobs: 1
)
end
it 'combines license_scanning into license_management' do
create(:ci_build, name: 'license_scanning', user: user)
expect(described_class.uncached_data[:usage_activity_by_stage_monthly][:secure]).to eq(
user_preferences_group_overview_security_dashboard: 1,
user_container_scanning_jobs: 1,
user_dast_jobs: 1,
user_dependency_scanning_jobs: 1,
user_license_management_jobs: 2,
user_sast_jobs: 1
)
end
it 'has to resort to 0 for counting license scan' do
allow(Gitlab::Database::BatchCount).to receive(:batch_distinct_count).and_raise(ActiveRecord::StatementInvalid)
allow(::Ci::Build).to receive(:distinct_count_by).and_raise(ActiveRecord::StatementInvalid)
expect(described_class.uncached_data[:usage_activity_by_stage_monthly][:secure]).to eq( expect(described_class.uncached_data[:usage_activity_by_stage_monthly][:secure]).to eq(
user_preferences_group_overview_security_dashboard: 1, user_preferences_group_overview_security_dashboard: 1,
user_container_scanning_jobs: 0, user_container_scanning_jobs: -1,
user_dast_jobs: 0, user_dast_jobs: -1,
user_dependency_scanning_jobs: 0, user_dependency_scanning_jobs: -1,
user_license_management_jobs: 0, user_license_management_jobs: -1,
user_sast_jobs: 0 user_sast_jobs: -1
) )
end end
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment