2019-07-31 22:56:46 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
RSpec.describe Projects::DeploymentsController do
|
2017-08-17 22:00:37 +05:30
|
|
|
include ApiHelpers
|
|
|
|
|
|
|
|
let(:user) { create(:user) }
|
2020-03-13 15:44:24 +05:30
|
|
|
let(:project) { create(:project, :repository) }
|
2017-08-17 22:00:37 +05:30
|
|
|
let(:environment) { create(:environment, name: 'production', project: project) }
|
|
|
|
|
|
|
|
before do
|
2018-11-18 11:00:15 +05:30
|
|
|
project.add_maintainer(user)
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
sign_in(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'GET #index' do
|
|
|
|
it 'returns list of deployments from last 8 hours' do
|
2018-12-13 13:39:08 +05:30
|
|
|
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-08-17 22:00:37 +05:30
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
get :index, params: deployment_params(after: 8.hours.ago)
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
expect(response).to be_ok
|
|
|
|
|
|
|
|
expect(json_response['deployments'].count).to eq(2)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns a list with deployments information' do
|
2018-12-13 13:39:08 +05:30
|
|
|
create(:deployment, :success, environment: environment)
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
get :index, params: deployment_params
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
expect(response).to be_ok
|
|
|
|
expect(response).to match_response_schema('deployments')
|
|
|
|
end
|
2020-07-28 23:09:34 +05:30
|
|
|
|
|
|
|
context 'anonymous user' do
|
|
|
|
let(:anonymous_user) { create(:user) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
sign_in(anonymous_user)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'project and metrics dashboard are public' do
|
|
|
|
before do
|
|
|
|
project.update!(
|
|
|
|
visibility_level: Gitlab::VisibilityLevel::PUBLIC,
|
|
|
|
project_feature_attributes: {
|
|
|
|
metrics_dashboard_access_level: Gitlab::VisibilityLevel::PUBLIC
|
|
|
|
}
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns a list with deployments information' do
|
|
|
|
create(:deployment, :success, environment: environment)
|
|
|
|
|
|
|
|
get :index, params: deployment_params
|
|
|
|
|
|
|
|
expect(response).to be_ok
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'project and metrics dashboard are private' do
|
|
|
|
before do
|
|
|
|
project.update!(
|
|
|
|
visibility_level: Gitlab::VisibilityLevel::PRIVATE,
|
|
|
|
project_feature_attributes: {
|
|
|
|
metrics_dashboard_access_level: Gitlab::VisibilityLevel::PRIVATE
|
|
|
|
}
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'responds with not found' do
|
|
|
|
create(:deployment, :success, environment: environment)
|
|
|
|
|
|
|
|
get :index, params: deployment_params
|
|
|
|
|
|
|
|
expect(response).to be_not_found
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
describe 'GET #metrics' do
|
2018-12-13 13:39:08 +05:30
|
|
|
let(:deployment) { create(:deployment, :success, project: project, environment: environment) }
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
context 'when metrics are disabled' do
|
|
|
|
it 'responds with not found' do
|
2019-09-30 21:07:59 +05:30
|
|
|
get :metrics, params: deployment_params(id: deployment.to_param)
|
2017-09-10 17:25:29 +05:30
|
|
|
|
|
|
|
expect(response).to be_not_found
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
context 'when metrics are enabled' do
|
|
|
|
context 'when environment has no metrics' do
|
|
|
|
before do
|
2019-09-30 21:07:59 +05:30
|
|
|
expect_next_instance_of(DeploymentMetrics) do |deployment_metrics|
|
|
|
|
allow(deployment_metrics).to receive(:has_metrics?).and_return(true)
|
|
|
|
|
|
|
|
expect(deployment_metrics).to receive(:metrics).and_return(nil)
|
|
|
|
end
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
it 'returns an empty 204 response' do
|
2019-09-30 21:07:59 +05:30
|
|
|
get :metrics, params: deployment_params(id: deployment.to_param)
|
2020-03-13 15:44:24 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:no_content)
|
2017-09-10 17:25:29 +05:30
|
|
|
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
|
|
|
|
|
2019-12-21 20:55:43 +05:30
|
|
|
it 'returns a metrics JSON document' do
|
2019-09-30 21:07:59 +05:30
|
|
|
expect_next_instance_of(DeploymentMetrics) do |deployment_metrics|
|
|
|
|
allow(deployment_metrics).to receive(:has_metrics?).and_return(true)
|
|
|
|
|
|
|
|
expect(deployment_metrics).to receive(:metrics).and_return(empty_metrics)
|
|
|
|
end
|
2017-09-10 17:25:29 +05:30
|
|
|
|
2019-09-30 21:07:59 +05:30
|
|
|
get :metrics, params: deployment_params(id: deployment.to_param)
|
2017-09-10 17:25:29 +05:30
|
|
|
|
|
|
|
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
|
2019-12-21 20:55:43 +05:30
|
|
|
|
|
|
|
it 'returns a 404 if the deployment failed' do
|
|
|
|
failed_deployment = create(
|
|
|
|
:deployment,
|
|
|
|
:failed,
|
|
|
|
project: project,
|
|
|
|
environment: environment
|
|
|
|
)
|
|
|
|
|
|
|
|
get :metrics, params: deployment_params(id: failed_deployment.to_param)
|
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:not_found)
|
2019-12-21 20:55:43 +05:30
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'GET #additional_metrics' do
|
2018-12-13 13:39:08 +05:30
|
|
|
let(:deployment) { create(:deployment, :success, project: project, environment: environment) }
|
2017-09-10 17:25:29 +05:30
|
|
|
|
|
|
|
context 'when metrics are disabled' do
|
|
|
|
it 'responds with not found' do
|
2019-09-30 21:07:59 +05:30
|
|
|
get :metrics, params: deployment_params(id: deployment.to_param)
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
expect(response).to be_not_found
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when metrics are enabled' do
|
|
|
|
context 'when environment has no metrics' do
|
|
|
|
before do
|
2019-09-30 21:07:59 +05:30
|
|
|
expect_next_instance_of(DeploymentMetrics) do |deployment_metrics|
|
|
|
|
allow(deployment_metrics).to receive(:has_metrics?).and_return(true)
|
|
|
|
|
|
|
|
expect(deployment_metrics).to receive(:additional_metrics).and_return({})
|
|
|
|
end
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns a empty response 204 response' do
|
2019-09-30 21:07:59 +05:30
|
|
|
get :additional_metrics, params: deployment_params(id: deployment.to_param, format: :json)
|
2020-03-13 15:44:24 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:no_content)
|
2017-09-10 17:25:29 +05:30
|
|
|
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
|
2019-09-30 21:07:59 +05:30
|
|
|
expect_next_instance_of(DeploymentMetrics) do |deployment_metrics|
|
|
|
|
allow(deployment_metrics).to receive(:has_metrics?).and_return(true)
|
|
|
|
|
|
|
|
expect(deployment_metrics).to receive(:additional_metrics).and_return(empty_metrics)
|
|
|
|
end
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns a metrics JSON document' do
|
2019-09-30 21:07:59 +05:30
|
|
|
get :additional_metrics, params: deployment_params(id: deployment.to_param, format: :json)
|
2017-09-10 17:25:29 +05:30
|
|
|
|
|
|
|
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
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def deployment_params(opts = {})
|
2023-05-27 22:25:52 +05:30
|
|
|
opts.reverse_merge(namespace_id: project.namespace, project_id: project, environment_id: environment.id)
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|