2016-06-14 07:51:12 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe CreateDeploymentService, services: true do
|
2016-06-14 08:44:09 -04:00
|
|
|
let(:project) { create(:empty_project) }
|
2016-06-14 07:51:12 -04:00
|
|
|
let(:user) { create(:user) }
|
|
|
|
|
|
|
|
let(:service) { described_class.new(project, user, params) }
|
|
|
|
|
|
|
|
describe '#execute' do
|
2016-10-17 11:10:26 -04:00
|
|
|
let(:options) { nil }
|
2016-06-14 07:51:12 -04:00
|
|
|
let(:params) do
|
|
|
|
{ environment: 'production',
|
|
|
|
ref: 'master',
|
2016-06-15 07:00:13 -04:00
|
|
|
tag: false,
|
2016-06-14 08:44:09 -04:00
|
|
|
sha: '97de212e80737a608d939f648d959671fb0a0142',
|
2016-10-17 11:10:26 -04:00
|
|
|
options: options
|
2016-06-14 07:51:12 -04:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
subject { service.execute }
|
|
|
|
|
|
|
|
context 'when no environments exist' do
|
|
|
|
it 'does create a new environment' do
|
|
|
|
expect { subject }.to change { Environment.count }.by(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does create a deployment' do
|
|
|
|
expect(subject).to be_persisted
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when environment exist' do
|
2016-10-17 11:10:26 -04:00
|
|
|
let!(:environment) { create(:environment, project: project, name: 'production') }
|
2016-06-14 07:51:12 -04:00
|
|
|
|
|
|
|
it 'does not create a new environment' do
|
|
|
|
expect { subject }.not_to change { Environment.count }
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does create a deployment' do
|
|
|
|
expect(subject).to be_persisted
|
|
|
|
end
|
2016-10-17 11:10:26 -04:00
|
|
|
|
|
|
|
context 'and start action is defined' do
|
|
|
|
let(:options) { { action: 'start' } }
|
|
|
|
|
|
|
|
context 'and environment is stopped' do
|
|
|
|
before do
|
|
|
|
environment.stop
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'makes environment available' do
|
|
|
|
subject
|
|
|
|
|
|
|
|
expect(environment.reload).to be_available
|
|
|
|
end
|
|
|
|
|
2016-10-17 15:06:10 -04:00
|
|
|
it 'does create a deployment' do
|
|
|
|
expect(subject).to be_persisted
|
2016-10-17 11:10:26 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'and stop action is defined' do
|
|
|
|
let(:options) { { action: 'stop' } }
|
|
|
|
|
|
|
|
context 'and environment is available' do
|
|
|
|
before do
|
|
|
|
environment.start
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'makes environment stopped' do
|
|
|
|
subject
|
|
|
|
|
|
|
|
expect(environment.reload).to be_stopped
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not create a deployment' do
|
|
|
|
expect(subject).to be_nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-06-14 07:51:12 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'for environment with invalid name' do
|
|
|
|
let(:params) do
|
2016-09-19 06:44:10 -04:00
|
|
|
{ environment: 'name,with,commas',
|
2016-06-14 07:51:12 -04:00
|
|
|
ref: 'master',
|
2016-06-15 07:00:13 -04:00
|
|
|
tag: false,
|
2016-06-14 08:44:09 -04:00
|
|
|
sha: '97de212e80737a608d939f648d959671fb0a0142',
|
2016-06-14 07:51:12 -04:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not create a new environment' do
|
|
|
|
expect { subject }.not_to change { Environment.count }
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not create a deployment' do
|
2016-10-17 15:06:10 -04:00
|
|
|
expect(subject).to be_nil
|
2016-06-14 07:51:12 -04:00
|
|
|
end
|
|
|
|
end
|
2016-09-14 16:32:11 -04:00
|
|
|
|
|
|
|
context 'when variables are used' do
|
|
|
|
let(:params) do
|
|
|
|
{ environment: 'review-apps/$CI_BUILD_REF_NAME',
|
|
|
|
ref: 'master',
|
|
|
|
tag: false,
|
|
|
|
sha: '97de212e80737a608d939f648d959671fb0a0142',
|
|
|
|
options: {
|
2016-09-14 17:00:15 -04:00
|
|
|
name: 'review-apps/$CI_BUILD_REF_NAME',
|
|
|
|
url: 'http://$CI_BUILD_REF_NAME.review-apps.gitlab.com'
|
2016-09-14 16:32:11 -04:00
|
|
|
},
|
|
|
|
variables: [
|
|
|
|
{ key: 'CI_BUILD_REF_NAME', value: 'feature-review-apps' }
|
|
|
|
]
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does create a new environment' do
|
|
|
|
expect { subject }.to change { Environment.count }.by(1)
|
|
|
|
|
|
|
|
expect(subject.environment.name).to eq('review-apps/feature-review-apps')
|
|
|
|
expect(subject.environment.external_url).to eq('http://feature-review-apps.review-apps.gitlab.com')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does create a new deployment' do
|
2016-09-14 17:00:15 -04:00
|
|
|
expect(subject).to be_persisted
|
2016-09-14 16:32:11 -04:00
|
|
|
end
|
2016-10-17 11:10:26 -04:00
|
|
|
|
|
|
|
context 'and environment exist' do
|
2016-10-17 15:06:10 -04:00
|
|
|
let!(:environment) { create(:environment, project: project, name: 'review-apps/feature-review-apps') }
|
|
|
|
|
2016-10-17 11:10:26 -04:00
|
|
|
it 'does not create a new environment' do
|
|
|
|
expect { subject }.not_to change { Environment.count }
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'updates external url' do
|
|
|
|
subject
|
|
|
|
|
|
|
|
expect(subject.environment.name).to eq('review-apps/feature-review-apps')
|
|
|
|
expect(subject.environment.external_url).to eq('http://feature-review-apps.review-apps.gitlab.com')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does create a new deployment' do
|
|
|
|
expect(subject).to be_persisted
|
|
|
|
end
|
|
|
|
end
|
2016-09-14 16:32:11 -04:00
|
|
|
end
|
2016-10-14 04:19:16 -04:00
|
|
|
|
|
|
|
context 'when project was removed' do
|
|
|
|
let(:project) { nil }
|
|
|
|
|
|
|
|
it 'does not create deployment or environment' do
|
|
|
|
expect { subject }.not_to raise_error
|
|
|
|
|
|
|
|
expect(Environment.count).to be_zero
|
|
|
|
expect(Deployment.count).to be_zero
|
|
|
|
end
|
|
|
|
end
|
2016-06-14 07:51:12 -04:00
|
|
|
end
|
2016-09-14 16:32:11 -04:00
|
|
|
|
2016-06-14 08:44:09 -04:00
|
|
|
describe 'processing of builds' do
|
|
|
|
let(:environment) { nil }
|
2016-10-14 04:19:16 -04:00
|
|
|
|
2016-06-14 08:44:09 -04:00
|
|
|
shared_examples 'does not create environment and deployment' do
|
|
|
|
it 'does not create a new environment' do
|
|
|
|
expect { subject }.not_to change { Environment.count }
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not create a new deployment' do
|
|
|
|
expect { subject }.not_to change { Deployment.count }
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not call a service' do
|
|
|
|
expect_any_instance_of(described_class).not_to receive(:execute)
|
|
|
|
subject
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples 'does create environment and deployment' do
|
|
|
|
it 'does create a new environment' do
|
|
|
|
expect { subject }.to change { Environment.count }.by(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does create a new deployment' do
|
|
|
|
expect { subject }.to change { Deployment.count }.by(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does call a service' do
|
|
|
|
expect_any_instance_of(described_class).to receive(:execute)
|
|
|
|
subject
|
|
|
|
end
|
2016-07-15 10:17:46 -04:00
|
|
|
|
|
|
|
it 'is set as deployable' do
|
|
|
|
subject
|
|
|
|
|
|
|
|
expect(Deployment.last.deployable).to eq(deployable)
|
|
|
|
end
|
2016-09-14 17:00:15 -04:00
|
|
|
|
|
|
|
it 'create environment has URL set' do
|
|
|
|
subject
|
|
|
|
|
|
|
|
expect(Deployment.last.environment.external_url).not_to be_nil
|
|
|
|
end
|
2016-06-14 08:44:09 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'without environment specified' do
|
|
|
|
let(:build) { create(:ci_build, project: project) }
|
2016-10-14 04:19:16 -04:00
|
|
|
|
2016-06-14 08:44:09 -04:00
|
|
|
it_behaves_like 'does not create environment and deployment' do
|
|
|
|
subject { build.success }
|
|
|
|
end
|
|
|
|
end
|
2016-10-14 04:19:16 -04:00
|
|
|
|
2016-06-14 08:44:09 -04:00
|
|
|
context 'when environment is specified' do
|
|
|
|
let(:pipeline) { create(:ci_pipeline, project: project) }
|
2016-09-14 17:00:15 -04:00
|
|
|
let(:build) { create(:ci_build, pipeline: pipeline, environment: 'production', options: options) }
|
|
|
|
let(:options) do
|
|
|
|
{ environment: { name: 'production', url: 'http://gitlab.com' } }
|
|
|
|
end
|
2016-06-14 08:44:09 -04:00
|
|
|
|
|
|
|
context 'when build succeeds' do
|
|
|
|
it_behaves_like 'does create environment and deployment' do
|
2016-07-15 10:17:46 -04:00
|
|
|
let(:deployable) { build }
|
|
|
|
|
2016-06-14 08:44:09 -04:00
|
|
|
subject { build.success }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when build fails' do
|
|
|
|
it_behaves_like 'does not create environment and deployment' do
|
|
|
|
subject { build.drop }
|
|
|
|
end
|
|
|
|
end
|
2016-07-15 10:17:46 -04:00
|
|
|
|
|
|
|
context 'when build is retried' do
|
|
|
|
it_behaves_like 'does create environment and deployment' do
|
2017-02-13 09:12:08 -05:00
|
|
|
before do
|
2017-02-15 05:12:07 -05:00
|
|
|
project.add_developer(user)
|
2017-02-13 09:12:08 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
let(:deployable) { Ci::Build.retry(build, user) }
|
2016-07-15 10:17:46 -04:00
|
|
|
|
|
|
|
subject { deployable.success }
|
|
|
|
end
|
|
|
|
end
|
2016-06-14 08:44:09 -04:00
|
|
|
end
|
|
|
|
end
|
2016-09-19 03:26:25 -04:00
|
|
|
|
|
|
|
describe "merge request metrics" do
|
|
|
|
let(:params) do
|
|
|
|
{
|
|
|
|
environment: 'production',
|
|
|
|
ref: 'master',
|
|
|
|
tag: false,
|
|
|
|
sha: '97de212e80737a608d939f648d959671fb0a0142b',
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:merge_request) { create(:merge_request, target_branch: 'master', source_branch: 'feature', source_project: project) }
|
|
|
|
|
|
|
|
context "while updating the 'first_deployed_to_production_at' time" do
|
|
|
|
before { merge_request.mark_as_merged }
|
|
|
|
|
|
|
|
context "for merge requests merged before the current deploy" do
|
|
|
|
it "sets the time if the deploy's environment is 'production'" do
|
|
|
|
time = Time.now
|
|
|
|
Timecop.freeze(time) { service.execute }
|
|
|
|
|
2016-10-17 08:40:02 -04:00
|
|
|
expect(merge_request.reload.metrics.first_deployed_to_production_at).to be_like_time(time)
|
2016-09-19 03:26:25 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't set the time if the deploy's environment is not 'production'" do
|
|
|
|
staging_params = params.merge(environment: 'staging')
|
|
|
|
service = described_class.new(project, user, staging_params)
|
|
|
|
service.execute
|
|
|
|
|
2016-09-20 17:21:43 -04:00
|
|
|
expect(merge_request.reload.metrics.first_deployed_to_production_at).to be_nil
|
2016-09-19 03:26:25 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not raise errors if the merge request does not have a metrics record' do
|
|
|
|
merge_request.metrics.destroy
|
|
|
|
|
|
|
|
expect(merge_request.reload.metrics).to be_nil
|
|
|
|
expect { service.execute }.not_to raise_error
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "for merge requests merged before the previous deploy" do
|
|
|
|
context "if the 'first_deployed_to_production_at' time is already set" do
|
|
|
|
it "does not overwrite the older 'first_deployed_to_production_at' time" do
|
|
|
|
# Previous deploy
|
|
|
|
time = Time.now
|
|
|
|
Timecop.freeze(time) { service.execute }
|
|
|
|
|
2016-10-17 08:40:02 -04:00
|
|
|
expect(merge_request.reload.metrics.first_deployed_to_production_at).to be_like_time(time)
|
2016-09-19 03:26:25 -04:00
|
|
|
|
|
|
|
# Current deploy
|
|
|
|
service = described_class.new(project, user, params)
|
|
|
|
Timecop.freeze(time + 12.hours) { service.execute }
|
|
|
|
|
2016-10-17 08:40:02 -04:00
|
|
|
expect(merge_request.reload.metrics.first_deployed_to_production_at).to be_like_time(time)
|
2016-09-19 03:26:25 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "if the 'first_deployed_to_production_at' time is not already set" do
|
|
|
|
it "does not overwrite the older 'first_deployed_to_production_at' time" do
|
|
|
|
# Previous deploy
|
2016-09-20 23:47:00 -04:00
|
|
|
time = 5.minutes.from_now
|
2016-09-19 03:26:25 -04:00
|
|
|
Timecop.freeze(time) { service.execute }
|
2016-09-20 23:47:00 -04:00
|
|
|
|
|
|
|
expect(merge_request.reload.metrics.merged_at).to be < merge_request.reload.metrics.first_deployed_to_production_at
|
|
|
|
|
2016-09-20 17:21:43 -04:00
|
|
|
merge_request.reload.metrics.update(first_deployed_to_production_at: nil)
|
2016-09-19 03:26:25 -04:00
|
|
|
|
|
|
|
expect(merge_request.reload.metrics.first_deployed_to_production_at).to be_nil
|
|
|
|
|
|
|
|
# Current deploy
|
|
|
|
service = described_class.new(project, user, params)
|
|
|
|
Timecop.freeze(time + 12.hours) { service.execute }
|
|
|
|
|
|
|
|
expect(merge_request.reload.metrics.first_deployed_to_production_at).to be_nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-06-14 07:51:12 -04:00
|
|
|
end
|