diff options
Diffstat (limited to 'spec/lib/generators/gitlab/analytics/internal_events_generator_spec.rb')
-rw-r--r-- | spec/lib/generators/gitlab/analytics/internal_events_generator_spec.rb | 213 |
1 files changed, 104 insertions, 109 deletions
diff --git a/spec/lib/generators/gitlab/analytics/internal_events_generator_spec.rb b/spec/lib/generators/gitlab/analytics/internal_events_generator_spec.rb index c52d17d4a5b..2d9356ca96d 100644 --- a/spec/lib/generators/gitlab/analytics/internal_events_generator_spec.rb +++ b/spec/lib/generators/gitlab/analytics/internal_events_generator_spec.rb @@ -10,81 +10,27 @@ RSpec.describe Gitlab::Analytics::InternalEventsGenerator, :silence_stdout, feat let(:tmpfile) { Tempfile.new('test-metadata') } let(:existing_key_paths) { {} } let(:description) { "This metric counts unique users viewing analytics metrics dashboard section" } - let(:group) { "group::analytics instrumentation" } - let(:stage) { "analytics" } + let(:group) { "analytics_instrumentation" } + let(:stage) { "analyze" } let(:section) { "analytics" } let(:mr) { "https://gitlab.com/some-group/some-project/-/merge_requests/123" } let(:event) { "view_analytics_dashboard" } let(:unique) { "user.id" } let(:time_frames) { %w[7d] } + let(:group_unknown) { false } let(:include_default_identifiers) { 'yes' } - let(:options) do + let(:base_options) do { time_frames: time_frames, free: true, mr: mr, group: group, - stage: stage, - section: section, event: event, unique: unique }.stringify_keys end - let(:key_path_without_time_frame) { "count_distinct_#{unique.sub('.', '_')}_from_#{event}" } - let(:key_path_7d) { "#{key_path_without_time_frame}_7d" } - let(:metric_definition_path_7d) { Dir.glob(File.join(temp_dir, "metrics/counts_7d/#{key_path_7d}.yml")).first } - let(:metric_definition_7d) do - { - "key_path" => key_path_7d, - "description" => description, - "product_section" => section, - "product_stage" => stage, - "product_group" => group, - "performance_indicator_type" => [], - "value_type" => "number", - "status" => "active", - "milestone" => "13.9", - "introduced_by_url" => mr, - "time_frame" => "7d", - "data_source" => "internal_events", - "data_category" => "optional", - "instrumentation_class" => "RedisHLLMetric", - "distribution" => %w[ce ee], - "tier" => %w[free premium ultimate], - "options" => { - "events" => [event] - }, - "events" => [{ "name" => event, "unique" => unique }] - } - end - - let(:key_path_all) { "count_total_#{event}" } - let(:metric_definition_path_all) { Dir.glob(File.join(temp_dir, "metrics/counts_all/#{key_path_all}.yml")).first } - let(:metric_definition_all) do - { - "key_path" => key_path_all, - "description" => description, - "product_section" => section, - "product_stage" => stage, - "product_group" => group, - "performance_indicator_type" => [], - "value_type" => "number", - "status" => "active", - "milestone" => "13.9", - "introduced_by_url" => mr, - "time_frame" => "all", - "data_source" => "internal_events", - "data_category" => "optional", - "instrumentation_class" => "TotalCountMetric", - "distribution" => %w[ce ee], - "tier" => %w[free premium ultimate], - "options" => { - "events" => [event] - }, - "events" => [{ "name" => event }] - } - end + let(:options) { base_options } before do stub_const("#{described_class}::TOP_LEVEL_DIR_EE", ee_temp_dir) @@ -98,6 +44,10 @@ RSpec.describe Gitlab::Analytics::InternalEventsGenerator, :silence_stdout, feat .and_return(description) end + allow(Gitlab::Analytics::GroupFetcher).to receive(:group_unknown?).and_return(group_unknown) + allow(Gitlab::Analytics::GroupFetcher).to receive(:stage_text).with(group).and_return(stage) + allow(Gitlab::Analytics::GroupFetcher).to receive(:section_text).with(group).and_return(section) + allow(Gitlab::TaskHelpers).to receive(:prompt).and_return(include_default_identifiers) allow(Gitlab::Usage::MetricDefinition).to receive(:definitions).and_return(existing_key_paths) end @@ -189,35 +139,85 @@ RSpec.describe Gitlab::Analytics::InternalEventsGenerator, :silence_stdout, feat end describe 'Creating metric definition file' do - context 'for single time frame' do - let(:time_frames) { %w[7d] } + let(:metric_dir) { temp_dir } + let(:base_key_path_unique) { "count_distinct_#{unique.sub('.', '_')}_from_#{event}" } + let(:base_key_path_total) { "count_total_#{event}" } + let(:base_metric_definition) do + { + "description" => description, + "product_section" => section, + "product_stage" => stage, + "product_group" => group, + "performance_indicator_type" => [], + "value_type" => "number", + "status" => "active", + "milestone" => "13.9", + "introduced_by_url" => mr, + "data_source" => "internal_events", + "data_category" => "optional", + "distribution" => %w[ce ee], + "tier" => %w[free premium ultimate], + "options" => { + "events" => [event] + } + } + end + + let(:metric_definition_extra) { {} } - it 'creates a metric definition file' do + shared_examples 'creates unique metric definitions' do |time_frames| + it 'creates a metric definiton for each of the time frames' do described_class.new([], options).invoke_all - expect(YAML.safe_load(File.read(metric_definition_path_7d))).to eq(metric_definition_7d) + time_frames.each do |time_frame| + key_path = "#{base_key_path_unique}_#{time_frame}" + metric_definition_path = Dir.glob(File.join(metric_dir, "metrics/counts_#{time_frame}/#{key_path}.yml")).first + metric_definition = base_metric_definition.merge( + "key_path" => key_path, + "time_frame" => time_frame, + "events" => [{ "name" => event, "unique" => unique }] + ).merge(metric_definition_extra) + expect(YAML.safe_load(File.read(metric_definition_path))).to eq(metric_definition) + end end + end - context 'with time frame "all"' do - let(:time_frames) { %w[all] } + shared_examples 'creates total metric definitions' do |time_frames| + it 'creates a metric definiton for each of the time frames' do + described_class.new([], options).invoke_all - it 'creates a total count metric definition file' do - described_class.new([], options).invoke_all - expect(YAML.safe_load(File.read(metric_definition_path_all))).to eq(metric_definition_all) + time_frames.each do |time_frame| + key_path = "#{base_key_path_total}_#{time_frame}" + metric_definition_path = Dir.glob(File.join(metric_dir, "metrics/counts_#{time_frame}/#{key_path}.yml")).first + metric_definition = base_metric_definition.merge( + "key_path" => key_path, + "time_frame" => time_frame, + "events" => [{ "name" => event }] + ).merge(metric_definition_extra) + expect(YAML.safe_load(File.read(metric_definition_path))).to eq(metric_definition) end end + end - context 'for ultimate only feature' do - let(:metric_definition_path_7d) do - Dir.glob(File.join(ee_temp_dir, temp_dir, "metrics/counts_7d/#{key_path_7d}.yml")).first - end + context 'for single time frame' do + let(:time_frames) { %w[7d] } - it 'creates a metric definition file' do - described_class.new([], options.merge(tiers: %w[ultimate])).invoke_all + it_behaves_like 'creates unique metric definitions', %w[7d] - expect(YAML.safe_load(File.read(metric_definition_path_7d))) - .to eq(metric_definition_7d.merge("tier" => ["ultimate"], "distribution" => ["ee"])) - end + context 'with time frame "all" and no "unique"' do + let(:time_frames) { %w[all] } + + let(:options) { base_options.except('unique') } + + it_behaves_like 'creates total metric definitions', %w[all] + end + + context 'for ultimate only feature' do + let(:metric_dir) { File.join(ee_temp_dir, temp_dir) } + let(:options) { base_options.merge(tiers: %w[ultimate]) } + let(:metric_definition_extra) { { "tier" => ["ultimate"], "distribution" => ["ee"] } } + + it_behaves_like 'creates unique metric definitions', %w[7d] end context 'with invalid time frame' do @@ -228,7 +228,16 @@ RSpec.describe Gitlab::Analytics::InternalEventsGenerator, :silence_stdout, feat end end + context 'with invalid time frame for unique metrics' do + let(:time_frames) { %w[all] } + + it 'raises error' do + expect { described_class.new([], options).invoke_all }.to raise_error(RuntimeError) + end + end + context 'with duplicated key path' do + let(:key_path_7d) { "#{base_key_path_unique}_7d" } let(:existing_key_paths) { { key_path_7d => true } } it 'raises error' do @@ -252,14 +261,14 @@ RSpec.describe Gitlab::Analytics::InternalEventsGenerator, :silence_stdout, feat context 'without obligatory parameter' do it 'raises error', :aggregate_failures do - %w[unique event mr section stage group].each do |option| + %w[event mr group].each do |option| expect { described_class.new([], options.without(option)).invoke_all } .to raise_error(RuntimeError) end end end - context 'with to short description' do + context 'with too short description' do it 'asks again for description' do allow_next_instance_of(described_class) do |instance| allow(instance).to receive(:ask) @@ -281,42 +290,28 @@ RSpec.describe Gitlab::Analytics::InternalEventsGenerator, :silence_stdout, feat end context 'for multiple time frames' do - let(:time_frames) { %w[7d 28d all] } - let(:key_path_28d) { "#{key_path_without_time_frame}_28d" } - let(:metric_definition_path_28d) { Dir.glob(File.join(temp_dir, "metrics/counts_28d/#{key_path_28d}.yml")).first } - let(:metric_definition_28d) do - metric_definition_7d.merge( - "key_path" => key_path_28d, - "time_frame" => "28d" - ) - end + let(:time_frames) { %w[7d 28d] } - it 'creates metric definition files' do - described_class.new([], options).invoke_all - - expect(YAML.safe_load(File.read(metric_definition_path_7d))).to eq(metric_definition_7d) - expect(YAML.safe_load(File.read(metric_definition_path_28d))).to eq(metric_definition_28d) - expect(YAML.safe_load(File.read(metric_definition_path_all))).to eq(metric_definition_all) - end + it_behaves_like 'creates unique metric definitions', %w[7d 28d] end context 'with default time frames' do - let(:time_frames) { nil } - let(:key_path_28d) { "#{key_path_without_time_frame}_28d" } - let(:metric_definition_path_28d) { Dir.glob(File.join(temp_dir, "metrics/counts_28d/#{key_path_28d}.yml")).first } - let(:metric_definition_28d) do - metric_definition_7d.merge( - "key_path" => key_path_28d, - "time_frame" => "28d" - ) - end + let(:options) { base_options.without('time_frames', 'unique') } - it 'creates metric definition files' do - described_class.new([], options.without('time_frames')).invoke_all + it_behaves_like 'creates total metric definitions', %w[7d 28d all] - expect(YAML.safe_load(File.read(metric_definition_path_7d))).to eq(metric_definition_7d) - expect(YAML.safe_load(File.read(metric_definition_path_28d))).to eq(metric_definition_28d) - expect(YAML.safe_load(File.read(metric_definition_path_all))).to eq(metric_definition_all) + context 'with unique' do + let(:options) { base_options.without('time_frames') } + + it_behaves_like 'creates unique metric definitions', %w[7d 28d] + + it "doesn't create a total 'all' metric" do + described_class.new([], options).invoke_all + + key_path = "#{base_key_path_total}_all" + + expect(Dir.glob(File.join(metric_dir, "metrics/counts_all/#{key_path}.yml")).first).to be_nil + end end end end |