2019-07-25 01:21:37 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-04-04 05:49:52 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 05:08:32 -04:00
|
|
|
RSpec.describe Gitlab::Ci::CronParser do
|
2017-04-04 05:49:52 -04:00
|
|
|
shared_examples_for "returns time in the future" do
|
|
|
|
it { is_expected.to be > Time.now }
|
|
|
|
end
|
|
|
|
|
2020-05-14 08:08:21 -04:00
|
|
|
shared_examples_for "returns time in the past" do
|
|
|
|
it { is_expected.to be < Time.now }
|
|
|
|
end
|
2017-04-04 05:49:52 -04:00
|
|
|
|
2020-05-14 08:08:21 -04:00
|
|
|
shared_examples_for 'when cron and cron_timezone are valid' do |returns_time_for_epoch|
|
|
|
|
context 'when specific time' do
|
|
|
|
let(:cron) { '3 4 5 6 *' }
|
|
|
|
let(:cron_timezone) { 'UTC' }
|
2017-04-04 05:49:52 -04:00
|
|
|
|
2020-05-14 08:08:21 -04:00
|
|
|
it_behaves_like returns_time_for_epoch
|
2017-04-04 05:49:52 -04:00
|
|
|
|
2020-05-14 08:08:21 -04:00
|
|
|
it 'returns exact time' do
|
|
|
|
expect(subject.min).to eq(3)
|
|
|
|
expect(subject.hour).to eq(4)
|
|
|
|
expect(subject.day).to eq(5)
|
|
|
|
expect(subject.month).to eq(6)
|
2017-04-04 05:49:52 -04:00
|
|
|
end
|
2020-05-14 08:08:21 -04:00
|
|
|
end
|
2017-04-04 05:49:52 -04:00
|
|
|
|
2020-05-14 08:08:21 -04:00
|
|
|
context 'when specific day of week' do
|
|
|
|
let(:cron) { '* * * * 0' }
|
|
|
|
let(:cron_timezone) { 'UTC' }
|
2017-04-04 05:49:52 -04:00
|
|
|
|
2020-05-14 08:08:21 -04:00
|
|
|
it_behaves_like returns_time_for_epoch
|
2017-04-04 05:49:52 -04:00
|
|
|
|
2020-05-14 08:08:21 -04:00
|
|
|
it 'returns exact day of week' do
|
|
|
|
expect(subject.wday).to eq(0)
|
2017-04-04 05:49:52 -04:00
|
|
|
end
|
2020-05-14 08:08:21 -04:00
|
|
|
end
|
2017-04-04 05:49:52 -04:00
|
|
|
|
2020-05-14 08:08:21 -04:00
|
|
|
context 'when slash used' do
|
|
|
|
let(:cron) { '*/10 */6 */10 */10 *' }
|
|
|
|
let(:cron_timezone) { 'UTC' }
|
2017-04-04 05:49:52 -04:00
|
|
|
|
2020-05-14 08:08:21 -04:00
|
|
|
it_behaves_like returns_time_for_epoch
|
2017-04-04 05:49:52 -04:00
|
|
|
|
2020-05-14 08:08:21 -04:00
|
|
|
it 'returns specific time' do
|
|
|
|
expect(subject.min).to be_in([0, 10, 20, 30, 40, 50])
|
|
|
|
expect(subject.hour).to be_in([0, 6, 12, 18])
|
|
|
|
expect(subject.day).to be_in([1, 11, 21, 31])
|
|
|
|
expect(subject.month).to be_in([1, 11])
|
2017-04-04 05:49:52 -04:00
|
|
|
end
|
2020-05-14 08:08:21 -04:00
|
|
|
end
|
2017-04-04 05:49:52 -04:00
|
|
|
|
2020-05-14 08:08:21 -04:00
|
|
|
context 'when range used' do
|
|
|
|
let(:cron) { '0,20,40 * 1-5 * *' }
|
|
|
|
let(:cron_timezone) { 'UTC' }
|
2017-04-04 05:49:52 -04:00
|
|
|
|
2020-05-14 08:08:21 -04:00
|
|
|
it_behaves_like returns_time_for_epoch
|
2017-04-04 05:49:52 -04:00
|
|
|
|
2020-05-14 08:08:21 -04:00
|
|
|
it 'returns specific time' do
|
|
|
|
expect(subject.min).to be_in([0, 20, 40])
|
|
|
|
expect(subject.day).to be_in((1..5).to_a)
|
2017-04-04 05:49:52 -04:00
|
|
|
end
|
2020-05-14 08:08:21 -04:00
|
|
|
end
|
2017-04-04 05:49:52 -04:00
|
|
|
|
2020-05-14 08:08:21 -04:00
|
|
|
context 'when cron_timezone is TZInfo format' do
|
|
|
|
before do
|
|
|
|
allow(Time).to receive(:zone)
|
|
|
|
.and_return(ActiveSupport::TimeZone['UTC'])
|
|
|
|
end
|
2017-04-04 05:49:52 -04:00
|
|
|
|
2020-05-14 08:08:21 -04:00
|
|
|
let(:hour_in_utc) do
|
|
|
|
ActiveSupport::TimeZone[cron_timezone]
|
|
|
|
.now.change(hour: 0).in_time_zone('UTC').hour
|
|
|
|
end
|
2017-05-03 11:23:20 -04:00
|
|
|
|
2020-05-14 08:08:21 -04:00
|
|
|
context 'when cron_timezone is US/Pacific' do
|
|
|
|
let(:cron) { '* 0 * * *' }
|
|
|
|
let(:cron_timezone) { 'US/Pacific' }
|
2017-05-03 11:23:20 -04:00
|
|
|
|
2020-05-14 08:08:21 -04:00
|
|
|
it_behaves_like returns_time_for_epoch
|
2017-05-03 11:23:20 -04:00
|
|
|
|
2020-05-14 08:08:21 -04:00
|
|
|
context 'when PST (Pacific Standard Time)' do
|
|
|
|
it 'converts time in server time zone' do
|
2020-10-02 08:09:03 -04:00
|
|
|
travel_to(Time.utc(2017, 1, 1)) do
|
2020-05-14 08:08:21 -04:00
|
|
|
expect(subject.hour).to eq(hour_in_utc)
|
2017-11-05 09:08:39 -05:00
|
|
|
end
|
|
|
|
end
|
2020-05-14 08:08:21 -04:00
|
|
|
end
|
2017-11-05 09:08:39 -05:00
|
|
|
|
2020-05-14 08:08:21 -04:00
|
|
|
context 'when PDT (Pacific Daylight Time)' do
|
|
|
|
it 'converts time in server time zone' do
|
2020-10-02 08:09:03 -04:00
|
|
|
travel_to(Time.utc(2017, 6, 1)) do
|
2020-05-14 08:08:21 -04:00
|
|
|
expect(subject.hour).to eq(hour_in_utc)
|
2017-11-05 09:08:39 -05:00
|
|
|
end
|
2017-05-03 11:23:20 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-05-14 08:08:21 -04:00
|
|
|
end
|
2017-05-03 11:23:20 -04:00
|
|
|
|
2020-05-14 08:08:21 -04:00
|
|
|
context 'when cron_timezone is ActiveSupport::TimeZone format' do
|
|
|
|
before do
|
|
|
|
allow(Time).to receive(:zone)
|
|
|
|
.and_return(ActiveSupport::TimeZone['UTC'])
|
|
|
|
end
|
2017-05-03 11:23:20 -04:00
|
|
|
|
2020-05-14 08:08:21 -04:00
|
|
|
let(:hour_in_utc) do
|
|
|
|
ActiveSupport::TimeZone[cron_timezone]
|
|
|
|
.now.change(hour: 0).in_time_zone('UTC').hour
|
|
|
|
end
|
2017-05-03 11:23:20 -04:00
|
|
|
|
2020-05-14 08:08:21 -04:00
|
|
|
context 'when cron_timezone is Berlin' do
|
|
|
|
let(:cron) { '* 0 * * *' }
|
|
|
|
let(:cron_timezone) { 'Berlin' }
|
2017-05-03 11:23:20 -04:00
|
|
|
|
2020-05-14 08:08:21 -04:00
|
|
|
it_behaves_like returns_time_for_epoch
|
2017-05-03 11:23:20 -04:00
|
|
|
|
2020-05-14 08:08:21 -04:00
|
|
|
context 'when CET (Central European Time)' do
|
|
|
|
it 'converts time in server time zone' do
|
2020-10-02 08:09:03 -04:00
|
|
|
travel_to(Time.utc(2017, 1, 1)) do
|
2020-05-14 08:08:21 -04:00
|
|
|
expect(subject.hour).to eq(hour_in_utc)
|
2017-11-05 09:08:39 -05:00
|
|
|
end
|
|
|
|
end
|
2020-05-14 08:08:21 -04:00
|
|
|
end
|
2017-11-05 09:08:39 -05:00
|
|
|
|
2020-05-14 08:08:21 -04:00
|
|
|
context 'when CEST (Central European Summer Time)' do
|
|
|
|
it 'converts time in server time zone' do
|
2020-10-02 08:09:03 -04:00
|
|
|
travel_to(Time.utc(2017, 6, 1)) do
|
2020-05-14 08:08:21 -04:00
|
|
|
expect(subject.hour).to eq(hour_in_utc)
|
2017-11-05 09:08:39 -05:00
|
|
|
end
|
2017-05-03 11:23:20 -04:00
|
|
|
end
|
|
|
|
end
|
2020-05-14 08:08:21 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-04-04 05:49:52 -04:00
|
|
|
|
2020-05-14 08:08:21 -04:00
|
|
|
shared_examples_for 'when cron_timezone is Eastern Time (US & Canada)' do |returns_time_for_epoch, year|
|
|
|
|
let(:cron) { '* 0 * * *' }
|
|
|
|
let(:cron_timezone) { 'Eastern Time (US & Canada)' }
|
2017-05-03 11:23:20 -04:00
|
|
|
|
2020-05-14 08:08:21 -04:00
|
|
|
before do
|
|
|
|
allow(Time).to receive(:zone)
|
|
|
|
.and_return(ActiveSupport::TimeZone['UTC'])
|
|
|
|
end
|
2017-05-03 11:23:20 -04:00
|
|
|
|
2020-05-14 08:08:21 -04:00
|
|
|
let(:hour_in_utc) do
|
|
|
|
ActiveSupport::TimeZone[cron_timezone]
|
|
|
|
.now.change(hour: 0).in_time_zone('UTC').hour
|
|
|
|
end
|
2017-11-05 09:08:39 -05:00
|
|
|
|
2020-05-14 08:08:21 -04:00
|
|
|
it_behaves_like returns_time_for_epoch
|
2019-11-25 22:06:24 -05:00
|
|
|
|
2020-05-14 08:08:21 -04:00
|
|
|
context 'when EST (Eastern Standard Time)' do
|
|
|
|
it 'converts time in server time zone' do
|
2020-10-02 08:09:03 -04:00
|
|
|
travel_to(Time.utc(2017, 1, 1)) do
|
2020-05-14 08:08:21 -04:00
|
|
|
expect(subject.hour).to eq(hour_in_utc)
|
2017-04-04 05:49:52 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-05-14 08:08:21 -04:00
|
|
|
context 'when EDT (Eastern Daylight Time)' do
|
|
|
|
it 'converts time in server time zone' do
|
2020-10-02 08:09:03 -04:00
|
|
|
travel_to(Time.utc(2017, 6, 1)) do
|
2020-05-14 08:08:21 -04:00
|
|
|
expect(subject.hour).to eq(hour_in_utc)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-04-04 05:49:52 -04:00
|
|
|
|
2020-05-14 08:08:21 -04:00
|
|
|
context 'when time crosses a Daylight Savings boundary' do
|
|
|
|
let(:cron) { '* 0 1 12 *'}
|
|
|
|
|
|
|
|
# Note this previously only failed if the time zone is set
|
|
|
|
# to a zone that observes Daylight Savings
|
|
|
|
# (e.g. America/Chicago) at the start of the test. Stubbing
|
|
|
|
# TZ doesn't appear to be enough.
|
|
|
|
it 'generates day without TZInfo::AmbiguousTime error' do
|
2020-10-02 08:09:03 -04:00
|
|
|
travel_to(Time.utc(2020, 1, 1)) do
|
2020-05-14 08:08:21 -04:00
|
|
|
expect(subject.year).to eq(year)
|
|
|
|
expect(subject.month).to eq(12)
|
|
|
|
expect(subject.day).to eq(1)
|
|
|
|
end
|
|
|
|
end
|
2017-05-03 11:23:20 -04:00
|
|
|
end
|
2020-05-14 08:08:21 -04:00
|
|
|
end
|
2017-05-03 11:23:20 -04:00
|
|
|
|
2020-05-14 08:08:21 -04:00
|
|
|
shared_examples_for 'when cron and cron_timezone are invalid' do
|
|
|
|
let(:cron) { 'invalid_cron' }
|
|
|
|
let(:cron_timezone) { 'invalid_cron_timezone' }
|
2017-05-03 11:23:20 -04:00
|
|
|
|
2020-05-14 08:08:21 -04:00
|
|
|
it { is_expected.to be_nil }
|
|
|
|
end
|
2017-05-03 11:23:20 -04:00
|
|
|
|
2020-05-14 08:08:21 -04:00
|
|
|
shared_examples_for 'when cron syntax is quoted' do
|
|
|
|
let(:cron) { "'0 * * * *'" }
|
|
|
|
let(:cron_timezone) { 'UTC' }
|
2017-05-03 11:23:20 -04:00
|
|
|
|
2020-05-14 08:08:21 -04:00
|
|
|
it { expect(subject).to be_nil }
|
|
|
|
end
|
2019-04-30 08:36:44 -04:00
|
|
|
|
2020-05-14 08:08:21 -04:00
|
|
|
shared_examples_for 'when cron syntax is rufus-scheduler syntax' do
|
|
|
|
let(:cron) { 'every 3h' }
|
|
|
|
let(:cron_timezone) { 'UTC' }
|
2019-04-30 08:36:44 -04:00
|
|
|
|
2020-05-14 08:08:21 -04:00
|
|
|
it { expect(subject).to be_nil }
|
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples_for 'when cron is scheduled to a non existent day' do
|
|
|
|
let(:cron) { '0 12 31 2 *' }
|
|
|
|
let(:cron_timezone) { 'UTC' }
|
|
|
|
|
|
|
|
it { expect(subject).to be_nil }
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#next_time_from' do
|
|
|
|
subject { described_class.new(cron, cron_timezone).next_time_from(Time.now) }
|
|
|
|
|
|
|
|
it_behaves_like 'when cron and cron_timezone are valid', 'returns time in the future'
|
|
|
|
|
|
|
|
it_behaves_like 'when cron_timezone is Eastern Time (US & Canada)', 'returns time in the future', 2020
|
|
|
|
|
|
|
|
it_behaves_like 'when cron and cron_timezone are invalid'
|
|
|
|
|
|
|
|
it_behaves_like 'when cron syntax is quoted'
|
|
|
|
|
|
|
|
it_behaves_like 'when cron syntax is rufus-scheduler syntax'
|
|
|
|
|
|
|
|
it_behaves_like 'when cron is scheduled to a non existent day'
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#previous_time_from' do
|
|
|
|
subject { described_class.new(cron, cron_timezone).previous_time_from(Time.now) }
|
|
|
|
|
|
|
|
it_behaves_like 'when cron and cron_timezone are valid', 'returns time in the past'
|
|
|
|
|
|
|
|
it_behaves_like 'when cron_timezone is Eastern Time (US & Canada)', 'returns time in the past', 2019
|
|
|
|
|
|
|
|
it_behaves_like 'when cron and cron_timezone are invalid'
|
|
|
|
|
|
|
|
it_behaves_like 'when cron syntax is quoted'
|
|
|
|
|
|
|
|
it_behaves_like 'when cron syntax is rufus-scheduler syntax'
|
|
|
|
|
|
|
|
it_behaves_like 'when cron is scheduled to a non existent day'
|
2017-04-04 05:49:52 -04:00
|
|
|
end
|
|
|
|
|
2017-04-04 06:14:49 -04:00
|
|
|
describe '#cron_valid?' do
|
|
|
|
subject { described_class.new(cron, Gitlab::Ci::CronParser::VALID_SYNTAX_SAMPLE_TIME_ZONE).cron_valid? }
|
|
|
|
|
|
|
|
context 'when cron is valid' do
|
|
|
|
let(:cron) { '* * * * *' }
|
|
|
|
|
|
|
|
it { is_expected.to eq(true) }
|
2017-04-04 05:49:52 -04:00
|
|
|
end
|
|
|
|
|
2017-04-04 06:14:49 -04:00
|
|
|
context 'when cron is invalid' do
|
|
|
|
let(:cron) { '*********' }
|
|
|
|
|
|
|
|
it { is_expected.to eq(false) }
|
2017-04-04 05:49:52 -04:00
|
|
|
end
|
2017-05-03 11:23:20 -04:00
|
|
|
|
|
|
|
context 'when cron syntax is quoted' do
|
|
|
|
let(:cron) { "'0 * * * *'" }
|
|
|
|
|
|
|
|
it { is_expected.to eq(false) }
|
|
|
|
end
|
2017-04-04 06:14:49 -04:00
|
|
|
end
|
|
|
|
|
2017-04-04 12:54:45 -04:00
|
|
|
describe '#cron_timezone_valid?' do
|
|
|
|
subject { described_class.new(Gitlab::Ci::CronParser::VALID_SYNTAX_SAMPLE_CRON, cron_timezone).cron_timezone_valid? }
|
2017-04-04 06:14:49 -04:00
|
|
|
|
|
|
|
context 'when cron is valid' do
|
2017-04-04 12:54:45 -04:00
|
|
|
let(:cron_timezone) { 'Europe/Istanbul' }
|
2017-04-04 06:14:49 -04:00
|
|
|
|
|
|
|
it { is_expected.to eq(true) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when cron is invalid' do
|
2017-04-04 12:54:45 -04:00
|
|
|
let(:cron_timezone) { 'Invalid-zone' }
|
2017-04-04 05:49:52 -04:00
|
|
|
|
2017-04-04 06:14:49 -04:00
|
|
|
it { is_expected.to eq(false) }
|
2017-04-04 05:49:52 -04:00
|
|
|
end
|
2017-05-03 11:23:20 -04:00
|
|
|
|
|
|
|
context 'when cron_timezone is ActiveSupport::TimeZone format' do
|
|
|
|
let(:cron_timezone) { 'Eastern Time (US & Canada)' }
|
|
|
|
|
|
|
|
it { is_expected.to eq(true) }
|
|
|
|
end
|
2017-04-04 05:49:52 -04:00
|
|
|
end
|
|
|
|
end
|