Commit f38779c6 authored by Pawel Chojnacki's avatar Pawel Chojnacki

Respond with 404 when metrics are not enabled or deployment_metrics is not implemented

parent 2a89d2c9
......@@ -11,13 +11,15 @@ class Projects::DeploymentsController < Projects::ApplicationController
end
def metrics
return render_404 unless deployment.has_metrics?
@metrics = deployment.metrics
if @metrics&.any?
render json: @metrics, status: :ok
else
head :no_content
end
rescue NotImplementedError
render_404
end
private
......
......@@ -42,39 +42,68 @@ describe Projects::DeploymentsController do
before do
allow(controller).to receive(:deployment).and_return(deployment)
end
context 'when environment has no metrics' do
context 'when metrics are disabled' do
before do
expect(deployment).to receive(:metrics).and_return(nil)
allow(deployment).to receive(:has_metrics?).and_return false
end
it 'returns a empty response 204 resposne' do
it 'responds with not found' do
get :metrics, deployment_params(id: deployment.id)
expect(response).to have_http_status(204)
expect(response.body).to eq('')
expect(response).to be_not_found
end
end
context 'when environment has some metrics' do
let(:empty_metrics) do
{
success: true,
metrics: {},
last_update: 42
}
context 'when metrics are enabled' do
before do
allow(deployment).to receive(:has_metrics?).and_return true
end
before do
expect(deployment).to receive(:metrics).and_return(empty_metrics)
context 'when environment has no metrics' do
before do
expect(deployment).to receive(:metrics).and_return(nil)
end
it 'returns a empty response 204 resposne' do
get :metrics, deployment_params(id: deployment.id)
expect(response).to have_http_status(204)
expect(response.body).to eq('')
end
end
it 'returns a metrics JSON document' do
get :metrics, deployment_params(id: deployment.id)
context 'when environment has some metrics' do
let(:empty_metrics) do
{
success: true,
metrics: {},
last_update: 42
}
end
before do
expect(deployment).to receive(:metrics).and_return(empty_metrics)
end
it 'returns a metrics JSON document' do
get :metrics, deployment_params(id: deployment.id)
expect(response).to be_ok
expect(json_response['success']).to be(true)
expect(json_response['metrics']).to eq({})
expect(json_response['last_update']).to eq(42)
end
end
context 'when metrics service does not implement deployment metrics' do
before do
allow(deployment).to receive(:metrics).and_raise(NotImplementedError)
end
it 'responds with not found' do
get :metrics, deployment_params(id: deployment.id)
expect(response).to be_ok
expect(json_response['success']).to be(true)
expect(json_response['metrics']).to eq({})
expect(json_response['last_update']).to eq(42)
expect(response).to be_not_found
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