diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2023-01-18 22:00:14 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2023-01-18 22:00:14 +0300 |
commit | 05f0ebba3a2c8ddf39e436f412dc2ab5bf1353b2 (patch) | |
tree | 11d0f2a6ec31c7793c184106cedc2ded3d9a2cc5 /spec/workers/run_pipeline_schedule_worker_spec.rb | |
parent | ec73467c23693d0db63a797d10194da9e72a74af (diff) |
Add latest changes from gitlab-org/gitlab@15-8-stable-eev15.8.0-rc42
Diffstat (limited to 'spec/workers/run_pipeline_schedule_worker_spec.rb')
-rw-r--r-- | spec/workers/run_pipeline_schedule_worker_spec.rb | 101 |
1 files changed, 80 insertions, 21 deletions
diff --git a/spec/workers/run_pipeline_schedule_worker_spec.rb b/spec/workers/run_pipeline_schedule_worker_spec.rb index 4fdf6149435..25158de3341 100644 --- a/spec/workers/run_pipeline_schedule_worker_spec.rb +++ b/spec/workers/run_pipeline_schedule_worker_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' -RSpec.describe RunPipelineScheduleWorker do +RSpec.describe RunPipelineScheduleWorker, feature_category: :continuous_integration do it 'has an until_executed deduplicate strategy' do expect(described_class.get_deduplicate_strategy).to eq(:until_executed) end @@ -21,6 +21,11 @@ RSpec.describe RunPipelineScheduleWorker do end end + it 'accepts an option' do + expect { worker.perform(pipeline_schedule.id, user.id, {}) }.not_to raise_error + expect { worker.perform(pipeline_schedule.id, user.id, {}, {}) }.to raise_error(ArgumentError) + end + context 'when a schedule not found' do it 'does not call the Service' do expect(Ci::CreatePipelineService).not_to receive(:new) @@ -56,37 +61,91 @@ RSpec.describe RunPipelineScheduleWorker do let(:create_pipeline_service) { instance_double(Ci::CreatePipelineService, execute: service_response) } let(:service_response) { instance_double(ServiceResponse, payload: pipeline, error?: false) } - before do - expect(Ci::CreatePipelineService).to receive(:new).with(project, user, ref: pipeline_schedule.ref).and_return(create_pipeline_service) + context 'when pipeline can be created' do + before do + expect(Ci::CreatePipelineService).to receive(:new).with(project, user, ref: pipeline_schedule.ref).and_return(create_pipeline_service) - expect(create_pipeline_service).to receive(:execute).with(:schedule, ignore_skip_ci: true, save_on_errors: false, schedule: pipeline_schedule).and_return(service_response) - end + expect(create_pipeline_service).to receive(:execute).with(:schedule, ignore_skip_ci: true, save_on_errors: false, schedule: pipeline_schedule).and_return(service_response) + end + + context "when pipeline is persisted" do + let(:pipeline) { instance_double(Ci::Pipeline, persisted?: true) } - context "when pipeline is persisted" do - let(:pipeline) { instance_double(Ci::Pipeline, persisted?: true) } + it "returns the service response" do + expect(worker.perform(pipeline_schedule.id, user.id)).to eq(service_response) + end - it "returns the service response" do - expect(worker.perform(pipeline_schedule.id, user.id)).to eq(service_response) + it "does not log errors" do + expect(worker).not_to receive(:log_extra_metadata_on_done) + + expect(worker.perform(pipeline_schedule.id, user.id)).to eq(service_response) + end + + it "changes the next_run_at" do + expect { worker.perform(pipeline_schedule.id, user.id) }.to change { pipeline_schedule.reload.next_run_at }.by(1.day) + end + + context 'when feature flag ci_use_run_pipeline_schedule_worker is disabled' do + before do + stub_feature_flags(ci_use_run_pipeline_schedule_worker: false) + end + + it 'does not change the next_run_at' do + expect { worker.perform(pipeline_schedule.id, user.id) }.not_to change { pipeline_schedule.reload.next_run_at } + end + end end - it "does not log errors" do - expect(worker).not_to receive(:log_extra_metadata_on_done) + context "when pipeline was not persisted" do + let(:service_response) { instance_double(ServiceResponse, error?: true, message: "Error", payload: pipeline) } + let(:pipeline) { instance_double(Ci::Pipeline, persisted?: false) } + + it "logs a pipeline creation error" do + expect(worker) + .to receive(:log_extra_metadata_on_done) + .with(:pipeline_creation_error, service_response.message) + .and_call_original - expect(worker.perform(pipeline_schedule.id, user.id)).to eq(service_response) + expect(worker.perform(pipeline_schedule.id, user.id)).to eq(service_response.message) + end end end - context "when pipeline was not persisted" do - let(:service_response) { instance_double(ServiceResponse, error?: true, message: "Error", payload: pipeline) } - let(:pipeline) { instance_double(Ci::Pipeline, persisted?: false) } + context 'when schedule is already executed' do + let(:time_in_future) { 1.hour.since } + + before do + pipeline_schedule.update_column(:next_run_at, time_in_future) + end + + it 'does not change the next_run_at' do + expect { worker.perform(pipeline_schedule.id, user.id) }.to not_change { pipeline_schedule.reload.next_run_at } + end + + it 'does not create a pipeline' do + expect(Ci::CreatePipelineService).not_to receive(:new) + + worker.perform(pipeline_schedule.id, user.id) + end + + context 'when feature flag ci_use_run_pipeline_schedule_worker is disabled' do + let(:pipeline) { instance_double(Ci::Pipeline, persisted?: true) } + + before do + stub_feature_flags(ci_use_run_pipeline_schedule_worker: false) + + expect(Ci::CreatePipelineService).to receive(:new).with(project, user, ref: pipeline_schedule.ref).and_return(create_pipeline_service) + + expect(create_pipeline_service).to receive(:execute).with(:schedule, ignore_skip_ci: true, save_on_errors: false, schedule: pipeline_schedule).and_return(service_response) + end - it "logs a pipeline creation error" do - expect(worker) - .to receive(:log_extra_metadata_on_done) - .with(:pipeline_creation_error, service_response.message) - .and_call_original + it 'does not change the next_run_at' do + expect { worker.perform(pipeline_schedule.id, user.id) }.to not_change { pipeline_schedule.reload.next_run_at } + end - expect(worker.perform(pipeline_schedule.id, user.id)).to eq(service_response.message) + it "returns the service response" do + expect(worker.perform(pipeline_schedule.id, user.id)).to eq(service_response) + end end end end |