diff options
-rw-r--r-- | lib/gitlab/prometheus.rb | 10 | ||||
-rw-r--r-- | spec/lib/gitlab/prometheus_spec.rb | 18 | ||||
-rw-r--r-- | spec/support/prometheus_helpers.rb | 10 |
3 files changed, 33 insertions, 5 deletions
diff --git a/lib/gitlab/prometheus.rb b/lib/gitlab/prometheus.rb index 37afec5e7df..94541009100 100644 --- a/lib/gitlab/prometheus.rb +++ b/lib/gitlab/prometheus.rb @@ -49,11 +49,11 @@ module Gitlab end def get(url) - begin - handle_response(HTTParty.get(url)) - rescue SocketError - raise PrometheusError, "Can't connect to #{url}" - end + handle_response(HTTParty.get(url)) + rescue SocketError + raise PrometheusError, "Can't connect to #{url}" + rescue OpenSSL::SSL::SSLError + raise PrometheusError, "#{url} contains invalid SSL data" end def handle_response(response) diff --git a/spec/lib/gitlab/prometheus_spec.rb b/spec/lib/gitlab/prometheus_spec.rb index 280264188e2..d8683669518 100644 --- a/spec/lib/gitlab/prometheus_spec.rb +++ b/spec/lib/gitlab/prometheus_spec.rb @@ -49,6 +49,24 @@ describe Gitlab::Prometheus, lib: true do end end + describe 'failure to reach a prometheus url' do + prometheus_invalid_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) + + 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 + + it 'raises a Gitlab::PrometheusError error when a SSLError is rescued' do + req_stub = stub_prometheus_request_with_ssl_exception(prometheus_invalid_url) + + 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 + end + end + describe '#query' do let(:prometheus_query) { prometheus_cpu_query('env-slug') } let(:query_url) { prometheus_query_url(prometheus_query) } diff --git a/spec/support/prometheus_helpers.rb b/spec/support/prometheus_helpers.rb index cc79b11616a..625a40e1154 100644 --- a/spec/support/prometheus_helpers.rb +++ b/spec/support/prometheus_helpers.rb @@ -33,6 +33,16 @@ 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) + end + def stub_all_prometheus_requests(environment_slug, body: nil, status: 200) stub_prometheus_request( prometheus_query_url(prometheus_memory_query(environment_slug)), |