Commit 48778ac5 authored by Pawel Chojnacki's avatar Pawel Chojnacki

Tests for query context variables

parent 6df5bd8b
...@@ -75,11 +75,6 @@ class PrometheusService < MonitoringService ...@@ -75,11 +75,6 @@ class PrometheusService < MonitoringService
with_reactive_cache(Gitlab::Prometheus::Queries::MatchedMetricsQuery.name, &:itself) with_reactive_cache(Gitlab::Prometheus::Queries::MatchedMetricsQuery.name, &:itself)
end end
def with_reactive_cache(name, *args, &block)
vals = args.map(&:to_s)
yield calculate_reactive_cache(name, *vals)
end
# Cache metrics for specific environment # Cache metrics for specific environment
def calculate_reactive_cache(query_class_name, *args) def calculate_reactive_cache(query_class_name, *args)
return unless active? && project && !project.pending_delete? return unless active? && project && !project.pending_delete?
......
...@@ -6,11 +6,11 @@ describe Gitlab::Prometheus::Queries::AdditionalMetricsEnvironmentQuery do ...@@ -6,11 +6,11 @@ describe Gitlab::Prometheus::Queries::AdditionalMetricsEnvironmentQuery do
end end
include_examples 'additional metrics query' do include_examples 'additional metrics query' do
let(:query_result) { described_class.new(client).query(environment.id) }
let(:query_params) { [environment.id] } let(:query_params) { [environment.id] }
it 'queries using specific time' do it 'queries using specific time' do
expect(client).to receive(:query_range).with(anything, start: 8.hours.ago.to_f, stop: Time.now.to_f) expect(client).to receive(:query_range).with(anything, start: 8.hours.ago.to_f, stop: Time.now.to_f)
expect(query_result).not_to be_nil expect(query_result).not_to be_nil
end end
end end
......
...@@ -11,6 +11,7 @@ RSpec.shared_examples 'additional metrics query' do ...@@ -11,6 +11,7 @@ RSpec.shared_examples 'additional metrics query' do
end end
let(:client) { double('prometheus_client') } let(:client) { double('prometheus_client') }
let(:query_result) { described_class.new(client).query(*query_params) }
let(:environment) { create(:environment, slug: 'environment-slug') } let(:environment) { create(:environment, slug: 'environment-slug') }
before do before do
...@@ -18,31 +19,52 @@ RSpec.shared_examples 'additional metrics query' do ...@@ -18,31 +19,52 @@ RSpec.shared_examples 'additional metrics query' do
allow(metric_group_class).to receive(:all).and_return([simple_metric_group(metrics: [simple_metric])]) allow(metric_group_class).to receive(:all).and_return([simple_metric_group(metrics: [simple_metric])])
end end
context 'metrics rendering' do context 'metrics query context' do
subject! { described_class.new(client) } subject! { described_class.new(client) }
before do shared_examples 'query context containing environment slug and filter' do
it 'query context contains ci_environment_slug' do
expect(subject).to receive(:query_metrics).with(hash_including(ci_environment_slug: environment.slug))
subject.query(*query_params)
end end
describe 'project has kubernetes service' do it 'query context contains environment filter' do
expect(subject).to receive(:query_metrics).with(
hash_including(
environment_filter: "container_name!=\"POD\",environment=\"#{environment.slug}\""
)
)
subject.query(*query_params)
end
end
describe 'project has Kubernetes service' do
let(:project) { create(:kubernetes_project) } let(:project) { create(:kubernetes_project) }
let(:environment) { create(:environment, slug: 'environment-slug', project: project) } let(:environment) { create(:environment, slug: 'environment-slug', project: project) }
let(:kube_namespace) { project.kubernetes_service.actual_namespace } let(:kube_namespace) { project.kubernetes_service.actual_namespace }
it 'query context contains kube namespace' do it_behaves_like 'query context containing environment slug and filter'
expect(subject).to receive(:query_metrics).with(
hash_including( it 'query context contains kube_namespace' do
kube_namespace: kube_namespace) expect(subject).to receive(:query_metrics).with(hash_including(kube_namespace: kube_namespace))
)
subject.query(*query_params)
end
end
describe 'project without Kubernetes service' do
it_behaves_like 'query context containing environment slug and filter'
it 'query context contains empty kube_namespace' do
expect(subject).to receive(:query_metrics).with(hash_including(kube_namespace: ''))
subject.query(*query_params) subject.query(*query_params)
end end
end end
end end
context 'with one group where two metrics is found' do context 'with one group where two metrics is found' do
let(:query_result) { described_class.new(client).query(*query_params) }
before do before do
allow(metric_group_class).to receive(:all).and_return([simple_metric_group]) allow(metric_group_class).to receive(:all).and_return([simple_metric_group])
end end
...@@ -77,7 +99,6 @@ RSpec.shared_examples 'additional metrics query' do ...@@ -77,7 +99,6 @@ RSpec.shared_examples 'additional metrics query' do
end end
context 'with two groups with one metric each' do context 'with two groups with one metric each' do
let(:query_result) { described_class.new(client).query(*query_params) }
let(:metrics) { [simple_metric(queries: [simple_query])] } let(:metrics) { [simple_metric(queries: [simple_query])] }
before do before do
......
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