Skip to content
Snippets Groups Projects
Commit 63a5d98a authored by Jose Ivan Vargas Lopez's avatar Jose Ivan Vargas Lopez
Browse files

Added specs

parent ad6ac17c
No related branches found
No related tags found
1 merge request!10994Added rescue block for the test method for the prometheus service
Loading
Loading
@@ -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)
Loading
Loading
Loading
Loading
@@ -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) }
Loading
Loading
Loading
Loading
@@ -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)),
Loading
Loading
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment