Commit 34d9b5e4 authored by Ash McKenzie's avatar Ash McKenzie

DRY up time travel logic

parent 9b657ad5
...@@ -65,8 +65,7 @@ describe Gitlab::UsageData do ...@@ -65,8 +65,7 @@ describe Gitlab::UsageData do
context 'for configure' do context 'for configure' do
it 'includes accurate usage_activity_by_stage data' do it 'includes accurate usage_activity_by_stage data' do
[29, 2].each do |n| for_defined_days_back do
Timecop.travel n.days.ago do
user = create(:user) user = create(:user)
cluster = create(:cluster, user: user) cluster = create(:cluster, user: user)
project = create(:project, creator: user) project = create(:project, creator: user)
...@@ -84,7 +83,6 @@ describe Gitlab::UsageData do ...@@ -84,7 +83,6 @@ describe Gitlab::UsageData do
create(:slack_slash_commands_service, project: project) create(:slack_slash_commands_service, project: project)
create(:prometheus_service, project: project) create(:prometheus_service, project: project)
end end
end
expect(described_class.uncached_data[:usage_activity_by_stage][:configure]).to eq( expect(described_class.uncached_data[:usage_activity_by_stage][:configure]).to eq(
clusters_applications_cert_managers: 2, clusters_applications_cert_managers: 2,
...@@ -127,8 +125,7 @@ describe Gitlab::UsageData do ...@@ -127,8 +125,7 @@ describe Gitlab::UsageData do
context 'for create' do context 'for create' do
it 'includes accurate usage_activity_by_stage data' do it 'includes accurate usage_activity_by_stage data' do
[29, 2].each do |n| for_defined_days_back do
Timecop.travel n.days.ago do
user = create(:user) user = create(:user)
project = create(:project, :repository_private, :github_imported, project = create(:project, :repository_private, :github_imported,
:test_repo, :remote_mirror, creator: user) :test_repo, :remote_mirror, creator: user)
...@@ -145,7 +142,6 @@ describe Gitlab::UsageData do ...@@ -145,7 +142,6 @@ describe Gitlab::UsageData do
create_list(:code_owner_rule, 3, approvals_required: 2) create_list(:code_owner_rule, 3, approvals_required: 2)
create_list(:code_owner_rule, 2) create_list(:code_owner_rule, 2)
end end
end
expect(described_class.uncached_data[:usage_activity_by_stage][:create]).to eq( expect(described_class.uncached_data[:usage_activity_by_stage][:create]).to eq(
deploy_keys: 2, deploy_keys: 2,
...@@ -180,15 +176,13 @@ describe Gitlab::UsageData do ...@@ -180,15 +176,13 @@ describe Gitlab::UsageData do
context 'for manage' do context 'for manage' do
it 'includes accurate usage_activity_by_stage data' do it 'includes accurate usage_activity_by_stage data' do
[29, 2].each do |n| for_defined_days_back do
Timecop.travel n.days.ago do
user = create(:user) user = create(:user)
create(:event, author: user) create(:event, author: user)
create(:group_member, user: user) create(:group_member, user: user)
create(:key, type: 'LDAPKey', user: user) create(:key, type: 'LDAPKey', user: user)
create(:group_member, ldap: true, user: user) create(:group_member, ldap: true, user: user)
end end
end
expect(described_class.uncached_data[:usage_activity_by_stage][:manage]).to eq( expect(described_class.uncached_data[:usage_activity_by_stage][:manage]).to eq(
events: 2, events: 2,
...@@ -207,8 +201,7 @@ describe Gitlab::UsageData do ...@@ -207,8 +201,7 @@ describe Gitlab::UsageData do
context 'for monitor' do context 'for monitor' do
it 'includes accurate usage_activity_by_stage data' do it 'includes accurate usage_activity_by_stage data' do
[29, 2].each do |n| for_defined_days_back do
Timecop.travel n.days.ago do
user = create(:user, dashboard: 'operations') user = create(:user, dashboard: 'operations')
cluster = create(:cluster, user: user) cluster = create(:cluster, user: user)
project = create(:project, creator: user) project = create(:project, creator: user)
...@@ -219,7 +212,6 @@ describe Gitlab::UsageData do ...@@ -219,7 +212,6 @@ describe Gitlab::UsageData do
create(:project_error_tracking_setting, project: project) create(:project_error_tracking_setting, project: project)
create(:project_tracing_setting, project: project) create(:project_tracing_setting, project: project)
end end
end
expect(described_class.uncached_data[:usage_activity_by_stage][:monitor]).to eq( expect(described_class.uncached_data[:usage_activity_by_stage][:monitor]).to eq(
clusters: 2, clusters: 2,
...@@ -244,11 +236,9 @@ describe Gitlab::UsageData do ...@@ -244,11 +236,9 @@ describe Gitlab::UsageData do
context 'for package' do context 'for package' do
it 'includes accurate usage_activity_by_stage data' do it 'includes accurate usage_activity_by_stage data' do
[29, 2].each do |n| for_defined_days_back do
Timecop.travel n.days.ago do
create(:project, packages: [create(:package)] ) create(:project, packages: [create(:package)] )
end end
end
expect(described_class.uncached_data[:usage_activity_by_stage][:package]).to eq( expect(described_class.uncached_data[:usage_activity_by_stage][:package]).to eq(
projects_with_packages: 2 projects_with_packages: 2
...@@ -263,8 +253,7 @@ describe Gitlab::UsageData do ...@@ -263,8 +253,7 @@ describe Gitlab::UsageData do
it 'includes accurate usage_activity_by_stage data' do it 'includes accurate usage_activity_by_stage data' do
stub_licensed_features(board_assignee_lists: true, board_milestone_lists: true) stub_licensed_features(board_assignee_lists: true, board_milestone_lists: true)
[29, 2].each do |n| for_defined_days_back do
Timecop.travel n.days.ago do
user = create(:user) user = create(:user)
project = create(:project, creator: user) project = create(:project, creator: user)
issue = create(:issue, project: project, author: User.support_bot) issue = create(:issue, project: project, author: User.support_bot)
...@@ -279,7 +268,6 @@ describe Gitlab::UsageData do ...@@ -279,7 +268,6 @@ describe Gitlab::UsageData do
create(:jira_service, :jira_cloud_service, active: true, project: create(:project, :jira_dvcs_cloud, creator: user)) create(:jira_service, :jira_cloud_service, active: true, project: create(:project, :jira_dvcs_cloud, creator: user))
create(:jira_service, active: true, project: create(:project, :jira_dvcs_server, creator: user)) create(:jira_service, active: true, project: create(:project, :jira_dvcs_server, creator: user))
end end
end
expect(described_class.uncached_data[:usage_activity_by_stage][:plan]).to eq( expect(described_class.uncached_data[:usage_activity_by_stage][:plan]).to eq(
assignee_lists: 2, assignee_lists: 2,
...@@ -316,15 +304,13 @@ describe Gitlab::UsageData do ...@@ -316,15 +304,13 @@ describe Gitlab::UsageData do
context 'for release' do context 'for release' do
it 'includes accurate usage_activity_by_stage data' do it 'includes accurate usage_activity_by_stage data' do
[29, 2].each do |n| for_defined_days_back do
Timecop.travel n.days.ago do
user = create(:user) user = create(:user)
create(:deployment, :failed, user: user) create(:deployment, :failed, user: user)
create(:project, :mirror, mirror_trigger_builds: true) create(:project, :mirror, mirror_trigger_builds: true)
create(:release, author: user) create(:release, author: user)
create(:deployment, :success, user: user) create(:deployment, :success, user: user)
end end
end
expect(described_class.uncached_data[:usage_activity_by_stage][:release]).to eq( expect(described_class.uncached_data[:usage_activity_by_stage][:release]).to eq(
deployments: 2, deployments: 2,
...@@ -347,8 +333,7 @@ describe Gitlab::UsageData do ...@@ -347,8 +333,7 @@ describe Gitlab::UsageData do
let_it_be(:user) { create(:user, group_view: :security_dashboard) } let_it_be(:user) { create(:user, group_view: :security_dashboard) }
before do before do
[29, 2].each do |n| for_defined_days_back do
Timecop.travel n.days.ago do
create(:ci_build, name: 'container_scanning', user: user) create(:ci_build, name: 'container_scanning', user: user)
create(:ci_build, name: 'dast', user: user) create(:ci_build, name: 'dast', user: user)
create(:ci_build, name: 'dependency_scanning', user: user) create(:ci_build, name: 'dependency_scanning', user: user)
...@@ -356,7 +341,6 @@ describe Gitlab::UsageData do ...@@ -356,7 +341,6 @@ describe Gitlab::UsageData do
create(:ci_build, name: 'sast', user: user) create(:ci_build, name: 'sast', user: user)
end end
end end
end
it 'includes accurate usage_activity_by_stage data' do it 'includes accurate usage_activity_by_stage data' do
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(
...@@ -370,11 +354,9 @@ describe Gitlab::UsageData do ...@@ -370,11 +354,9 @@ describe Gitlab::UsageData do
end end
it 'combines license_scanning into license_management' do it 'combines license_scanning into license_management' do
[29, 2].each do |n| for_defined_days_back do
Timecop.travel n.days.ago do
create(:ci_build, name: 'license_scanning', user: user) create(:ci_build, name: 'license_scanning', user: user)
end end
end
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,
...@@ -403,8 +385,7 @@ describe Gitlab::UsageData do ...@@ -403,8 +385,7 @@ describe Gitlab::UsageData do
context 'for verify' do context 'for verify' do
it 'includes accurate usage_activity_by_stage data' do it 'includes accurate usage_activity_by_stage data' do
[29, 2].each do |n| for_defined_days_back do
Timecop.travel n.days.ago do
user = create(:user) user = create(:user)
create(:ci_build, user: user) create(:ci_build, user: user)
create(:ci_empty_pipeline, source: :external, user: user) create(:ci_empty_pipeline, source: :external, user: user)
...@@ -416,7 +397,6 @@ describe Gitlab::UsageData do ...@@ -416,7 +397,6 @@ describe Gitlab::UsageData do
create(:clusters_applications_runner, :installed) create(:clusters_applications_runner, :installed)
create(:github_service) create(:github_service)
end end
end
expect(described_class.uncached_data[:usage_activity_by_stage][:verify]).to eq( expect(described_class.uncached_data[:usage_activity_by_stage][:verify]).to eq(
ci_builds: 2, ci_builds: 2,
...@@ -448,4 +428,12 @@ describe Gitlab::UsageData do ...@@ -448,4 +428,12 @@ describe Gitlab::UsageData do
end end
end end
end end
def for_defined_days_back(days: [29, 2])
days.each do |n|
Timecop.travel(n.days.ago) do
yield
end
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