debian-mirror-gitlab/spec/lib/gitlab/slash_commands/deploy_spec.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

183 lines
6.3 KiB
Ruby
Raw Normal View History

2019-12-04 20:38:33 +05:30
# frozen_string_literal: true
2017-08-17 22:00:37 +05:30
require 'spec_helper'
2020-07-28 23:09:34 +05:30
RSpec.describe Gitlab::SlashCommands::Deploy do
2017-08-17 22:00:37 +05:30
describe '#execute' do
2020-03-13 15:44:24 +05:30
let(:project) { create(:project, :repository) }
2017-08-17 22:00:37 +05:30
let(:user) { create(:user) }
2018-03-27 19:54:05 +05:30
let(:chat_name) { double(:chat_name, user: user) }
2017-08-17 22:00:37 +05:30
let(:regex_match) { described_class.match('deploy staging to production') }
before do
# Make it possible to trigger protected manual actions for developers.
#
project.add_developer(user)
create(:protected_branch, :developers_can_merge,
name: 'master', project: project)
end
subject do
2018-03-27 19:54:05 +05:30
described_class.new(project, chat_name).execute(regex_match)
2017-08-17 22:00:37 +05:30
end
context 'if no environment is defined' do
it 'does not execute an action' do
expect(subject[:response_type]).to be(:ephemeral)
2017-09-10 17:25:29 +05:30
expect(subject[:text]).to eq "Couldn't find a deployment manual action."
2017-08-17 22:00:37 +05:30
end
end
context 'with environment' do
let!(:staging) { create(:environment, name: 'staging', project: project) }
let!(:pipeline) { create(:ci_pipeline, project: project) }
2022-07-23 23:45:48 +05:30
let!(:build) { create(:ci_build, pipeline: pipeline, environment: 'production') }
2018-12-13 13:39:08 +05:30
let!(:deployment) { create(:deployment, :success, environment: staging, deployable: build) }
2017-08-17 22:00:37 +05:30
context 'without actions' do
it 'does not execute an action' do
expect(subject[:response_type]).to be(:ephemeral)
2017-09-10 17:25:29 +05:30
expect(subject[:text]).to eq "Couldn't find a deployment manual action."
2017-08-17 22:00:37 +05:30
end
end
2017-09-10 17:25:29 +05:30
context 'when single action has been matched' do
before do
2017-08-17 22:00:37 +05:30
create(:ci_build, :manual, pipeline: pipeline,
name: 'first',
environment: 'production')
end
it 'returns success result' do
expect(subject[:response_type]).to be(:in_channel)
2017-09-10 17:25:29 +05:30
expect(subject[:text])
.to start_with('Deployment started from staging to production')
2017-08-17 22:00:37 +05:30
end
2017-09-10 17:25:29 +05:30
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')
2017-08-17 22:00:37 +05:30
create(:ci_build, :manual, pipeline: pipeline,
name: 'second',
environment: 'production')
end
2017-09-10 17:25:29 +05:30
it 'returns error about too many actions defined' do
expect(subject[:text]).to eq("Couldn't find a deployment manual action.")
2017-08-17 22:00:37 +05:30
expect(subject[:response_type]).to be(:ephemeral)
end
end
2017-09-10 17:25:29 +05:30
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')
2017-08-17 22:00:37 +05:30
create(:ci_build, :manual, :teardown_environment,
pipeline: pipeline, name: 'teardown', environment: 'production')
end
2017-09-10 17:25:29 +05:30
it 'deploys to production' do
expect(subject[:text])
.to start_with('Deployment started from staging to production')
2017-08-17 22:00:37 +05:30
expect(subject[:response_type]).to be(:in_channel)
end
end
end
2021-12-07 22:27:20 +05:30
context 'with extra spaces in the deploy command' do
let(:regex_match) { described_class.match('deploy staging to production ') }
before do
create(:ci_build, :manual, pipeline: pipeline, name: 'production', environment: 'production')
create(:ci_build, :manual, pipeline: pipeline, name: 'not prod', environment: 'not prod')
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
2017-08-17 22:00:37 +05:30
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
2021-12-07 22:27:20 +05:30
it 'matches the environment with spaces in it' do
match = described_class.match('deploy staging env to production env')
expect(match[:from]).to eq('staging env')
expect(match[:to]).to eq('production env')
end
it 'matches the environment name with surrounding spaces' do
match = described_class.match('deploy staging to production ')
# The extra spaces are stripped later in the code
expect(match[:from]).to eq('staging')
expect(match[:to]).to eq('production')
end
it 'returns nil for text that is not a deploy command' do
match = described_class.match('foo bar')
expect(match).to be_nil
end
it 'returns nil for a partial command' do
match = described_class.match('deploy staging to ')
expect(match).to be_nil
end
context 'with ReDoS attempts' do
def duration_for(&block)
start = Time.zone.now
yield if block_given?
Time.zone.now - start
end
it 'has smaller than linear execution time growth with a malformed "to"' do
Timeout.timeout(3.seconds) do
sample1 = duration_for { described_class.match("deploy abc t" + "o" * 1000 + "X") }
sample2 = duration_for { described_class.match("deploy abc t" + "o" * 4000 + "X") }
expect((sample2 / sample1) < 4).to be_truthy
end
end
end
2017-08-17 22:00:37 +05:30
end
end