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