diff options
author | Alex Groleau <agroleau@gitlab.com> | 2019-08-27 12:41:39 -0400 |
---|---|---|
committer | Alex Groleau <agroleau@gitlab.com> | 2019-08-27 12:41:39 -0400 |
commit | aa01f092829facd1044ad02f334422b7dbdc8b0e (patch) | |
tree | a754bf2497820432df7da0f2108bb7527a8dd7b8 /spec/lib/gitlab/prometheus_client_spec.rb | |
parent | a1d9c9994a9a4d79b824c3fd9322688303ac8b03 (diff) | |
parent | 6b10779053ff4233c7a64c5ab57754fce63f6710 (diff) | |
download | gitlab-ce-runner-metrics-extractor.tar.gz |
Merge branch 'master' of gitlab_gitlab:gitlab-org/gitlab-cerunner-metrics-extractor
Diffstat (limited to 'spec/lib/gitlab/prometheus_client_spec.rb')
-rw-r--r-- | spec/lib/gitlab/prometheus_client_spec.rb | 49 |
1 files changed, 38 insertions, 11 deletions
diff --git a/spec/lib/gitlab/prometheus_client_spec.rb b/spec/lib/gitlab/prometheus_client_spec.rb index f15ae83a02c..86a1c14ed3f 100644 --- a/spec/lib/gitlab/prometheus_client_spec.rb +++ b/spec/lib/gitlab/prometheus_client_spec.rb @@ -1,9 +1,11 @@ +# frozen_string_literal: true + require 'spec_helper' describe Gitlab::PrometheusClient do include PrometheusHelpers - subject { described_class.new(RestClient::Resource.new('https://prometheus.example.com')) } + subject { described_class.new('https://prometheus.example.com') } describe '#ping' do it 'issues a "query" request to the API endpoint' do @@ -79,8 +81,16 @@ describe Gitlab::PrometheusClient do expect(req_stub).to have_been_requested end - it 'raises a Gitlab::PrometheusClient::Error error when a RestClient::Exception is rescued' do - req_stub = stub_prometheus_request_with_exception(prometheus_url, RestClient::Exception) + it 'raises a Gitlab::PrometheusClient::Error error when a Gitlab::HTTP::ResponseError is rescued' do + req_stub = stub_prometheus_request_with_exception(prometheus_url, Gitlab::HTTP::ResponseError) + + expect { subject } + .to raise_error(Gitlab::PrometheusClient::Error, "Network connection error") + expect(req_stub).to have_been_requested + end + + it 'raises a Gitlab::PrometheusClient::Error error when a Gitlab::HTTP::ResponseError with a code is rescued' do + req_stub = stub_prometheus_request_with_exception(prometheus_url, Gitlab::HTTP::ResponseError.new(code: 400)) expect { subject } .to raise_error(Gitlab::PrometheusClient::Error, "Network connection error") @@ -89,13 +99,13 @@ describe Gitlab::PrometheusClient do end context 'ping' do - subject { described_class.new(RestClient::Resource.new(prometheus_url)).ping } + subject { described_class.new(prometheus_url).ping } it_behaves_like 'exceptions are raised' end context 'proxy' do - subject { described_class.new(RestClient::Resource.new(prometheus_url)).proxy('query', { query: '1' }) } + subject { described_class.new(prometheus_url).proxy('query', { query: '1' }) } it_behaves_like 'exceptions are raised' end @@ -310,15 +320,32 @@ describe Gitlab::PrometheusClient do end end - context 'when RestClient::Exception is raised' do + context 'when Gitlab::HTTP::ResponseError is raised' do before do - stub_prometheus_request_with_exception(query_url, RestClient::Exception) + stub_prometheus_request_with_exception(query_url, response_error) + end + + context "without response code" do + let(:response_error) { Gitlab::HTTP::ResponseError } + it 'raises PrometheusClient::Error' do + expect { subject.proxy('query', { query: prometheus_query }) }.to( + raise_error(Gitlab::PrometheusClient::Error, 'Network connection error') + ) + end end - it 'raises PrometheusClient::Error' do - expect { subject.proxy('query', { query: prometheus_query }) }.to( - raise_error(Gitlab::PrometheusClient::Error, 'Network connection error') - ) + context "with response code" do + let(:response_error) do + response = Net::HTTPResponse.new(1.1, 400, '{}sumpthin') + allow(response).to receive(:body) { '{}' } + Gitlab::HTTP::ResponseError.new(response) + end + + it 'raises Gitlab::PrometheusClient::QueryError' do + expect { subject.proxy('query', { query: prometheus_query }) }.to( + raise_error(Gitlab::PrometheusClient::QueryError, 'Bad data received') + ) + end end end end |