Commit 359d00cd authored by rpereira2's avatar rpereira2 Committed by Peter Leitzen

Add spec for RestClient raising exception

parent 90be98aa
...@@ -270,7 +270,7 @@ describe Gitlab::PrometheusClient do ...@@ -270,7 +270,7 @@ describe Gitlab::PrometheusClient do
end end
describe 'proxy' do describe 'proxy' do
context 'query' do context 'get API' 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) }
...@@ -278,7 +278,8 @@ describe Gitlab::PrometheusClient do ...@@ -278,7 +278,8 @@ describe Gitlab::PrometheusClient do
Timecop.freeze { example.run } Timecop.freeze { example.run }
end end
it 'returns full response from the API call' do context 'when response status code is 200' do
it 'returns response object' do
req_stub = stub_prometheus_request(query_url, body: prometheus_value_body('vector')) req_stub = stub_prometheus_request(query_url, body: prometheus_value_body('vector'))
response = subject.proxy('query', { query: prometheus_query }) response = subject.proxy('query', { query: prometheus_query })
...@@ -296,40 +297,29 @@ describe Gitlab::PrometheusClient do ...@@ -296,40 +297,29 @@ describe Gitlab::PrometheusClient do
end end
end end
context 'query_range' do context 'when response status code is not 200' do
let(:prometheus_query) { prometheus_memory_query('env-slug') } it 'returns response object' do
let(:query_url) { prometheus_query_range_url(prometheus_query, start: 2.hours.ago) } req_stub = stub_prometheus_request(query_url, status: 400, body: { error: 'error' })
around do |example|
Timecop.freeze { example.run }
end
it 'returns full response' do response = subject.proxy('query', { query: prometheus_query })
req_stub = stub_prometheus_request(query_url, body: prometheus_values_body('vector'))
response = subject.proxy('query_range', {
query: prometheus_query,
start: 2.hours.ago.to_f,
end: Time.now.to_f,
step: 60
})
json_response = JSON.parse(response.body) json_response = JSON.parse(response.body)
expect(response.code).to eq(200)
expect(json_response).to eq({
"status" => "success",
"data" => {
"resultType" => "vector",
"result" => [{
"metric" => {},
"values" => [
[1488758662.506, "0.00002996364761904785"],
[1488758722.506, "0.00003090239047619091"]
]
}]
}
})
expect(req_stub).to have_been_requested expect(req_stub).to have_been_requested
expect(response.code).to eq(400)
expect(json_response).to eq('error' => 'error')
end
end
context 'when RestClient::Exception is raised' do
before do
stub_prometheus_request_with_exception(query_url, RestClient::Exception)
end
it 'raises PrometheusClient::Error' do
expect { subject.proxy('query', { query: prometheus_query }) }.to(
raise_error(Gitlab::PrometheusClient::Error, 'Network connection error')
)
end
end end
end end
end end
......
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