Commit 64e81195 authored by Jose Ivan Vargas's avatar Jose Ivan Vargas

Improved code styling and added a HTTParty rescue block

parent 63a5d98a
......@@ -54,6 +54,8 @@ module Gitlab
raise PrometheusError, "Can't connect to #{url}"
rescue OpenSSL::SSL::SSLError
raise PrometheusError, "#{url} contains invalid SSL data"
rescue HTTParty::Error
raise PrometheusError, "An error has ocurred"
end
def handle_response(response)
......
......@@ -49,21 +49,33 @@ describe Gitlab::Prometheus, lib: true do
end
end
describe 'failure to reach a prometheus url' do
prometheus_invalid_url = 'https://prometheus.invalid.example.com'
describe 'failure to reach a provided prometheus url' do
let(:prometheus_url) {"https://prometheus.invalid.example.com"}
it 'raises a Gitlab::PrometheusError error when a SocketError is rescued' do
req_stub = stub_prometheus_request_with_socket_exception(prometheus_invalid_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_invalid_url) }.to raise_error(Gitlab::PrometheusError, "Can't connect to #{prometheus_invalid_url}")
expect(req_stub).to have_been_requested
end
expect { subject.send(:get, prometheus_url) }
.to raise_error(Gitlab::PrometheusError, "Can't connect to #{prometheus_url}")
expect(req_stub).to have_been_requested
end
it 'raises a Gitlab::PrometheusError error when a SSLError is rescued' do
req_stub = stub_prometheus_request_with_ssl_exception(prometheus_invalid_url)
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_invalid_url) }.to raise_error(Gitlab::PrometheusError, "#{prometheus_invalid_url} contains invalid SSL data")
expect(req_stub).to have_been_requested
expect { subject.send(:get, prometheus_url) }
.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)
expect { subject.send(:get, prometheus_url) }
.to raise_error(Gitlab::PrometheusError, "An error has ocurred")
expect(req_stub).to have_been_requested
end
end
end
......
......@@ -93,12 +93,11 @@ describe PrometheusService, models: true, caching: true do
[404, 500].each do |status|
context "when Prometheus responds with #{status}" do
body_response = 'QUERY_FAILED'
before do
stub_all_prometheus_requests(environment.slug, status: status, body: body_response)
stub_all_prometheus_requests(environment.slug, status: status, body: "QUERY FAILED!")
end
it { is_expected.to eq(success: false, result: %(#{status} - \"#{body_response}\")) }
it { is_expected.to eq(success: false, result: %(#{status} - "QUERY FAILED!")) }
end
end
end
......
......@@ -33,14 +33,8 @@ module PrometheusHelpers
})
end
def stub_prometheus_request_with_socket_exception(url)
WebMock.stub_request(:get, url)
.to_raise(SocketError)
end
def stub_prometheus_request_with_ssl_exception(url)
WebMock.stub_request(:get, url)
.to_raise(OpenSSL::SSL::SSLError)
def stub_prometheus_request_with_exception(url, exception_type)
WebMock.stub_request(:get, url).to_raise(exception_type)
end
def stub_all_prometheus_requests(environment_slug, body: nil, status: 200)
......
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