2016-11-21 11:26:35 -05:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2017-07-10 10:24:02 -04:00
|
|
|
describe Gitlab::SlashCommands::Deploy do
|
2016-11-21 11:26:35 -05:00
|
|
|
describe '#execute' do
|
2017-08-02 15:55:11 -04:00
|
|
|
let(:project) { create(:project) }
|
2016-11-21 11:26:35 -05:00
|
|
|
let(:user) { create(:user) }
|
|
|
|
let(:regex_match) { described_class.match('deploy staging to production') }
|
|
|
|
|
|
|
|
before do
|
2017-05-31 05:09:29 -04:00
|
|
|
# Make it possible to trigger protected manual actions for developers.
|
|
|
|
#
|
|
|
|
project.add_developer(user)
|
|
|
|
|
|
|
|
create(:protected_branch, :developers_can_merge,
|
|
|
|
name: 'master', project: project)
|
2016-11-21 11:26:35 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
subject do
|
|
|
|
described_class.new(project, user).execute(regex_match)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'if no environment is defined' do
|
2017-01-10 13:43:58 -05:00
|
|
|
it 'does not execute an action' do
|
|
|
|
expect(subject[:response_type]).to be(:ephemeral)
|
2017-07-28 06:16:07 -04:00
|
|
|
expect(subject[:text]).to eq "Couldn't find a deployment manual action."
|
2016-11-21 11:26:35 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with environment' do
|
|
|
|
let!(:staging) { create(:environment, name: 'staging', project: project) }
|
2017-04-06 07:40:04 -04:00
|
|
|
let!(:pipeline) { create(:ci_pipeline, project: project) }
|
|
|
|
let!(:build) { create(:ci_build, pipeline: pipeline) }
|
2016-11-21 11:26:35 -05:00
|
|
|
let!(:deployment) { create(:deployment, environment: staging, deployable: build) }
|
|
|
|
|
|
|
|
context 'without actions' do
|
2017-01-10 13:43:58 -05:00
|
|
|
it 'does not execute an action' do
|
|
|
|
expect(subject[:response_type]).to be(:ephemeral)
|
2017-07-28 06:16:07 -04:00
|
|
|
expect(subject[:text]).to eq "Couldn't find a deployment manual action."
|
2016-11-21 11:26:35 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-07-28 04:56:50 -04:00
|
|
|
context 'when single action has been matched' do
|
|
|
|
before do
|
2017-04-06 07:40:04 -04:00
|
|
|
create(:ci_build, :manual, pipeline: pipeline,
|
|
|
|
name: 'first',
|
|
|
|
environment: 'production')
|
2016-11-21 11:26:35 -05:00
|
|
|
end
|
|
|
|
|
2016-11-21 13:40:04 -05:00
|
|
|
it 'returns success result' do
|
2017-01-10 13:43:58 -05:00
|
|
|
expect(subject[:response_type]).to be(:in_channel)
|
2017-07-28 04:56:50 -04:00
|
|
|
expect(subject[:text])
|
|
|
|
.to start_with('Deployment started from staging to production')
|
2016-11-21 11:26:35 -05:00
|
|
|
end
|
2017-07-28 04:56:50 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when more than one action has been matched' do
|
|
|
|
context 'when there is no specific actions with a environment name' do
|
|
|
|
before do
|
|
|
|
create(:ci_build, :manual, pipeline: pipeline,
|
|
|
|
name: 'first',
|
|
|
|
environment: 'production')
|
2016-11-21 11:26:35 -05:00
|
|
|
|
2017-04-06 07:40:04 -04:00
|
|
|
create(:ci_build, :manual, pipeline: pipeline,
|
|
|
|
name: 'second',
|
|
|
|
environment: 'production')
|
2016-11-21 11:26:35 -05:00
|
|
|
end
|
|
|
|
|
2017-07-28 04:56:50 -04:00
|
|
|
it 'returns error about too many actions defined' do
|
2017-07-28 06:16:07 -04:00
|
|
|
expect(subject[:text]).to eq("Couldn't find a deployment manual action.")
|
2017-01-10 13:43:58 -05:00
|
|
|
expect(subject[:response_type]).to be(:ephemeral)
|
2016-11-21 11:26:35 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-07-28 04:56:50 -04:00
|
|
|
context 'when one of the actions is environement specific action' do
|
|
|
|
before do
|
|
|
|
create(:ci_build, :manual, pipeline: pipeline,
|
|
|
|
name: 'first',
|
|
|
|
environment: 'production')
|
|
|
|
|
|
|
|
create(:ci_build, :manual, pipeline: pipeline,
|
|
|
|
name: 'production',
|
|
|
|
environment: 'production')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'deploys to production' do
|
|
|
|
expect(subject[:text])
|
|
|
|
.to start_with('Deployment started from staging to production')
|
|
|
|
expect(subject[:response_type]).to be(:in_channel)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when one of the actions is a teardown action' do
|
|
|
|
before do
|
|
|
|
create(:ci_build, :manual, pipeline: pipeline,
|
|
|
|
name: 'first',
|
|
|
|
environment: 'production')
|
|
|
|
|
2016-11-21 12:22:03 -05:00
|
|
|
create(:ci_build, :manual, :teardown_environment,
|
2017-04-06 07:40:04 -04:00
|
|
|
pipeline: pipeline, name: 'teardown', environment: 'production')
|
2016-11-21 11:26:35 -05:00
|
|
|
end
|
|
|
|
|
2017-07-28 04:56:50 -04:00
|
|
|
it 'deploys to production' do
|
|
|
|
expect(subject[:text])
|
|
|
|
.to start_with('Deployment started from staging to production')
|
2017-01-10 13:43:58 -05:00
|
|
|
expect(subject[:response_type]).to be(:in_channel)
|
2016-11-21 11:26:35 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'self.match' do
|
|
|
|
it 'matches the environment' do
|
|
|
|
match = described_class.match('deploy staging to production')
|
|
|
|
|
|
|
|
expect(match[:from]).to eq('staging')
|
|
|
|
expect(match[:to]).to eq('production')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|