diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-04-07 15:09:34 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-04-07 15:09:34 +0300 |
commit | 903ccf7c93eb9490c76857bffe744249cc07de09 (patch) | |
tree | 603a3162e91999160e4efc74f351f9405f422d61 /spec/lib/gitlab/prometheus | |
parent | 41cb558299b483b44b45351730ee4c0e9fe4ca2c (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec/lib/gitlab/prometheus')
-rw-r--r-- | spec/lib/gitlab/prometheus/queries/validate_query_spec.rb | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/spec/lib/gitlab/prometheus/queries/validate_query_spec.rb b/spec/lib/gitlab/prometheus/queries/validate_query_spec.rb new file mode 100644 index 00000000000..9c0170718f4 --- /dev/null +++ b/spec/lib/gitlab/prometheus/queries/validate_query_spec.rb @@ -0,0 +1,62 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe Gitlab::Prometheus::Queries::ValidateQuery do + include PrometheusHelpers + + let(:api_url) { 'https://prometheus.example.com' } + let(:client) { Gitlab::PrometheusClient.new(api_url) } + let(:query) { 'avg(metric)' } + + subject { described_class.new(client) } + + context 'valid query' do + before do + allow(client).to receive(:query).with(query) + end + + it 'passess query to prometheus' do + expect(subject.query(query)).to eq(valid: true) + + expect(client).to have_received(:query).with(query) + end + end + + context 'invalid query' do + let(:query) { 'invalid query' } + let(:error_message) { "invalid parameter 'query': 1:9: parse error: unexpected identifier \"query\"" } + + it 'returns invalid' do + Timecop.freeze do + stub_prometheus_query_error( + prometheus_query_with_time_url(query, Time.now), + error_message + ) + + expect(subject.query(query)).to eq(valid: false, error: error_message) + end + end + end + + context 'when exceptions occur' do + context 'Gitlab::HTTP::BlockedUrlError' do + let(:api_url) { 'http://192.168.1.1' } + + let(:message) do + "URL 'http://192.168.1.1/api/v1/query?query=avg%28metric%29&time=#{Time.now.to_f}'" \ + " is blocked: Requests to the local network are not allowed" + end + + before do + stub_application_setting(allow_local_requests_from_web_hooks_and_services: false) + end + + it 'catches exception and returns invalid' do + Timecop.freeze do + expect(subject.query(query)).to eq(valid: false, error: message) + end + end + end + end +end |