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