BigW Consortium Gitlab

Commit 63a5d98a by Jose Ivan Vargas

Added specs

parent ad6ac17c
......@@ -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)
......
......@@ -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) }
......
......@@ -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)),
......
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