debian-mirror-gitlab/spec/workers/pipeline_schedule_worker_spec.rb

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

136 lines
3.6 KiB
Ruby
Raw Normal View History

2019-07-07 11:18:12 +05:30
# frozen_string_literal: true
2017-08-17 22:00:37 +05:30
require 'spec_helper'
2023-03-17 16:20:25 +05:30
RSpec.describe PipelineScheduleWorker, :sidekiq_inline, feature_category: :continuous_integration do
2019-07-07 11:18:12 +05:30
include ExclusiveLeaseHelpers
2017-08-17 22:00:37 +05:30
subject { described_class.new.perform }
2020-04-08 14:13:33 +05:30
let_it_be(:project) { create(:project, :repository) }
let_it_be(:user) { create(:user) }
2017-08-17 22:00:37 +05:30
let!(:pipeline_schedule) do
create(:ci_pipeline_schedule, :nightly, project: project, owner: user)
end
before do
2019-02-15 15:39:39 +05:30
stub_application_setting(auto_devops_enabled: false)
2017-08-17 22:00:37 +05:30
stub_ci_pipeline_to_return_yaml_file
2023-03-04 22:38:38 +05:30
end
2017-08-17 22:00:37 +05:30
2023-03-04 22:38:38 +05:30
around do |example|
travel_to(pipeline_schedule.next_run_at + 1.hour) do
example.run
end
2017-09-10 17:25:29 +05:30
end
2017-08-17 22:00:37 +05:30
2017-09-10 17:25:29 +05:30
context 'when the schedule is runnable by the user' do
2017-08-17 22:00:37 +05:30
before do
2018-11-18 11:00:15 +05:30
project.add_maintainer(user)
2017-08-17 22:00:37 +05:30
end
2023-03-17 16:20:25 +05:30
context 'when there is a scheduled pipeline within next_run_at' do
2018-03-17 18:26:18 +05:30
shared_examples 'successful scheduling' do
2023-03-04 22:38:38 +05:30
it 'creates a new pipeline' do
2019-02-15 15:39:39 +05:30
expect { subject }.to change { project.ci_pipelines.count }.by(1)
2023-03-04 22:38:38 +05:30
last_pipeline = project.ci_pipelines.last
expect(last_pipeline).to be_schedule
expect(last_pipeline.pipeline_schedule).to eq(pipeline_schedule)
end
it 'updates next_run_at' do
expect { subject }.to change { pipeline_schedule.reload.next_run_at }.by(1.day)
end
2018-03-17 18:26:18 +05:30
2023-03-04 22:38:38 +05:30
it 'does not change active status' do
expect { subject }.not_to change { pipeline_schedule.reload.active? }.from(true)
2018-03-17 18:26:18 +05:30
end
2017-09-10 17:25:29 +05:30
end
2017-08-17 22:00:37 +05:30
2023-04-23 21:23:45 +05:30
it_behaves_like 'successful scheduling'
2017-09-10 17:25:29 +05:30
2018-03-17 18:26:18 +05:30
context 'when the latest commit contains [ci skip]' do
before do
allow_any_instance_of(Ci::Pipeline)
.to receive(:git_commit_message)
.and_return('some commit [ci skip]')
end
2017-09-10 17:25:29 +05:30
2023-04-23 21:23:45 +05:30
it_behaves_like 'successful scheduling'
2017-09-10 17:25:29 +05:30
end
2017-08-17 22:00:37 +05:30
end
2018-03-17 18:26:18 +05:30
context 'when the schedule is deactivated' do
2017-09-10 17:25:29 +05:30
before do
pipeline_schedule.deactivate!
end
it 'does not creates a new pipeline' do
2019-02-15 15:39:39 +05:30
expect { subject }.not_to change { project.ci_pipelines.count }
end
end
context 'when gitlab-ci.yml is corrupted' do
before do
stub_ci_pipeline_yaml_file(YAML.dump(rspec: { variables: 'rspec' } ))
end
2019-09-04 21:01:54 +05:30
it 'does not creates a new pipeline' do
expect { subject }.not_to change { project.ci_pipelines.count }
2017-09-10 17:25:29 +05:30
end
2017-08-17 22:00:37 +05:30
end
end
2017-09-10 17:25:29 +05:30
context 'when the schedule is not runnable by the user' do
2019-02-15 15:39:39 +05:30
it 'does not deactivate the schedule' do
2017-09-10 17:25:29 +05:30
subject
2019-02-15 15:39:39 +05:30
expect(pipeline_schedule.reload.active).to be_truthy
end
it 'does not create a pipeline' do
expect { subject }.not_to change { project.ci_pipelines.count }
end
it 'does not raise an exception' do
expect { subject }.not_to raise_error
end
end
context 'when .gitlab-ci.yml is missing in the project' do
before do
stub_ci_pipeline_yaml_file(nil)
project.add_maintainer(user)
end
it 'does not create a pipeline' do
expect { subject }.not_to change { project.ci_pipelines.count }
2017-08-17 22:00:37 +05:30
end
2019-02-15 15:39:39 +05:30
it 'does not raise an exception' do
expect { subject }.not_to raise_error
2017-08-17 22:00:37 +05:30
end
end
2022-04-04 11:22:00 +05:30
context 'when the project is missing' do
before do
project.delete
end
it 'does not raise an exception' do
expect { subject }.not_to raise_error
end
end
2023-03-17 16:20:25 +05:30
context 'when max retry attempts reach' do
let!(:lease) { stub_exclusive_lease_taken(described_class.name.underscore) }
it 'does not raise error' do
expect(lease).to receive(:try_obtain).exactly(described_class::LOCK_RETRY + 1).times
expect { subject }.to raise_error(Gitlab::ExclusiveLeaseHelpers::FailedToObtainLockError)
end
end
2017-08-17 22:00:37 +05:30
end