Commit 23ffad4f authored by Imre Farkas's avatar Imre Farkas

Merge branch '300555-add-tests-for-usagemetricdefinitiongenerator-generator' into 'master'

Add spec for UsageMetricDefinitionGenerator file creation

See merge request gitlab-org/gitlab!56551
parents b2078b70 2d8e5dce
---
# See Usage Ping metrics dictionary docs https://docs.gitlab.com/ee/development/usage_ping/metrics_dictionary.html
key_path: counts_weekly.test_metric
description:
product_section:
product_stage:
product_group:
product_category:
value_type: number
status: implemented
milestone: "13.9"
introduced_by_url:
time_frame: 7d
data_source:
distribution:
- ce
# Add here corresponding tiers
# tier:
# - free
# - premium
# - ultimate
---
# See Usage Ping metrics dictionary docs https://docs.gitlab.com/ee/development/usage_ping/metrics_dictionary.html
key_path: counts_weekly.test_metric
name: test metric name
description:
product_section:
product_stage:
product_group:
product_category:
value_type: number
status: implemented
milestone: "13.9"
introduced_by_url:
time_frame: 7d
data_source:
distribution:
- ce
# Add here corresponding tiers
# tier:
# - free
# - premium
# - ultimate
......@@ -3,10 +3,72 @@
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_request(:get, 'https://::1:9090/-/ready')
.to_return(
status: 200,
body: [{}].to_json,
headers: { 'Content-Type' => 'application/json' }
)
stub_request(:get, %r{^https://::1:9090/api/v1/query\?query=.*})
.to_return(
status: 200,
body: [{}].to_json,
headers: { 'Content-Type' => 'application/json' }
)
end
after do
FileUtils.rm_rf(temp_dir)
end
describe 'Creating metric definition file' do
let(:sample_metric) { load_sample_metric_definition }
# 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
context 'with product_intelligence_metrics_names_suggestions feature ON' do
let(:sample_metric) { load_sample_metric_definition(filename: 'sample_metric_with_name_suggestions.yml') }
it 'creates a metric definition file using the template' do
stub_feature_flags(product_intelligence_metrics_names_suggestions: true)
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
context 'with product_intelligence_metrics_names_suggestions feature OFF' do
it 'creates a metric definition file using the template' do
stub_feature_flags(product_intelligence_metrics_names_suggestions: false)
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
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,18 +104,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
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")
......@@ -65,7 +121,7 @@ RSpec.describe Gitlab::UsageMetricDefinitionGenerator 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
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)).keys).not_to include(:name)
......
......@@ -242,4 +242,12 @@ module UsageDataHelpers
end
end
end
def load_sample_metric_definition(filename: 'sample_metric.yml')
load_metric_yaml(fixture_file("lib/generators/gitlab/usage_metric_definition_generator/#{filename}"))
end
def load_metric_yaml(data)
::Gitlab::Config::Loader::Yaml.new(data).load_raw!
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