diff options
Diffstat (limited to 'spec/lib/generators')
-rw-r--r-- | spec/lib/generators/gitlab/usage_metric_definition/redis_hll_generator_spec.rb | 30 | ||||
-rw-r--r-- | spec/lib/generators/gitlab/usage_metric_definition_generator_spec.rb | 70 |
2 files changed, 70 insertions, 30 deletions
diff --git a/spec/lib/generators/gitlab/usage_metric_definition/redis_hll_generator_spec.rb b/spec/lib/generators/gitlab/usage_metric_definition/redis_hll_generator_spec.rb new file mode 100644 index 00000000000..021fb8f5f58 --- /dev/null +++ b/spec/lib/generators/gitlab/usage_metric_definition/redis_hll_generator_spec.rb @@ -0,0 +1,30 @@ +# frozen_string_literal: true + +require 'generator_helper' + +RSpec.describe Gitlab::UsageMetricDefinition::RedisHllGenerator do + include UsageDataHelpers + + let(:category) { 'test_category' } + let(:event) { 'i_test_event' } + let(:args) { [category, event] } + let(:temp_dir) { Dir.mktmpdir } + + # Interpolating to preload the class + # See https://github.com/rspec/rspec-mocks/issues/1079 + before do + stub_const("#{Gitlab::UsageMetricDefinitionGenerator}::TOP_LEVEL_DIR", temp_dir) + # Stub Prometheus requests from Gitlab::Utils::UsageData + stub_prometheus_queries + end + + it 'creates metric definition files' do + described_class.new(args).invoke_all + + weekly_metric_definition_path = Dir.glob(File.join(temp_dir, 'metrics/counts_7d/*i_test_event_weekly.yml')).first + monthly_metric_definition_path = Dir.glob(File.join(temp_dir, 'metrics/counts_28d/*i_test_event_monthly.yml')).first + + expect(YAML.safe_load(File.read(weekly_metric_definition_path))).to include("key_path" => "redis_hll_counters.test_category.i_test_event_weekly") + expect(YAML.safe_load(File.read(monthly_metric_definition_path))).to include("key_path" => "redis_hll_counters.test_category.i_test_event_monthly") + end +end diff --git a/spec/lib/generators/gitlab/usage_metric_definition_generator_spec.rb b/spec/lib/generators/gitlab/usage_metric_definition_generator_spec.rb index b62eac14e3e..f8c055ae111 100644 --- a/spec/lib/generators/gitlab/usage_metric_definition_generator_spec.rb +++ b/spec/lib/generators/gitlab/usage_metric_definition_generator_spec.rb @@ -3,10 +3,42 @@ require 'generator_helper' RSpec.describe Gitlab::UsageMetricDefinitionGenerator do + include UsageDataHelpers + + let(:key_path) { 'counts_weekly.test_metric' } + let(:dir) { '7d' } + let(:temp_dir) { Dir.mktmpdir } + + before do + stub_const("#{described_class}::TOP_LEVEL_DIR", temp_dir) + # Stub Prometheus requests from Gitlab::Utils::UsageData + stub_prometheus_queries + end + + after do + FileUtils.rm_rf(temp_dir) + end + + describe 'Creating metric definition file' do + # Stub version so that `milestone` key remains constant between releases to prevent flakiness. + before do + stub_const('Gitlab::VERSION', '13.9.0') + allow(::Gitlab::Usage::Metrics::NamesSuggestions::Generator).to receive(:generate).and_return('test metric name') + end + + let(:sample_metric) { load_sample_metric_definition(filename: 'sample_metric_with_name_suggestions.yml') } + + it 'creates a metric definition file using the template' do + described_class.new([key_path], { 'dir' => dir }).invoke_all + + metric_definition_path = Dir.glob(File.join(temp_dir, 'metrics/counts_7d/*_test_metric.yml')).first + + expect(YAML.safe_load(File.read(metric_definition_path))).to eq(sample_metric) + end + end + describe 'Validation' do - let(:key_path) { 'counter.category.event' } - let(:dir) { '7d' } - let(:options) { [key_path, '--dir', dir, '--pretend'] } + let(:options) { [key_path, '--dir', dir] } subject { described_class.start(options) } @@ -42,34 +74,12 @@ RSpec.describe Gitlab::UsageMetricDefinitionGenerator do end describe 'Name suggestions' do - let(:temp_dir) { Dir.mktmpdir } - - before do - stub_const("#{described_class}::TOP_LEVEL_DIR", temp_dir) - end - - context 'with product_intelligence_metrics_names_suggestions feature ON' do - it 'adds name key to metric definition' do - stub_feature_flags(product_intelligence_metrics_names_suggestions: true) - - expect(::Gitlab::Usage::Metrics::NamesSuggestions::Generator).to receive(:generate).and_return('some name') - described_class.new(['counts_weekly.test_metric'], { 'dir' => '7d' }).invoke_all - metric_definition_path = Dir.glob(File.join(temp_dir, 'metrics/counts_7d/*_test_metric.yml')).first + it 'adds name key to metric definition' do + expect(::Gitlab::Usage::Metrics::NamesSuggestions::Generator).to receive(:generate).and_return('some name') + described_class.new([key_path], { 'dir' => dir }).invoke_all + metric_definition_path = Dir.glob(File.join(temp_dir, 'metrics/counts_7d/*_test_metric.yml')).first - expect(YAML.safe_load(File.read(metric_definition_path))).to include("name" => "some name") - end - end - - context 'with product_intelligence_metrics_names_suggestions feature OFF' do - it 'adds name key to metric definition' do - stub_feature_flags(product_intelligence_metrics_names_suggestions: false) - - expect(::Gitlab::Usage::Metrics::NamesSuggestions::Generator).not_to receive(:generate) - described_class.new(['counts_weekly.test_metric'], { 'dir' => '7d' }).invoke_all - metric_definition_path = Dir.glob(File.join(temp_dir, 'metrics/counts_7d/*_test_metric.yml')).first - - expect(YAML.safe_load(File.read(metric_definition_path)).keys).not_to include(:name) - end + expect(YAML.safe_load(File.read(metric_definition_path))).to include("name" => "some name") end end end |