diff options
author | Shinya Maeda <gitlab.shinyamaeda@gmail.com> | 2017-03-23 03:54:49 +0900 |
---|---|---|
committer | Shinya Maeda <gitlab.shinyamaeda@gmail.com> | 2017-04-06 23:46:58 +0900 |
commit | 5f715f1d32c6f5ce25b3721bde8f476173afadc8 (patch) | |
tree | aae1982a02c2c53c0da9229154e45b6fecb01f61 /spec | |
parent | 46e4ed6bd0c8c256bce6d35b4bb992d77fd09971 (diff) | |
download | gitlab-ce-5f715f1d32c6f5ce25b3721bde8f476173afadc8.tar.gz |
Add scheduled_trigger model. Add cron parser. Plus, specs.
Diffstat (limited to 'spec')
-rw-r--r-- | spec/factories/ci/scheduled_triggers.rb | 42 | ||||
-rw-r--r-- | spec/lib/ci/cron_parser_spec.rb | 128 | ||||
-rw-r--r-- | spec/models/ci/scheduled_trigger_spec.rb | 38 | ||||
-rw-r--r-- | spec/workers/scheduled_trigger_worker_spec.rb | 11 |
4 files changed, 219 insertions, 0 deletions
diff --git a/spec/factories/ci/scheduled_triggers.rb b/spec/factories/ci/scheduled_triggers.rb new file mode 100644 index 00000000000..9d45f4b4962 --- /dev/null +++ b/spec/factories/ci/scheduled_triggers.rb @@ -0,0 +1,42 @@ +FactoryGirl.define do + factory :ci_scheduled_trigger, class: Ci::ScheduledTrigger do + project factory: :empty_project + owner factory: :user + ref 'master' + + trait :cron_nightly_build do + cron '0 1 * * *' + cron_time_zone 'Europe/Istanbul' + end + + trait :cron_weekly_build do + cron '0 1 * * 5' + cron_time_zone 'Europe/Istanbul' + end + + trait :cron_monthly_build do + cron '0 1 22 * *' + cron_time_zone 'Europe/Istanbul' + end + + trait :cron_every_5_minutes do + cron '*/5 * * * *' + cron_time_zone 'Europe/Istanbul' + end + + trait :cron_every_5_hours do + cron '* */5 * * *' + cron_time_zone 'Europe/Istanbul' + end + + trait :cron_every_5_days do + cron '* * */5 * *' + cron_time_zone 'Europe/Istanbul' + end + + trait :cron_every_5_months do + cron '* * * */5 *' + cron_time_zone 'Europe/Istanbul' + end + end +end diff --git a/spec/lib/ci/cron_parser_spec.rb b/spec/lib/ci/cron_parser_spec.rb new file mode 100644 index 00000000000..58eb26c9421 --- /dev/null +++ b/spec/lib/ci/cron_parser_spec.rb @@ -0,0 +1,128 @@ +require 'spec_helper' + +module Ci + describe CronParser, lib: true do + describe '#next_time_from_now' do + subject { described_class.new(cron, cron_time_zone).next_time_from_now } + + context 'when cron and cron_time_zone are valid' do + context 'at 00:00, 00:10, 00:20, 00:30, 00:40, 00:50' do + let(:cron) { '*/10 * * * *' } + let(:cron_time_zone) { 'US/Pacific' } + + it 'returns next time from now' do + time = Time.now.in_time_zone(cron_time_zone) + time = time + 10.minutes + time = time.change(sec: 0, min: time.min-time.min%10) + is_expected.to eq(time) + end + end + + context 'at 10:00, 20:00' do + let(:cron) { '0 */10 * * *' } + let(:cron_time_zone) { 'US/Pacific' } + + it 'returns next time from now' do + time = Time.now.in_time_zone(cron_time_zone) + time = time + 10.hours + time = time.change(sec: 0, min: 0, hour: time.hour-time.hour%10) + is_expected.to eq(time) + end + end + + context 'when cron is every 10 days' do + let(:cron) { '0 0 */10 * *' } + let(:cron_time_zone) { 'US/Pacific' } + + it 'returns next time from now' do + time = Time.now.in_time_zone(cron_time_zone) + time = time + 10.days + time = time.change(sec: 0, min: 0, hour: 0, day: time.day-time.day%10) + is_expected.to eq(time) + end + end + + context 'when cron is every week 2:00 AM' do + let(:cron) { '0 2 * * *' } + let(:cron_time_zone) { 'US/Pacific' } + + it 'returns next time from now' do + time = Time.now.in_time_zone(cron_time_zone) + is_expected.to eq(time.change(sec: 0, min: 0, hour: 2, day: time.day+1)) + end + end + + context 'when cron_time_zone is US/Pacific' do + let(:cron) { '0 1 * * *' } + let(:cron_time_zone) { 'US/Pacific' } + + it 'returns next time from now' do + time = Time.now.in_time_zone(cron_time_zone) + is_expected.to eq(time.change(sec: 0, min: 0, hour: 1, day: time.day+1)) + end + end + + context 'when cron_time_zone is Europe/London' do + let(:cron) { '0 1 * * *' } + let(:cron_time_zone) { 'Europe/London' } + + it 'returns next time from now' do + time = Time.now.in_time_zone(cron_time_zone) + is_expected.to eq(time.change(sec: 0, min: 0, hour: 1, day: time.day+1)) + end + end + + context 'when cron_time_zone is Asia/Tokyo' do + let(:cron) { '0 1 * * *' } + let(:cron_time_zone) { 'Asia/Tokyo' } + + it 'returns next time from now' do + time = Time.now.in_time_zone(cron_time_zone) + is_expected.to eq(time.change(sec: 0, min: 0, hour: 1, day: time.day+1)) + end + end + end + + context 'when cron is given and cron_time_zone is not given' do + let(:cron) { '0 1 * * *' } + + it 'returns next time from now in utc' do + obj = described_class.new(cron).next_time_from_now + time = Time.now.in_time_zone('UTC') + expect(obj).to eq(time.change(sec: 0, min: 0, hour: 1, day: time.day+1)) + end + end + + context 'when cron and cron_time_zone are invalid' do + let(:cron) { 'hack' } + let(:cron_time_zone) { 'hack' } + + it 'returns nil' do + is_expected.to be_nil + end + end + end + + describe '#valid_syntax?' do + subject { described_class.new(cron, cron_time_zone).valid_syntax? } + + context 'when cron and cron_time_zone are valid' do + let(:cron) { '* * * * *' } + let(:cron_time_zone) { 'Europe/Istanbul' } + + it 'returns true' do + is_expected.to eq(true) + end + end + + context 'when cron and cron_time_zone are invalid' do + let(:cron) { 'hack' } + let(:cron_time_zone) { 'hack' } + + it 'returns false' do + is_expected.to eq(false) + end + end + end + end +end diff --git a/spec/models/ci/scheduled_trigger_spec.rb b/spec/models/ci/scheduled_trigger_spec.rb new file mode 100644 index 00000000000..68ba9c379b8 --- /dev/null +++ b/spec/models/ci/scheduled_trigger_spec.rb @@ -0,0 +1,38 @@ +require 'spec_helper' +require 'rufus-scheduler' # Included in sidekiq-cron + +describe Ci::ScheduledTrigger, models: true do + + describe 'associations' do + it { is_expected.to belong_to(:project) } + it { is_expected.to belong_to(:owner) } + end + + describe '#schedule_next_run!' do + context 'when cron and cron_time_zone are vaild' do + context 'when nightly build' do + it 'schedules next run' do + scheduled_trigger = create(:ci_scheduled_trigger, :cron_nightly_build) + scheduled_trigger.schedule_next_run! + puts "scheduled_trigger: #{scheduled_trigger.inspect}" + + expect(scheduled_trigger.cron).to be_nil + end + end + + context 'when weekly build' do + + end + + context 'when monthly build' do + + end + end + + context 'when cron and cron_time_zone are invaild' do + it 'schedules nothing' do + + end + end + end +end diff --git a/spec/workers/scheduled_trigger_worker_spec.rb b/spec/workers/scheduled_trigger_worker_spec.rb new file mode 100644 index 00000000000..c17536720a4 --- /dev/null +++ b/spec/workers/scheduled_trigger_worker_spec.rb @@ -0,0 +1,11 @@ +require 'spec_helper' + +describe ScheduledTriggerWorker do + subject { described_class.new.perform } + + context '#perform' do # TODO: + it 'does' do + is_expected.to be_nil + end + end +end |