2019-07-07 11:18:12 +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 Ci::PipelineSchedule do
|
2021-12-11 22:18:48 +05:30
|
|
|
let_it_be_with_reload(:project) { create_default(:project) }
|
2021-09-04 01:27:46 +05:30
|
|
|
|
2019-09-04 21:01:54 +05:30
|
|
|
subject { build(:ci_pipeline_schedule) }
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
it { is_expected.to belong_to(:project) }
|
|
|
|
it { is_expected.to belong_to(:owner) }
|
|
|
|
|
|
|
|
it { is_expected.to have_many(:pipelines) }
|
2017-09-10 17:25:29 +05:30
|
|
|
it { is_expected.to have_many(:variables) }
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
it { is_expected.to respond_to(:ref) }
|
|
|
|
it { is_expected.to respond_to(:cron) }
|
|
|
|
it { is_expected.to respond_to(:cron_timezone) }
|
|
|
|
it { is_expected.to respond_to(:description) }
|
|
|
|
it { is_expected.to respond_to(:next_run_at) }
|
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
it_behaves_like 'includes Limitable concern' do
|
2021-09-04 01:27:46 +05:30
|
|
|
subject { build(:ci_pipeline_schedule, project: project) }
|
2020-05-24 23:13:21 +05:30
|
|
|
end
|
|
|
|
|
2022-03-02 08:16:31 +05:30
|
|
|
it_behaves_like 'cleanup by a loose foreign key' do
|
|
|
|
let!(:parent) { create(:user) }
|
|
|
|
let!(:model) { create(:ci_pipeline_schedule, owner: parent) }
|
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
describe 'validations' do
|
2020-05-24 23:13:21 +05:30
|
|
|
it 'does not allow invalid cron patterns' do
|
2017-08-17 22:00:37 +05:30
|
|
|
pipeline_schedule = build(:ci_pipeline_schedule, cron: '0 0 0 * *')
|
|
|
|
|
|
|
|
expect(pipeline_schedule).not_to be_valid
|
|
|
|
end
|
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
it 'does not allow invalid cron patterns' do
|
2017-08-17 22:00:37 +05:30
|
|
|
pipeline_schedule = build(:ci_pipeline_schedule, cron_timezone: 'invalid')
|
|
|
|
|
|
|
|
expect(pipeline_schedule).not_to be_valid
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when active is false' do
|
|
|
|
it 'does not allow nullified ref' do
|
|
|
|
pipeline_schedule = build(:ci_pipeline_schedule, :inactive, ref: nil)
|
|
|
|
|
|
|
|
expect(pipeline_schedule).not_to be_valid
|
|
|
|
end
|
|
|
|
end
|
2019-07-31 22:56:46 +05:30
|
|
|
|
|
|
|
context 'when cron contains trailing whitespaces' do
|
|
|
|
it 'strips the attribute' do
|
|
|
|
pipeline_schedule = build(:ci_pipeline_schedule, cron: ' 0 0 * * * ')
|
|
|
|
|
|
|
|
expect(pipeline_schedule).to be_valid
|
|
|
|
expect(pipeline_schedule.cron).to eq('0 0 * * *')
|
|
|
|
end
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
2019-09-04 21:01:54 +05:30
|
|
|
describe '.runnable_schedules' do
|
|
|
|
subject { described_class.runnable_schedules }
|
|
|
|
|
|
|
|
let!(:pipeline_schedule) do
|
2021-01-03 14:25:43 +05:30
|
|
|
travel_to(1.day.ago) do
|
2019-09-04 21:01:54 +05:30
|
|
|
create(:ci_pipeline_schedule, :hourly)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns the runnable schedule' do
|
|
|
|
is_expected.to eq([pipeline_schedule])
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when there are no runnable schedules' do
|
|
|
|
let!(:pipeline_schedule) { }
|
|
|
|
|
|
|
|
it 'returns an empty array' do
|
|
|
|
is_expected.to be_empty
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.preloaded' do
|
|
|
|
subject { described_class.preloaded }
|
|
|
|
|
|
|
|
before do
|
|
|
|
create_list(:ci_pipeline_schedule, 3)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'preloads the associations' do
|
|
|
|
subject
|
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
query = ActiveRecord::QueryRecorder.new { subject.map(&:project).each(&:route) }
|
2019-09-04 21:01:54 +05:30
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
expect(query.count).to eq(3)
|
2019-09-04 21:01:54 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-04-29 21:17:54 +05:30
|
|
|
describe '.owned_by' do
|
|
|
|
let(:user) { create(:user) }
|
|
|
|
let!(:owned_pipeline_schedule) { create(:ci_pipeline_schedule, owner: user) }
|
|
|
|
let!(:other_pipeline_schedule) { create(:ci_pipeline_schedule) }
|
|
|
|
|
|
|
|
subject { described_class.owned_by(user) }
|
|
|
|
|
|
|
|
it 'returns owned pipeline schedules' do
|
|
|
|
is_expected.to eq([owned_pipeline_schedule])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
describe '#set_next_run_at' do
|
2021-09-04 01:27:46 +05:30
|
|
|
using RSpec::Parameterized::TableSyntax
|
|
|
|
|
2021-11-11 11:23:49 +05:30
|
|
|
where(:worker_cron, :schedule_cron, :plan_limit, :now, :result) do
|
|
|
|
'0 1 2 3 *' | '0 1 * * *' | nil | Time.zone.local(2021, 3, 2, 1, 0) | Time.zone.local(2022, 3, 2, 1, 0)
|
|
|
|
'0 1 2 3 *' | '0 1 * * *' | (1.day.in_minutes / 1.hour.in_minutes).to_i | Time.zone.local(2021, 3, 2, 1, 0) | Time.zone.local(2022, 3, 2, 1, 0)
|
|
|
|
'*/5 * * * *' | '*/1 * * * *' | nil | Time.zone.local(2021, 5, 27, 11, 0) | Time.zone.local(2021, 5, 27, 11, 5)
|
|
|
|
'*/5 * * * *' | '*/1 * * * *' | (1.day.in_minutes / 1.hour.in_minutes).to_i | Time.zone.local(2021, 5, 27, 11, 0) | Time.zone.local(2021, 5, 27, 12, 0)
|
|
|
|
'*/5 * * * *' | '*/1 * * * *' | (1.day.in_minutes / 10).to_i | Time.zone.local(2021, 5, 27, 11, 0) | Time.zone.local(2021, 5, 27, 11, 10)
|
|
|
|
'*/5 * * * *' | '*/1 * * * *' | 200 | Time.zone.local(2021, 5, 27, 11, 0) | Time.zone.local(2021, 5, 27, 11, 10)
|
|
|
|
'*/5 * * * *' | '0 * * * *' | nil | Time.zone.local(2021, 5, 27, 11, 0) | Time.zone.local(2021, 5, 27, 12, 5)
|
|
|
|
'*/5 * * * *' | '0 * * * *' | (1.day.in_minutes / 10).to_i | Time.zone.local(2021, 5, 27, 11, 0) | Time.zone.local(2021, 5, 27, 12, 0)
|
|
|
|
'*/5 * * * *' | '0 * * * *' | (1.day.in_minutes / 1.hour.in_minutes).to_i | Time.zone.local(2021, 5, 27, 11, 0) | Time.zone.local(2021, 5, 27, 12, 0)
|
|
|
|
'*/5 * * * *' | '0 * * * *' | (1.day.in_minutes / 2.hours.in_minutes).to_i | Time.zone.local(2021, 5, 27, 11, 0) | Time.zone.local(2021, 5, 27, 12, 5)
|
|
|
|
'*/5 * * * *' | '0 1 * * *' | (1.day.in_minutes / 1.hour.in_minutes).to_i | Time.zone.local(2021, 5, 27, 1, 0) | Time.zone.local(2021, 5, 28, 1, 0)
|
|
|
|
'*/5 * * * *' | '0 1 * * *' | (1.day.in_minutes / 10).to_i | Time.zone.local(2021, 5, 27, 1, 0) | Time.zone.local(2021, 5, 28, 1, 0)
|
|
|
|
'*/5 * * * *' | '0 1 * * *' | (1.day.in_minutes / 8).to_i | Time.zone.local(2021, 5, 27, 1, 0) | Time.zone.local(2021, 5, 28, 1, 0)
|
|
|
|
'*/5 * * * *' | '0 1 1 * *' | (1.day.in_minutes / 1.hour.in_minutes).to_i | Time.zone.local(2021, 5, 1, 1, 0) | Time.zone.local(2021, 6, 1, 1, 0)
|
|
|
|
'*/9 * * * *' | '0 1 1 * *' | (1.day.in_minutes / 1.hour.in_minutes).to_i | Time.zone.local(2021, 5, 1, 1, 9) | Time.zone.local(2021, 6, 1, 1, 0)
|
|
|
|
'*/5 * * * *' | '59 14 * * *' | (1.day.in_minutes / 1.hour.in_minutes).to_i | Time.zone.local(2021, 5, 1, 15, 0) | Time.zone.local(2021, 5, 2, 15, 0)
|
|
|
|
'*/5 * * * *' | '45 21 1 2 *' | (1.day.in_minutes / 5).to_i | Time.zone.local(2021, 2, 1, 21, 45) | Time.zone.local(2022, 2, 1, 21, 45)
|
2021-09-04 01:27:46 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
with_them do
|
|
|
|
let(:pipeline_schedule) { create(:ci_pipeline_schedule, cron: schedule_cron) }
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2019-09-04 21:01:54 +05:30
|
|
|
before do
|
|
|
|
allow(Settings).to receive(:cron_jobs) do
|
2021-09-04 01:27:46 +05:30
|
|
|
{ 'pipeline_schedule_worker' => { 'cron' => worker_cron } }
|
2019-09-04 21:01:54 +05:30
|
|
|
end
|
2021-09-04 01:27:46 +05:30
|
|
|
|
|
|
|
create(:plan_limits, :default_plan, ci_daily_pipeline_schedule_triggers: plan_limit) if plan_limit
|
|
|
|
|
|
|
|
# Setting this here to override initial save with the current time
|
|
|
|
pipeline_schedule.next_run_at = now
|
2019-09-04 21:01:54 +05:30
|
|
|
end
|
|
|
|
|
2021-09-04 01:27:46 +05:30
|
|
|
it 'updates next_run_at' do
|
|
|
|
travel_to(now) do
|
|
|
|
pipeline_schedule.set_next_run_at
|
|
|
|
|
|
|
|
expect(pipeline_schedule.next_run_at).to eq(result)
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-09-04 21:01:54 +05:30
|
|
|
context 'when there are two different pipeline schedules in different time zones' do
|
|
|
|
let(:pipeline_schedule_1) { create(:ci_pipeline_schedule, :weekly, cron_timezone: 'Eastern Time (US & Canada)') }
|
|
|
|
let(:pipeline_schedule_2) { create(:ci_pipeline_schedule, :weekly, cron_timezone: 'UTC') }
|
|
|
|
|
|
|
|
it 'sets different next_run_at' do
|
|
|
|
expect(pipeline_schedule_1.next_run_at).not_to eq(pipeline_schedule_2.next_run_at)
|
|
|
|
end
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
describe '#schedule_next_run!' do
|
|
|
|
let!(:pipeline_schedule) { create(:ci_pipeline_schedule, :nightly) }
|
|
|
|
|
2019-09-30 21:07:59 +05:30
|
|
|
before do
|
|
|
|
pipeline_schedule.update_column(:next_run_at, nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'updates next_run_at' do
|
|
|
|
expect { pipeline_schedule.schedule_next_run! }
|
|
|
|
.to change { pipeline_schedule.next_run_at }
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2019-09-30 21:07:59 +05:30
|
|
|
context 'when record is invalid' do
|
|
|
|
before do
|
2021-06-08 01:23:25 +05:30
|
|
|
allow(pipeline_schedule).to receive(:save!) { raise ActiveRecord::RecordInvalid, pipeline_schedule }
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
2019-09-30 21:07:59 +05:30
|
|
|
it 'nullifies the next run at' do
|
|
|
|
pipeline_schedule.schedule_next_run!
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2019-09-30 21:07:59 +05:30
|
|
|
expect(pipeline_schedule.next_run_at).to be_nil
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
describe '#job_variables' do
|
|
|
|
let!(:pipeline_schedule) { create(:ci_pipeline_schedule) }
|
|
|
|
|
|
|
|
let!(:pipeline_schedule_variables) do
|
|
|
|
create_list(:ci_pipeline_schedule_variable, 2, pipeline_schedule: pipeline_schedule)
|
|
|
|
end
|
|
|
|
|
|
|
|
subject { pipeline_schedule.job_variables }
|
|
|
|
|
|
|
|
before do
|
|
|
|
pipeline_schedule.reload
|
|
|
|
end
|
|
|
|
|
|
|
|
it { is_expected.to contain_exactly(*pipeline_schedule_variables.map(&:to_runner_variable)) }
|
|
|
|
end
|
2021-09-04 01:27:46 +05:30
|
|
|
|
|
|
|
describe '#daily_limit' do
|
|
|
|
let(:pipeline_schedule) { build(:ci_pipeline_schedule) }
|
|
|
|
|
|
|
|
subject(:daily_limit) { pipeline_schedule.daily_limit }
|
|
|
|
|
|
|
|
context 'when there is no limit' do
|
|
|
|
before do
|
|
|
|
create(:plan_limits, :default_plan, ci_daily_pipeline_schedule_triggers: 0)
|
|
|
|
end
|
|
|
|
|
|
|
|
it { is_expected.to be_nil }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when there is a limit' do
|
|
|
|
before do
|
|
|
|
create(:plan_limits, :default_plan, ci_daily_pipeline_schedule_triggers: 144)
|
|
|
|
end
|
|
|
|
|
|
|
|
it { is_expected.to eq(144) }
|
|
|
|
end
|
|
|
|
end
|
2022-04-04 11:22:00 +05:30
|
|
|
|
2022-05-07 20:08:51 +05:30
|
|
|
describe '#for_tag?' do
|
|
|
|
context 'when the target is a tag' do
|
|
|
|
before do
|
|
|
|
subject.ref = 'refs/tags/v1.0'
|
|
|
|
end
|
|
|
|
|
|
|
|
it { expect(subject.for_tag?).to eq(true) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the target is a branch' do
|
|
|
|
before do
|
|
|
|
subject.ref = 'refs/heads/main'
|
|
|
|
end
|
|
|
|
|
|
|
|
it { expect(subject.for_tag?).to eq(false) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when there is no ref' do
|
|
|
|
before do
|
|
|
|
subject.ref = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
it { expect(subject.for_tag?).to eq(false) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#ref_for_display' do
|
|
|
|
context 'when the target is a tag' do
|
|
|
|
before do
|
|
|
|
subject.ref = 'refs/tags/v1.0'
|
|
|
|
end
|
|
|
|
|
|
|
|
it { expect(subject.ref_for_display).to eq('v1.0') }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the target is a branch' do
|
|
|
|
before do
|
|
|
|
subject.ref = 'refs/heads/main'
|
|
|
|
end
|
|
|
|
|
|
|
|
it { expect(subject.ref_for_display).to eq('main') }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the ref is ambiguous' do
|
|
|
|
before do
|
|
|
|
subject.ref = 'release-2.8'
|
|
|
|
end
|
|
|
|
|
|
|
|
it { expect(subject.ref_for_display).to eq('release-2.8') }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when there is no ref' do
|
|
|
|
before do
|
|
|
|
subject.ref = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
it { expect(subject.ref_for_display).to eq(nil) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-04-04 11:22:00 +05:30
|
|
|
context 'loose foreign key on ci_pipeline_schedules.project_id' do
|
|
|
|
it_behaves_like 'cleanup by a loose foreign key' do
|
|
|
|
let!(:parent) { create(:project) }
|
|
|
|
let!(:model) { create(:ci_pipeline_schedule, project: parent) }
|
|
|
|
end
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|