Commit 0a81eafa authored by Sean McGivern's avatar Sean McGivern

Merge branch 'rp-fix-custom-metric-validation-error' into 'master'

Return error message in custom metric validation when prometheus URL is blocked

See merge request gitlab-org/gitlab!27863
parents b83696a5 d3a10c19
---
title: Display error message in custom metric form validation when prometheus URL
is blocked
merge_request: 27863
author:
type: fixed
...@@ -7,7 +7,7 @@ module Gitlab ...@@ -7,7 +7,7 @@ module Gitlab
def query(query) def query(query)
client_query(query) client_query(query)
{ valid: true } { valid: true }
rescue Gitlab::PrometheusClient::QueryError => ex rescue Gitlab::PrometheusClient::QueryError, Gitlab::HTTP::BlockedUrlError => ex
{ valid: false, error: ex.message } { valid: false, error: ex.message }
end end
......
...@@ -3,7 +3,10 @@ ...@@ -3,7 +3,10 @@
require 'spec_helper' require 'spec_helper'
describe Gitlab::Prometheus::Queries::ValidateQuery do describe Gitlab::Prometheus::Queries::ValidateQuery do
let(:client) { double('prometheus_client') } include PrometheusHelpers
let(:api_url) { 'https://prometheus.example.com' }
let(:client) { Gitlab::PrometheusClient.new(api_url) }
let(:query) { 'avg(metric)' } let(:query) { 'avg(metric)' }
subject { described_class.new(client) } subject { described_class.new(client) }
...@@ -21,16 +24,39 @@ describe Gitlab::Prometheus::Queries::ValidateQuery do ...@@ -21,16 +24,39 @@ describe Gitlab::Prometheus::Queries::ValidateQuery do
end end
context 'invalid query' do context 'invalid query' do
let(:message) { 'message' } let(:query) { 'invalid query' }
let(:error_message) { "invalid parameter 'query': 1:9: parse error: unexpected identifier \"query\"" }
before do
allow(client).to receive(:query).with(query).and_raise(Gitlab::PrometheusClient::QueryError.new(message)) 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
end
it 'passes query to prometheus' do context 'when exceptions occur' do
expect(subject.query(query)).to eq(valid: false, error: message) context 'Gitlab::HTTP::BlockedUrlError' do
let(:api_url) { 'http://192.168.1.1' }
expect(client).to have_received(:query).with(query) 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 end
end end
...@@ -68,6 +68,21 @@ module PrometheusHelpers ...@@ -68,6 +68,21 @@ module PrometheusHelpers
}) })
end end
def stub_prometheus_query_error(url, error_message = 'error', body: {}, headers: {})
response = {
status: 'error',
errorType: 'bad_data',
error: error_message
}.merge(body)
WebMock.stub_request(:get, url)
.to_return({
status: 400,
headers: { 'Content-Type' => 'application/json' }.merge(headers),
body: response.to_json
})
end
def stub_prometheus_request_with_exception(url, exception_type) def stub_prometheus_request_with_exception(url, exception_type)
WebMock.stub_request(:get, url).to_raise(exception_type) WebMock.stub_request(:get, url).to_raise(exception_type)
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