Commit 63a5d98a authored by Jose Ivan Vargas's avatar Jose Ivan Vargas

Added specs

parent ad6ac17c
...@@ -49,11 +49,11 @@ module Gitlab ...@@ -49,11 +49,11 @@ module Gitlab
end end
def get(url) def get(url)
begin
handle_response(HTTParty.get(url)) handle_response(HTTParty.get(url))
rescue SocketError rescue SocketError
raise PrometheusError, "Can't connect to #{url}" raise PrometheusError, "Can't connect to #{url}"
end rescue OpenSSL::SSL::SSLError
raise PrometheusError, "#{url} contains invalid SSL data"
end end
def handle_response(response) def handle_response(response)
......
...@@ -49,6 +49,24 @@ describe Gitlab::Prometheus, lib: true do ...@@ -49,6 +49,24 @@ describe Gitlab::Prometheus, lib: true do
end end
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 describe '#query' do
let(:prometheus_query) { prometheus_cpu_query('env-slug') } let(:prometheus_query) { prometheus_cpu_query('env-slug') }
let(:query_url) { prometheus_query_url(prometheus_query) } let(:query_url) { prometheus_query_url(prometheus_query) }
......
...@@ -33,6 +33,16 @@ module PrometheusHelpers ...@@ -33,6 +33,16 @@ module PrometheusHelpers
}) })
end 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) def stub_all_prometheus_requests(environment_slug, body: nil, status: 200)
stub_prometheus_request( stub_prometheus_request(
prometheus_query_url(prometheus_memory_query(environment_slug)), prometheus_query_url(prometheus_memory_query(environment_slug)),
......
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