summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobert Speicher <robert@gitlab.com>2017-05-05 13:50:03 +0000
committerRobert Speicher <robert@gitlab.com>2017-05-05 13:50:03 +0000
commit10c1bf2d77fd0ab21309d0b136cbc0ac11f56c77 (patch)
treeb171476c60bec5cacffbf1b65b09f83c8d6ce44f
parentfaf2ce89cd4eafdccb78823cf1a32c338a8d9a79 (diff)
parent6eb9e981c61969a904ccbae2c5f52f562b02d199 (diff)
downloadgitlab-ce-10c1bf2d77fd0ab21309d0b136cbc0ac11f56c77.tar.gz
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
-rw-r--r--changelogs/unreleased/prometheus-integration-test-setting-fix.yml4
-rw-r--r--lib/gitlab/prometheus.rb6
-rw-r--r--spec/lib/gitlab/prometheus_spec.rb30
-rw-r--r--spec/models/project_services/prometheus_service_spec.rb2
-rw-r--r--spec/support/prometheus_helpers.rb4
5 files changed, 45 insertions, 1 deletions
diff --git a/changelogs/unreleased/prometheus-integration-test-setting-fix.yml b/changelogs/unreleased/prometheus-integration-test-setting-fix.yml
new file mode 100644
index 00000000000..45b7c2263e6
--- /dev/null
+++ b/changelogs/unreleased/prometheus-integration-test-setting-fix.yml
@@ -0,0 +1,4 @@
+---
+title: Prevent 500 errors caused by testing the Prometheus service
+merge_request: 10994
+author:
diff --git a/lib/gitlab/prometheus.rb b/lib/gitlab/prometheus.rb
index 62239779454..8827507955d 100644
--- a/lib/gitlab/prometheus.rb
+++ b/lib/gitlab/prometheus.rb
@@ -50,6 +50,12 @@ module Gitlab
def 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
def handle_response(response)
diff --git a/spec/lib/gitlab/prometheus_spec.rb b/spec/lib/gitlab/prometheus_spec.rb
index 280264188e2..fc453a2704b 100644
--- a/spec/lib/gitlab/prometheus_spec.rb
+++ b/spec/lib/gitlab/prometheus_spec.rb
@@ -49,6 +49,36 @@ describe Gitlab::Prometheus, lib: true do
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
let(:prometheus_query) { prometheus_cpu_query('env-slug') }
let(:query_url) { prometheus_query_url(prometheus_query) }
diff --git a/spec/models/project_services/prometheus_service_spec.rb b/spec/models/project_services/prometheus_service_spec.rb
index d15079b686b..f3126bc1e57 100644
--- a/spec/models/project_services/prometheus_service_spec.rb
+++ b/spec/models/project_services/prometheus_service_spec.rb
@@ -94,7 +94,7 @@ describe PrometheusService, models: true, caching: true do
[404, 500].each do |status|
context "when Prometheus responds with #{status}" 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
it { is_expected.to eq(success: false, result: %(#{status} - "QUERY FAILED!")) }
diff --git a/spec/support/prometheus_helpers.rb b/spec/support/prometheus_helpers.rb
index cc79b11616a..a204365431b 100644
--- a/spec/support/prometheus_helpers.rb
+++ b/spec/support/prometheus_helpers.rb
@@ -33,6 +33,10 @@ module PrometheusHelpers
})
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)
stub_prometheus_request(
prometheus_query_url(prometheus_memory_query(environment_slug)),