summaryrefslogtreecommitdiff
path: root/spec/services/environments/stop_stale_service_spec.rb
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2023-01-18 19:00:14 +0000
committerGitLab Bot <gitlab-bot@gitlab.com>2023-01-18 19:00:14 +0000
commit05f0ebba3a2c8ddf39e436f412dc2ab5bf1353b2 (patch)
tree11d0f2a6ec31c7793c184106cedc2ded3d9a2cc5 /spec/services/environments/stop_stale_service_spec.rb
parentec73467c23693d0db63a797d10194da9e72a74af (diff)
downloadgitlab-ce-05f0ebba3a2c8ddf39e436f412dc2ab5bf1353b2.tar.gz
Add latest changes from gitlab-org/gitlab@15-8-stable-eev15.8.0-rc42
Diffstat (limited to 'spec/services/environments/stop_stale_service_spec.rb')
-rw-r--r--spec/services/environments/stop_stale_service_spec.rb49
1 files changed, 49 insertions, 0 deletions
diff --git a/spec/services/environments/stop_stale_service_spec.rb b/spec/services/environments/stop_stale_service_spec.rb
new file mode 100644
index 00000000000..46d770c30cc
--- /dev/null
+++ b/spec/services/environments/stop_stale_service_spec.rb
@@ -0,0 +1,49 @@
+# frozen_string_literal: true
+
+require 'spec_helper'
+
+RSpec.describe Environments::StopStaleService,
+ :clean_gitlab_redis_shared_state,
+ :sidekiq_inline,
+ feature_category: :continuous_delivery do
+ let_it_be(:project) { create(:project, :repository) }
+ let_it_be(:user) { create(:user) }
+
+ let(:params) { { after: nil } }
+ let(:service) { described_class.new(project, user, params) }
+
+ describe '#execute' do
+ subject { service.execute }
+
+ let_it_be(:project) { create(:project, :repository) }
+ let_it_be(:user) { create(:user) }
+ let_it_be(:stale_environment) { create(:environment, project: project, updated_at: 2.weeks.ago) }
+ let_it_be(:stale_environment2) { create(:environment, project: project, updated_at: 2.weeks.ago) }
+ let_it_be(:recent_environment) { create(:environment, project: project, updated_at: Date.today) }
+
+ let_it_be(:params) { { before: 1.week.ago } }
+
+ before do
+ allow(service).to receive(:can?).with(user, :stop_environment, project).and_return(true)
+ end
+
+ it 'only stops stale environments' do
+ spy_service = Environments::AutoStopWorker.new
+
+ allow(Environments::AutoStopWorker).to receive(:new) { spy_service }
+
+ expect(spy_service).to receive(:perform).with(stale_environment.id).and_call_original
+ expect(spy_service).to receive(:perform).with(stale_environment2.id).and_call_original
+ expect(spy_service).not_to receive(:perform).with(recent_environment.id)
+
+ expect(Environment).to receive(:deployed_and_updated_before).with(project.id, params[:before]).and_call_original
+ expect(Environment).to receive(:without_protected).with(project).and_call_original
+
+ expect(subject.success?).to be_truthy
+
+ expect(stale_environment.reload).to be_stopped
+ expect(stale_environment2.reload).to be_stopped
+ expect(recent_environment.reload).to be_available
+ end
+ end
+end