2017-03-27 05:22:43 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe Projects::DeploymentsController do
|
|
|
|
include ApiHelpers
|
|
|
|
|
|
|
|
let(:user) { create(:user) }
|
2017-08-02 15:55:11 -04:00
|
|
|
let(:project) { create(:project) }
|
2017-03-27 05:22:43 -04:00
|
|
|
let(:environment) { create(:environment, name: 'production', project: project) }
|
|
|
|
|
|
|
|
before do
|
2018-07-11 10:36:08 -04:00
|
|
|
project.add_maintainer(user)
|
2017-03-27 05:22:43 -04:00
|
|
|
|
|
|
|
sign_in(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'GET #index' do
|
2017-04-24 13:55:07 -04:00
|
|
|
it 'returns list of deployments from last 8 hours' do
|
2018-11-04 19:37:40 -05:00
|
|
|
create(:deployment, :success, environment: environment, created_at: 9.hours.ago)
|
|
|
|
create(:deployment, :success, environment: environment, created_at: 7.hours.ago)
|
|
|
|
create(:deployment, :success, environment: environment)
|
2017-03-27 05:22:43 -04:00
|
|
|
|
2018-12-17 17:52:17 -05:00
|
|
|
get :index, params: deployment_params(after: 8.hours.ago)
|
2017-03-27 05:22:43 -04:00
|
|
|
|
|
|
|
expect(response).to be_ok
|
|
|
|
|
|
|
|
expect(json_response['deployments'].count).to eq(2)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns a list with deployments information' do
|
2018-11-04 19:37:40 -05:00
|
|
|
create(:deployment, :success, environment: environment)
|
2017-03-27 05:22:43 -04:00
|
|
|
|
2018-12-17 17:52:17 -05:00
|
|
|
get :index, params: deployment_params
|
2017-03-27 05:22:43 -04:00
|
|
|
|
2017-04-24 13:55:07 -04:00
|
|
|
expect(response).to be_ok
|
|
|
|
expect(response).to match_response_schema('deployments')
|
2017-03-27 05:22:43 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-05-09 00:15:34 -04:00
|
|
|
describe 'GET #metrics' do
|
2018-11-04 19:37:40 -05:00
|
|
|
let(:deployment) { create(:deployment, :success, project: project, environment: environment) }
|
2017-05-09 00:15:34 -04:00
|
|
|
|
|
|
|
before do
|
|
|
|
allow(controller).to receive(:deployment).and_return(deployment)
|
|
|
|
end
|
2017-06-16 10:53:02 -04:00
|
|
|
|
2017-05-12 12:16:22 -04:00
|
|
|
context 'when metrics are disabled' do
|
2017-05-09 00:15:34 -04:00
|
|
|
before do
|
2017-05-12 12:16:22 -04:00
|
|
|
allow(deployment).to receive(:has_metrics?).and_return false
|
2017-05-09 00:15:34 -04:00
|
|
|
end
|
|
|
|
|
2017-05-12 12:16:22 -04:00
|
|
|
it 'responds with not found' do
|
2018-12-17 17:52:17 -05:00
|
|
|
get :metrics, params: deployment_params(id: deployment.id)
|
2017-05-12 12:16:22 -04:00
|
|
|
|
|
|
|
expect(response).to be_not_found
|
2017-05-09 00:15:34 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-05-12 12:16:22 -04:00
|
|
|
context 'when metrics are enabled' do
|
|
|
|
before do
|
|
|
|
allow(deployment).to receive(:has_metrics?).and_return true
|
2017-05-09 00:15:34 -04:00
|
|
|
end
|
|
|
|
|
2017-05-12 12:16:22 -04:00
|
|
|
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
|
2018-12-17 17:52:17 -05:00
|
|
|
get :metrics, params: deployment_params(id: deployment.id)
|
2017-10-19 14:28:19 -04:00
|
|
|
expect(response).to have_gitlab_http_status(204)
|
2017-05-12 12:16:22 -04:00
|
|
|
expect(response.body).to eq('')
|
|
|
|
end
|
2017-05-09 00:15:34 -04:00
|
|
|
end
|
|
|
|
|
2017-05-12 12:16:22 -04:00
|
|
|
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
|
2018-12-17 17:52:17 -05:00
|
|
|
get :metrics, params: deployment_params(id: deployment.id)
|
2017-05-12 12:16:22 -04:00
|
|
|
|
|
|
|
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
|
2018-12-17 17:52:17 -05:00
|
|
|
get :metrics, params: deployment_params(id: deployment.id)
|
2017-05-09 00:15:34 -04:00
|
|
|
|
2017-05-12 12:16:22 -04:00
|
|
|
expect(response).to be_not_found
|
|
|
|
end
|
2017-05-09 00:15:34 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-06-06 20:36:59 -04:00
|
|
|
describe 'GET #additional_metrics' do
|
2018-11-04 19:37:40 -05:00
|
|
|
let(:deployment) { create(:deployment, :success, project: project, environment: environment) }
|
2017-06-06 20:36:59 -04:00
|
|
|
|
|
|
|
before do
|
|
|
|
allow(controller).to receive(:deployment).and_return(deployment)
|
|
|
|
end
|
2017-06-16 10:53:02 -04:00
|
|
|
|
2017-06-06 20:36:59 -04:00
|
|
|
context 'when metrics are disabled' do
|
|
|
|
before do
|
|
|
|
allow(deployment).to receive(:has_metrics?).and_return false
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'responds with not found' do
|
2018-12-17 17:52:17 -05:00
|
|
|
get :metrics, params: deployment_params(id: deployment.id)
|
2017-06-06 20:36:59 -04:00
|
|
|
|
|
|
|
expect(response).to be_not_found
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when metrics are enabled' do
|
2018-02-23 15:33:33 -05:00
|
|
|
let(:prometheus_adapter) { double('prometheus_adapter', can_query?: true) }
|
2017-06-06 20:36:59 -04:00
|
|
|
|
|
|
|
before do
|
2018-02-23 15:33:33 -05:00
|
|
|
allow(deployment).to receive(:prometheus_adapter).and_return(prometheus_adapter)
|
2017-06-06 20:36:59 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when environment has no metrics' do
|
|
|
|
before do
|
|
|
|
expect(deployment).to receive(:additional_metrics).and_return({})
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns a empty response 204 response' do
|
2018-12-17 17:52:17 -05:00
|
|
|
get :additional_metrics, params: deployment_params(id: deployment.id, format: :json)
|
2017-10-19 14:28:19 -04:00
|
|
|
expect(response).to have_gitlab_http_status(204)
|
2017-06-06 20:36:59 -04:00
|
|
|
expect(response.body).to eq('')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when environment has some metrics' do
|
|
|
|
let(:empty_metrics) do
|
|
|
|
{
|
|
|
|
success: true,
|
|
|
|
metrics: {},
|
|
|
|
last_update: 42
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
expect(deployment).to receive(:additional_metrics).and_return(empty_metrics)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns a metrics JSON document' do
|
2018-12-17 17:52:17 -05:00
|
|
|
get :additional_metrics, params: deployment_params(id: deployment.id, format: :json)
|
2017-06-06 20:36:59 -04:00
|
|
|
|
|
|
|
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
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-05-09 00:15:34 -04:00
|
|
|
def deployment_params(opts = {})
|
|
|
|
opts.reverse_merge(namespace_id: project.namespace,
|
|
|
|
project_id: project,
|
|
|
|
environment_id: environment.id)
|
2017-03-27 05:22:43 -04:00
|
|
|
end
|
|
|
|
end
|