diff options
Diffstat (limited to 'spec/lib/gitlab/usage')
11 files changed, 400 insertions, 46 deletions
diff --git a/spec/lib/gitlab/usage/metric_definition_spec.rb b/spec/lib/gitlab/usage/metric_definition_spec.rb index 1127d1cd477..070586319a5 100644 --- a/spec/lib/gitlab/usage/metric_definition_spec.rb +++ b/spec/lib/gitlab/usage/metric_definition_spec.rb @@ -20,7 +20,8 @@ RSpec.describe Gitlab::Usage::MetricDefinition do distribution: %w(ee ce), tier: %w(free starter premium ultimate bronze silver gold), name: 'uuid', - data_category: 'standard' + data_category: 'standard', + removed_by_url: 'http://gdk.test' } end @@ -132,6 +133,7 @@ RSpec.describe Gitlab::Usage::MetricDefinition do :tier | %w(test ee) :name | 'count_<adjective_describing>_boards' :repair_issue_url | nil + :removed_by_url | 1 :instrumentation_class | 'Metric_Class' :instrumentation_class | 'metricClass' @@ -177,6 +179,24 @@ RSpec.describe Gitlab::Usage::MetricDefinition do end end + describe '#valid_service_ping_status?' do + context 'when metric has active status' do + it 'has to return true' do + attributes[:status] = 'active' + + expect(described_class.new(path, attributes).valid_service_ping_status?).to be_truthy + end + end + + context 'when metric has removed status' do + it 'has to return false' do + attributes[:status] = 'removed' + + expect(described_class.new(path, attributes).valid_service_ping_status?).to be_falsey + end + end + end + describe 'statuses' do using RSpec::Parameterized::TableSyntax diff --git a/spec/lib/gitlab/usage/metric_spec.rb b/spec/lib/gitlab/usage/metric_spec.rb index 19d2d3048eb..10ae94e746b 100644 --- a/spec/lib/gitlab/usage/metric_spec.rb +++ b/spec/lib/gitlab/usage/metric_spec.rb @@ -51,4 +51,31 @@ RSpec.describe Gitlab::Usage::Metric do expect(described_class.new(issue_count_metric_definiton).with_suggested_name).to eq({ counts: { issues: 'count_issues' } }) end end + + context 'unavailable metric' do + let(:instrumentation_class) { "UnavailableMetric" } + let(:issue_count_metric_definiton) do + double(:issue_count_metric_definiton, + attributes.merge({ attributes: attributes, instrumentation_class: instrumentation_class }) + ) + end + + before do + unavailable_metric_class = Class.new(Gitlab::Usage::Metrics::Instrumentations::CountIssuesMetric) do + def available? + false + end + end + + stub_const("Gitlab::Usage::Metrics::Instrumentations::#{instrumentation_class}", unavailable_metric_class) + end + + [:with_value, :with_instrumentation, :with_suggested_name].each do |method_name| + describe "##{method_name}" do + it 'returns an empty hash' do + expect(described_class.new(issue_count_metric_definiton).public_send(method_name)).to eq({}) + end + end + end + end end diff --git a/spec/lib/gitlab/usage/metrics/instrumentations/collected_data_categories_metric_spec.rb b/spec/lib/gitlab/usage/metrics/instrumentations/collected_data_categories_metric_spec.rb index 1b2170baf17..92d4de3c462 100644 --- a/spec/lib/gitlab/usage/metrics/instrumentations/collected_data_categories_metric_spec.rb +++ b/spec/lib/gitlab/usage/metrics/instrumentations/collected_data_categories_metric_spec.rb @@ -7,7 +7,7 @@ RSpec.describe Gitlab::Usage::Metrics::Instrumentations::CollectedDataCategories let(:expected_value) { %w[standard subscription operational optional] } before do - allow_next_instance_of(ServicePing::PermitDataCategoriesService) do |instance| + allow_next_instance_of(ServicePing::PermitDataCategories) do |instance| expect(instance).to receive(:execute).and_return(expected_value) end end diff --git a/spec/lib/gitlab/usage/metrics/instrumentations/count_bulk_imports_entities_metric_spec.rb b/spec/lib/gitlab/usage/metrics/instrumentations/count_bulk_imports_entities_metric_spec.rb new file mode 100644 index 00000000000..b85d5a3ebf9 --- /dev/null +++ b/spec/lib/gitlab/usage/metrics/instrumentations/count_bulk_imports_entities_metric_spec.rb @@ -0,0 +1,106 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Gitlab::Usage::Metrics::Instrumentations::CountBulkImportsEntitiesMetric do + let_it_be(:user) { create(:user) } + let_it_be(:bulk_import_projects) do + create_list(:bulk_import_entity, 3, source_type: 'project_entity', created_at: 3.weeks.ago) + end + + let_it_be(:bulk_import_groups) do + create_list(:bulk_import_entity, 3, source_type: 'group_entity', created_at: 3.weeks.ago) + end + + let_it_be(:old_bulk_import_project) do + create(:bulk_import_entity, source_type: 'project_entity', created_at: 2.months.ago) + end + + context 'with no source_type' do + context 'with all time frame' do + let(:expected_value) { 7 } + let(:expected_query) do + "SELECT COUNT(\"bulk_import_entities\".\"id\") FROM \"bulk_import_entities\"" + end + + it_behaves_like 'a correct instrumented metric value and query', time_frame: 'all', options: {} + end + + context 'for 28d time frame' do + let(:expected_value) { 6 } + let(:start) { 30.days.ago.to_s(:db) } + let(:finish) { 2.days.ago.to_s(:db) } + let(:expected_query) do + "SELECT COUNT(\"bulk_import_entities\".\"id\") FROM \"bulk_import_entities\""\ + " WHERE \"bulk_import_entities\".\"created_at\" BETWEEN '#{start}' AND '#{finish}'" + end + + it_behaves_like 'a correct instrumented metric value and query', time_frame: '28d', options: {} + end + end + + context 'with invalid source_type' do + it 'raises ArgumentError' do + expect { described_class.new(time_frame: 'all', options: { source_type: 'random' }) } + .to raise_error(ArgumentError, /source_type/) + end + end + + context 'with source_type project_entity' do + context 'with all time frame' do + let(:expected_value) { 4 } + let(:expected_query) do + "SELECT COUNT(\"bulk_import_entities\".\"id\") FROM \"bulk_import_entities\""\ + " WHERE \"bulk_import_entities\".\"source_type\" = 1" + end + + it_behaves_like 'a correct instrumented metric value and query', + time_frame: 'all', + options: { source_type: 'project_entity' } + end + + context 'for 28d time frame' do + let(:expected_value) { 3 } + let(:start) { 30.days.ago.to_s(:db) } + let(:finish) { 2.days.ago.to_s(:db) } + let(:expected_query) do + "SELECT COUNT(\"bulk_import_entities\".\"id\") FROM \"bulk_import_entities\""\ + " WHERE \"bulk_import_entities\".\"created_at\" BETWEEN '#{start}' AND '#{finish}'"\ + " AND \"bulk_import_entities\".\"source_type\" = 1" + end + + it_behaves_like 'a correct instrumented metric value and query', + time_frame: '28d', + options: { source_type: 'project_entity' } + end + end + + context 'with source_type group_entity' do + context 'with all time frame' do + let(:expected_value) { 3 } + let(:expected_query) do + "SELECT COUNT(\"bulk_import_entities\".\"id\") FROM \"bulk_import_entities\""\ + " WHERE \"bulk_import_entities\".\"source_type\" = 0" + end + + it_behaves_like 'a correct instrumented metric value and query', + time_frame: 'all', + options: { source_type: 'group_entity' } + end + + context 'for 28d time frame' do + let(:expected_value) { 3 } + let(:start) { 30.days.ago.to_s(:db) } + let(:finish) { 2.days.ago.to_s(:db) } + let(:expected_query) do + "SELECT COUNT(\"bulk_import_entities\".\"id\") FROM \"bulk_import_entities\""\ + " WHERE \"bulk_import_entities\".\"created_at\" BETWEEN '#{start}' AND '#{finish}'"\ + " AND \"bulk_import_entities\".\"source_type\" = 0" + end + + it_behaves_like 'a correct instrumented metric value and query', + time_frame: '28d', + options: { source_type: 'group_entity' } + end + end +end diff --git a/spec/lib/gitlab/usage/metrics/instrumentations/count_imported_projects_metric_spec.rb b/spec/lib/gitlab/usage/metrics/instrumentations/count_imported_projects_metric_spec.rb new file mode 100644 index 00000000000..4c86410d609 --- /dev/null +++ b/spec/lib/gitlab/usage/metrics/instrumentations/count_imported_projects_metric_spec.rb @@ -0,0 +1,70 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Gitlab::Usage::Metrics::Instrumentations::CountImportedProjectsMetric do + let_it_be(:user) { create(:user) } + let_it_be(:gitea_imports) do + create_list(:project, 3, import_type: 'gitea', creator_id: user.id, created_at: 3.weeks.ago) + end + + let_it_be(:bitbucket_imports) do + create_list(:project, 2, import_type: 'bitbucket', creator_id: user.id, created_at: 3.weeks.ago) + end + + let_it_be(:old_import) { create(:project, import_type: 'gitea', creator_id: user.id, created_at: 2.months.ago) } + + context 'with import_type gitea' do + context 'with all time frame' do + let(:expected_value) { 4 } + let(:expected_query) do + "SELECT COUNT(\"projects\".\"id\") FROM \"projects\" WHERE \"projects\".\"import_type\" = 'gitea'" + end + + it_behaves_like 'a correct instrumented metric value and query', + time_frame: 'all', + options: { import_type: 'gitea' } + end + + context 'for 28d time frame' do + let(:expected_value) { 3 } + let(:start) { 30.days.ago.to_s(:db) } + let(:finish) { 2.days.ago.to_s(:db) } + let(:expected_query) do + "SELECT COUNT(\"projects\".\"id\") FROM \"projects\" WHERE \"projects\".\"created_at\""\ + " BETWEEN '#{start}' AND '#{finish}' AND \"projects\".\"import_type\" = 'gitea'" + end + + it_behaves_like 'a correct instrumented metric value and query', + time_frame: '28d', + options: { import_type: 'gitea' } + end + end + + context 'with import_type bitbucket' do + context 'with all time frame' do + let(:expected_value) { 2 } + let(:expected_query) do + "SELECT COUNT(\"projects\".\"id\") FROM \"projects\" WHERE \"projects\".\"import_type\" = 'bitbucket'" + end + + it_behaves_like 'a correct instrumented metric value and query', + time_frame: 'all', + options: { import_type: 'bitbucket' } + end + + context 'for 28d time frame' do + let(:expected_value) { 2 } + let(:start) { 30.days.ago.to_s(:db) } + let(:finish) { 2.days.ago.to_s(:db) } + let(:expected_query) do + "SELECT COUNT(\"projects\".\"id\") FROM \"projects\" WHERE \"projects\".\"created_at\""\ + " BETWEEN '#{start}' AND '#{finish}' AND \"projects\".\"import_type\" = 'bitbucket'" + end + + it_behaves_like 'a correct instrumented metric value and query', + time_frame: '28d', + options: { import_type: 'bitbucket' } + end + end +end diff --git a/spec/lib/gitlab/usage/metrics/instrumentations/database_metric_spec.rb b/spec/lib/gitlab/usage/metrics/instrumentations/database_metric_spec.rb index ea5ae1970de..8e7bd7b84e6 100644 --- a/spec/lib/gitlab/usage/metrics/instrumentations/database_metric_spec.rb +++ b/spec/lib/gitlab/usage/metrics/instrumentations/database_metric_spec.rb @@ -71,6 +71,33 @@ RSpec.describe Gitlab::Usage::Metrics::Instrumentations::DatabaseMetric do end end + context 'with availability defined' do + subject do + described_class.tap do |metric_class| + metric_class.relation { Issue } + metric_class.operation :count + metric_class.available? { false } + end.new(time_frame: 'all') + end + + it 'responds to #available? properly' do + expect(subject.available?).to eq(false) + end + end + + context 'with availability not defined' do + subject do + Class.new(described_class) do + relation { Issue } + operation :count + end.new(time_frame: 'all') + end + + it 'responds to #available? properly' do + expect(subject.available?).to eq(true) + end + end + context 'with cache_start_and_finish_as called' do subject do described_class.tap do |metric_class| @@ -134,4 +161,17 @@ RSpec.describe Gitlab::Usage::Metrics::Instrumentations::DatabaseMetric do end end end + + context 'with unimplemented operation method used' do + subject do + described_class.tap do |metric_class| + metric_class.relation { Issue } + metric_class.operation :invalid_operation + end.new(time_frame: 'all') + end + + it 'raises an error' do + expect { subject }.to raise_error(described_class::UnimplementedOperationError) + end + end end diff --git a/spec/lib/gitlab/usage/metrics/instrumentations/redis_hll_metric_spec.rb b/spec/lib/gitlab/usage/metrics/instrumentations/redis_hll_metric_spec.rb index 347a2c779cb..97306051533 100644 --- a/spec/lib/gitlab/usage/metrics/instrumentations/redis_hll_metric_spec.rb +++ b/spec/lib/gitlab/usage/metrics/instrumentations/redis_hll_metric_spec.rb @@ -25,4 +25,28 @@ RSpec.describe Gitlab::Usage::Metrics::Instrumentations::RedisHLLMetric, :clean_ it 'raise exception if events options is not present' do expect { described_class.new(time_frame: '28d') }.to raise_error(ArgumentError) end + + describe 'children classes' do + let(:options) { { events: ['i_quickactions_approve'] } } + + context 'availability not defined' do + subject { Class.new(described_class).new(time_frame: nil, options: options) } + + it 'returns default availability' do + expect(subject.available?).to eq(true) + end + end + + context 'availability defined' do + subject do + Class.new(described_class) do + available? { false } + end.new(time_frame: nil, options: options) + end + + it 'returns defined availability' do + expect(subject.available?).to eq(false) + end + end + end end diff --git a/spec/lib/gitlab/usage/metrics/instrumentations/redis_metric_spec.rb b/spec/lib/gitlab/usage/metrics/instrumentations/redis_metric_spec.rb index fb3bd1ba834..831f775ec9a 100644 --- a/spec/lib/gitlab/usage/metrics/instrumentations/redis_metric_spec.rb +++ b/spec/lib/gitlab/usage/metrics/instrumentations/redis_metric_spec.rb @@ -20,4 +20,28 @@ RSpec.describe Gitlab::Usage::Metrics::Instrumentations::RedisMetric, :clean_git it 'raises an exception if counter_class option is not present' do expect { described_class.new(event: 'pushes') }.to raise_error(ArgumentError) end + + describe 'children classes' do + let(:options) { { event: 'pushes', counter_class: 'SourceCodeCounter' } } + + context 'availability not defined' do + subject { Class.new(described_class).new(time_frame: nil, options: options) } + + it 'returns default availability' do + expect(subject.available?).to eq(true) + end + end + + context 'availability defined' do + subject do + Class.new(described_class) do + available? { false } + end.new(time_frame: nil, options: options) + end + + it 'returns defined availability' do + expect(subject.available?).to eq(false) + end + end + end end diff --git a/spec/lib/gitlab/usage/metrics/query_spec.rb b/spec/lib/gitlab/usage/metrics/query_spec.rb index 60c8d044a64..65b8a7a046b 100644 --- a/spec/lib/gitlab/usage/metrics/query_spec.rb +++ b/spec/lib/gitlab/usage/metrics/query_spec.rb @@ -11,6 +11,22 @@ RSpec.describe Gitlab::Usage::Metrics::Query do it 'does not mix a nil column with keyword arguments' do expect(described_class.for(:count, User, nil)).to eq('SELECT COUNT("users"."id") FROM "users"') end + + it 'removes order from passed relation' do + expect(described_class.for(:count, User.order(:email), nil)).to eq('SELECT COUNT("users"."id") FROM "users"') + end + + it 'returns valid raw SQL for join relations' do + expect(described_class.for(:count, User.joins(:issues), :email)).to eq( + 'SELECT COUNT("users"."email") FROM "users" INNER JOIN "issues" ON "issues"."author_id" = "users"."id"' + ) + end + + it 'returns valid raw SQL for join relations with joined columns' do + expect(described_class.for(:count, User.joins(:issues), 'issue.weight')).to eq( + 'SELECT COUNT("issue"."weight") FROM "users" INNER JOIN "issues" ON "issues"."author_id" = "users"."id"' + ) + end end describe '.distinct_count' do @@ -21,6 +37,22 @@ RSpec.describe Gitlab::Usage::Metrics::Query do it 'does not mix a nil column with keyword arguments' do expect(described_class.for(:distinct_count, Issue, nil)).to eq('SELECT COUNT(DISTINCT "issues"."id") FROM "issues"') end + + it 'removes order from passed relation' do + expect(described_class.for(:distinct_count, User.order(:email), nil)).to eq('SELECT COUNT(DISTINCT "users"."id") FROM "users"') + end + + it 'returns valid raw SQL for join relations' do + expect(described_class.for(:distinct_count, User.joins(:issues), :email)).to eq( + 'SELECT COUNT(DISTINCT "users"."email") FROM "users" INNER JOIN "issues" ON "issues"."author_id" = "users"."id"' + ) + end + + it 'returns valid raw SQL for join relations with joined columns' do + expect(described_class.for(:distinct_count, User.joins(:issues), 'issue.weight')).to eq( + 'SELECT COUNT(DISTINCT "issue"."weight") FROM "users" INNER JOIN "issues" ON "issues"."author_id" = "users"."id"' + ) + end end describe '.sum' do diff --git a/spec/lib/gitlab/usage/service_ping/legacy_metric_timing_decorator_spec.rb b/spec/lib/gitlab/usage/service_ping/legacy_metric_timing_decorator_spec.rb new file mode 100644 index 00000000000..46592379b3d --- /dev/null +++ b/spec/lib/gitlab/usage/service_ping/legacy_metric_timing_decorator_spec.rb @@ -0,0 +1,35 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Gitlab::Usage::ServicePing::LegacyMetricTimingDecorator do + using RSpec::Parameterized::TableSyntax + + let(:duration) { 123 } + + where(:metric_value, :metric_class) do + 1 | Integer + "value" | String + true | TrueClass + false | FalseClass + nil | NilClass + end + + with_them do + let(:decorated_object) { described_class.new(metric_value, duration) } + + it 'exposes a duration with the correct value' do + expect(decorated_object.duration).to eq(duration) + end + + it 'imitates wrapped class', :aggregate_failures do + expect(decorated_object).to eq metric_value + expect(decorated_object.class).to eq metric_class + expect(decorated_object.is_a?(metric_class)).to be_truthy + # rubocop:disable Style/ClassCheck + expect(decorated_object.kind_of?(metric_class)).to be_truthy + # rubocop:enable Style/ClassCheck + expect({ metric: decorated_object }.to_json).to eql({ metric: metric_value }.to_json) + end + end +end diff --git a/spec/lib/gitlab/usage/service_ping_report_spec.rb b/spec/lib/gitlab/usage/service_ping_report_spec.rb index b6119ab52ec..e7096988035 100644 --- a/spec/lib/gitlab/usage/service_ping_report_spec.rb +++ b/spec/lib/gitlab/usage/service_ping_report_spec.rb @@ -92,49 +92,6 @@ RSpec.describe Gitlab::Usage::ServicePingReport, :use_clean_rails_memory_store_c end context 'cross test values against queries' do - # TODO: fix failing metrics https://gitlab.com/gitlab-org/gitlab/-/issues/353559 - let(:failing_todo_metrics) do - ["counts.labels", - "counts.jira_imports_total_imported_issues_count", - "counts.in_product_marketing_email_create_0_sent", - "counts.in_product_marketing_email_create_0_cta_clicked", - "counts.in_product_marketing_email_create_1_sent", - "counts.in_product_marketing_email_create_1_cta_clicked", - "counts.in_product_marketing_email_create_2_sent", - "counts.in_product_marketing_email_create_2_cta_clicked", - "counts.in_product_marketing_email_verify_0_sent", - "counts.in_product_marketing_email_verify_0_cta_clicked", - "counts.in_product_marketing_email_verify_1_sent", - "counts.in_product_marketing_email_verify_1_cta_clicked", - "counts.in_product_marketing_email_verify_2_sent", - "counts.in_product_marketing_email_verify_2_cta_clicked", - "counts.in_product_marketing_email_trial_0_sent", - "counts.in_product_marketing_email_trial_0_cta_clicked", - "counts.in_product_marketing_email_trial_1_sent", - "counts.in_product_marketing_email_trial_1_cta_clicked", - "counts.in_product_marketing_email_trial_2_sent", - "counts.in_product_marketing_email_trial_2_cta_clicked", - "counts.in_product_marketing_email_team_0_sent", - "counts.in_product_marketing_email_team_0_cta_clicked", - "counts.in_product_marketing_email_team_1_sent", - "counts.in_product_marketing_email_team_1_cta_clicked", - "counts.in_product_marketing_email_team_2_sent", - "counts.in_product_marketing_email_team_2_cta_clicked", - "counts.in_product_marketing_email_experience_0_sent", - "counts.in_product_marketing_email_team_short_0_sent", - "counts.in_product_marketing_email_team_short_0_cta_clicked", - "counts.in_product_marketing_email_trial_short_0_sent", - "counts.in_product_marketing_email_trial_short_0_cta_clicked", - "counts.in_product_marketing_email_admin_verify_0_sent", - "counts.in_product_marketing_email_admin_verify_0_cta_clicked", - "counts.ldap_users", - "usage_activity_by_stage.create.projects_with_sectional_code_owner_rules", - "usage_activity_by_stage.monitor.clusters_integrations_prometheus", - "usage_activity_by_stage.monitor.projects_with_enabled_alert_integrations_histogram", - "usage_activity_by_stage_monthly.create.projects_with_sectional_code_owner_rules", - "usage_activity_by_stage_monthly.monitor.clusters_integrations_prometheus"] - end - def fetch_value_by_query(query) # Because test cases are run inside a transaction, if any query raise and error all queries that follows # it are automatically canceled by PostgreSQL, to avoid that problem, and to provide exhaustive information @@ -157,6 +114,24 @@ RSpec.describe Gitlab::Usage::ServicePingReport, :use_clean_rails_memory_store_c accumulator end + def type_cast_to_defined_type(value, metric_definition) + case metric_definition&.attributes&.fetch(:value_type) + when "string" + value.to_s + when "number" + value.to_i + when "object" + case metric_definition&.json_schema&.fetch("type") + when "array" + value.to_a + else + value.to_h + end + else + value + end + end + before do stub_usage_data_connections stub_object_store_settings @@ -169,12 +144,13 @@ RSpec.describe Gitlab::Usage::ServicePingReport, :use_clean_rails_memory_store_c let(:service_ping_payload) { described_class.for(output: :all_metrics_values) } let(:metrics_queries_with_values) { build_payload_from_queries(described_class.for(output: :metrics_queries)) } + let(:metric_definitions) { ::Gitlab::Usage::MetricDefinition.definitions } it 'generates queries that match collected data', :aggregate_failures do message = "Expected %{query} result to match %{value} for %{key_path} metric" metrics_queries_with_values.each do |key_path, query, value| - next if failing_todo_metrics.include?(key_path.join('.')) + value = type_cast_to_defined_type(value, metric_definitions[key_path.join('.')]) expect(value).to( eq(service_ping_payload.dig(*key_path)), |