BigW Consortium Gitlab

Commit 2fa766e1 by Lin Jen-Shin

Only deploy if environment exists; Update tests accordingly

parent 4968f226
...@@ -27,7 +27,7 @@ class CreateDeploymentService ...@@ -27,7 +27,7 @@ class CreateDeploymentService
private private
def executable? def executable?
project && job.environment.present? project && job.environment.present? && environment
end end
def deploy def deploy
......
...@@ -295,5 +295,20 @@ describe Ci::CreatePipelineService, services: true do ...@@ -295,5 +295,20 @@ describe Ci::CreatePipelineService, services: true do
expect(Environment.find_by(name: "review/master")).not_to be_nil expect(Environment.find_by(name: "review/master")).not_to be_nil
end end
end end
context 'when environment with invalid name' do
before do
config = YAML.dump(deploy: { environment: { name: 'name,with,commas' }, script: 'ls' })
stub_ci_pipeline_yaml_file(config)
end
it 'does not create an environment' do
expect do
result = execute_service
expect(result).to be_persisted
end.not_to change { Environment.count }
end
end
end end
end end
...@@ -14,84 +14,68 @@ describe CreateDeploymentService, services: true do ...@@ -14,84 +14,68 @@ describe CreateDeploymentService, services: true do
let(:project) { job.project } let(:project) { job.project }
let!(:environment) do
create(:environment, project: project, name: 'production')
end
let(:service) { described_class.new(job) } let(:service) { described_class.new(job) }
describe '#execute' do describe '#execute' do
subject { service.execute } subject { service.execute }
context 'when no environments exist' do context 'when environment exists' do
it 'does create a new environment' do it 'creates a deployment' do
expect { subject }.to change { Environment.count }.by(1)
end
it 'does create a deployment' do
expect(subject).to be_persisted expect(subject).to be_persisted
end end
end end
context 'when environment exist' do context 'when environment does not exist' do
let!(:environment) { create(:environment, project: project, name: 'production') } let(:environment) {}
it 'does not create a new environment' do it 'does not create a deployment' do
expect { subject }.not_to change { Environment.count } expect do
end expect(subject).to be_nil
end.not_to change { Deployment.count }
it 'does create a deployment' do
expect(subject).to be_persisted
end end
end
context 'and start action is defined' do context 'when start action is defined' do
let(:options) { { action: 'start' } } let(:options) { { action: 'start' } }
context 'and environment is stopped' do context 'and environment is stopped' do
before do before do
environment.stop environment.stop
end end
it 'makes environment available' do it 'makes environment available' do
subject subject
expect(environment.reload).to be_available expect(environment.reload).to be_available
end end
it 'does create a deployment' do it 'creates a deployment' do
expect(subject).to be_persisted expect(subject).to be_persisted
end
end end
end end
end
context 'and stop action is defined' do context 'when stop action is defined' do
let(:options) { { action: 'stop' } } 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 context 'and environment is available' do
expect(subject).to be_nil before do
end environment.start
end end
end
end
context 'for environment with invalid name' do it 'makes environment stopped' do
before do subject
job.update(environment: 'name,with,commas')
end
it 'does not create a new environment' do expect(environment.reload).to be_stopped
expect { subject }.not_to change { Environment.count } end
end
it 'does not create a deployment' do it 'does not create a deployment' do
expect(subject).to be_nil expect(subject).to be_nil
end
end end
end end
...@@ -102,41 +86,29 @@ describe CreateDeploymentService, services: true do ...@@ -102,41 +86,29 @@ describe CreateDeploymentService, services: true do
end end
before do before do
environment.update(name: 'review-apps/master')
job.update(environment: 'review-apps/$CI_COMMIT_REF_NAME') job.update(environment: 'review-apps/$CI_COMMIT_REF_NAME')
end end
it 'does create a new environment' do it 'creates a new deployment' do
expect { subject }.to change { Environment.count }.by(1)
expect(subject.environment.name).to eq('review-apps/master')
expect(subject.environment.external_url).to eq('http://master.review-apps.gitlab.com')
end
it 'does create a new deployment' do
expect(subject).to be_persisted expect(subject).to be_persisted
end end
context 'and environment exist' do it 'does not create a new environment' do
let!(:environment) { create(:environment, project: project, name: 'review-apps/master') } expect { subject }.not_to change { Environment.count }
end
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/master') it 'updates external url' do
expect(subject.environment.external_url).to eq('http://master.review-apps.gitlab.com') subject
end
it 'does create a new deployment' do expect(subject.environment.name).to eq('review-apps/master')
expect(subject).to be_persisted expect(subject.environment.external_url).to eq('http://master.review-apps.gitlab.com')
end
end end
end end
context 'when project was removed' do context 'when project was removed' do
let(:environment) {}
before do before do
job.update(project: nil) job.update(project: nil)
end end
...@@ -151,34 +123,26 @@ describe CreateDeploymentService, services: true do ...@@ -151,34 +123,26 @@ describe CreateDeploymentService, services: true do
end end
describe 'processing of builds' do describe 'processing of builds' do
let(:environment) { nil } shared_examples 'does not create deployment' do
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 it 'does not create a new deployment' do
expect { subject }.not_to change { Deployment.count } expect { subject }.not_to change { Deployment.count }
end end
it 'does not call a service' do it 'does not call a service' do
expect_any_instance_of(described_class).not_to receive(:execute) expect_any_instance_of(described_class).not_to receive(:execute)
subject subject
end end
end end
shared_examples 'does create environment and deployment' do shared_examples 'creates deployment' do
it 'does create a new environment' do it 'creates a new deployment' 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) expect { subject }.to change { Deployment.count }.by(1)
end end
it 'does call a service' do it 'calls a service' do
expect_any_instance_of(described_class).to receive(:execute) expect_any_instance_of(described_class).to receive(:execute)
subject subject
end end
...@@ -188,7 +152,7 @@ describe CreateDeploymentService, services: true do ...@@ -188,7 +152,7 @@ describe CreateDeploymentService, services: true do
expect(Deployment.last.deployable).to eq(deployable) expect(Deployment.last.deployable).to eq(deployable)
end end
it 'create environment has URL set' do it 'updates environment URL' do
subject subject
expect(Deployment.last.environment.external_url).not_to be_nil expect(Deployment.last.environment.external_url).not_to be_nil
...@@ -196,41 +160,39 @@ describe CreateDeploymentService, services: true do ...@@ -196,41 +160,39 @@ describe CreateDeploymentService, services: true do
end end
context 'without environment specified' do context 'without environment specified' do
let(:build) { create(:ci_build, project: project) } let(:job) { create(:ci_build) }
it_behaves_like 'does not create environment and deployment' do it_behaves_like 'does not create deployment' do
subject { build.success } subject { job.success }
end end
end end
context 'when environment is specified' do context 'when environment is specified' do
let(:pipeline) { create(:ci_pipeline, project: project) } let(:deployable) { job }
let(:build) { create(:ci_build, pipeline: pipeline, environment: 'production', options: options) }
let(:options) do let(:options) do
{ environment: { name: 'production', url: 'http://gitlab.com' } } { environment: { name: 'production', url: 'http://gitlab.com' } }
end end
context 'when build succeeds' do context 'when job succeeds' do
it_behaves_like 'does create environment and deployment' do it_behaves_like 'creates deployment' do
let(:deployable) { build } subject { job.success }
subject { build.success }
end end
end end
context 'when build fails' do context 'when job fails' do
it_behaves_like 'does not create environment and deployment' do it_behaves_like 'does not create deployment' do
subject { build.drop } subject { job.drop }
end end
end end
context 'when build is retried' do context 'when job is retried' do
it_behaves_like 'does create environment and deployment' do it_behaves_like 'creates deployment' do
before do before do
project.add_developer(user) project.add_developer(user)
end end
let(:deployable) { Ci::Build.retry(build, user) } let(:deployable) { Ci::Build.retry(job, user) }
subject { deployable.success } subject { deployable.success }
end end
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment