2016-06-16 23:09:34 +05:30
|
|
|
require 'spec_helper'
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
describe CreateDeploymentService do
|
2016-06-16 23:09:34 +05:30
|
|
|
let(:user) { create(:user) }
|
2017-09-10 17:25:29 +05:30
|
|
|
let(:options) { nil }
|
|
|
|
|
|
|
|
let(:job) do
|
|
|
|
create(:ci_build,
|
|
|
|
ref: 'master',
|
|
|
|
tag: false,
|
|
|
|
environment: 'production',
|
|
|
|
options: { environment: options })
|
|
|
|
end
|
2016-06-16 23:09:34 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
let(:project) { job.project }
|
2016-06-16 23:09:34 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
let!(:environment) do
|
|
|
|
create(:environment, project: project, name: 'production')
|
|
|
|
end
|
2016-06-16 23:09:34 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
let(:service) { described_class.new(job) }
|
2016-06-16 23:09:34 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
before do
|
|
|
|
allow_any_instance_of(Deployment).to receive(:create_ref)
|
|
|
|
end
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
describe '#execute' do
|
|
|
|
subject { service.execute }
|
2016-06-16 23:09:34 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
context 'when environment exists' do
|
|
|
|
it 'creates a deployment' do
|
2016-06-16 23:09:34 +05:30
|
|
|
expect(subject).to be_persisted
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
context 'when environment does not exist' do
|
|
|
|
let(:environment) {}
|
2016-06-16 23:09:34 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
it 'does not create a deployment' do
|
|
|
|
expect do
|
|
|
|
expect(subject).to be_nil
|
|
|
|
end.not_to change { Deployment.count }
|
2016-06-16 23:09:34 +05:30
|
|
|
end
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
2016-11-03 12:29:30 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
context 'when start action is defined' do
|
|
|
|
let(:options) { { action: 'start' } }
|
2016-11-03 12:29:30 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
context 'and environment is stopped' do
|
|
|
|
before do
|
|
|
|
environment.stop
|
|
|
|
end
|
2016-11-03 12:29:30 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
it 'makes environment available' do
|
|
|
|
subject
|
2016-11-03 12:29:30 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
expect(environment.reload).to be_available
|
|
|
|
end
|
2016-11-03 12:29:30 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
it 'creates a deployment' do
|
|
|
|
expect(subject).to be_persisted
|
2016-11-03 12:29:30 +05:30
|
|
|
end
|
|
|
|
end
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
2016-11-03 12:29:30 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
context 'when stop action is defined' do
|
|
|
|
let(:options) { { action: 'stop' } }
|
2016-11-03 12:29:30 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
context 'and environment is available' do
|
|
|
|
before do
|
|
|
|
environment.start
|
|
|
|
end
|
2016-11-03 12:29:30 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
it 'makes environment stopped' do
|
|
|
|
subject
|
2016-11-03 12:29:30 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
expect(environment.reload).to be_stopped
|
|
|
|
end
|
2016-11-03 12:29:30 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
it 'does not create a deployment' do
|
|
|
|
expect(subject).to be_nil
|
2016-11-03 12:29:30 +05:30
|
|
|
end
|
|
|
|
end
|
2016-06-16 23:09:34 +05:30
|
|
|
end
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
context 'when variables are used' do
|
|
|
|
let(:options) do
|
|
|
|
{ name: 'review-apps/$CI_COMMIT_REF_NAME',
|
|
|
|
url: 'http://$CI_COMMIT_REF_NAME.review-apps.gitlab.com' }
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
environment.update(name: 'review-apps/master')
|
|
|
|
job.update(environment: 'review-apps/$CI_COMMIT_REF_NAME')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'creates a new deployment' do
|
|
|
|
expect(subject).to be_persisted
|
2016-06-16 23:09:34 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not create a new environment' do
|
|
|
|
expect { subject }.not_to change { Environment.count }
|
|
|
|
end
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
it 'updates external url' do
|
|
|
|
subject
|
|
|
|
|
|
|
|
expect(subject.environment.name).to eq('review-apps/master')
|
|
|
|
expect(subject.environment.external_url).to eq('http://master.review-apps.gitlab.com')
|
2016-06-16 23:09:34 +05:30
|
|
|
end
|
|
|
|
end
|
2016-09-29 09:46:39 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
context 'when project was removed' do
|
|
|
|
let(:environment) {}
|
|
|
|
|
|
|
|
before do
|
|
|
|
job.update(project: nil)
|
2016-09-29 09:46:39 +05:30
|
|
|
end
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
it 'does not create deployment or environment' do
|
|
|
|
expect { subject }.not_to raise_error
|
2016-09-29 09:46:39 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
expect(Environment.count).to be_zero
|
|
|
|
expect(Deployment.count).to be_zero
|
2016-09-29 09:46:39 +05:30
|
|
|
end
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
|
|
|
end
|
2016-09-29 09:46:39 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
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' } })
|
2016-09-29 09:46:39 +05:30
|
|
|
end
|
2016-11-03 12:29:30 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
it { is_expected.to eq('http://review/master') }
|
|
|
|
end
|
2016-11-03 12:29:30 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
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
|
2016-11-03 12:29:30 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
let!(:environment) do
|
|
|
|
create(:environment,
|
|
|
|
project: job.project,
|
|
|
|
name: 'production',
|
|
|
|
slug: 'prod-slug',
|
|
|
|
external_url: 'http://review/old')
|
|
|
|
end
|
2016-11-03 12:29:30 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
it { is_expected.to eq('http://review/prod-slug') }
|
|
|
|
end
|
2016-11-03 12:29:30 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
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' } })
|
2016-11-03 12:29:30 +05:30
|
|
|
end
|
2017-09-10 17:25:29 +05:30
|
|
|
|
|
|
|
it { is_expected.to eq('http://review/host') }
|
2016-11-03 12:29:30 +05:30
|
|
|
end
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
context 'when yaml environment does not have url' do
|
|
|
|
let(:job) { create(:ci_build, environment: 'staging') }
|
2016-11-03 12:29:30 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
let!(:environment) do
|
|
|
|
create(:environment, project: job.project, name: job.environment)
|
|
|
|
end
|
2016-11-03 12:29:30 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
it 'returns the external_url from persisted environment' do
|
|
|
|
is_expected.to be_nil
|
2016-11-03 12:29:30 +05:30
|
|
|
end
|
2016-09-29 09:46:39 +05:30
|
|
|
end
|
2016-06-16 23:09:34 +05:30
|
|
|
end
|
2016-09-29 09:46:39 +05:30
|
|
|
|
2016-06-16 23:09:34 +05:30
|
|
|
describe 'processing of builds' do
|
2017-09-10 17:25:29 +05:30
|
|
|
shared_examples 'does not create deployment' do
|
2016-06-16 23:09:34 +05:30
|
|
|
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-09-10 17:25:29 +05:30
|
|
|
|
2016-06-16 23:09:34 +05:30
|
|
|
subject
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
shared_examples 'creates deployment' do
|
|
|
|
it 'creates a new deployment' do
|
2016-06-16 23:09:34 +05:30
|
|
|
expect { subject }.to change { Deployment.count }.by(1)
|
|
|
|
end
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
it 'calls a service' do
|
2016-06-16 23:09:34 +05:30
|
|
|
expect_any_instance_of(described_class).to receive(:execute)
|
2017-09-10 17:25:29 +05:30
|
|
|
|
2016-06-16 23:09:34 +05:30
|
|
|
subject
|
|
|
|
end
|
2016-08-24 12:49:21 +05:30
|
|
|
|
|
|
|
it 'is set as deployable' do
|
|
|
|
subject
|
|
|
|
|
|
|
|
expect(Deployment.last.deployable).to eq(deployable)
|
|
|
|
end
|
2016-09-29 09:46:39 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
it 'updates environment URL' do
|
2016-09-29 09:46:39 +05:30
|
|
|
subject
|
|
|
|
|
|
|
|
expect(Deployment.last.environment.external_url).not_to be_nil
|
|
|
|
end
|
2016-06-16 23:09:34 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
context 'without environment specified' do
|
2017-09-10 17:25:29 +05:30
|
|
|
let(:job) { create(:ci_build) }
|
2016-11-03 12:29:30 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
it_behaves_like 'does not create deployment' do
|
|
|
|
subject { job.success }
|
2016-06-16 23:09:34 +05:30
|
|
|
end
|
|
|
|
end
|
2016-11-03 12:29:30 +05:30
|
|
|
|
2016-06-16 23:09:34 +05:30
|
|
|
context 'when environment is specified' do
|
2017-09-10 17:25:29 +05:30
|
|
|
let(:deployable) { job }
|
|
|
|
|
2016-09-29 09:46:39 +05:30
|
|
|
let(:options) do
|
|
|
|
{ environment: { name: 'production', url: 'http://gitlab.com' } }
|
|
|
|
end
|
2016-06-16 23:09:34 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
context 'when job succeeds' do
|
|
|
|
it_behaves_like 'creates deployment' do
|
|
|
|
subject { job.success }
|
2016-06-16 23:09:34 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
context 'when job fails' do
|
|
|
|
it_behaves_like 'does not create deployment' do
|
|
|
|
subject { job.drop }
|
2016-06-16 23:09:34 +05:30
|
|
|
end
|
|
|
|
end
|
2016-08-24 12:49:21 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
context 'when job is retried' do
|
|
|
|
it_behaves_like 'creates deployment' do
|
2017-08-17 22:00:37 +05:30
|
|
|
before do
|
2017-09-10 17:25:29 +05:30
|
|
|
stub_not_protect_default_branch
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
project.add_developer(user)
|
|
|
|
end
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
let(:deployable) { Ci::Build.retry(job, user) }
|
2016-08-24 12:49:21 +05:30
|
|
|
|
|
|
|
subject { deployable.success }
|
|
|
|
end
|
|
|
|
end
|
2016-06-16 23:09:34 +05:30
|
|
|
end
|
|
|
|
end
|
2016-09-29 09:46:39 +05:30
|
|
|
|
|
|
|
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-09-10 17:25:29 +05:30
|
|
|
before do
|
2018-03-17 18:26:18 +05:30
|
|
|
merge_request.metrics.update!(merged_at: Time.now)
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
2016-09-29 09:46:39 +05:30
|
|
|
|
|
|
|
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-11-03 12:29:30 +05:30
|
|
|
expect(merge_request.reload.metrics.first_deployed_to_production_at).to be_like_time(time)
|
2016-09-29 09:46:39 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't set the time if the deploy's environment is not 'production'" do
|
2017-09-10 17:25:29 +05:30
|
|
|
job.update(environment: 'staging')
|
|
|
|
service = described_class.new(job)
|
2016-09-29 09:46:39 +05:30
|
|
|
service.execute
|
|
|
|
|
|
|
|
expect(merge_request.reload.metrics.first_deployed_to_production_at).to be_nil
|
|
|
|
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-11-03 12:29:30 +05:30
|
|
|
expect(merge_request.reload.metrics.first_deployed_to_production_at).to be_like_time(time)
|
2016-09-29 09:46:39 +05:30
|
|
|
|
|
|
|
# Current deploy
|
2017-09-10 17:25:29 +05:30
|
|
|
service = described_class.new(job)
|
2016-09-29 09:46:39 +05:30
|
|
|
Timecop.freeze(time + 12.hours) { service.execute }
|
|
|
|
|
2016-11-03 12:29:30 +05:30
|
|
|
expect(merge_request.reload.metrics.first_deployed_to_production_at).to be_like_time(time)
|
2016-09-29 09:46:39 +05:30
|
|
|
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
|
|
|
|
time = 5.minutes.from_now
|
|
|
|
Timecop.freeze(time) { service.execute }
|
|
|
|
|
|
|
|
expect(merge_request.reload.metrics.merged_at).to be < merge_request.reload.metrics.first_deployed_to_production_at
|
|
|
|
|
|
|
|
merge_request.reload.metrics.update(first_deployed_to_production_at: nil)
|
|
|
|
|
|
|
|
expect(merge_request.reload.metrics.first_deployed_to_production_at).to be_nil
|
|
|
|
|
|
|
|
# Current deploy
|
2017-09-10 17:25:29 +05:30
|
|
|
service = described_class.new(job)
|
2016-09-29 09:46:39 +05:30
|
|
|
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-16 23:09:34 +05:30
|
|
|
end
|