2016-06-14 07:51:12 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2017-07-10 10:24:02 -04:00
|
|
|
describe CreateDeploymentService do
|
2016-06-14 07:51:12 -04:00
|
|
|
let(:user) { create(:user) }
|
2017-06-01 09:01:32 -04: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 07:51:12 -04:00
|
|
|
|
2017-06-02 12:24:20 -04:00
|
|
|
let!(:environment) do
|
|
|
|
create(:environment, project: project, name: 'production')
|
|
|
|
end
|
|
|
|
|
2017-06-01 09:01:32 -04:00
|
|
|
let(:service) { described_class.new(job) }
|
2016-06-14 07:51:12 -04:00
|
|
|
|
2017-08-10 13:44:17 -04:00
|
|
|
before do
|
|
|
|
allow_any_instance_of(Deployment).to receive(:create_ref)
|
|
|
|
end
|
|
|
|
|
2016-06-14 07:51:12 -04:00
|
|
|
describe '#execute' do
|
|
|
|
subject { service.execute }
|
|
|
|
|
2017-06-02 12:24:20 -04:00
|
|
|
context 'when environment exists' do
|
|
|
|
it 'creates a deployment' do
|
2016-06-14 07:51:12 -04:00
|
|
|
expect(subject).to be_persisted
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-06-02 12:24:20 -04:00
|
|
|
context 'when environment does not exist' do
|
|
|
|
let(:environment) {}
|
2016-06-14 07:51:12 -04:00
|
|
|
|
2017-06-02 12:24:20 -04:00
|
|
|
it 'does not create a deployment' do
|
|
|
|
expect do
|
|
|
|
expect(subject).to be_nil
|
|
|
|
end.not_to change { Deployment.count }
|
2016-06-14 07:51:12 -04:00
|
|
|
end
|
2017-06-02 12:24:20 -04:00
|
|
|
end
|
2016-10-17 11:10:26 -04:00
|
|
|
|
2017-06-02 12:24:20 -04:00
|
|
|
context 'when start action is defined' do
|
|
|
|
let(:options) { { action: 'start' } }
|
2016-10-17 11:10:26 -04:00
|
|
|
|
2017-06-02 12:24:20 -04:00
|
|
|
context 'and environment is stopped' do
|
|
|
|
before do
|
|
|
|
environment.stop
|
|
|
|
end
|
2016-10-17 11:10:26 -04:00
|
|
|
|
2017-06-02 12:24:20 -04:00
|
|
|
it 'makes environment available' do
|
|
|
|
subject
|
2016-10-17 11:10:26 -04:00
|
|
|
|
2017-06-02 12:24:20 -04:00
|
|
|
expect(environment.reload).to be_available
|
|
|
|
end
|
2016-10-17 11:10:26 -04:00
|
|
|
|
2017-06-02 12:24:20 -04:00
|
|
|
it 'creates a deployment' do
|
|
|
|
expect(subject).to be_persisted
|
2016-10-17 11:10:26 -04:00
|
|
|
end
|
|
|
|
end
|
2017-06-02 12:24:20 -04:00
|
|
|
end
|
2016-10-17 11:10:26 -04:00
|
|
|
|
2017-06-02 12:24:20 -04:00
|
|
|
context 'when stop action is defined' do
|
|
|
|
let(:options) { { action: 'stop' } }
|
2016-10-17 11:10:26 -04:00
|
|
|
|
2017-06-02 12:24:20 -04:00
|
|
|
context 'and environment is available' do
|
|
|
|
before do
|
|
|
|
environment.start
|
2016-10-17 11:10:26 -04:00
|
|
|
end
|
2016-06-14 07:51:12 -04:00
|
|
|
|
2017-06-02 12:24:20 -04:00
|
|
|
it 'makes environment stopped' do
|
|
|
|
subject
|
2016-06-14 07:51:12 -04:00
|
|
|
|
2017-06-02 12:24:20 -04:00
|
|
|
expect(environment.reload).to be_stopped
|
|
|
|
end
|
2016-06-14 07:51:12 -04:00
|
|
|
|
2017-06-02 12:24:20 -04:00
|
|
|
it 'does not create a deployment' do
|
|
|
|
expect(subject).to be_nil
|
|
|
|
end
|
2016-06-14 07:51:12 -04:00
|
|
|
end
|
|
|
|
end
|
2016-09-14 16:32:11 -04:00
|
|
|
|
|
|
|
context 'when variables are used' do
|
2017-06-01 09:01:32 -04: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 12:24:20 -04:00
|
|
|
environment.update(name: 'review-apps/master')
|
2017-06-01 09:01:32 -04:00
|
|
|
job.update(environment: 'review-apps/$CI_COMMIT_REF_NAME')
|
2016-09-14 16:32:11 -04:00
|
|
|
end
|
|
|
|
|
2017-06-02 12:24:20 -04:00
|
|
|
it 'creates 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
|
|
|
|
2017-06-02 12:24:20 -04:00
|
|
|
it 'does not create a new environment' do
|
|
|
|
expect { subject }.not_to change { Environment.count }
|
|
|
|
end
|
2016-10-17 11:10:26 -04:00
|
|
|
|
2017-06-02 12:24:20 -04:00
|
|
|
it 'updates external url' do
|
|
|
|
subject
|
2016-10-17 11:10:26 -04:00
|
|
|
|
2017-06-02 12:24:20 -04: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 11:10:26 -04:00
|
|
|
end
|
2016-09-14 16:32:11 -04:00
|
|
|
end
|
2016-10-14 04:19:16 -04:00
|
|
|
|
|
|
|
context 'when project was removed' do
|
2017-06-02 12:24:20 -04:00
|
|
|
let(:environment) {}
|
|
|
|
|
2017-06-01 09:01:32 -04:00
|
|
|
before do
|
|
|
|
job.update(project: nil)
|
|
|
|
end
|
2016-10-14 04:19:16 -04: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 07:51:12 -04:00
|
|
|
end
|
2016-09-14 16:32:11 -04:00
|
|
|
|
2017-06-21 07:53:19 -04:00
|
|
|
describe '#expanded_environment_url' do
|
|
|
|
subject { service.send(:expanded_environment_url) }
|
|
|
|
|
|
|
|
context 'when yaml environment uses $CI_COMMIT_REF_NAME' do
|
|
|
|
let(:job) do
|
|
|
|
create(:ci_build,
|
|
|
|
ref: 'master',
|
|
|
|
options: { environment: { url: 'http://review/$CI_COMMIT_REF_NAME' } })
|
|
|
|
end
|
|
|
|
|
|
|
|
it { is_expected.to eq('http://review/master') }
|
|
|
|
end
|
|
|
|
|
2017-06-21 08:22:26 -04:00
|
|
|
context 'when yaml environment uses $CI_ENVIRONMENT_SLUG' do
|
|
|
|
let(:job) do
|
|
|
|
create(:ci_build,
|
|
|
|
ref: 'master',
|
|
|
|
environment: 'production',
|
|
|
|
options: { environment: { url: 'http://review/$CI_ENVIRONMENT_SLUG' } })
|
|
|
|
end
|
|
|
|
|
|
|
|
let!(:environment) do
|
|
|
|
create(:environment,
|
|
|
|
project: job.project,
|
|
|
|
name: 'production',
|
|
|
|
slug: 'prod-slug',
|
|
|
|
external_url: 'http://review/old')
|
|
|
|
end
|
|
|
|
|
|
|
|
it { is_expected.to eq('http://review/prod-slug') }
|
|
|
|
end
|
|
|
|
|
2017-06-21 07:53:19 -04:00
|
|
|
context 'when yaml environment uses yaml_variables containing symbol keys' do
|
|
|
|
let(:job) do
|
|
|
|
create(:ci_build,
|
|
|
|
yaml_variables: [{ key: :APP_HOST, value: 'host' }],
|
|
|
|
options: { environment: { url: 'http://review/$APP_HOST' } })
|
|
|
|
end
|
|
|
|
|
|
|
|
it { is_expected.to eq('http://review/host') }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when yaml environment does not have url' do
|
|
|
|
let(:job) { create(:ci_build, environment: 'staging') }
|
|
|
|
|
|
|
|
let!(:environment) do
|
|
|
|
create(:environment, project: job.project, name: job.environment)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns the external_url from persisted environment' do
|
2017-06-21 08:22:26 -04:00
|
|
|
is_expected.to be_nil
|
2017-06-21 07:53:19 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-06-14 08:44:09 -04:00
|
|
|
describe 'processing of builds' do
|
2017-06-02 12:24:20 -04:00
|
|
|
shared_examples 'does not create deployment' do
|
2016-06-14 08:44:09 -04: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 12:24:20 -04:00
|
|
|
|
2016-06-14 08:44:09 -04:00
|
|
|
subject
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-06-02 12:24:20 -04:00
|
|
|
shared_examples 'creates deployment' do
|
|
|
|
it 'creates a new deployment' do
|
2016-06-14 08:44:09 -04:00
|
|
|
expect { subject }.to change { Deployment.count }.by(1)
|
|
|
|
end
|
|
|
|
|
2017-06-02 12:24:20 -04:00
|
|
|
it 'calls a service' do
|
2016-06-14 08:44:09 -04:00
|
|
|
expect_any_instance_of(described_class).to receive(:execute)
|
2017-06-02 12:24:20 -04:00
|
|
|
|
2016-06-14 08:44:09 -04:00
|
|
|
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
|
|
|
|
2017-06-02 12:24:20 -04:00
|
|
|
it 'updates environment URL' do
|
2016-09-14 17:00:15 -04:00
|
|
|
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
|
2017-06-02 12:24:20 -04:00
|
|
|
let(:job) { create(:ci_build) }
|
2016-10-14 04:19:16 -04:00
|
|
|
|
2017-06-02 12:24:20 -04:00
|
|
|
it_behaves_like 'does not create deployment' do
|
|
|
|
subject { job.success }
|
2016-06-14 08:44:09 -04:00
|
|
|
end
|
|
|
|
end
|
2016-10-14 04:19:16 -04:00
|
|
|
|
2016-06-14 08:44:09 -04:00
|
|
|
context 'when environment is specified' do
|
2017-06-02 12:24:20 -04:00
|
|
|
let(:deployable) { job }
|
|
|
|
|
2016-09-14 17:00:15 -04:00
|
|
|
let(:options) do
|
|
|
|
{ environment: { name: 'production', url: 'http://gitlab.com' } }
|
|
|
|
end
|
2016-06-14 08:44:09 -04:00
|
|
|
|
2017-06-02 12:24:20 -04:00
|
|
|
context 'when job succeeds' do
|
|
|
|
it_behaves_like 'creates deployment' do
|
|
|
|
subject { job.success }
|
2016-06-14 08:44:09 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-06-02 12:24:20 -04:00
|
|
|
context 'when job fails' do
|
|
|
|
it_behaves_like 'does not create deployment' do
|
|
|
|
subject { job.drop }
|
2016-06-14 08:44:09 -04:00
|
|
|
end
|
|
|
|
end
|
2016-07-15 10:17:46 -04:00
|
|
|
|
2017-06-02 12:24:20 -04:00
|
|
|
context 'when job is retried' do
|
|
|
|
it_behaves_like 'creates deployment' do
|
2017-02-13 09:12:08 -05:00
|
|
|
before do
|
2017-07-18 10:32:34 -04:00
|
|
|
stub_not_protect_default_branch
|
|
|
|
|
|
|
|
project.add_developer(user)
|
2017-02-13 09:12:08 -05:00
|
|
|
end
|
|
|
|
|
2017-06-02 12:24:20 -04:00
|
|
|
let(:deployable) { Ci::Build.retry(job, 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(: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 14:18:56 -04:00
|
|
|
before do
|
|
|
|
merge_request.mark_as_merged
|
|
|
|
end
|
2016-09-19 03:26:25 -04: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 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
|
2017-06-01 09:01:32 -04:00
|
|
|
job.update(environment: 'staging')
|
|
|
|
service = described_class.new(job)
|
2016-09-19 03:26:25 -04:00
|
|
|
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
|
2017-06-01 09:01:32 -04:00
|
|
|
service = described_class.new(job)
|
2016-09-19 03:26:25 -04:00
|
|
|
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
|
2017-06-01 09:01:32 -04:00
|
|
|
service = described_class.new(job)
|
2016-09-19 03:26:25 -04: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 07:51:12 -04:00
|
|
|
end
|