diff options
Diffstat (limited to 'spec/workers/environments/auto_stop_worker_spec.rb')
-rw-r--r-- | spec/workers/environments/auto_stop_worker_spec.rb | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/spec/workers/environments/auto_stop_worker_spec.rb b/spec/workers/environments/auto_stop_worker_spec.rb new file mode 100644 index 00000000000..1983cfa18ea --- /dev/null +++ b/spec/workers/environments/auto_stop_worker_spec.rb @@ -0,0 +1,71 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Environments::AutoStopWorker do + include CreateEnvironmentsHelpers + + subject { worker.perform(environment_id) } + + let_it_be(:project) { create(:project, :repository) } + let_it_be(:developer) { create(:user).tap { |u| project.add_developer(u) } } + let_it_be(:reporter) { create(:user).tap { |u| project.add_reporter(u) } } + + before_all do + project.repository.add_branch(developer, 'review/feature', 'master') + end + + let!(:environment) { create_review_app(user, project, 'review/feature').environment } + let(:environment_id) { environment.id } + let(:worker) { described_class.new } + let(:user) { developer } + + it 'stops the environment' do + expect { subject } + .to change { Environment.find_by_name('review/feature').state } + .from('available').to('stopped') + end + + it 'executes the stop action' do + expect { subject } + .to change { Ci::Build.find_by_name('stop_review_app').status } + .from('manual').to('pending') + end + + context 'when user does not have a permission to play the stop action' do + let(:user) { reporter } + + it 'raises an error' do + expect { subject }.to raise_error(Gitlab::Access::AccessDeniedError) + end + end + + context 'when the environment has already been stopped' do + before do + environment.stop! + end + + it 'does not execute the stop action' do + expect { subject } + .not_to change { Ci::Build.find_by_name('stop_review_app').status } + end + end + + context 'when there are no deployments and associted stop actions' do + let!(:environment) { create(:environment) } + + it 'stops the environment' do + subject + + expect(environment.reload).to be_stopped + end + end + + context 'when there are no corresponding environment record' do + let!(:environment) { double(:environment, id: non_existing_record_id) } + + it 'ignores the invalid record' do + expect { subject }.not_to raise_error + end + end +end |