diff options
author | Pawel Chojnacki <pawel@chojnacki.ws> | 2017-06-05 19:26:46 +0200 |
---|---|---|
committer | Pawel Chojnacki <pawel@chojnacki.ws> | 2017-06-05 19:26:46 +0200 |
commit | eccc187a70a6745ca02211648814c89ff390dfa3 (patch) | |
tree | 5ecdb9c0ade646105d1051a54ec5d7076c7a16cf /spec | |
parent | 223d07b38315a68fe39df90a7675f043d8b7acaf (diff) | |
download | gitlab-ce-eccc187a70a6745ca02211648814c89ff390dfa3.tar.gz |
Additional Metrics of deployment tests
Diffstat (limited to 'spec')
4 files changed, 179 insertions, 142 deletions
diff --git a/spec/lib/gitlab/prometheus/queries/additional_metrics_deployment_query_spec.rb b/spec/lib/gitlab/prometheus/queries/additional_metrics_deployment_query_spec.rb new file mode 100644 index 00000000000..93a9ce38d80 --- /dev/null +++ b/spec/lib/gitlab/prometheus/queries/additional_metrics_deployment_query_spec.rb @@ -0,0 +1,29 @@ +require 'spec_helper' + +describe Gitlab::Prometheus::Queries::AdditionalMetricsDeploymentQuery, lib: true do + include Prometheus::AdditionalMetricsQueryHelper + include Prometheus::MetricBuilders + + let(:metric_group_class) { Gitlab::Prometheus::MetricGroup } + let(:metric_class) { Gitlab::Prometheus::Metric } + + let(:client) { double('prometheus_client') } + let(:environment) { create(:environment, slug: 'environment-slug') } + let(:deployment) { create(:deployment, environment: environment) } + + subject(:query_result) { described_class.new(client).query(deployment.id) } + + around do |example| + Timecop.freeze { example.run } + end + + include_examples 'additional metrics query' do + it 'queries using specific time' do + expect(client).to receive(:query_range).with(anything, + start: (deployment.created_at - 30.minutes).to_f, + stop: (deployment.created_at + 30.minutes).to_f) + + expect(query_result).not_to be_nil + end + end +end diff --git a/spec/lib/gitlab/prometheus/queries/additional_metrics_query_spec.rb b/spec/lib/gitlab/prometheus/queries/additional_metrics_query_spec.rb index 2291c4d67bb..e5db1326597 100644 --- a/spec/lib/gitlab/prometheus/queries/additional_metrics_query_spec.rb +++ b/spec/lib/gitlab/prometheus/queries/additional_metrics_query_spec.rb @@ -4,9 +4,6 @@ describe Gitlab::Prometheus::Queries::AdditionalMetricsQuery, lib: true do include Prometheus::AdditionalMetricsQueryHelper include Prometheus::MetricBuilders - let(:metric_group_class) { Gitlab::Prometheus::MetricGroup } - let(:metric_class) { Gitlab::Prometheus::Metric } - let(:client) { double('prometheus_client') } let(:environment) { create(:environment, slug: 'environment-slug') } @@ -16,142 +13,10 @@ describe Gitlab::Prometheus::Queries::AdditionalMetricsQuery, lib: true do Timecop.freeze { example.run } end - context 'with one group where two metrics is found' do - before do - allow(metric_group_class).to receive(:all).and_return([simple_metric_group]) - allow(client).to receive(:label_values).and_return(metric_names) - end - - context 'some queries return results' do - before do - allow(client).to receive(:query_range).with('query_range_a', any_args).and_return(query_range_result) - allow(client).to receive(:query_range).with('query_range_b', any_args).and_return(query_range_result) - allow(client).to receive(:query_range).with('query_range_empty', any_args).and_return([]) - end - - it 'return group data only for queries with results' do - expected = [ - { - group: 'name', - priority: 1, - metrics: [ - { - title: 'title', weight: nil, y_label: 'Values', queries: [ - { query_range: 'query_range_a', result: query_range_result }, - { query_range: 'query_range_b', label: 'label', unit: 'unit', result: query_range_result } - ] - } - ] - } - ] - - expect(query_result).to eq(expected) - end - end - end - - context 'with two groups with one metric each' do - let(:metrics) { [simple_metric(queries: [simple_query])] } - before do - allow(metric_group_class).to receive(:all).and_return( - [ - simple_metric_group('group_a', [simple_metric(queries: [simple_query])]), - simple_metric_group('group_b', [simple_metric(title: 'title_b', queries: [simple_query('b')])]) - ]) - allow(client).to receive(:label_values).and_return(metric_names) - end - - context 'both queries return results' do - before do - allow(client).to receive(:query_range).with('query_range_a', any_args).and_return(query_range_result) - allow(client).to receive(:query_range).with('query_range_b', any_args).and_return(query_range_result) - end - - it 'return group data both queries' do - expected = [ - { - group: 'group_a', - priority: 1, - metrics: [ - { - title: 'title', - weight: nil, - y_label: 'Values', - queries: [ - { - query_range: 'query_range_a', - result: [ - { - metric: {}, - values: [[1488758662.506, '0.00002996364761904785'], [1488758722.506, '0.00003090239047619091']] } - ] - } - ] - } - ] - }, - { - group: 'group_b', - priority: 1, - metrics: [ - { - title: 'title_b', - weight: nil, - y_label: 'Values', - queries: [ - { - query_range: 'query_range_b', result: [ - { - metric: {}, - values: [[1488758662.506, '0.00002996364761904785'], [1488758722.506, '0.00003090239047619091']] - } - ] - } - ] - } - ] - } - ] - - expect(query_result).to eq(expected) - end - end - - context 'one query returns result' do - before do - allow(client).to receive(:query_range).with('query_range_a', any_args).and_return(query_range_result) - allow(client).to receive(:query_range).with('query_range_b', any_args).and_return([]) - end - - 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(query_result).not_to be_nil - end - - it 'return group data only for query with results' do - expected = [ - { - group: 'group_a', - priority: 1, - metrics: [ - { - title: 'title', - weight: nil, - y_label: 'Values', - queries: [ - { - query_range: 'query_range_a', - result: query_range_result - } - ] - } - ] - } - ] - - expect(query_result).to eq(expected) - end + include_examples 'additional metrics query' 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(query_result).not_to be_nil end end end diff --git a/spec/support/prometheus/additional_metrics_shared_examples.rb b/spec/support/prometheus/additional_metrics_shared_examples.rb new file mode 100644 index 00000000000..96a9e1f5049 --- /dev/null +++ b/spec/support/prometheus/additional_metrics_shared_examples.rb @@ -0,0 +1,143 @@ +RSpec.shared_examples 'additional metrics query' do + include Prometheus::MetricBuilders + + before do + allow(client).to receive(:label_values).and_return(metric_names) + allow(metric_group_class).to receive(:all).and_return([simple_metric_group(metrics: [simple_metric])]) + end + + let(:metric_group_class) { Gitlab::Prometheus::MetricGroup } + let(:metric_class) { Gitlab::Prometheus::Metric } + + context 'with one group where two metrics is found' do + before do + allow(metric_group_class).to receive(:all).and_return([simple_metric_group]) + end + + context 'some queries return results' do + before do + allow(client).to receive(:query_range).with('query_range_a', any_args).and_return(query_range_result) + allow(client).to receive(:query_range).with('query_range_b', any_args).and_return(query_range_result) + allow(client).to receive(:query_range).with('query_range_empty', any_args).and_return([]) + end + + it 'return group data only for queries with results' do + expected = [ + { + group: 'name', + priority: 1, + metrics: [ + { + title: 'title', weight: nil, y_label: 'Values', queries: [ + { query_range: 'query_range_a', result: query_range_result }, + { query_range: 'query_range_b', label: 'label', unit: 'unit', result: query_range_result } + ] + } + ] + } + ] + + expect(query_result).to eq(expected) + end + end + end + + context 'with two groups with one metric each' do + let(:metrics) { [simple_metric(queries: [simple_query])] } + before do + allow(metric_group_class).to receive(:all).and_return( + [ + simple_metric_group(name: 'group_a', metrics: [simple_metric(queries: [simple_query])]), + simple_metric_group(name: 'group_b', metrics: [simple_metric(title: 'title_b', queries: [simple_query('b')])]) + ]) + allow(client).to receive(:label_values).and_return(metric_names) + end + + context 'both queries return results' do + before do + allow(client).to receive(:query_range).with('query_range_a', any_args).and_return(query_range_result) + allow(client).to receive(:query_range).with('query_range_b', any_args).and_return(query_range_result) + end + + it 'return group data both queries' do + expected = [ + { + group: 'group_a', + priority: 1, + metrics: [ + { + title: 'title', + weight: nil, + y_label: 'Values', + queries: [ + { + query_range: 'query_range_a', + result: [ + { + metric: {}, + values: [[1488758662.506, '0.00002996364761904785'], [1488758722.506, '0.00003090239047619091']] } + ] + } + ] + } + ] + }, + { + group: 'group_b', + priority: 1, + metrics: [ + { + title: 'title_b', + weight: nil, + y_label: 'Values', + queries: [ + { + query_range: 'query_range_b', result: [ + { + metric: {}, + values: [[1488758662.506, '0.00002996364761904785'], [1488758722.506, '0.00003090239047619091']] + } + ] + } + ] + } + ] + } + ] + + expect(query_result).to eq(expected) + end + end + + context 'one query returns result' do + before do + allow(client).to receive(:query_range).with('query_range_a', any_args).and_return(query_range_result) + allow(client).to receive(:query_range).with('query_range_b', any_args).and_return([]) + end + + it 'return group data only for query with results' do + expected = [ + { + group: 'group_a', + priority: 1, + metrics: [ + { + title: 'title', + weight: nil, + y_label: 'Values', + queries: [ + { + query_range: 'query_range_a', + result: query_range_result + } + ] + } + ] + } + ] + + expect(query_result).to eq(expected) + end + end + end +end
\ No newline at end of file diff --git a/spec/support/prometheus/metric_builders.rb b/spec/support/prometheus/metric_builders.rb index 2d54ecdfb5c..c57694bf7fd 100644 --- a/spec/support/prometheus/metric_builders.rb +++ b/spec/support/prometheus/metric_builders.rb @@ -8,7 +8,7 @@ module Prometheus [simple_query, simple_query('b', label: 'label', unit: 'unit')] end - def simple_metric(title: 'title', required_metrics: [], queries: []) + def simple_metric(title: 'title', required_metrics: [], queries: [simple_query]) Gitlab::Prometheus::Metric.new(title, required_metrics, nil, nil, queries) end @@ -20,8 +20,8 @@ module Prometheus ] end - def simple_metric_group(name = 'name', metrics = simple_metrics) + def simple_metric_group(name: 'name', metrics: simple_metrics) Gitlab::Prometheus::MetricGroup.new(name, 1, metrics) end end -end
\ No newline at end of file +end |