Commit 10c1bf2d authored by Robert Speicher's avatar Robert Speicher

Merge branch 'prometheus-integration-test-setting-fix' into 'master'

Added rescue block for the test method for the prometheus service

Closes #31451

See merge request !10994
parents faf2ce89 6eb9e981
---
title: Prevent 500 errors caused by testing the Prometheus service
merge_request: 10994
author:
...@@ -50,6 +50,12 @@ module Gitlab ...@@ -50,6 +50,12 @@ module Gitlab
def get(url) def get(url)
handle_response(HTTParty.get(url)) 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"
rescue HTTParty::Error
raise PrometheusError, "Network connection error"
end end
def handle_response(response) def handle_response(response)
......
...@@ -49,6 +49,36 @@ describe Gitlab::Prometheus, lib: true do ...@@ -49,6 +49,36 @@ describe Gitlab::Prometheus, lib: true do
end end
end end
describe 'failure to reach a provided prometheus url' do
let(:prometheus_url) {"https://prometheus.invalid.example.com"}
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_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_exception(prometheus_url, OpenSSL::SSL::SSLError)
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, "Network connection error")
expect(req_stub).to have_been_requested
end
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) }
......
...@@ -94,7 +94,7 @@ describe PrometheusService, models: true, caching: true do ...@@ -94,7 +94,7 @@ describe PrometheusService, models: true, caching: true do
[404, 500].each do |status| [404, 500].each do |status|
context "when Prometheus responds with #{status}" do context "when Prometheus responds with #{status}" do
before do before do
stub_all_prometheus_requests(environment.slug, status: status, body: 'QUERY FAILED!') stub_all_prometheus_requests(environment.slug, status: status, body: "QUERY FAILED!")
end end
it { is_expected.to eq(success: false, result: %(#{status} - "QUERY FAILED!")) } it { is_expected.to eq(success: false, result: %(#{status} - "QUERY FAILED!")) }
......
...@@ -33,6 +33,10 @@ module PrometheusHelpers ...@@ -33,6 +33,10 @@ module PrometheusHelpers
}) })
end end
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) 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