2016-06-14 11:51:12 +00:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe CreateDeploymentService, services: true do
|
|
|
|
let(:user) { create(:user) }
|
2017-06-01 13:01:32 +00:00
|
|
|
let(:options) { nil }
|
|
|
|
|
|
|
|
let(:job) do
|
|
|
|
create(:ci_build,
|
|
|
|
ref: 'master',
|
|
|
|
tag: false,
|
|
|
|
environment: 'production',
|
|
|
|
options: { environment: options })
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:project) { job.project }
|
2016-06-14 11:51:12 +00:00
|
|
|
|
2017-06-02 16:24:20 +00:00
|
|
|
let!(:environment) do
|
|
|
|
create(:environment, project: project, name: 'production')
|
|
|
|
end
|
|
|
|
|
2017-06-01 13:01:32 +00:00
|
|
|
let(:service) { described_class.new(job) }
|
2016-06-14 11:51:12 +00:00
|
|
|
|
|
|
|
describe '#execute' do
|
|
|
|
subject { service.execute }
|
|
|
|
|
2017-06-02 16:24:20 +00:00
|
|
|
context 'when environment exists' do
|
|
|
|
it 'creates a deployment' do
|
2016-06-14 11:51:12 +00:00
|
|
|
expect(subject).to be_persisted
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-06-02 16:24:20 +00:00
|
|
|
context 'when environment does not exist' do
|
|
|
|
let(:environment) {}
|
2016-06-14 11:51:12 +00:00
|
|
|
|
2017-06-02 16:24:20 +00:00
|
|
|
it 'does not create a deployment' do
|
|
|
|
expect do
|
|
|
|
expect(subject).to be_nil
|
|
|
|
end.not_to change { Deployment.count }
|
2016-06-14 11:51:12 +00:00
|
|
|
end
|
2017-06-02 16:24:20 +00:00
|
|
|
end
|
2016-10-17 15:10:26 +00:00
|
|
|
|
2017-06-02 16:24:20 +00:00
|
|
|
context 'when start action is defined' do
|
|
|
|
let(:options) { { action: 'start' } }
|
2016-10-17 15:10:26 +00:00
|
|
|
|
2017-06-02 16:24:20 +00:00
|
|
|
context 'and environment is stopped' do
|
|
|
|
before do
|
|
|
|
environment.stop
|
|
|
|
end
|
2016-10-17 15:10:26 +00:00
|
|
|
|
2017-06-02 16:24:20 +00:00
|
|
|
it 'makes environment available' do
|
|
|
|
subject
|
2016-10-17 15:10:26 +00:00
|
|
|
|
2017-06-02 16:24:20 +00:00
|
|
|
expect(environment.reload).to be_available
|
|
|
|
end
|
2016-10-17 15:10:26 +00:00
|
|
|
|
2017-06-02 16:24:20 +00:00
|
|
|
it 'creates a deployment' do
|
|
|
|
expect(subject).to be_persisted
|
2016-10-17 15:10:26 +00:00
|
|
|
end
|
|
|
|
end
|
2017-06-02 16:24:20 +00:00
|
|
|
end
|
2016-10-17 15:10:26 +00:00
|
|
|
|
2017-06-02 16:24:20 +00:00
|
|
|
context 'when stop action is defined' do
|
|
|
|
let(:options) { { action: 'stop' } }
|
2016-10-17 15:10:26 +00:00
|
|
|
|
2017-06-02 16:24:20 +00:00
|
|
|
context 'and environment is available' do
|
|
|
|
before do
|
|
|
|
environment.start
|
2016-10-17 15:10:26 +00:00
|
|
|
end
|
2016-06-14 11:51:12 +00:00
|
|
|
|
2017-06-02 16:24:20 +00:00
|
|
|
it 'makes environment stopped' do
|
|
|
|
subject
|
2016-06-14 11:51:12 +00:00
|
|
|
|
2017-06-02 16:24:20 +00:00
|
|
|
expect(environment.reload).to be_stopped
|
|
|
|
end
|
2016-06-14 11:51:12 +00:00
|
|
|
|
2017-06-02 16:24:20 +00:00
|
|
|
it 'does not create a deployment' do
|
|
|
|
expect(subject).to be_nil
|
|
|
|
end
|
2016-06-14 11:51:12 +00:00
|
|
|
end
|
|
|
|
end
|
2016-09-14 20:32:11 +00:00
|
|
|
|
|
|
|
context 'when variables are used' do
|
2017-06-01 13:01:32 +00:00
|
|
|
let(:options) do
|
|
|
|
{ name: 'review-apps/$CI_COMMIT_REF_NAME',
|
|
|
|
url: 'http://$CI_COMMIT_REF_NAME.review-apps.gitlab.com' }
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
2017-06-02 16:24:20 +00:00
|
|
|
environment.update(name: 'review-apps/master')
|
2017-06-01 13:01:32 +00:00
|
|
|
job.update(environment: 'review-apps/$CI_COMMIT_REF_NAME')
|
2016-09-14 20:32:11 +00:00
|
|
|
end
|
|
|
|
|
2017-06-02 16:24:20 +00:00
|
|
|
it 'creates a new deployment' do
|
2016-09-14 21:00:15 +00:00
|
|
|
expect(subject).to be_persisted
|
2016-09-14 20:32:11 +00:00
|
|
|
end
|
2016-10-17 15:10:26 +00:00
|
|
|
|
2017-06-02 16:24:20 +00:00
|
|
|
it 'does not create a new environment' do
|
|
|
|
expect { subject }.not_to change { Environment.count }
|
|
|
|
end
|
2016-10-17 15:10:26 +00:00
|
|
|
|
2017-06-02 16:24:20 +00:00
|
|
|
it 'updates external url' do
|
|
|
|
subject
|
2016-10-17 15:10:26 +00:00
|
|
|
|
2017-06-02 16:24:20 +00:00
|
|
|
expect(subject.environment.name).to eq('review-apps/master')
|
|
|
|
expect(subject.environment.external_url).to eq('http://master.review-apps.gitlab.com')
|
2016-10-17 15:10:26 +00:00
|
|
|
end
|
2016-09-14 20:32:11 +00:00
|
|
|
end
|
2016-10-14 08:19:16 +00:00
|
|
|
|
|
|
|
context 'when project was removed' do
|
2017-06-02 16:24:20 +00:00
|
|
|
let(:environment) {}
|
|
|
|
|
2017-06-01 13:01:32 +00:00
|
|
|
before do
|
|
|
|
job.update(project: nil)
|
|
|
|
end
|
2016-10-14 08:19:16 +00:00
|
|
|
|
|
|
|
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 11:51:12 +00:00
|
|
|
end
|
2016-09-14 20:32:11 +00:00
|
|
|
|
2016-06-14 12:44:09 +00:00
|
|
|
describe 'processing of builds' do
|
2017-06-02 16:24:20 +00:00
|
|
|
shared_examples 'does not create deployment' do
|
2016-06-14 12:44:09 +00:00
|
|
|
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)
|
2017-06-02 16:24:20 +00:00
|
|
|
|
2016-06-14 12:44:09 +00:00
|
|
|
subject
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-06-02 16:24:20 +00:00
|
|
|
shared_examples 'creates deployment' do
|
|
|
|
it 'creates a new deployment' do
|
2016-06-14 12:44:09 +00:00
|
|
|
expect { subject }.to change { Deployment.count }.by(1)
|
|
|
|
end
|
|
|
|
|
2017-06-02 16:24:20 +00:00
|
|
|
it 'calls a service' do
|
2016-06-14 12:44:09 +00:00
|
|
|
expect_any_instance_of(described_class).to receive(:execute)
|
2017-06-02 16:24:20 +00:00
|
|
|
|
2016-06-14 12:44:09 +00:00
|
|
|
subject
|
|
|
|
end
|
2016-07-15 14:17:46 +00:00
|
|
|
|
|
|
|
it 'is set as deployable' do
|
|
|
|
subject
|
|
|
|
|
|
|
|
expect(Deployment.last.deployable).to eq(deployable)
|
|
|
|
end
|
2016-09-14 21:00:15 +00:00
|
|
|
|
2017-06-02 16:24:20 +00:00
|
|
|
it 'updates environment URL' do
|
2016-09-14 21:00:15 +00:00
|
|
|
subject
|
|
|
|
|
|
|
|
expect(Deployment.last.environment.external_url).not_to be_nil
|
|
|
|
end
|
2016-06-14 12:44:09 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'without environment specified' do
|
2017-06-02 16:24:20 +00:00
|
|
|
let(:job) { create(:ci_build) }
|
2016-10-14 08:19:16 +00:00
|
|
|
|
2017-06-02 16:24:20 +00:00
|
|
|
it_behaves_like 'does not create deployment' do
|
|
|
|
subject { job.success }
|
2016-06-14 12:44:09 +00:00
|
|
|
end
|
|
|
|
end
|
2016-10-14 08:19:16 +00:00
|
|
|
|
2016-06-14 12:44:09 +00:00
|
|
|
context 'when environment is specified' do
|
2017-06-02 16:24:20 +00:00
|
|
|
let(:deployable) { job }
|
|
|
|
|
2016-09-14 21:00:15 +00:00
|
|
|
let(:options) do
|
|
|
|
{ environment: { name: 'production', url: 'http://gitlab.com' } }
|
|
|
|
end
|
2016-06-14 12:44:09 +00:00
|
|
|
|
2017-06-02 16:24:20 +00:00
|
|
|
context 'when job succeeds' do
|
|
|
|
it_behaves_like 'creates deployment' do
|
|
|
|
subject { job.success }
|
2016-06-14 12:44:09 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-06-02 16:24:20 +00:00
|
|
|
context 'when job fails' do
|
|
|
|
it_behaves_like 'does not create deployment' do
|
|
|
|
subject { job.drop }
|
2016-06-14 12:44:09 +00:00
|
|
|
end
|
|
|
|
end
|
2016-07-15 14:17:46 +00:00
|
|
|
|
2017-06-02 16:24:20 +00:00
|
|
|
context 'when job is retried' do
|
|
|
|
it_behaves_like 'creates deployment' do
|
2017-02-13 14:12:08 +00:00
|
|
|
before do
|
2017-02-15 10:12:07 +00:00
|
|
|
project.add_developer(user)
|
2017-02-13 14:12:08 +00:00
|
|
|
end
|
|
|
|
|
2017-06-02 16:24:20 +00:00
|
|
|
let(:deployable) { Ci::Build.retry(job, user) }
|
2016-07-15 14:17:46 +00:00
|
|
|
|
|
|
|
subject { deployable.success }
|
|
|
|
end
|
|
|
|
end
|
2016-06-14 12:44:09 +00:00
|
|
|
end
|
|
|
|
end
|
2016-09-19 07:26:25 +00:00
|
|
|
|
|
|
|
describe "merge request metrics" do
|
|
|
|
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
|
2017-06-14 18:18:56 +00:00
|
|
|
before do
|
|
|
|
merge_request.mark_as_merged
|
|
|
|
end
|
2016-09-19 07:26:25 +00:00
|
|
|
|
|
|
|
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 12:40:02 +00:00
|
|
|
expect(merge_request.reload.metrics.first_deployed_to_production_at).to be_like_time(time)
|
2016-09-19 07:26:25 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't set the time if the deploy's environment is not 'production'" do
|
2017-06-01 13:01:32 +00:00
|
|
|
job.update(environment: 'staging')
|
|
|
|
service = described_class.new(job)
|
2016-09-19 07:26:25 +00:00
|
|
|
service.execute
|
|
|
|
|
2016-09-20 21:21:43 +00:00
|
|
|
expect(merge_request.reload.metrics.first_deployed_to_production_at).to be_nil
|
2016-09-19 07:26:25 +00: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 12:40:02 +00:00
|
|
|
expect(merge_request.reload.metrics.first_deployed_to_production_at).to be_like_time(time)
|
2016-09-19 07:26:25 +00:00
|
|
|
|
|
|
|
# Current deploy
|
2017-06-01 13:01:32 +00:00
|
|
|
service = described_class.new(job)
|
2016-09-19 07:26:25 +00:00
|
|
|
Timecop.freeze(time + 12.hours) { service.execute }
|
|
|
|
|
2016-10-17 12:40:02 +00:00
|
|
|
expect(merge_request.reload.metrics.first_deployed_to_production_at).to be_like_time(time)
|
2016-09-19 07:26:25 +00: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-21 03:47:00 +00:00
|
|
|
time = 5.minutes.from_now
|
2016-09-19 07:26:25 +00:00
|
|
|
Timecop.freeze(time) { service.execute }
|
2016-09-21 03:47:00 +00:00
|
|
|
|
|
|
|
expect(merge_request.reload.metrics.merged_at).to be < merge_request.reload.metrics.first_deployed_to_production_at
|
|
|
|
|
2016-09-20 21:21:43 +00:00
|
|
|
merge_request.reload.metrics.update(first_deployed_to_production_at: nil)
|
2016-09-19 07:26:25 +00:00
|
|
|
|
|
|
|
expect(merge_request.reload.metrics.first_deployed_to_production_at).to be_nil
|
|
|
|
|
|
|
|
# Current deploy
|
2017-06-01 13:01:32 +00:00
|
|
|
service = described_class.new(job)
|
2016-09-19 07:26:25 +00:00
|
|
|
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 11:51:12 +00:00
|
|
|
end
|