Fix prometheus client tests
This commit is contained in:
parent
80d4c0675f
commit
9c0b10da41
|
@ -50,10 +50,12 @@ module Gitlab
|
|||
response = rest_client[path].get(params: args)
|
||||
handle_response(response)
|
||||
rescue SocketError
|
||||
raise PrometheusError, "Can't connect to #{url}"
|
||||
raise PrometheusError, "Can't connect to #{rest_client.url}"
|
||||
rescue OpenSSL::SSL::SSLError
|
||||
raise PrometheusError, "#{url} contains invalid SSL data"
|
||||
rescue HTTParty::Error
|
||||
raise PrometheusError, "#{rest_client.url} contains invalid SSL data"
|
||||
rescue RestClient::ExceptionWithResponse => ex
|
||||
handle_exception_response(ex.response)
|
||||
rescue RestClient::Exception
|
||||
raise PrometheusError, "Network connection error"
|
||||
end
|
||||
|
||||
|
@ -61,7 +63,14 @@ module Gitlab
|
|||
json_data = JSON.parse(response.body)
|
||||
if response.code == 200 && json_data['status'] == 'success'
|
||||
json_data['data'] || {}
|
||||
elsif response.code == 400
|
||||
else
|
||||
raise PrometheusError, "#{response.code} - #{response.body}"
|
||||
end
|
||||
end
|
||||
|
||||
def handle_exception_response(response)
|
||||
if response.code == 400
|
||||
json_data = JSON.parse(response.body)
|
||||
raise PrometheusError, json_data['error'] || 'Bad data received'
|
||||
else
|
||||
raise PrometheusError, "#{response.code} - #{response.body}"
|
||||
|
|
|
@ -3,7 +3,7 @@ require 'spec_helper'
|
|||
describe Gitlab::PrometheusClient do
|
||||
include PrometheusHelpers
|
||||
|
||||
subject { described_class.new(api_url: 'https://prometheus.example.com') }
|
||||
subject { described_class.new(RestClient::Resource.new('https://prometheus.example.com')) }
|
||||
|
||||
describe '#ping' do
|
||||
it 'issues a "query" request to the API endpoint' do
|
||||
|
@ -52,11 +52,13 @@ describe Gitlab::PrometheusClient do
|
|||
describe 'failure to reach a provided prometheus url' do
|
||||
let(:prometheus_url) {"https://prometheus.invalid.example.com"}
|
||||
|
||||
subject { described_class.new(RestClient::Resource.new(prometheus_url)) }
|
||||
|
||||
context 'exceptions are raised' do
|
||||
it 'raises a Gitlab::PrometheusError error when a SocketError is rescued' do
|
||||
req_stub = stub_prometheus_request_with_exception(prometheus_url, SocketError)
|
||||
|
||||
expect { subject.send(:get, prometheus_url) }
|
||||
expect { subject.send(:get, '/', {}) }
|
||||
.to raise_error(Gitlab::PrometheusError, "Can't connect to #{prometheus_url}")
|
||||
expect(req_stub).to have_been_requested
|
||||
end
|
||||
|
@ -64,15 +66,15 @@ describe Gitlab::PrometheusClient do
|
|||
it 'raises a Gitlab::PrometheusError error when a SSLError is rescued' do
|
||||
req_stub = stub_prometheus_request_with_exception(prometheus_url, OpenSSL::SSL::SSLError)
|
||||
|
||||
expect { subject.send(:get, prometheus_url) }
|
||||
expect { subject.send(:get, '/', {}) }
|
||||
.to raise_error(Gitlab::PrometheusError, "#{prometheus_url} contains invalid SSL data")
|
||||
expect(req_stub).to have_been_requested
|
||||
end
|
||||
|
||||
it 'raises a Gitlab::PrometheusError error when a HTTParty::Error is rescued' do
|
||||
req_stub = stub_prometheus_request_with_exception(prometheus_url, HTTParty::Error)
|
||||
it 'raises a Gitlab::PrometheusError error when a RestClient::Exception is rescued' do
|
||||
req_stub = stub_prometheus_request_with_exception(prometheus_url, RestClient::Exception)
|
||||
|
||||
expect { subject.send(:get, prometheus_url) }
|
||||
expect { subject.send(:get, '/', {}) }
|
||||
.to raise_error(Gitlab::PrometheusError, "Network connection error")
|
||||
expect(req_stub).to have_been_requested
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue