2017-05-07 18:35:56 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2017-07-10 10:24:02 -04:00
|
|
|
describe Ci::PipelineSchedule do
|
2017-05-07 18:35:56 -04:00
|
|
|
it { is_expected.to belong_to(:project) }
|
|
|
|
it { is_expected.to belong_to(:owner) }
|
|
|
|
|
|
|
|
it { is_expected.to have_many(:pipelines) }
|
2017-06-21 05:25:01 -04:00
|
|
|
it { is_expected.to have_many(:variables) }
|
2017-05-07 18:35:56 -04:00
|
|
|
|
|
|
|
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) }
|
|
|
|
it { is_expected.to respond_to(:deleted_at) }
|
|
|
|
|
|
|
|
describe 'validations' do
|
|
|
|
it 'does not allow invalid cron patters' do
|
|
|
|
pipeline_schedule = build(:ci_pipeline_schedule, cron: '0 0 0 * *')
|
|
|
|
|
|
|
|
expect(pipeline_schedule).not_to be_valid
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not allow invalid cron patters' do
|
|
|
|
pipeline_schedule = build(:ci_pipeline_schedule, cron_timezone: 'invalid')
|
|
|
|
|
|
|
|
expect(pipeline_schedule).not_to be_valid
|
|
|
|
end
|
2017-05-26 08:41:32 -04:00
|
|
|
|
|
|
|
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
|
2017-05-07 18:35:56 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
describe '#set_next_run_at' do
|
|
|
|
let!(:pipeline_schedule) { create(:ci_pipeline_schedule, :nightly) }
|
|
|
|
|
|
|
|
context 'when creates new pipeline schedule' do
|
|
|
|
let(:expected_next_run_at) do
|
2017-06-21 09:48:12 -04:00
|
|
|
Gitlab::Ci::CronParser.new(pipeline_schedule.cron, pipeline_schedule.cron_timezone)
|
|
|
|
.next_time_from(Time.now)
|
2017-05-07 18:35:56 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'updates next_run_at automatically' do
|
2017-07-25 13:09:00 -04:00
|
|
|
expect(described_class.last.next_run_at).to eq(expected_next_run_at)
|
2017-05-07 18:35:56 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when updates cron of exsisted pipeline schedule' do
|
|
|
|
let(:new_cron) { '0 0 1 1 *' }
|
|
|
|
|
|
|
|
let(:expected_next_run_at) do
|
2017-06-21 09:48:12 -04:00
|
|
|
Gitlab::Ci::CronParser.new(new_cron, pipeline_schedule.cron_timezone)
|
|
|
|
.next_time_from(Time.now)
|
2017-05-07 18:35:56 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'updates next_run_at automatically' do
|
|
|
|
pipeline_schedule.update!(cron: new_cron)
|
|
|
|
|
2017-07-25 13:09:00 -04:00
|
|
|
expect(described_class.last.next_run_at).to eq(expected_next_run_at)
|
2017-05-07 18:35:56 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#schedule_next_run!' do
|
|
|
|
let!(:pipeline_schedule) { create(:ci_pipeline_schedule, :nightly) }
|
|
|
|
|
|
|
|
context 'when reschedules after 10 days from now' do
|
|
|
|
let(:future_time) { 10.days.from_now }
|
|
|
|
|
|
|
|
let(:expected_next_run_at) do
|
2017-06-21 09:48:12 -04:00
|
|
|
Gitlab::Ci::CronParser.new(pipeline_schedule.cron, pipeline_schedule.cron_timezone)
|
|
|
|
.next_time_from(future_time)
|
2017-05-07 18:35:56 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'points to proper next_run_at' do
|
|
|
|
Timecop.freeze(future_time) do
|
|
|
|
pipeline_schedule.schedule_next_run!
|
|
|
|
|
|
|
|
expect(pipeline_schedule.next_run_at).to eq(expected_next_run_at)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#real_next_run' do
|
|
|
|
subject do
|
|
|
|
described_class.last.real_next_run(worker_cron: worker_cron,
|
|
|
|
worker_time_zone: worker_time_zone)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when GitLab time_zone is UTC' do
|
|
|
|
before do
|
|
|
|
allow(Time).to receive(:zone)
|
|
|
|
.and_return(ActiveSupport::TimeZone[worker_time_zone])
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:worker_time_zone) { 'UTC' }
|
|
|
|
|
|
|
|
context 'when cron_timezone is Eastern Time (US & Canada)' do
|
|
|
|
before do
|
|
|
|
create(:ci_pipeline_schedule, :nightly,
|
|
|
|
cron_timezone: 'Eastern Time (US & Canada)')
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:worker_cron) { '0 1 2 3 *' }
|
|
|
|
|
|
|
|
it 'returns the next time worker executes' do
|
|
|
|
expect(subject.min).to eq(0)
|
|
|
|
expect(subject.hour).to eq(1)
|
|
|
|
expect(subject.day).to eq(2)
|
|
|
|
expect(subject.month).to eq(3)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-06-27 03:55:27 -04:00
|
|
|
|
|
|
|
describe '#job_variables' do
|
2017-07-04 04:45:07 -04:00
|
|
|
let!(:pipeline_schedule) { create(:ci_pipeline_schedule) }
|
2017-07-03 12:51:21 -04:00
|
|
|
|
2017-06-27 03:55:27 -04:00
|
|
|
let!(:pipeline_schedule_variables) do
|
|
|
|
create_list(:ci_pipeline_schedule_variable, 2, pipeline_schedule: pipeline_schedule)
|
|
|
|
end
|
|
|
|
|
|
|
|
subject { pipeline_schedule.job_variables }
|
|
|
|
|
2017-07-03 09:13:10 -04:00
|
|
|
before do
|
|
|
|
pipeline_schedule.reload
|
|
|
|
end
|
|
|
|
|
2017-07-05 12:24:20 -04:00
|
|
|
it { is_expected.to contain_exactly(*pipeline_schedule_variables.map(&:to_runner_variable)) }
|
2017-06-27 03:55:27 -04:00
|
|
|
end
|
2017-05-07 18:35:56 -04:00
|
|
|
end
|