diff options
author | Jose Ivan Vargas <jvargas@gitlab.com> | 2017-05-02 16:53:49 -0500 |
---|---|---|
committer | Jose Ivan Vargas <jvargas@gitlab.com> | 2017-05-04 13:56:26 -0500 |
commit | 64e811957795293b647bda7aef23ffbd92082614 (patch) | |
tree | a988937a8af241c4e79c7d92e6da7c17bd09ddbd | |
parent | 63a5d98a7c921289e9b43f4b54c03614427f7eda (diff) | |
download | gitlab-ce-64e811957795293b647bda7aef23ffbd92082614.tar.gz |
Improved code styling and added a HTTParty rescue block
-rw-r--r-- | lib/gitlab/prometheus.rb | 2 | ||||
-rw-r--r-- | spec/lib/gitlab/prometheus_spec.rb | 34 | ||||
-rw-r--r-- | spec/models/project_services/prometheus_service_spec.rb | 5 | ||||
-rw-r--r-- | spec/support/prometheus_helpers.rb | 10 |
4 files changed, 29 insertions, 22 deletions
diff --git a/lib/gitlab/prometheus.rb b/lib/gitlab/prometheus.rb index 94541009100..aa9810c5c41 100644 --- a/lib/gitlab/prometheus.rb +++ b/lib/gitlab/prometheus.rb @@ -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) diff --git a/spec/lib/gitlab/prometheus_spec.rb b/spec/lib/gitlab/prometheus_spec.rb index d8683669518..8e187df6ca4 100644 --- a/spec/lib/gitlab/prometheus_spec.rb +++ b/spec/lib/gitlab/prometheus_spec.rb @@ -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 diff --git a/spec/models/project_services/prometheus_service_spec.rb b/spec/models/project_services/prometheus_service_spec.rb index 5ef1b53be15..f3126bc1e57 100644 --- a/spec/models/project_services/prometheus_service_spec.rb +++ b/spec/models/project_services/prometheus_service_spec.rb @@ -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 diff --git a/spec/support/prometheus_helpers.rb b/spec/support/prometheus_helpers.rb index 625a40e1154..a204365431b 100644 --- a/spec/support/prometheus_helpers.rb +++ b/spec/support/prometheus_helpers.rb @@ -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) |