From 48778ac58973fa7cab09deaaf2e93806aa37113d Mon Sep 17 00:00:00 2001 From: Pawel Chojnacki Date: Mon, 31 Jul 2017 15:26:38 +0200 Subject: Tests for query context variables --- .../additional_metrics_shared_examples.rb | 45 ++++++++++++++++------ 1 file changed, 33 insertions(+), 12 deletions(-) (limited to 'spec/support/prometheus') diff --git a/spec/support/prometheus/additional_metrics_shared_examples.rb b/spec/support/prometheus/additional_metrics_shared_examples.rb index fcb5c315b98..174297af158 100644 --- a/spec/support/prometheus/additional_metrics_shared_examples.rb +++ b/spec/support/prometheus/additional_metrics_shared_examples.rb @@ -11,6 +11,7 @@ RSpec.shared_examples 'additional metrics query' do end let(:client) { double('prometheus_client') } + let(:query_result) { described_class.new(client).query(*query_params) } let(:environment) { create(:environment, slug: 'environment-slug') } before 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])]) end - context 'metrics rendering' do + context 'metrics query context' do 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 + 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 + describe 'project has Kubernetes service' do 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 } - it 'query context contains kube namespace' do - expect(subject).to receive(:query_metrics).with( - hash_including( - kube_namespace: kube_namespace) - ) + it_behaves_like 'query context containing environment slug and filter' + + it 'query context contains kube_namespace' do + 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) end end end context 'with one group where two metrics is found' do - let(:query_result) { described_class.new(client).query(*query_params) } - before do allow(metric_group_class).to receive(:all).and_return([simple_metric_group]) end @@ -77,7 +99,6 @@ RSpec.shared_examples 'additional metrics query' do end 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])] } before do -- cgit v1.2.1